]> git.llucax.com Git - software/mutest.git/blob - mutest.h
938918f6449cae707d472b09154cf7f1212d3315
[software/mutest.git] / mutest.h
1
2 #include <stdio.h> /* fprintf() */
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 /* check that an expression evaluates to true, continue if the check fails */
9 #define mu_check(exp) \
10         do { \
11                 mu_print(MU_CHECK, "\t\t* Checking mu_check(%s)...\n", #exp); \
12                 if (exp) ++mutest_passed_checks; \
13                 else { \
14                         ++mutest_failed_checks; \
15                         mutest_case_failed = 1; \
16                         mu_print(MU_ERROR, "%s:%d: mu_check(%s) failed, " \
17                                         "resuming test case\n", __FILE__, \
18                                         __LINE__, #exp); \
19                 } \
20         } while (0)
21
22 /*
23  * ensure that an expression evaluates to true, abort the current test
24  * case if the check fails
25  */
26 #define mu_ensure(exp) \
27         do { \
28                 mu_print(MU_CHECK, "\t\t* Checking mu_ensure(%s)...\n", #exp);\
29                 if (exp) ++mutest_passed_checks; \
30                 else { \
31                         ++mutest_failed_checks; \
32                         mutest_case_failed = 1; \
33                         mu_print(MU_ERROR, "%s:%d: mu_ensure(%s) failed, " \
34                                         "aborting test case\n", __FILE__, \
35                                         __LINE__, #exp); \
36                         return; \
37                 } \
38         } while (0)
39
40 /*
41  * you don't need to pay any attention to what's next, unless you want to do
42  * some customization, of course, in which case, you're encouraged to take
43  * a look an play =)
44  */
45
46 /* verbosity level (each level shows all the previous levels too) */
47 enum {
48         MU_QUIET = 0, /* be completely quiet */
49         MU_ERROR,     /* shows errors only */
50         MU_SUMMARY,   /* shows a summary */
51         MU_SUITE,     /* shows test suites progress */
52         MU_CASE,      /* shows test cases progress */
53         MU_CHECK      /* shows the current running check */
54 };
55
56 /* print a message according to the verbosity level */
57 #define mu_print(level, ...) \
58         do { \
59                 if (mutest_verbose_level >= level) { \
60                         if (mutest_verbose_level == MU_ERROR) \
61                                 fprintf(stderr, __VA_ARGS__); \
62                         else \
63                                 fprintf(stdout, __VA_ARGS__); \
64                 } \
65         } while (0)
66
67 /*
68  * this function implements the test suites execution, you should generate
69  * a module with this function using mkmutest, or take a look to that script
70  * if you want to implement your own customized version */
71 void mu_run_suites();
72
73 /* macro for running a single initialization function */
74 #ifndef mu_run_init
75 #define mu_run_init(name) \
76         { \
77                 int name(); \
78                 int r; \
79                 mu_print(MU_CASE, "\t+ Executing initialization function " \
80                                 "'" #name "'...\n"); \
81                 if ((r = name())) { \
82                         mu_print(MU_ERROR, "%s:" #name ": initialization " \
83                                         "function failed (returned %d), " \
84                                         "skipping test suite...\n", \
85                                         mutest_suite_name, r); \
86                         ++mutest_skipped_suites; \
87                         break; \
88                 } \
89         } do { } while (0)
90 #endif /* mu_run_init */
91
92 /* macro for running a single test case */
93 #ifndef mu_run_case
94 #define mu_run_case(name) \
95         do { \
96                 mu_print(MU_CASE, "\t* Executing test case '" #name "'...\n");\
97                 mutest_case_name = #name; \
98                 void name(); \
99                 name(); \
100                 if (mutest_case_failed) { \
101                         ++mutest_failed_cases; \
102                         mutest_suite_failed = 1; \
103                 } else ++mutest_passed_cases; \
104                 mutest_case_failed = 0; \
105         } while (0)
106 #endif /* mu_run_case */
107
108 /* macro for running a single termination function */
109 #ifndef mu_run_term
110 #define mu_run_term(name) \
111         do { \
112                 mu_print(MU_CASE, "\t- Executing termination function '" \
113                                 #name "'...\n"); \
114                 void name(); \
115                 name(); \
116         } while (0)
117 #endif /* mu_run_term */
118
119 /*
120  * mutest exported variables for internal use, do not use directly unless you
121  *  know what you're doing.
122  */
123 extern const char* mutest_suite_name;
124 extern int mutest_failed_suites;
125 extern int mutest_passed_suites;
126 extern int mutest_skipped_suites;
127 extern int mutest_suite_failed;
128 /* test cases */
129 extern const char* mutest_case_name;
130 extern int mutest_failed_cases;
131 extern int mutest_passed_cases;
132 extern int mutest_case_failed;
133 /* checks */
134 extern int mutest_failed_checks;
135 extern int mutest_passed_checks;
136 /* verbosity */
137 extern int mutest_verbose_level;
138
139 #ifdef __cplusplus
140 }
141 #endif
142