]> rtime.felk.cvut.cz Git - wvtest.git/blob - cpp/wvtest.h
3fc64f3196a21370cf588b77e37e0aa0f3840869
[wvtest.git] / cpp / wvtest.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  *   Copyright (C) 1997-2003 Net Integration Technologies, Inc.
4  *
5  * Part of an automated testing framework.  You can declare a "test function"
6  * using WVTEST_MAIN, and call WVPASS and WVFAIL from there.  These produce
7  * formatted data on stdout that can be read by external testrunner scripts.
8  * 
9  * More than one WVTEST_MAIN is allowed in a single program, and they all
10  * get run.
11  */ 
12 #ifndef __WVTEST_H
13 #define __WVTEST_H
14
15 #ifndef WVTEST_CONFIGURED
16 # error "Missing settings: HAVE_VALGRIND_MEMCHECK_H HAVE_WVCRASH WVTEST_CONFIGURED"
17 #endif
18
19 #include <time.h>
20
21 class WvTest
22 {
23     typedef void MainFunc();
24     const char *descr, *idstr;
25     MainFunc *main;
26     int slowness;
27     WvTest *next;
28     static WvTest *first, *last;
29     static int fails, runs;
30     static time_t start_time;
31     static bool run_twice;
32     
33     static void alarm_handler(int sig);
34    
35     static void print_result(bool start, const char *file, int line, 
36             const char *condstr, bool result);
37 public:
38     WvTest(const char *_descr, const char *_idstr, MainFunc *_main, int _slow);
39     static int run_all(const char * const *prefixes = NULL);
40     static void start(const char *file, int line, const char *condstr);
41     static void check(bool cond);
42     static inline bool start_check(const char *file, int line,
43                                    const char *condstr, bool cond)
44         { start(file, line, condstr); check(cond); return cond; }
45     static bool start_check_eq(const char *file, int line,
46                                const char *a, const char *b, bool expect_pass);
47     static bool start_check_eq(const char *file, int line, int a, int b,
48                                bool expect_pass);
49     static bool start_check_lt(const char *file, int line,
50                                const char *a, const char *b);
51     static bool start_check_lt(const char *file, int line, int a, int b);
52 };
53
54
55 #define WVPASS(cond) \
56     WvTest::start_check(__FILE__, __LINE__, #cond, (cond))
57 #define WVPASSEQ(a, b) \
58     WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), true)
59 #define WVPASSLT(a, b) \
60     WvTest::start_check_lt(__FILE__, __LINE__, (a), (b))
61 #define WVFAIL(cond) \
62     WvTest::start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond))
63 #define WVFAILEQ(a, b) \
64     WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), false)
65 #define WVPASSNE(a, b) WVFAILEQ(a, b)
66 #define WVFAILNE(a, b) WVPASSEQ(a, b)
67
68 #define WVTEST_MAIN3(descr, ff, ll, slowness) \
69     static void _wvtest_main_##ll(); \
70     static WvTest _wvtest_##ll(descr, ff, _wvtest_main_##ll, slowness); \
71     static void _wvtest_main_##ll()
72 #define WVTEST_MAIN2(descr, ff, ll, slowness) \
73     WVTEST_MAIN3(descr, ff, ll, slowness)
74 #define WVTEST_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 0)
75 #define WVTEST_SLOW_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 1)
76
77
78 #endif // __WVTEST_H