Just hand over memory ownership when the caller is supposed to free it (we have befor...
[m6w6/libmemcached] / libtest / test.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Gearmand client and server library.
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2010 Brian Aker
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following disclaimer
18 * in the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * * The names of its contributors may not be used to endorse or
22 * promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39 #pragma once
40
41 #include <libtest/visibility.h>
42
43 /*
44 Structures for generic tests.
45 */
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdint.h>
50
51 #if !defined(__cplusplus)
52 # include <stdbool.h>
53 #endif
54
55 typedef struct world_st world_st;
56 typedef struct collection_st collection_st;
57 typedef struct test_st test_st;
58
59 enum test_return_t {
60 TEST_SUCCESS, /* Backwards compatibility */
61 TEST_FAILURE,
62 TEST_MEMORY_ALLOCATION_FAILURE,
63 TEST_SKIPPED,
64 TEST_FATAL, // Collection should not be continued
65 TEST_MAXIMUM_RETURN /* Always add new error code before */
66 };
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 typedef enum test_return_t test_return_t;
73
74 #ifdef __cplusplus
75 }
76 #endif
77
78
79 typedef void *(*test_callback_create_fn)(test_return_t *error);
80 typedef test_return_t (*test_callback_fn)(void *);
81 typedef test_return_t (*test_callback_runner_fn)(test_callback_fn, void *);
82 typedef test_return_t (*test_callback_error_fn)(test_return_t, void *);
83
84 /**
85 A structure describing the test case.
86 */
87 struct test_st {
88 const char *name;
89 bool requires_flush;
90 test_callback_fn test_fn;
91 };
92
93
94 /**
95 A structure which describes a collection of test cases.
96 */
97 struct collection_st {
98 const char *name;
99 test_callback_fn pre;
100 test_callback_fn post;
101 test_st *tests;
102 };
103
104
105 /**
106 Structure which houses the actual callers for the test cases contained in
107 the collections.
108 */
109 typedef struct {
110 test_callback_runner_fn pre;
111 test_callback_runner_fn run;
112 test_callback_runner_fn post;
113 } world_runner_st;
114
115
116 /**
117 world_st is the structure which is passed to the test implementation to be filled.
118 This must be implemented in order for the test framework to load the tests. We call
119 get_world() in order to fill this structure.
120 */
121
122 struct world_st {
123 collection_st *collections;
124
125 /* These methods are called outside of any collection call. */
126 test_callback_create_fn create;
127 test_callback_fn destroy;
128
129 struct {
130 /* This is called a the beginning of any test run. */
131 test_callback_fn startup;
132
133 /* This called on a test if the test requires a flush call (the bool is from test_st) */
134 test_callback_fn flush;
135
136 /**
137 These are run before/after the test. If implemented. Their execution is not controlled
138 by the test.
139 */
140 test_callback_fn pre_run;
141 test_callback_fn post_run;
142
143 /**
144 If an error occurs during the test, this is called.
145 */
146 test_callback_error_fn on_error;
147 } test;
148
149 struct {
150 /* This is called a the beginning of any collection run. */
151 test_callback_fn startup;
152
153 /* This is called at the end of any collection run. */
154 test_callback_fn shutdown;
155 } collection;
156
157
158 /**
159 Runner represents the callers for the tests. If not implemented we will use
160 a set of default implementations.
161 */
162 world_runner_st *runner;
163 };
164
165
166
167 /**
168 @note world_stats_st is a simple structure for tracking test successes.
169 */
170 typedef struct {
171 uint32_t collection_success;
172 uint32_t collection_skipped;
173 uint32_t collection_failed;
174 uint32_t collection_total;
175 uint32_t success;
176 uint32_t skipped;
177 uint32_t failed;
178 uint32_t total;
179 } world_stats_st;
180
181 #ifdef __cplusplus
182 extern "C" {
183 #endif
184
185 /* Help function for use with gettimeofday() */
186 LIBTEST_API
187 long int timedif(struct timeval a, struct timeval b);
188
189 /* How we make all of this work :) */
190 LIBTEST_API
191 void get_world(world_st *world);
192
193 LIBTEST_INTERNAL_API
194 void create_core(void);
195
196 /**
197 @note Friendly print function for errors.
198 */
199 LIBTEST_API
200 const char *test_strerror(test_return_t code);
201
202 #define test_fail(A) \
203 do \
204 { \
205 if (1) { \
206 fprintf(stderr, "\nFailed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
207 create_core(); \
208 return TEST_FAILURE; \
209 } \
210 } while (0)
211
212 #define test_true(A) \
213 do \
214 { \
215 if (! (A)) { \
216 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
217 create_core(); \
218 return TEST_FAILURE; \
219 } \
220 } while (0)
221
222 #define test_true_got(A,B) \
223 do \
224 { \
225 if (! (A)) { \
226 fprintf(stderr, "\nAssertion failed at %s:%d: \"%s\" received \"%s\"\n", __FILE__, __LINE__, #A, (B));\
227 create_core(); \
228 return TEST_FAILURE; \
229 } \
230 } while (0)
231
232 #define test_compare(A,B) \
233 do \
234 { \
235 if ((A) != (B)) \
236 { \
237 fprintf(stderr, "\n%s:%d: Expected %s, got %lu\n", __FILE__, __LINE__, #A, (unsigned long)(B)); \
238 create_core(); \
239 return TEST_FAILURE; \
240 } \
241 } while (0)
242
243 #define test_skip(A,B) \
244 do \
245 { \
246 if ((A) != (B)) \
247 { \
248 return TEST_SKIPPED; \
249 } \
250 } while (0)
251
252 #define test_compare_got(A,B,C) \
253 do \
254 { \
255 if ((A) != (B)) \
256 { \
257 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, #A, (C)); \
258 create_core(); \
259 return TEST_FAILURE; \
260 } \
261 } while (0)
262
263 #define test_false(A) \
264 do \
265 { \
266 if ((A)) { \
267 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
268 create_core(); \
269 return TEST_FAILURE; \
270 } \
271 } while (0)
272
273 #define test_false_with(A,B) \
274 do \
275 { \
276 if ((A)) { \
277 fprintf(stderr, "\nAssertion failed at %s:%d: %s with %s\n", __FILE__, __LINE__, #A, (B));\
278 create_core(); \
279 return TEST_FAILURE; \
280 } \
281 } while (0)
282
283 #define test_strcmp(A,B) \
284 do \
285 { \
286 if (strcmp((A), (B))) \
287 { \
288 fprintf(stderr, "\n%s:%d: `%s` -> `%s`\n", __FILE__, __LINE__, (A), (B)); \
289 create_core(); \
290 return TEST_FAILURE; \
291 } \
292 } while (0)
293
294 #define test_memcmp(A,B,C) \
295 do \
296 { \
297 if (memcmp((A), (B), (C))) \
298 { \
299 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
300 create_core(); \
301 return TEST_FAILURE; \
302 } \
303 } while (0)
304
305 #define STRINGIFY(x) #x
306 #define TOSTRING(x) STRINGIFY(x)
307 #define AT __FILE__ ":" TOSTRING(__LINE__)
308
309 #ifdef __cplusplus
310 #define STRING_WITH_LEN(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
311 #else
312 #define STRING_WITH_LEN(X) (X), ((size_t)((sizeof(X) - 1)))
313 #endif
314
315 #ifdef __cplusplus
316 #define STRING_PARAM_WITH_LEN(X) X, static_cast<size_t>(sizeof(X) - 1)
317 #else
318 #define STRING_PARAM_WITH_LEN(X) X, (size_t)((sizeof(X) - 1))
319 #endif
320
321 #ifdef __cplusplus
322 }
323 #endif