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 #ifndef _TINYTEST_MACROS_H
27 #define _TINYTEST_MACROS_H
28
29
30 #define TT_STMT_BEGIN do {
31 #define TT_STMT_END } while (0)
32
33
34
35 #ifndef TT_EXIT_TEST_FUNCTION
36 #define TT_EXIT_TEST_FUNCTION TT_STMT_BEGIN goto end; TT_STMT_END
37 #endif
38
39
40 #ifndef TT_DECLARE
41 #define TT_DECLARE(prefix, args) \
42 TT_STMT_BEGIN \
43 printf("\n %s %s:%d: ",prefix,__FILE__,__LINE__); \
44 printf args ; \
45 TT_STMT_END
46 #endif
47
48
49 #define TT_GRIPE(args) TT_DECLARE("FAIL", args)
50
51
52 #define TT_BLATHER(args) \
53 TT_STMT_BEGIN \
54 if (_tinytest_get_verbosity()>1) TT_DECLARE(" OK", args); \
55 TT_STMT_END
56
57 #define TT_DIE(args) \
58 TT_STMT_BEGIN \
59 _tinytest_set_test_failed(); \
60 TT_GRIPE(args); \
61 TT_EXIT_TEST_FUNCTION; \
62 TT_STMT_END
63
64 #define TT_FAIL(args) \
65 TT_STMT_BEGIN \
66 _tinytest_set_test_failed(); \
67 TT_GRIPE(args); \
68 TT_STMT_END
69
70
71 #define tt_abort_printf(msg) TT_DIE(msg)
72 #define tt_abort_perror(op) TT_DIE(("%s: %s [%d]",(op),strerror(errno), errno))
73 #define tt_abort_msg(msg) TT_DIE(("%s", msg))
74 #define tt_abort() TT_DIE(("%s", "(Failed.)"))
75
76
77 #define tt_fail_printf(msg) TT_FAIL(msg)
78 #define tt_fail_perror(op) TT_FAIL(("%s: %s [%d]",(op),strerror(errno), errno))
79 #define tt_fail_msg(msg) TT_FAIL(("%s", msg))
80 #define tt_fail() TT_FAIL(("%s", "(Failed.)"))
81
82
83 #define tt_skip() \
84 TT_STMT_BEGIN \
85 _tinytest_set_test_skipped(); \
86 TT_EXIT_TEST_FUNCTION; \
87 TT_STMT_END
88
89 #define _tt_want(b, msg, fail) \
90 TT_STMT_BEGIN \
91 if (!(b)) { \
92 _tinytest_set_test_failed(); \
93 TT_GRIPE(("%s",msg)); \
94 fail; \
95 } else { \
96 TT_BLATHER(("%s",msg)); \
97 } \
98 TT_STMT_END
99
100
101 #define tt_want_msg(b, msg) \
102 _tt_want(b, msg, );
103
104
105 #define tt_assert_msg(b, msg) \
106 _tt_want(b, msg, TT_EXIT_TEST_FUNCTION);
107
108
109 #define tt_want(b) tt_want_msg( (b), "want("#b")")
110
111 #define tt_assert(b) tt_assert_msg((b), "assert("#b")")
112
113 #define tt_assert_test_fmt_type(a,b,str_test,type,test,printf_type,printf_fmt, \
114 setup_block,cleanup_block,die_on_fail) \
115 TT_STMT_BEGIN \
116 type _val1 = (type)(a); \
117 type _val2 = (type)(b); \
118 int _tt_status = (test); \
119 if (!_tt_status || _tinytest_get_verbosity()>1) { \
120 printf_type _print; \
121 printf_type _print1; \
122 printf_type _print2; \
123 type _value = _val1; \
124 setup_block; \
125 _print1 = _print; \
126 _value = _val2; \
127 setup_block; \
128 _print2 = _print; \
129 TT_DECLARE(_tt_status?" OK":"FAIL", \
130 ("assert(%s): "printf_fmt" vs "printf_fmt, \
131 str_test, _print1, _print2)); \
132 _print = _print1; \
133 cleanup_block; \
134 _print = _print2; \
135 cleanup_block; \
136 if (!_tt_status) { \
137 _tinytest_set_test_failed(); \
138 die_on_fail ; \
139 } \
140 } \
141 TT_STMT_END
142
143 #define tt_assert_test_type(a,b,str_test,type,test,fmt,die_on_fail) \
144 tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
145 {_print=_value;},{},die_on_fail)
146
147
148
149 #define tt_assert_op_type(a,op,b,type,fmt) \
150 tt_assert_test_type(a,b,#a" "#op" "#b,type,(_val1 op _val2),fmt, \
151 TT_EXIT_TEST_FUNCTION)
152
153 #define tt_int_op(a,op,b) \
154 tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2), \
155 "%ld",TT_EXIT_TEST_FUNCTION)
156
157 #define tt_uint_op(a,op,b) \
158 tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
159 (_val1 op _val2),"%lu",TT_EXIT_TEST_FUNCTION)
160
161 #define tt_ptr_op(a,op,b) \
162 tt_assert_test_type(a,b,#a" "#op" "#b,void*, \
163 (_val1 op _val2),"%p",TT_EXIT_TEST_FUNCTION)
164
165 #define tt_str_op(a,op,b) \
166 tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
167 (strcmp(_val1,_val2) op 0),"<%s>",TT_EXIT_TEST_FUNCTION)
168
169 #define tt_want_int_op(a,op,b) \
170 tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2),"%ld",(void)0)
171
172 #define tt_want_uint_op(a,op,b) \
173 tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
174 (_val1 op _val2),"%lu",(void)0)
175
176 #define tt_want_ptr_op(a,op,b) \
177 tt_assert_test_type(a,b,#a" "#op" "#b,void*, \
178 (_val1 op _val2),"%p",(void)0)
179
180 #define tt_want_str_op(a,op,b) \
181 tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
182 (strcmp(_val1,_val2) op 0),"<%s>",(void)0)
183
184 #endif