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