]> rtime.felk.cvut.cz Git - wvtest.git/blob - cpp/wvtest.h
Merge branch 'master' of git://github.com/wlach/wvtest
[wvtest.git] / cpp / 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>
16
17 class WvTest
18 {
19     typedef void MainFunc();
20     const char *descr, *idstr;
21     MainFunc *main;
22     int slowness;
23     WvTest *next;
24     static WvTest *first, *last;
25     static int fails, runs;
26     static time_t start_time;
27     static bool run_twice;
28     
29     static void alarm_handler(int sig);
30    
31     static void print_result(bool start, const char *file, int line, 
32             const char *condstr, bool result);
33 public:
34     WvTest(const char *_descr, const char *_idstr, MainFunc *_main, int _slow);
35     static int run_all(const char * const *prefixes = NULL);
36     static void start(const char *file, int line, const char *condstr);
37     static void check(bool cond);
38     static inline bool start_check(const char *file, int line,
39                                    const char *condstr, bool cond)
40         { start(file, line, condstr); check(cond); return cond; }
41     static bool start_check_eq(const char *file, int line,
42                                const char *a, const char *b, bool expect_pass);
43     static bool start_check_eq(const char *file, int line,
44                                const std::string &a, const std::string &b, 
45                                bool expect_pass);
46     static bool start_check_eq(const char *file, int line, int a, int b,
47                                bool expect_pass);
48     static bool start_check_lt(const char *file, int line,
49                                const char *a, const char *b);
50     static bool start_check_lt(const char *file, int line, int a, int b);
51 };
52
53
54 #define WVPASS(cond) \
55     WvTest::start_check(__FILE__, __LINE__, #cond, (cond))
56 #define WVPASSEQ(a, b) \
57     WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), true)
58 #define WVPASSLT(a, b) \
59     WvTest::start_check_lt(__FILE__, __LINE__, (a), (b))
60 #define WVFAIL(cond) \
61     WvTest::start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond))
62 #define WVFAILEQ(a, b) \
63     WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), false)
64 #define WVPASSNE(a, b) WVFAILEQ(a, b)
65 #define WVFAILNE(a, b) WVPASSEQ(a, b)
66
67 #define WVTEST_MAIN3(descr, ff, ll, slowness) \
68     static void _wvtest_main_##ll(); \
69     static WvTest _wvtest_##ll(descr, ff, _wvtest_main_##ll, slowness); \
70     static void _wvtest_main_##ll()
71 #define WVTEST_MAIN2(descr, ff, ll, slowness) \
72     WVTEST_MAIN3(descr, ff, ll, slowness)
73 #define WVTEST_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 0)
74 #define WVTEST_SLOW_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 1)
75
76
77 #endif // __WVTEST_H