1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Gearmand client and server library.
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2010 Brian Aker
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
21 * * The names of its contributors may not be used to endorse or
22 * promote products derived from this software without specific prior
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.
41 #include <libtest/visibility.h>
44 Structures for generic tests.
51 #if !defined(__cplusplus)
55 typedef struct world_st world_st
;
56 typedef struct collection_st collection_st
;
57 typedef struct test_st test_st
;
60 TEST_SUCCESS
, /* Backwards compatibility */
62 TEST_MEMORY_ALLOCATION_FAILURE
,
64 TEST_FATAL
, // Collection should not be continued
65 TEST_MAXIMUM_RETURN
/* Always add new error code before */
72 typedef enum test_return_t test_return_t
;
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 *);
85 A structure describing the test case.
90 test_callback_fn test_fn
;
95 A structure which describes a collection of test cases.
97 struct collection_st
{
100 test_callback_fn post
;
106 Structure which houses the actual callers for the test cases contained in
110 test_callback_runner_fn pre
;
111 test_callback_runner_fn run
;
112 test_callback_runner_fn post
;
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.
123 collection_st
*collections
;
125 /* These methods are called outside of any collection call. */
126 test_callback_create_fn create
;
127 test_callback_fn destroy
;
130 /* This is called a the beginning of any test run. */
131 test_callback_fn startup
;
133 /* This called on a test if the test requires a flush call (the bool is from test_st) */
134 test_callback_fn flush
;
137 These are run before/after the test. If implemented. Their execution is not controlled
140 test_callback_fn pre_run
;
141 test_callback_fn post_run
;
144 If an error occurs during the test, this is called.
146 test_callback_error_fn on_error
;
150 /* This is called a the beginning of any collection run. */
151 test_callback_fn startup
;
153 /* This is called at the end of any collection run. */
154 test_callback_fn shutdown
;
159 Runner represents the callers for the tests. If not implemented we will use
160 a set of default implementations.
162 world_runner_st
*runner
;
168 @note world_stats_st is a simple structure for tracking test successes.
171 uint32_t collection_success
;
172 uint32_t collection_skipped
;
173 uint32_t collection_failed
;
174 uint32_t collection_total
;
185 /* Help function for use with gettimeofday() */
187 long int timedif(struct timeval a
, struct timeval b
);
189 /* How we make all of this work :) */
191 void get_world(world_st
*world
);
194 void create_core(void);
197 @note Friendly print function for errors.
200 const char *test_strerror(test_return_t code
);
202 #define test_fail(A) \
206 fprintf(stderr, "\nFailed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
208 return TEST_FAILURE; \
212 #define test_true(A) \
216 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
218 return TEST_FAILURE; \
222 #define test_true_got(A,B) \
226 fprintf(stderr, "\nAssertion failed at %s:%d: \"%s\" received \"%s\"\n", __FILE__, __LINE__, #A, (B));\
228 return TEST_FAILURE; \
232 #define test_compare(A,B) \
237 fprintf(stderr, "\n%s:%d: Expected %s, got %lu\n", __FILE__, __LINE__, #A, (unsigned long)(B)); \
239 return TEST_FAILURE; \
243 #define test_skip(A,B) \
248 return TEST_SKIPPED; \
252 #define test_compare_got(A,B,C) \
257 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, #A, (C)); \
259 return TEST_FAILURE; \
263 #define test_false(A) \
267 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
269 return TEST_FAILURE; \
273 #define test_false_with(A,B) \
277 fprintf(stderr, "\nAssertion failed at %s:%d: %s with %s\n", __FILE__, __LINE__, #A, (B));\
279 return TEST_FAILURE; \
283 #define test_strcmp(A,B) \
286 if (strcmp((A), (B))) \
288 fprintf(stderr, "\n%s:%d: `%s` -> `%s`\n", __FILE__, __LINE__, (A), (B)); \
290 return TEST_FAILURE; \
294 #define test_memcmp(A,B,C) \
297 if (memcmp((A), (B), (C))) \
299 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
301 return TEST_FAILURE; \
305 #define STRINGIFY(x) #x
306 #define TOSTRING(x) STRINGIFY(x)
307 #define AT __FILE__ ":" TOSTRING(__LINE__)
310 #define STRING_WITH_LEN(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
312 #define STRING_WITH_LEN(X) (X), ((size_t)((sizeof(X) - 1)))
316 #define STRING_PARAM_WITH_LEN(X) X, static_cast<size_t>(sizeof(X) - 1)
318 #define STRING_PARAM_WITH_LEN(X) X, (size_t)((sizeof(X) - 1))