Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/common.h
blob: 500f74030413b1040f5c1153be9a89f50937f280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
   common.h - common test routines
   This file is part of the nss-pam-ldapd library.

   Copyright (C) 2011, 2012 Arthur de Jong

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#ifndef TEST__COMMON_H
#define TEST__COMMON_H 1

#include <errno.h>

#ifndef __ASSERT_FUNCTION
#define __ASSERT_FUNCTION ""
#endif /* not __ASSERT_FUNCTION */

/* try to find the actual assert function */
#ifndef HAVE___ASSERT_FAIL
/* for Solaris: */
#ifdef sun
#define __assert_fail(assertion, file, line, function)                      \
  __assert(assertion, file, line)
#endif
/* for FreeBSD: */
#ifdef __FreeBSD__
#define __assert_fail(assertion, file, line, function)                      \
  __assert(assertion, file, line, function)
#endif
#endif /* not HAVE___ASSERT_FAIL */

/* extra assertion function that epxects both strings to be the same
   (special macro because strcmp() can be a macro that turns ugly in assert) */
#define assertstreq(str1, str2)                                             \
  (assertstreq_impl(str1, str2,                                             \
                    "strcmp(" __STRING(str1) ", " __STRING(str2) ") == 0",  \
                    __FILE__, __LINE__, __ASSERT_FUNCTION))

static inline void assertstreq_impl(const char *str1, const char *str2,
                                    const char *assertion, const char *file,
                                    int line, const char *function)
{
  if (strcmp(str1, str2) != 0)
    __assert_fail(assertion, file, line, function);
}

/* extra assertion function that expects expr to be valid and prints an
   error message that include errno otherwise */
#define assertok(expr)                                                      \
  ((expr)                                                                   \
   ? (void) (0)                                                             \
   : __assertok_fail(__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))


static inline void __assertok_fail(const char *expr, const char *file,
                                   int line, const char *function)
{
  char msg[120];
  snprintf(msg, sizeof(msg), "%s (errno=\"%s\")", expr, strerror(errno));
  __assert_fail(msg, file, line, function);
}


#endif /* not TEST__COMMON_H */