Merge in all of libtest updates.
[m6w6/libmemcached] / libtest / test.h
1 /*
2 * uTest Copyright (C) 2011 Data Differential, http://datadifferential.com/
3 *
4 * Use and distribution licensed under the BSD license. See
5 * the COPYING file in the parent directory for full text.
6 */
7
8 #pragma once
9
10 #ifndef __INTEL_COMPILER
11 #pragma GCC diagnostic ignored "-Wold-style-cast"
12 #endif
13
14 #include <libtest/stream.h>
15
16 LIBTEST_API
17 const char* default_socket();
18
19 LIBTEST_API
20 void set_default_socket(const char *socket);
21
22
23 /**
24 A structure describing the test case.
25 */
26 struct test_st {
27 const char *name;
28 bool requires_flush;
29 test_callback_fn *test_fn;
30 };
31
32 LIBTEST_API
33 bool test_is_local(void);
34
35 #define test_assert_errno(A) \
36 do \
37 { \
38 if ((A)) { \
39 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
40 perror(#A); \
41 fprintf(stderr, "\n"); \
42 create_core(); \
43 assert((A)); \
44 } \
45 } while (0)
46
47 #define test_assert(A, B) \
48 do \
49 { \
50 if ((A)) { \
51 fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
52 fprintf(stderr, "\n"); \
53 create_core(); \
54 assert((A)); \
55 } \
56 } while (0)
57
58 #define test_truth(A) \
59 do \
60 { \
61 if (! (A)) { \
62 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
63 create_core(); \
64 return TEST_FAILURE; \
65 } \
66 } while (0)
67
68 #define test_true(A) \
69 do \
70 { \
71 if (! (A)) { \
72 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
73 create_core(); \
74 return TEST_FAILURE; \
75 } \
76 } while (0)
77
78 template <class T_comparable, class T_hint>
79 bool _true_hint(const char *file, int line, const char *func, T_comparable __expected, const char *assertation_label, T_hint __hint)
80 {
81 if (__expected == false)
82 {
83 libtest::stream::make_cerr(file, line, func) << "Assertation \"" << assertation_label << "\" failed, hint: " << __hint;
84 return false;
85 }
86
87 return true;
88 }
89
90 #define test_true_got(__expected, __hint) \
91 do \
92 { \
93 if (not _true_hint(__FILE__, __LINE__, __func__, ((__expected)), #__expected, ((__hint)))) \
94 { \
95 create_core(); \
96 return TEST_FAILURE; \
97 } \
98 } while (0)
99
100 #define test_skip(A,B) \
101 do \
102 { \
103 if ((A) != (B)) \
104 { \
105 return TEST_SKIPPED; \
106 } \
107 } while (0)
108
109 #define test_fail(A) \
110 do \
111 { \
112 if (1) { \
113 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
114 create_core(); \
115 return TEST_FAILURE; \
116 } \
117 } while (0)
118
119
120 #define test_false(A) \
121 do \
122 { \
123 if ((A)) { \
124 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
125 create_core(); \
126 return TEST_FAILURE; \
127 } \
128 } while (0)
129
130 #define test_false_with(A,B) \
131 do \
132 { \
133 if ((A)) { \
134 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
135 create_core(); \
136 return TEST_FAILURE; \
137 } \
138 } while (0)
139
140 template <class T_comparable>
141 bool _compare(const char *file, int line, const char *func, T_comparable __expected, T_comparable __actual)
142 {
143 if (__expected != __actual)
144 {
145 libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\"";
146 return false;
147 }
148
149 return true;
150 }
151
152 template <class T_comparable>
153 bool _compare_zero(const char *file, int line, const char *func, T_comparable __actual)
154 {
155 if (T_comparable(0) != __actual)
156 {
157 libtest::stream::make_cerr(file, line, func) << "Expected 0 got \"" << __actual << "\"";
158 return false;
159 }
160
161 return true;
162 }
163
164 template <class T_comparable, class T_hint>
165 bool _compare_hint(const char *file, int line, const char *func, T_comparable __expected, T_comparable __actual, T_hint __hint)
166 {
167 if (__expected != __actual)
168 {
169 libtest::stream::make_cerr(file, line, func) << "Expected \"" << __expected << "\" got \"" << __actual << "\" Additionally: \"" << __hint << "\"";
170
171 return false;
172 }
173
174 return true;
175 }
176
177 #define test_compare(__expected, __actual) \
178 do \
179 { \
180 if (not _compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)))) \
181 { \
182 create_core(); \
183 return TEST_FAILURE; \
184 } \
185 } while (0)
186
187 #define test_zero(__actual) \
188 do \
189 { \
190 if (not _compare_zero(__FILE__, __LINE__, __func__, ((__actual)))) \
191 { \
192 create_core(); \
193 return TEST_FAILURE; \
194 } \
195 } while (0)
196
197 #define test_compare_got(__expected, __actual, __hint) \
198 do \
199 { \
200 if (not _compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint))) \
201 { \
202 create_core(); \
203 return TEST_FAILURE; \
204 } \
205 } while (0)
206
207
208 #define test_strcmp(A,B) \
209 do \
210 { \
211 if (strcmp((A), (B))) \
212 { \
213 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, (A), (B)); \
214 create_core(); \
215 return TEST_FAILURE; \
216 } \
217 } while (0)
218
219 #define test_memcmp(A,B,C) \
220 do \
221 { \
222 if (memcmp((A), (B), (C))) \
223 { \
224 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
225 create_core(); \
226 return TEST_FAILURE; \
227 } \
228 } while (0)
229
230 #define test_return_if(__test_return_t) \
231 do \
232 { \
233 if ((__test_return_t) != TEST_SUCCESS) \
234 { \
235 return __test_return_t; \
236 } \
237 } while (0)
238