]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/wvtest/c/wvtest.h
wvtest: WVFAIL(cond) returns cond
[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_start(const char *file, int line, const char *condstr);
30 void wvtest_check(bool cond, const char *reason);
31 static inline bool wvtest_start_check(const char *file, int line,
32                                       const char *condstr, bool cond)
33 { wvtest_start(file, line, condstr); wvtest_check(cond, NULL); return cond; }
34 bool wvtest_start_check_eq(const char *file, int line,
35                            int a, int b, bool expect_pass,
36                            const char *a_str, const char *b_str);
37 bool wvtest_start_check_lt(const char *file, int line,
38                            int a, int b);
39 bool wvtest_start_check_eq_ptr(const char *file, int line,
40                                void *a, void *b, bool expect_pass);
41 bool wvtest_start_check_eq_str(const char *file, int line,
42                                const char *a, const char *b, bool expect_pass);
43 bool wvtest_start_check_lt_str(const char *file, int line,
44                                const char *a, const char *b);
45 int wvtest_start_check_frsh(const char *file, int line,
46                             const char *condstr, int frsh_retval);
47
48
49 #define WVPASS(cond) \
50     wvtest_start_check(__FILE__, __LINE__, #cond, (cond))
51 #define WVPASSEQ(a, b) \
52         wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), true, #a, #b)
53 #define WVPASSLT(a, b) \
54     wvtest_start_check_lt(__FILE__, __LINE__, (a), (b))
55 #define WVPASSEQPTR(a, b) \
56     wvtest_start_check_eq_ptr(__FILE__, __LINE__, (a), (b), true)
57 #define WVFAILEQPTR(a, b) \
58     wvtest_start_check_eq_ptr(__FILE__, __LINE__, (a), (b), false)
59 #define WVPASSEQSTR(a, b) \
60     wvtest_start_check_eq_str(__FILE__, __LINE__, (a), (b), true)
61 #define WVPASSLTSTR(a, b) \
62     wvtest_start_check_lt_str(__FILE__, __LINE__, (a), (b))
63 #define WVFAIL(cond)                                                    \
64         ({ typeof (cond) cond_ = (cond);                                \
65            wvtest_start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond_)); \
66            cond_; })
67 #define WVFAILEQ(a, b)                                                  \
68         wvtest_start_check_eq(__FILE__, __LINE__, (a), (b), false, #a, #b)
69 #define WVFAILEQSTR(a, b) \
70         wvtest_start_check_eq_str(__FILE__, __LINE__, (a), (b), false)
71 #define WVPASSNE(a, b) WVFAILEQ(a, b)
72 #define WVPASSNESTR(a, b) WVFAILEQSTR(a, b)
73 #define WVFAILNE(a, b) WVPASSEQ(a, b)
74 #define WVFAILNESTR(a, b) WVPASSEQSTR(a, b)
75 #define WVFRSH(frshretval) \
76         wvtest_start_check_frsh(__FILE__, __LINE__, #frshretval, (frshretval))
77
78
79 #define WVTEST_MAIN3(_descr, ff, ll, _slowness)                         \
80         static void _wvtest_main_##ll();                                \
81         struct WvTest _wvtest_##ll = \
82         { .descr = _descr, .idstr = ff ":" #ll, .main = _wvtest_main_##ll, .slowness = _slowness }; \
83         static void _wvtest_register_##ll() __attribute__ ((constructor)); \
84         static void _wvtest_register_##ll() { wvtest_register(&_wvtest_##ll); } \
85         static void _wvtest_main_##ll()
86 #define WVTEST_MAIN2(descr, ff, ll, slowness)   \
87         WVTEST_MAIN3(descr, ff, ll, slowness)
88 #define WVTEST_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 0)
89 #define WVTEST_SLOW_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 1)
90
91
92 #endif // __WVTEST_H