]> rtime.felk.cvut.cz Git - wvtest.git/blob - cpp/wvtest.h
Import basic C++ wvtest from wvstreams project.
[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 #include <time.h>
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, int a, int b,
44                                bool expect_pass);
45     static bool start_check_lt(const char *file, int line,
46                                const char *a, const char *b);
47     static bool start_check_lt(const char *file, int line, int a, int b);
48 };
49
50
51 #define WVPASS(cond) \
52     WvTest::start_check(__FILE__, __LINE__, #cond, (cond))
53 #define WVPASSEQ(a, b) \
54     WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), true)
55 #define WVPASSLT(a, b) \
56     WvTest::start_check_lt(__FILE__, __LINE__, (a), (b))
57 #define WVFAIL(cond) \
58     WvTest::start_check(__FILE__, __LINE__, "NOT(" #cond ")", !(cond))
59 #define WVFAILEQ(a, b) \
60     WvTest::start_check_eq(__FILE__, __LINE__, (a), (b), false)
61
62 #define WVTEST_MAIN3(descr, ff, ll, slowness) \
63     static void _wvtest_main_##ll(); \
64     static WvTest _wvtest_##ll(descr, ff, _wvtest_main_##ll, slowness); \
65     static void _wvtest_main_##ll()
66 #define WVTEST_MAIN2(descr, ff, ll, slowness) \
67     WVTEST_MAIN3(descr, ff, ll, slowness)
68 #define WVTEST_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 0)
69 #define WVTEST_SLOW_MAIN(descr) WVTEST_MAIN2(descr, __FILE__, __LINE__, 1)
70
71
72 #endif // __WVTEST_H