testrunner.h
Go to the documentation of this file.
1 /*
2  * testrunner.h
3  *
4  * Created on: Jun 19, 2013
5  * Author: petera
6  */
7 
8 /*
9 
10 file mysuite.c:
11 
12 SUITE(mysuite)
13 
14 static void setup(test *t) {}
15 
16 static void teardown(test *t) {}
17 
18 TEST(mytest) {
19  printf("mytest runs now..\n");
20  return 0;
21 } TEST_END
22 
23 SUITE_TESTS(mysuite)
24  ADD_TEST(mytest)
25 SUITE_END(mysuite)
26 
27 
28 
29 file mysuite2.c:
30 
31 SUITE(mysuite2)
32 
33 static void setup(test *t) {}
34 
35 static void teardown(test *t) {}
36 
37 TEST(mytest2a) {
38  printf("mytest2a runs now..\n");
39  return 0;
40 } TEST_END
41 
42 TEST(mytest2b) {
43  printf("mytest2b runs now..\n");
44  return 0;
45 } TEST_END
46 
47 SUITE_TESTS(mysuite2)
48  ADD_TEST(mytest2a)
49  ADD_TEST(mytest2b)
50 SUITE_END(mysuite2)
51 
52 
53 some other file.c:
54 
55 void add_suites() {
56  ADD_SUITE(mysuite);
57  ADD_SUITE(mysuite2);
58 }
59  */
60 
61 #ifndef TESTRUNNER_H_
62 #define TESTRUNNER_H_
63 
64 #define TEST_RES_OK 0
65 #define TEST_RES_FAIL -1
66 #define TEST_RES_ASSERT -2
67 
68 #define ERREXIT() if (get_abort_on_error()) abort(); else inc_error_count()
69 
70 struct test_s;
71 
72 typedef int (*test_f)(struct test_s *t);
73 
74 typedef struct test_s {
76  char name[256];
77  void *data;
78  void (*setup)(struct test_s *t);
79  void (*teardown)(struct test_s *t);
80  struct test_s *_next;
81  unsigned char test_result;
82 } test;
83 
84 typedef struct test_res_s {
85  char name[256];
86  struct test_res_s *_next;
88 
89 #define TEST_CHECK(x) if (!(x)) { \
90  printf(" TEST FAIL %s:%d\n", __FILE__, __LINE__); \
91  goto __fail_stop; \
92 }
93 #define TEST_CHECK_EQ(x, y) if ((x) != (y)) { \
94  printf(" TEST FAIL %s:%d, %d != %d\n", __FILE__, __LINE__, (int)(x), (int)(y)); \
95  goto __fail_stop; \
96 }
97 #define TEST_CHECK_NEQ(x, y) if ((x) == (y)) { \
98  printf(" TEST FAIL %s:%d, %d == %d\n", __FILE__, __LINE__, (int)(x), (int)(y)); \
99  goto __fail_stop; \
100 }
101 #define TEST_CHECK_GT(x, y) if ((x) <= (y)) { \
102  printf(" TEST FAIL %s:%d, %d <= %d\n", __FILE__, __LINE__, (int)(x), (int)(y)); \
103  goto __fail_stop; \
104 }
105 #define TEST_CHECK_LT(x, y) if ((x) >= (y)) { \
106  printf(" TEST FAIL %s:%d, %d >= %d\n", __FILE__, __LINE__, (int)(x), (int)(y)); \
107  goto __fail_stop; \
108 }
109 #define TEST_CHECK_GE(x, y) if ((x) < (y)) { \
110  printf(" TEST FAIL %s:%d, %d < %d\n", __FILE__, __LINE__, (int)(x), (int)(y)); \
111  goto __fail_stop; \
112 }
113 #define TEST_CHECK_LE(x, y) if ((x) > (y)) { \
114  printf(" TEST FAIL %s:%d, %d > %d\n", __FILE__, __LINE__, (int)(x), (int)(y)); \
115  goto __fail_stop; \
116 }
117 #define TEST_ASSERT(x) if (!(x)) { \
118  printf(" TEST ASSERT %s:%d\n", __FILE__, __LINE__); \
119  goto __fail_assert; \
120 }
121 
122 #define DBGT(...) printf(__VA_ARGS__)
123 
124 #define str(s) #s
125 
126 #define SUITE(sui)
127 
128 #define SUITE_TESTS(sui) \
129  void _add_suite_tests_##sui(void) {
130 
131 #define SUITE_END(sui) \
132  }
133 
134 #define ADD_TEST(tf) \
135  _add_test(__test_##tf, str(tf), setup, teardown, 0);
136 
137 #define ADD_TEST_NON_DEFAULT(tf) \
138  _add_test(__test_##tf, str(tf), setup, teardown, 1);
139 
140 #define ADD_SUITE(sui) \
141  extern void _add_suite_tests_##sui(void); \
142  _add_suite_tests_##sui();
143 
144 #define TEST(tf) \
145  static int __test_##tf(struct test_s *t) { do
146 
147 #define TEST_END \
148  while(0); \
149  __fail_stop: return TEST_RES_FAIL; \
150  __fail_assert: return TEST_RES_ASSERT; \
151  }
152 
153 int set_abort_on_error(int val);
155 int get_error_count(void);
156 void inc_error_count(void);
157 
158 void add_suites(void);
159 void test_init(void (*on_stop)(test *t));
160 // returns 0 if all tests ok, -1 if any test failed, -2 on badness
161 int run_tests(int argc, char **args);
162 void _add_suite(const char *suite_name);
163 void _add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t), int non_default);
164 
165 #endif /* TESTRUNNER_H_ */
Definition: testrunner.h:84
struct test_res_s * _next
Definition: testrunner.h:86
char name[256]
Definition: testrunner.h:85
Definition: testrunner.h:74
char name[256]
Definition: testrunner.h:76
void(* setup)(struct test_s *t)
Definition: testrunner.h:78
unsigned char test_result
Definition: testrunner.h:81
test_f f
Definition: testrunner.h:75
void * data
Definition: testrunner.h:77
struct test_s * _next
Definition: testrunner.h:80
void(* teardown)(struct test_s *t)
Definition: testrunner.h:79
int get_error_count(void)
void _add_test(test_f f, char *name, void(*setup)(test *t), void(*teardown)(test *t), int non_default)
struct test_s test
void add_suites(void)
void inc_error_count(void)
void test_init(void(*on_stop)(test *t))
int get_abort_on_error(void)
struct test_res_s test_res
int(* test_f)(struct test_s *t)
Definition: testrunner.h:72
void _add_suite(const char *suite_name)
int run_tests(int argc, char **args)
int set_abort_on_error(int val)