]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/wvtest/c/wvtest.h
wvtest: Simplify the code by combining start() and check() to a single function
[frescor/frsh-forb.git] / src / wvtest / c / wvtest.h
1 /* -*- Mode: C -*-
2  * WvTest:
3  *   Copyright (C) 1997-2009 Net Integration Technologies, Inc.
4  *       Licensed under the GNU Library General Public License, version 2.
5  *       See the included file named LICENSE for license information.
6  */
7 #ifndef __WVTEST_H
8 #define __WVTEST_H
9
10 #ifndef WVTEST_CONFIGURED
11 # error "Missing settings: HAVE_VALGRIND_MEMCHECK_H HAVE_WVCRASH WVTEST_CONFIGURED"
12 #endif
13
14 #include <time.h>
15 #include <string.h>
16 #include <stdbool.h>
17
18 typedef void wvtest_mainfunc();
19
20 struct WvTest {
21         const char *descr, *idstr;
22         wvtest_mainfunc *main;
23         int slowness;
24         struct WvTest *next;
25 };
26
27 void wvtest_register(struct WvTest *ptr);
28 int wvtest_run_all(char * const *prefixes);
29 void wvtest_check(const char *file, int line, const char *condstr, bool cond, const char *reason);
30 static inline bool wvtest_start_check(const char *file, int line,
31                                       const char *condstr, bool cond)
32 { wvtest_check(file, line, condstr, cond, NULL); return cond; }
33 bool wvtest_start_check_eq(const char *file, int line,
34                            int a, int b, bool expect_pass,
35                            const char *a_str, const char *b_str);
36 bool wvtest_start_check_lt(const char *file, int line,
37                            int a, int b);
38 bool wvtest_start_check_eq_ptr(const char *file, int line,
39                                void *a, void *b, bool expect_pass);
40 bool wvtest_start_check_eq_str(const char *file, int line,
41                                const char *a, const char *b, bool expect_pass);
42 bool wvtest_start_check_lt_str(const char *file, int line,
43                                const char *a, const char *b);
44 int wvtest_start_check_frsh(const char *file, int line,
45                             const char *condstr, int frsh_retval);
46 int wvtest_start_check_errno(const char *file, int line,
47                              const char *condstr, int retval);
48
49
50 #define WVPASS(cond) \
51     wvtest_start_check(__FILE__, __LINE__, #cond, (cond))
52 #define WVPASSEQ(a, b) \
53         wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), true, #a, #b)
54 #define WVPASSLT(a, b) \
55     wvtest_start_check_lt(__FILE__, __LINE__, (a), (b))
56 #define WVPASSEQPTR(a, b) \
57     wvtest_start_check_eq_ptr(__FILE__, __LINE__, (a), (b), true)
58 #define WVFAILEQPTR(a, b) \
59     wvtest_start_check_eq_ptr(__FILE__, __LINE__, (a), (b), false)
60 #define WVPASSEQSTR(a, b) \
61     wvtest_start_check_eq_str(__FILE__, __LINE__, (a), (b), true)
62 #define WVPASSLTSTR(a, b) \
63     wvtest_start_check_lt_str(__FILE__, __LINE__, (a), (b))
64 #define WVFAIL(cond)                                                    \
65         ({ typeof (cond) cond_ = (cond);                                \
66            wvtest_start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond_)); \
67            cond_; })
68 #define WVFAILEQ(a, b)                                                  \
69         wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), false, #a, #b)
70 #define WVFAILEQSTR(a, b) \
71         wvtest_start_check_eq_str(__FILE__, __LINE__, (a), (b), false)
72 #define WVPASSNE(a, b) WVFAILEQ(a, b)
73 #define WVPASSNESTR(a, b) WVFAILEQSTR(a, b)
74 #define WVFAILNE(a, b) WVPASSEQ(a, b)
75 #define WVFAILNESTR(a, b) WVPASSEQSTR(a, b)
76 #define WVFRSH(frshretval) \
77         wvtest_start_check_frsh(__FILE__, __LINE__, #frshretval, (frshretval))
78 #define WVERRNO(retval) \
79         wvtest_start_check_errno(__FILE__, __LINE__, #retval, (retval))
80
81
82 #define WVTEST_MAIN3(_descr, ff, ll, _slowness)                         \
83         static void _wvtest_main_##ll();                                \
84         struct WvTest _wvtest_##ll = \
85         { .descr = _descr, .idstr = ff ":" #ll, .main = _wvtest_main_##ll, .slowness = _slowness }; \
86         static void _wvtest_register_##ll() __attribute__ ((constructor)); \
87         static void _wvtest_register_##ll() { wvtest_register(&_wvtest_##ll); } \
88         static void _wvtest_main_##ll()
89 #define WVTEST_MAIN2(descr, ff, ll, slowness)   \
90         WVTEST_MAIN3(descr, ff, ll, slowness)
91 #define WVTEST_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 0)
92 #define WVTEST_SLOW_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 1)
93
94
95 #endif // __WVTEST_H