2 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
11 Structures for generic tests.
18 #include <libtest/visibility.h>
23 TEST_SUCCESS
= 0, /* Backwards compatibility */
25 TEST_MEMORY_ALLOCATION_FAILURE
,
27 TEST_FATAL
, // Collection should not be continued
28 TEST_MAXIMUM_RETURN
/* Always add new error code before */
35 typedef void* (test_callback_create_fn
)(enum test_return_t
*);
36 typedef enum test_return_t (test_callback_fn
)(void *);
37 typedef enum test_return_t (test_callback_runner_fn
)(test_callback_fn
*, void *);
38 typedef enum test_return_t (test_callback_error_fn
)(const enum test_return_t
, void *);
45 A structure describing the test case.
50 test_callback_fn
*test_fn
;
55 A structure which describes a collection of test cases.
57 struct collection_st
{
59 test_callback_fn
*pre
;
60 test_callback_fn
*post
;
61 struct test_st
*tests
;
66 Structure which houses the actual callers for the test cases contained in
69 struct world_runner_st
{
70 test_callback_runner_fn
*pre
;
71 test_callback_runner_fn
*run
;
72 test_callback_runner_fn
*post
;
77 world_st is the structure which is passed to the test implementation to be filled.
78 This must be implemented in order for the test framework to load the tests. We call
79 get_world() in order to fill this structure.
83 collection_st
*collections
;
85 /* These methods are called outside of any collection call. */
86 test_callback_create_fn
*create
;
87 test_callback_fn
*destroy
;
89 /* This is called a the beginning of any collection run. */
90 test_callback_fn
*collection_startup
;
92 /* This is called a the end of any collection run. */
93 test_callback_fn
*collection_shutdown
;
95 /* This is called a the beginning of any run. */
96 test_callback_fn
*run_startup
;
98 /* This called on a test if the test requires a flush call (the bool is from test_st) */
99 test_callback_fn
*flush
;
102 These are run before/after the test. If implemented. Their execution is not controlled
105 test_callback_fn
*pre_run
;
106 test_callback_fn
*post_run
;
109 If an error occurs during the test, this is called.
111 test_callback_error_fn
*on_error
;
114 Runner represents the callers for the tests. If not implemented we will use
115 a set of default implementations.
117 world_runner_st
*runner
;
123 collection_startup(NULL
),
124 collection_shutdown(NULL
),
137 world_st(const world_st
&);
138 world_st
& operator=(const world_st
&);
144 @note world_stats_st is a simple structure for tracking test successes.
146 struct world_stats_st
{
147 int32_t collection_success
;
148 int32_t collection_skipped
;
149 int32_t collection_failed
;
150 int32_t collection_total
;
158 collection_success(0),
159 collection_skipped(0),
160 collection_failed(0),
169 #define TEST_STRINGIFY(x) #x
170 #define TEST_TOSTRING(x) TEST_STRINGIFY(x)
171 #define TEST_AT __FILE__ ":" TEST_TOSTRING(__LINE__)
177 /* How we make all of this work :) */
179 void get_world(world_st
*world
);
182 void create_core(void);
185 @note Friendly print function for errors.
188 const char *test_strerror(test_return_t code
);
190 #define test_assert_errno(A) \
194 fprintf(stderr, "\nAssertion failed at %s:%d: ", __FILE__, __LINE__);\
196 fprintf(stderr, "\n"); \
202 #define test_truth(A) \
206 fprintf(stderr, "\nAssertion failed 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_skip(A,B) \
237 return TEST_SKIPPED; \
241 #define test_fail(A) \
245 fprintf(stderr, "\nFailed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
247 return TEST_FAILURE; \
252 #define test_false(A) \
256 fprintf(stderr, "\nAssertion failed in %s:%d: %s\n", __FILE__, __LINE__, #A);\
258 return TEST_FAILURE; \
262 #define test_false_with(A,B) \
266 fprintf(stderr, "\nAssertion failed at %s:%d: %s with %s\n", __FILE__, __LINE__, #A, (B));\
268 return TEST_FAILURE; \
273 #define test_compare(A,B) \
278 fprintf(stderr, "\n%s:%d: Expected %s, got %lu\n", __FILE__, __LINE__, #A, (unsigned long)(B)); \
280 return TEST_FAILURE; \
284 #define test_compare_got(A,B,C) \
289 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, #A, (C)); \
291 return TEST_FAILURE; \
296 #define test_strcmp(A,B) \
299 if (strcmp((A), (B))) \
301 fprintf(stderr, "\n%s:%d: Expected %s, got %s\n", __FILE__, __LINE__, (A), (B)); \
303 return TEST_FAILURE; \
307 #define test_memcmp(A,B,C) \
310 if (memcmp((A), (B), (C))) \
312 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
314 return TEST_FAILURE; \
323 #define test_literal_param(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
325 #define test_literal_param(X) (X), ((size_t)((sizeof(X) - 1)))
328 #define test_string_make_from_cstr(X) (X), ((X) ? strlen(X) : 0)