Blame tests/common.h

Packit 6bd9ab
/*
Packit 6bd9ab
   common.h - common test routines
Packit 6bd9ab
   This file is part of the nss-pam-ldapd library.
Packit 6bd9ab
Packit 6bd9ab
   Copyright (C) 2011, 2012 Arthur de Jong
Packit 6bd9ab
Packit 6bd9ab
   This library is free software; you can redistribute it and/or
Packit 6bd9ab
   modify it under the terms of the GNU Lesser General Public
Packit 6bd9ab
   License as published by the Free Software Foundation; either
Packit 6bd9ab
   version 2.1 of the License, or (at your option) any later version.
Packit 6bd9ab
Packit 6bd9ab
   This library is distributed in the hope that it will be useful,
Packit 6bd9ab
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6bd9ab
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6bd9ab
   Lesser General Public License for more details.
Packit 6bd9ab
Packit 6bd9ab
   You should have received a copy of the GNU Lesser General Public
Packit 6bd9ab
   License along with this library; if not, write to the Free Software
Packit 6bd9ab
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6bd9ab
   02110-1301 USA
Packit 6bd9ab
*/
Packit 6bd9ab
Packit 6bd9ab
#ifndef TEST__COMMON_H
Packit 6bd9ab
#define TEST__COMMON_H 1
Packit 6bd9ab
Packit 6bd9ab
#include <errno.h>
Packit 6bd9ab
Packit 6bd9ab
#ifndef __ASSERT_FUNCTION
Packit 6bd9ab
#define __ASSERT_FUNCTION ""
Packit 6bd9ab
#endif /* not __ASSERT_FUNCTION */
Packit 6bd9ab
Packit 6bd9ab
/* try to find the actual assert function */
Packit 6bd9ab
#ifndef HAVE___ASSERT_FAIL
Packit 6bd9ab
/* for Solaris: */
Packit 6bd9ab
#ifdef sun
Packit 6bd9ab
#define __assert_fail(assertion, file, line, function)                      \
Packit 6bd9ab
  __assert(assertion, file, line)
Packit 6bd9ab
#endif
Packit 6bd9ab
/* for FreeBSD: */
Packit 6bd9ab
#ifdef __FreeBSD__
Packit 6bd9ab
#define __assert_fail(assertion, file, line, function)                      \
Packit 6bd9ab
  __assert(assertion, file, line, function)
Packit 6bd9ab
#endif
Packit 6bd9ab
#endif /* not HAVE___ASSERT_FAIL */
Packit 6bd9ab
Packit 6bd9ab
/* extra assertion function that epxects both strings to be the same
Packit 6bd9ab
   (special macro because strcmp() can be a macro that turns ugly in assert) */
Packit 6bd9ab
#define assertstreq(str1, str2)                                             \
Packit 6bd9ab
  (assertstreq_impl(str1, str2,                                             \
Packit 6bd9ab
                    "strcmp(" __STRING(str1) ", " __STRING(str2) ") == 0",  \
Packit 6bd9ab
                    __FILE__, __LINE__, __ASSERT_FUNCTION))
Packit 6bd9ab
Packit 6bd9ab
static inline void assertstreq_impl(const char *str1, const char *str2,
Packit 6bd9ab
                                    const char *assertion, const char *file,
Packit 6bd9ab
                                    int line, const char *function)
Packit 6bd9ab
{
Packit 6bd9ab
  if (strcmp(str1, str2) != 0)
Packit 6bd9ab
    __assert_fail(assertion, file, line, function);
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
/* extra assertion function that expects expr to be valid and prints an
Packit 6bd9ab
   error message that include errno otherwise */
Packit 6bd9ab
#define assertok(expr)                                                      \
Packit 6bd9ab
  ((expr)                                                                   \
Packit 6bd9ab
   ? (void) (0)                                                             \
Packit 6bd9ab
   : __assertok_fail(__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
static inline void __assertok_fail(const char *expr, const char *file,
Packit 6bd9ab
                                   int line, const char *function)
Packit 6bd9ab
{
Packit 6bd9ab
  char msg[120];
Packit 6bd9ab
  snprintf(msg, sizeof(msg), "%s (errno=\"%s\")", expr, strerror(errno));
Packit 6bd9ab
  __assert_fail(msg, file, line, function);
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
#endif /* not TEST__COMMON_H */