m4: -fmudflip is long since gone
[awesomized/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 #include <libtest/lite.h>
44
45 /**
46 A structure describing the test case.
47 */
48 struct test_st {
49 const char *name;
50 bool requires_flush;
51 test_callback_fn *test_fn;
52 };
53
54 #define test_assert_errno(A) \
55 do \
56 { \
57 if ((A)) { \
58 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
59 perror(#A); \
60 fprintf(stderr, "\n"); \
61 libtest::create_core(); \
62 assert((A)); \
63 } \
64 } while (0)
65
66 #define test_truth(A) \
67 do \
68 { \
69 if (! (A)) { \
70 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
71 libtest::create_core(); \
72 return TEST_FAILURE; \
73 } \
74 } while (0)
75
76 #define test_true(A) \
77 do \
78 { \
79 if (! (A)) { \
80 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
81 libtest::create_core(); \
82 return TEST_FAILURE; \
83 } \
84 } while (0)
85
86 #define test_true_got(A, B) test_true(A);
87 #define test_true_hint(A, B) test_true(A);
88
89 #define test_compare_hint(A, B, C) test_compare(A, B);
90 #define test_compare_got(A, B, C) test_compare(A, B);
91
92 #define test_skip(__expected, __actual) \
93 do \
94 { \
95 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), false) == false) \
96 { \
97 return TEST_SKIPPED; \
98 } \
99 } while (0)
100
101 #define test_skip_valgrind() \
102 do \
103 { \
104 if (libtest::_in_valgrind(__FILE__, __LINE__, __func__)) \
105 { \
106 return TEST_SKIPPED; \
107 } \
108 } while (0)
109
110 #define test_fail(A) \
111 do \
112 { \
113 if (1) { \
114 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
115 libtest::create_core(); \
116 return TEST_FAILURE; \
117 } \
118 } while (0)
119
120
121 #define test_false(A) \
122 do \
123 { \
124 if ((A)) { \
125 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
126 libtest::create_core(); \
127 return TEST_FAILURE; \
128 } \
129 } while (0)
130
131 #define test_false_with(A,B) \
132 do \
133 { \
134 if ((A)) { \
135 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
136 libtest::create_core(); \
137 return TEST_FAILURE; \
138 } \
139 } while (0)
140
141 #define test_ne_compare(__expected, __actual) \
142 do \
143 { \
144 if (libtest::_ne_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
145 { \
146 libtest::create_core(); \
147 return TEST_FAILURE; \
148 } \
149 } while (0)
150
151 #define test_compare(__expected, __actual) \
152 do \
153 { \
154 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
155 { \
156 libtest::create_core(); \
157 return TEST_FAILURE; \
158 } \
159 } while (0)
160
161 #define test_zero(__actual) \
162 do \
163 { \
164 if (libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual))) == false) \
165 { \
166 libtest::create_core(); \
167 return TEST_FAILURE; \
168 } \
169 } while (0)
170
171 #define test_null test_zero
172
173 #define test_compare_warn(__expected, __actual) \
174 do \
175 { \
176 void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual)), true); \
177 } while (0)
178
179 #define test_warn(__truth, __explain) \
180 do \
181 { \
182 void(libtest::_assert_truth(__FILE__, __LINE__, __func__, bool((__truth)), #__truth, __explain)); \
183 } while (0)
184
185 #define test_strcmp(__expected, __actual) \
186 do \
187 { \
188 void(libtest::_compare_strcmp(__FILE__, __LINE__, __func__, (__expected), (__actual))); \
189 } while (0)
190
191 #define test_memcmp(A,B,C) \
192 do \
193 { \
194 if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
195 { \
196 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
197 libtest::create_core(); \
198 return TEST_FAILURE; \
199 } \
200 } while (0)
201
202 #define test_return_if(__test_return_t) \
203 do \
204 { \
205 if ((__test_return_t) != TEST_SUCCESS) \
206 { \
207 return __test_return_t; \
208 } \
209 } while (0)
210