Import/Merge of libtest latest.
[m6w6/libmemcached] / libtest / test.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #pragma once
38
39 #ifndef __INTEL_COMPILER
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
41 #endif
42
43 /**
44 A structure describing the test case.
45 */
46 struct test_st {
47 const char *name;
48 bool requires_flush;
49 test_callback_fn *test_fn;
50 };
51
52 #define test_assert_errno(A) \
53 do \
54 { \
55 if ((A)) { \
56 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
57 perror(#A); \
58 fprintf(stderr, "\n"); \
59 libtest::create_core(); \
60 assert((A)); \
61 } \
62 } while (0)
63
64 #define test_assert(A, B) \
65 do \
66 { \
67 if ((A)) { \
68 fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
69 fprintf(stderr, "\n"); \
70 libtest::create_core(); \
71 assert((A)); \
72 } \
73 } while (0)
74
75 #define test_truth(A) \
76 do \
77 { \
78 if (! (A)) { \
79 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
80 libtest::create_core(); \
81 return TEST_FAILURE; \
82 } \
83 } while (0)
84
85 #define test_true(A) \
86 do \
87 { \
88 if (! (A)) { \
89 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
90 libtest::create_core(); \
91 return TEST_FAILURE; \
92 } \
93 } while (0)
94
95 #define test_true_got(A, B) test_true(A);
96 #define test_true_hint(A, B) test_true(A);
97
98 #define test_compare_hint(A, B, C) test_compare(A, B);
99 #define test_compare_got(A, B, C) test_compare(A, B);
100
101 #define test_skip(__expected, __actual) \
102 do \
103 { \
104 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), false) == false) \
105 { \
106 return TEST_SKIPPED; \
107 } \
108 } while (0)
109
110 #define test_skip_valgrind() \
111 do \
112 { \
113 if (libtest::_in_valgrind(__FILE__, __LINE__, __func__)) \
114 { \
115 return TEST_SKIPPED; \
116 } \
117 } while (0)
118
119 #define test_fail(A) \
120 do \
121 { \
122 if (1) { \
123 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
124 libtest::create_core(); \
125 return TEST_FAILURE; \
126 } \
127 } while (0)
128
129
130 #define test_false(A) \
131 do \
132 { \
133 if ((A)) { \
134 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
135 libtest::create_core(); \
136 return TEST_FAILURE; \
137 } \
138 } while (0)
139
140 #define test_false_with(A,B) \
141 do \
142 { \
143 if ((A)) { \
144 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
145 libtest::create_core(); \
146 return TEST_FAILURE; \
147 } \
148 } while (0)
149
150 #define test_ne_compare(__expected, __actual) \
151 do \
152 { \
153 if (libtest::_ne_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
154 { \
155 libtest::create_core(); \
156 return TEST_FAILURE; \
157 } \
158 } while (0)
159
160 #define test_compare(__expected, __actual) \
161 do \
162 { \
163 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
164 { \
165 libtest::create_core(); \
166 return TEST_FAILURE; \
167 } \
168 } while (0)
169
170 #define test_zero(__actual) \
171 do \
172 { \
173 if (not libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual)))) \
174 { \
175 libtest::create_core(); \
176 return TEST_FAILURE; \
177 } \
178 } while (0)
179
180 #define test_null test_zero
181
182 #define test_compare_warn(__expected, __actual) \
183 do \
184 { \
185 void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual)), true); \
186 } while (0)
187
188 #define test_warn(__truth, __explain) \
189 do \
190 { \
191 void(libtest::_assert_truth(__FILE__, __LINE__, __func__, bool((__truth)), #__truth, __explain)); \
192 } while (0)
193
194 #define test_strcmp(__expected, __actual) \
195 do \
196 { \
197 void(libtest::_compare_strcmp(__FILE__, __LINE__, __func__, (__expected), (__actual))); \
198 } while (0)
199
200 #define test_memcmp(A,B,C) \
201 do \
202 { \
203 if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
204 { \
205 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
206 libtest::create_core(); \
207 return TEST_FAILURE; \
208 } \
209 } while (0)
210
211 #define test_return_if(__test_return_t) \
212 do \
213 { \
214 if ((__test_return_t) != TEST_SUCCESS) \
215 { \
216 return __test_return_t; \
217 } \
218 } while (0)
219