C Unit Test Framework  0.1
 All Files Functions Enumerations Enumerator Macros Pages
testlib.h
Go to the documentation of this file.
1 #ifndef _TESTLIB_H_
2 #define _TESTLIB_H_
3 
4 #include <test_config.h>
5 
6 #include <stdbool.h>
7 #include <stdlib.h> /* for size_t */
8 #include <stdint.h>
9 
10 /* This file uses the 'do {...} while (0)' trick to create a regular statement
11  * instead of a compound statement. This way can use this macro in code like
12  *
13  * if (<condition>)
14  * CALL_FUNCS(a);
15  * else
16  * bar;
17  *
18  * Macros simply using '{ ... }' will not work this way, because the last ';' in
19  * the compound statement terminates the IF. This will lead to a compiler error.
20  *
21  * Howver, using MS compilers on Warning Level 4, 'while (0)' creates a warninig
22  * 'warning C4127: conditional expression is constant'. Therefore we create a
23  * new macro ONCE which does disable the warning just for this case.
24  */
25 #ifdef _MSC_VER
26 # define ONCE __pragma(warning(push)) \
27  __pragma(warning(disable:4127)) \
28  while (0) \
29  __pragma(warning(pop))
30 #else
31 # define ONCE while (0)
32 #endif
33 
40 #define UTEST_MAIN() int main(int argc, char *argv[]) { \
41  return testlib_main(argc, argv); \
42  }
43 
47 #define UFATAL(condition) \
48  do { \
49  if (testlib_fatal(condition, #condition, __FILE__, __LINE__)) { \
50  return; \
51  } \
52  } ONCE
53 
63 #define UVERIFY(condition) \
64  do { \
65  if (testlib_verify(condition, #condition, __FILE__, __LINE__)) { \
66  return; \
67  } \
68  } ONCE
69 
78 #define UVERIFY2(condition, info) \
79  do { \
80  if (testlib_verify2(condition, #condition, info, __FILE__, __LINE__)) { \
81  return; \
82  } \
83  } ONCE
84 
97 #define UCOMPARE(actual, expected) \
98  do { \
99  if (testlib_compare(actual, expected, #actual, #expected, __FILE__, __LINE__)) { \
100  return; \
101  } \
102  } ONCE
103 
105 #define UCOMPARE64(actual, expected) \
106  do { \
107  if (testlib_compare64(actual, expected, #actual, #expected, __FILE__, __LINE__)) { \
108  return; \
109  } \
110  } ONCE
111 
125 #define UCOMPAREF(actual, expected) \
126  do { \
127  if (testlib_comparef(actual, expected, #actual, #expected, __FILE__, __LINE__)) { \
128  return; \
129  } \
130  } ONCE
131 
151 #define UFUZZY_COMPAREF(actual, expected) \
152  do { \
153  if (testlib_fuzzy_comparef(actual, expected, #actual, #expected, \
154  __FILE__, __LINE__)) { \
155  return; \
156  } \
157  } ONCE
158 
164 #define UCOMPARESTR(actual, expected) \
165  do { \
166  if (testlib_comparestr(actual, expected, #actual, #expected, __FILE__, __LINE__)) { \
167  return; \
168  } \
169  } ONCE
170 
176 #define UCOMPAREMEM(actual, actuallen, expected, expectedlen) \
177  do { \
178  if (testlib_comparemem(actual, actuallen, expected, expectedlen, __FILE__, __LINE__)) { \
179  return; \
180  } \
181  } ONCE
182 
183 #define UREGISTER_NAME(name) \
184  do { \
185  testlib_register_name(name); \
186  } ONCE
187 
197 #define UREGISTER_INIT(func) \
198  do { \
199  testlib_register_init(func); \
200  } ONCE
201 
212 #define UREGISTER_CLEANUP(func) \
213  do { \
214  testlib_register_cleanup(func); \
215  } ONCE
216 
222 #define UREGISTER_TEST(test) \
223  do { \
224  testlib_register_test(test, #test, 0, 0); \
225  } ONCE
226 
232 #define UREGISTER_DATADRIVEN_TEST(test, testdata) \
233  do { \
234  testlib_register_datadriven_test(test, #test, testdata, #testdata, 0, 0); \
235  } ONCE
236 
244 #define UREGISTER_TEST2(test, init, cleanup) \
245  do { \
246  testlib_register_test(test, #test, init, cleanup); \
247  } ONCE
248 
260 #define UREGISTER_DATADRIVEN_TEST2(test, testdata, init, cleanup) \
261  do { \
262  testlib_register_datadriven_test(test, #test, testdata, #testdata, init, cleanup); \
263  } ONCE
264 
265 
288 #define UEXPECT_FAIL(dataIndex, comment, mode) \
289  do { \
290  testlib_expect_fail(dataIndex, comment, mode); \
291  } ONCE
292 
297  Abort = 0,
301 };
302 
303 #ifdef ENABLE_BENCHMARK
304 
339 # define UBENCHMARK for (test_benchmark_start(); test_benchmark_done(); test_benchmark_next())
340 void test_benchmark_start();
341 bool test_benchmark_done();
342 void test_benchmark_next();
343 #else
344 # define UBENCHMARK while (0)
345 #endif
346 
347 int testlib_fatal(int condition, const char *scondition, const char *file, int line);
348 int testlib_verify(int condition, const char *scondition, const char *file, int line);
349 int testlib_verify2(int condition, const char *scondition, const char *info, const char *file, int line);
350 int testlib_compare(int actual, int expected, const char *sactual, const char *sexpected, const char *file, int line);
351 int testlib_compare64(int64_t actual, int64_t expected, const char *sactual, const char *sexpected, const char *file, int line);
352 int testlib_comparef(double actual, double expected, const char *sactual, const char *sexpected, const char *file, int line);
353 int testlib_fuzzy_comparef(double actual, double expected, const char *sactual, const char *sexpected, const char *file, int line);
354 int testlib_comparestr(const char *actual, const char *expected, const char *sactual, const char *sexpected, const char *file, int line);
355 int testlib_comparemem(const unsigned char *actual, size_t actuallen, const unsigned char *expected, size_t expectedlen, const char *file, int line);
356 void testlib_expect_fail(const char *dataIndex, const char *comment, enum testlib_fail_mode mode);
357 
358 void testlib_add_column(const char *name, const char *fmt);
359 void testlib_add_row(const char *name, ...);
360 void *testlib_fetch(const char *name);
361 int testlib_fetch_int(const char *name);
362 unsigned int testlib_fetch_uint(const char *name);
363 double testlib_fetch_double(const char *name);
364 
365 typedef void (*testfunction)();
366 
367 void testlib_register_name(const char *name);
368 void testlib_register_init(testfunction func);
369 void testlib_register_cleanup(testfunction func);
370 void testlib_register_test(testfunction test, const char *stest, testfunction init, testfunction cleanup);
371 void testlib_register_datadriven_test(testfunction test, const char *stest, testfunction testdata, const char *stestdata, testfunction init, testfunction cleanup);
372 void testlib_run_tests(const char *testname, const char *testset);
373 void testlib_list_tests();
374 int testlib_verbose();
375 int testlib_silent();
376 void testlib_info(const char *msg);
377 
378 struct testlib_stat {
379  int num_tests;
380  int num_passed;
381  int num_failed;
382  int num_skipped;
383 };
384 
385 const struct testlib_stat *testlib_result();
386 int testlib_main(int argc, char *argv[]);
387 
388 #define UINFO(msg) testlib_info(msg)
389 
390 #endif /* _TESTLIB_H_ */
391 
unsigned int testlib_fetch_uint(const char *name)
This function behaves exactly like testlib_fetch() but returns an unsigned int value previously store...
Definition: testlib.c:465
void testlib_run_tests(const char *testname, const char *testset)
Executes registered test functions.
Definition: testlib.c:924
int testlib_main(int argc, char *argv[])
Test main implementation used by UTEST_MAIN.
Definition: testlib.c:1221
Continues execution of the test after the expected failure.
Definition: testlib.h:300
struct testlib_stat * testlib_result()
Returns the test result statistic.
Definition: testlib.c:1044
void testlib_add_row(const char *name,...)
Adds a new data row for data-driven tests.
Definition: testlib.c:348
int testlib_silent()
Decrease verbosity level to get less output.
Definition: testlib.c:198
double testlib_fetch_double(const char *name)
This function behaves exactly like testlib_fetch() but returns a double value previously stored in a ...
Definition: testlib.c:483
int testlib_verbose()
Increase verbosity level to get more output.
Definition: testlib.c:192
Aborts the execution of the test.
Definition: testlib.h:297
void testlib_add_column(const char *name, const char *fmt)
Adds a new data column for data-driven tests.
Definition: testlib.c:332
void * testlib_fetch(const char *name)
Returns the data for the given column name of the current test dataset.
Definition: testlib.c:429
testlib_fail_mode
This enum describes the modes for handling an expected failure of the UVERIFY() or UCOMPARE() macros...
Definition: testlib.h:296
int testlib_fetch_int(const char *name)
This function behaves exactly like testlib_fetch() but returns an int value previously stored in a %i...
Definition: testlib.c:447
void testlib_list_tests()
Lists all registered test functions.
Definition: testlib.c:1033