raw: Add run_as_child_process function.
[swifty.git] / src / raw / test / test.h
1 /*
2  * generic test suite
3  *
4  * test macros and headers
5  */
6
7 #ifndef TEST_H_
8 #define TEST_H_         1
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 #include <stdio.h>
15 #include <string.h>
16
17 /* Test function type. */
18 typedef void (*test_fn)(void);
19
20 /* Run test function f in another process. */
21 void run_as_child_process(test_fn f);
22
23 /*
24  * uncommend EXIT_IF_FAIL macro in order to stop test execution
25  * at first failed test
26  */
27
28 /*#define EXIT_IF_FAIL  1*/
29
30 #if defined (EXIT_IF_FAIL)
31 #define test_do_fail()                  \
32         do {                            \
33                 printf("failed\n");     \
34                 exit(EXIT_FAILURE);     \
35         } while (0)
36 #else
37 #define test_do_fail()                  \
38         printf("failed\n")
39 #endif
40
41 #define test_do_pass()                  \
42         printf("passed\n")
43
44 #define test(test)                                              \
45         do {                                                    \
46                 size_t i;                                       \
47                 int t = (test);                                 \
48                                                                 \
49                 printf("%s", __FUNCTION__);                     \
50                 fflush(stdout);                                 \
51                                                                 \
52                 for (i = 0; i < 60 - strlen(__FUNCTION__); i++) \
53                         putchar('.');                           \
54                                                                 \
55                 if (!t)                                         \
56                         test_do_fail();                         \
57                 else                                            \
58                         test_do_pass();                         \
59                                                                 \
60                 fflush(stdout);                                 \
61         } while (0)
62
63 #define start_suite()                   \
64         do {                                            \
65                         printf("\n==== Starting %s ====\n", __FUNCTION__); \
66         } while (0)
67 #ifdef __cplusplus
68 }
69 #endif
70
71 #endif