Updating for 1.0.2 release
[awesomized/libmemcached] / libtest / test.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #pragma once
24
25 #ifndef __INTEL_COMPILER
26 #pragma GCC diagnostic ignored "-Wold-style-cast"
27 #endif
28
29 /**
30 A structure describing the test case.
31 */
32 struct test_st {
33 const char *name;
34 bool requires_flush;
35 test_callback_fn *test_fn;
36 };
37
38 #define test_assert_errno(A) \
39 do \
40 { \
41 if ((A)) { \
42 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
43 perror(#A); \
44 fprintf(stderr, "\n"); \
45 libtest::create_core(); \
46 assert((A)); \
47 } \
48 } while (0)
49
50 #define test_assert(A, B) \
51 do \
52 { \
53 if ((A)) { \
54 fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
55 fprintf(stderr, "\n"); \
56 libtest::create_core(); \
57 assert((A)); \
58 } \
59 } while (0)
60
61 #define test_truth(A) \
62 do \
63 { \
64 if (! (A)) { \
65 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
66 libtest::create_core(); \
67 return TEST_FAILURE; \
68 } \
69 } while (0)
70
71 #define test_true(A) \
72 do \
73 { \
74 if (! (A)) { \
75 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
76 libtest::create_core(); \
77 return TEST_FAILURE; \
78 } \
79 } while (0)
80
81 #define test_true_got(__expected, __hint) \
82 do \
83 { \
84 if (not libtest::_compare_truth_hint(__FILE__, __LINE__, __func__, ((__expected)), #__expected, ((__hint)))) \
85 { \
86 libtest::create_core(); \
87 return TEST_FAILURE; \
88 } \
89 } while (0)
90
91 #define test_skip(A,B) \
92 do \
93 { \
94 if ((A) != (B)) \
95 { \
96 return TEST_SKIPPED; \
97 } \
98 } while (0)
99
100 #define test_fail(A) \
101 do \
102 { \
103 if (1) { \
104 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
105 libtest::create_core(); \
106 return TEST_FAILURE; \
107 } \
108 } while (0)
109
110
111 #define test_false(A) \
112 do \
113 { \
114 if ((A)) { \
115 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
116 libtest::create_core(); \
117 return TEST_FAILURE; \
118 } \
119 } while (0)
120
121 #define test_false_with(A,B) \
122 do \
123 { \
124 if ((A)) { \
125 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
126 libtest::create_core(); \
127 return TEST_FAILURE; \
128 } \
129 } while (0)
130
131 #define test_compare(__expected, __actual) \
132 do \
133 { \
134 if (not libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)))) \
135 { \
136 libtest::create_core(); \
137 return TEST_FAILURE; \
138 } \
139 } while (0)
140
141 #define test_zero(__actual) \
142 do \
143 { \
144 if (not libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual)))) \
145 { \
146 libtest::create_core(); \
147 return TEST_FAILURE; \
148 } \
149 } while (0)
150
151 #define test_null test_zero
152
153 #define test_compare_got(__expected, __actual, __hint) \
154 do \
155 { \
156 if (not libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint))) \
157 { \
158 libtest::create_core(); \
159 return TEST_FAILURE; \
160 } \
161 } while (0)
162
163 #define test_compare_hint test_compare_got
164
165 #define test_compare_warn(__expected, __actual) \
166 do \
167 { \
168 void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual))); \
169 } while (0)
170
171 #define test_compare_warn_hint(__expected, __actual, __hint) \
172 do \
173 { \
174 libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint)); \
175 } while (0)
176
177 #define test_warn(__truth) \
178 do \
179 { \
180 void(libtest::_truth(__FILE__, __LINE__, __func__, (__truth))); \
181 } while (0)
182
183 #define test_warn_hint(__truth, __hint) \
184 do \
185 { \
186 void(libtest::_compare_truth_hint(__FILE__, __LINE__, __func__, (__truth), #__truth, (__hint))); \
187 } while (0)
188
189
190 #define test_strcmp(A,B) \
191 do \
192 { \
193 if (strcmp((A), (B))) \
194 { \
195 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, (A), (B)); \
196 libtest::create_core(); \
197 return TEST_FAILURE; \
198 } \
199 } while (0)
200
201 #define test_memcmp(A,B,C) \
202 do \
203 { \
204 if (memcmp((A), (B), (C))) \
205 { \
206 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
207 libtest::create_core(); \
208 return TEST_FAILURE; \
209 } \
210 } while (0)
211
212 #define test_return_if(__test_return_t) \
213 do \
214 { \
215 if ((__test_return_t) != TEST_SUCCESS) \
216 { \
217 return __test_return_t; \
218 } \
219 } while (0)
220