-
Notifications
You must be signed in to change notification settings - Fork 3
/
minunit_ext.h
125 lines (113 loc) · 3.4 KB
/
minunit_ext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
*
* \file minunit_ext.h
* \brief JTN002 - MinUnit -- a minimal unit testing framework for C
*
* This is an extended version of MinUnit.
* (see http://www.jera.com/techinfo/jtns/jtn002.html)
*
* \author Johannes Schlatow <[email protected]>
*/
/*
* Configuration:
*
* You should modify the MU_PRINT_* macros to map to your preferred output functions.
* E.g. you might want to use printf.
*
*/
/* Usage example:
*
* // test functions
* static MU_RET test_1() {
* MU_ASSERT("This will fail", 0==1);
* return MU_NULL;
* }
*
* static MU_RET test_2() {
* MU_ASSERT("This will succeed", true);
* return MU_NULL;
* }
*
* // you can specify tests that serves as
* // init or cleanup functions
* static MU_RET init() {
* // [...]
* }
*
* static MU_RET cleanup() {
* // [...]
* }
*
* // defining a suite
* MU_RET my_test_suite() {
* mu_tests_run = 0;
* mu_tests = 4;
*
* MU_RUN_TEST(init);
*
* MU_RUN_TEST(test_1);
* MU_RUN_TEST(test_2);
*
* MU_RUN_TEST(cleanup);
* return MU_NULL;
* }
*
* int main() {
* MU_RET result = my_test_suite();
* if (result != MU_NULL) {
* printf("%s\n", result);
* } else {
* printf("ALL TESTS PASSED\n");
* }
*
* printf("Tests run: %d of %d\n", mu_tests_run, mu_tests);
*
* return mu_tests-mu_tests_run;
* }
*
*/
#ifndef MINUNIT_H
#define MINUNIT_H
/// default return value
#define MU_NULL NULL
/// return type
#define MU_RET char*
/// enable/disable output of succeeded tests and assertions
#ifdef MU_PRINT
#define MU_PRINT_ERROR(msg) DEBUG_PRINT(DEBUG_LEVEL_ERROR, msg)
#define MU_PRINT_INFO(msg) DEBUG_PRINT(DEBUG_LEVEL_INFO, msg)
#define MU_PRINT_VERBOSE(msg) DEBUG_PRINT(DEBUG_LEVEL_VERBOSE, msg)
#else
#define MU_PRINT_ERROR(msg)
#define MU_PRINT_INFO(msg)
#define MU_PRINT_VERBOSE(msg)
#endif
/// helpers
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__":"TOSTRING(__LINE__)
/// classic assertion, use within tests
#define MU_ASSERT(message, test) do { if (!(test)) { \
return "Assertion failed: \""message"\" ("#test")\n" \
" in "AT"\n"; \
} else MU_PRINT_VERBOSE("Assertion ok: \""message"\" ("#test")\n"); } \
while (0)
/// assert and print output directly; use within bg threads
#define MU_ASSERT_PRINT(message, test) do { if (!(test)) { \
MU_PRINT_ERROR("Assertion failed: \""message"\" ("#test")\n" \
" in "AT"\n"); \
} else MU_PRINT_VERBOSE("Assertion ok: \""message"\" ("#test")\n"); } \
while (0)
/// run a test function
//// remark: the do-while construct might be surprising; this is a standard C idiom
//// for writing a macro that contains multiple statements.
#define MU_RUN_TEST(test) do { char *message = test(); \
if (message) return message; \
MU_PRINT_INFO("Test ok: \""#test"\"\n"); \
mu_tests_run++; } \
while (0)
/// test counter
extern int mu_tests_run;
/// total number of tests
extern int mu_tests;
#endif