Update libtest/fedora/lion fixes
[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 #include <libtest/stream.h>
30 #include <libtest/comparison.hpp>
31
32 /**
33 A structure describing the test case.
34 */
35 struct test_st {
36 const char *name;
37 bool requires_flush;
38 test_callback_fn *test_fn;
39 };
40
41 #define test_assert_errno(A) \
42 do \
43 { \
44 if ((A)) { \
45 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
46 perror(#A); \
47 fprintf(stderr, "\n"); \
48 create_core(); \
49 assert((A)); \
50 } \
51 } while (0)
52
53 #define test_assert(A, B) \
54 do \
55 { \
56 if ((A)) { \
57 fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
58 fprintf(stderr, "\n"); \
59 create_core(); \
60 assert((A)); \
61 } \
62 } while (0)
63
64 #define test_truth(A) \
65 do \
66 { \
67 if (! (A)) { \
68 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
69 create_core(); \
70 return TEST_FAILURE; \
71 } \
72 } while (0)
73
74 #define test_true(A) \
75 do \
76 { \
77 if (! (A)) { \
78 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
79 create_core(); \
80 return TEST_FAILURE; \
81 } \
82 } while (0)
83
84 #define test_true_got(__expected, __hint) \
85 do \
86 { \
87 if (not libtest::_compare_true_hint(__FILE__, __LINE__, __func__, ((__expected)), #__expected, ((__hint)))) \
88 { \
89 create_core(); \
90 return TEST_FAILURE; \
91 } \
92 } while (0)
93
94 #define test_skip(A,B) \
95 do \
96 { \
97 if ((A) != (B)) \
98 { \
99 return TEST_SKIPPED; \
100 } \
101 } while (0)
102
103 #define test_fail(A) \
104 do \
105 { \
106 if (1) { \
107 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
108 create_core(); \
109 return TEST_FAILURE; \
110 } \
111 } while (0)
112
113
114 #define test_false(A) \
115 do \
116 { \
117 if ((A)) { \
118 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
119 create_core(); \
120 return TEST_FAILURE; \
121 } \
122 } while (0)
123
124 #define test_false_with(A,B) \
125 do \
126 { \
127 if ((A)) { \
128 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
129 create_core(); \
130 return TEST_FAILURE; \
131 } \
132 } while (0)
133
134 #define test_compare(__expected, __actual) \
135 do \
136 { \
137 if (not libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)))) \
138 { \
139 create_core(); \
140 return TEST_FAILURE; \
141 } \
142 } while (0)
143
144 #define test_zero(__actual) \
145 do \
146 { \
147 if (not libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual)))) \
148 { \
149 create_core(); \
150 return TEST_FAILURE; \
151 } \
152 } while (0)
153
154 #define test_compare_got(__expected, __actual, __hint) \
155 do \
156 { \
157 if (not libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint))) \
158 { \
159 create_core(); \
160 return TEST_FAILURE; \
161 } \
162 } while (0)
163
164
165 #define test_strcmp(A,B) \
166 do \
167 { \
168 if (strcmp((A), (B))) \
169 { \
170 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, (A), (B)); \
171 create_core(); \
172 return TEST_FAILURE; \
173 } \
174 } while (0)
175
176 #define test_memcmp(A,B,C) \
177 do \
178 { \
179 if (memcmp((A), (B), (C))) \
180 { \
181 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
182 create_core(); \
183 return TEST_FAILURE; \
184 } \
185 } while (0)
186
187 #define test_return_if(__test_return_t) \
188 do \
189 { \
190 if ((__test_return_t) != TEST_SUCCESS) \
191 { \
192 return __test_return_t; \
193 } \
194 } while (0)
195