Merge in all changes related to being able to read configuration files.
[awesomized/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 typedef enum {
60 TEST_SUCCESS= 0, /* Backwards compatibility */
61 TEST_FAILURE,
62 TEST_MEMORY_ALLOCATION_FAILURE,
63 TEST_SKIPPED,
64 TEST_MAXIMUM_RETURN /* Always add new error code before */
65 } test_return_t;
66
67 typedef void *(*test_callback_create_fn)(test_return_t *error);
68 typedef test_return_t (*test_callback_fn)(void *);
69 typedef test_return_t (*test_callback_runner_fn)(test_callback_fn, void *);
70 typedef test_return_t (*test_callback_error_fn)(test_return_t, void *);
71
72 /**
73 A structure describing the test case.
74 */
75 struct test_st {
76 const char *name;
77 bool requires_flush;
78 test_callback_fn test_fn;
79 };
80
81
82 /**
83 A structure which describes a collection of test cases.
84 */
85 struct collection_st {
86 const char *name;
87 test_callback_fn pre;
88 test_callback_fn post;
89 test_st *tests;
90 };
91
92
93 /**
94 Structure which houses the actual callers for the test cases contained in
95 the collections.
96 */
97 typedef struct {
98 test_callback_runner_fn pre;
99 test_callback_runner_fn run;
100 test_callback_runner_fn post;
101 } world_runner_st;
102
103
104 /**
105 world_st is the structure which is passed to the test implementation to be filled.
106 This must be implemented in order for the test framework to load the tests. We call
107 get_world() in order to fill this structure.
108 */
109
110 struct world_st {
111 collection_st *collections;
112
113 /* These methods are called outside of any collection call. */
114 test_callback_create_fn create;
115 test_callback_fn destroy;
116
117 struct {
118 /* This is called a the beginning of any test run. */
119 test_callback_fn startup;
120
121 /* This called on a test if the test requires a flush call (the bool is from test_st) */
122 test_callback_fn flush;
123
124 /**
125 These are run before/after the test. If implemented. Their execution is not controlled
126 by the test.
127 */
128 test_callback_fn pre_run;
129 test_callback_fn post_run;
130
131 /**
132 If an error occurs during the test, this is called.
133 */
134 test_callback_error_fn on_error;
135 } test;
136
137 struct {
138 /* This is called a the beginning of any collection run. */
139 test_callback_fn startup;
140
141 /* This is called at the end of any collection run. */
142 test_callback_fn shutdown;
143 } collection;
144
145
146 /**
147 Runner represents the callers for the tests. If not implemented we will use
148 a set of default implementations.
149 */
150 world_runner_st *runner;
151 };
152
153
154
155 /**
156 @note world_stats_st is a simple structure for tracking test successes.
157 */
158 typedef struct {
159 uint32_t collection_success;
160 uint32_t collection_skipped;
161 uint32_t collection_failed;
162 uint32_t collection_total;
163 uint32_t success;
164 uint32_t skipped;
165 uint32_t failed;
166 uint32_t total;
167 } world_stats_st;
168
169 #ifdef __cplusplus
170 extern "C" {
171 #endif
172
173 /* Help function for use with gettimeofday() */
174 LIBTEST_API
175 long int timedif(struct timeval a, struct timeval b);
176
177 /* How we make all of this work :) */
178 LIBTEST_API
179 void get_world(world_st *world);
180
181 LIBTEST_INTERNAL_API
182 void create_core(void);
183
184 /**
185 @note Friendly print function for errors.
186 */
187 LIBTEST_API
188 const char *test_strerror(test_return_t code);
189
190 #define test_fail(A) \
191 do \
192 { \
193 if (1) { \
194 fprintf(stderr, "\nFailed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
195 create_core(); \
196 return TEST_FAILURE; \
197 } \
198 } while (0)
199
200 #define test_true(A) \
201 do \
202 { \
203 if (! (A)) { \
204 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
205 create_core(); \
206 return TEST_FAILURE; \
207 } \
208 } while (0)
209
210 #define test_true_got(A,B) \
211 do \
212 { \
213 if (! (A)) { \
214 fprintf(stderr, "\nAssertion failed at %s:%d: \"%s\" received \"%s\"\n", __FILE__, __LINE__, #A, (B));\
215 create_core(); \
216 return TEST_FAILURE; \
217 } \
218 } while (0)
219
220 #define test_false(A) \
221 do \
222 { \
223 if ((A)) { \
224 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
225 create_core(); \
226 return TEST_FAILURE; \
227 } \
228 } while (0)
229
230 #define test_false_with(A,B) \
231 do \
232 { \
233 if ((A)) { \
234 fprintf(stderr, "\nAssertion failed at %s:%d: %s with %s\n", __FILE__, __LINE__, #A, (B));\
235 create_core(); \
236 return TEST_FAILURE; \
237 } \
238 } while (0)
239
240 #define test_strcmp(A,B) \
241 do \
242 { \
243 if (strcmp((A), (B))) \
244 { \
245 fprintf(stderr, "\n%s:%d: %s -> %s\n", __FILE__, __LINE__, (A), (B)); \
246 create_core(); \
247 return TEST_FAILURE; \
248 } \
249 } while (0)
250
251
252 #define STRINGIFY(x) #x
253 #define TOSTRING(x) STRINGIFY(x)
254 #define AT __FILE__ ":" TOSTRING(__LINE__)
255
256 #ifdef __cplusplus
257 #define STRING_WITH_LEN(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
258 #else
259 #define STRING_WITH_LEN(X) (X), ((size_t)((sizeof(X) - 1)))
260 #endif
261
262 #ifdef __cplusplus
263 #define STRING_PARAM_WITH_LEN(X) X, static_cast<size_t>(sizeof(X) - 1)
264 #else
265 #define STRING_PARAM_WITH_LEN(X) X, (size_t)((sizeof(X) - 1))
266 #endif
267
268 #ifdef __cplusplus
269 }
270 #endif