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
= 0, /* Backwards compatibility */
62 TEST_MEMORY_ALLOCATION_FAILURE
,
64 TEST_MAXIMUM_RETURN
/* Always add new error code before */
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 *);
73 A structure describing the test case.
78 test_callback_fn test_fn
;
83 A structure which describes a collection of test cases.
85 struct collection_st
{
88 test_callback_fn post
;
94 Structure which houses the actual callers for the test cases contained in
98 test_callback_runner_fn pre
;
99 test_callback_runner_fn run
;
100 test_callback_runner_fn post
;
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.
111 collection_st
*collections
;
113 /* These methods are called outside of any collection call. */
114 test_callback_create_fn create
;
115 test_callback_fn destroy
;
118 /* This is called a the beginning of any test run. */
119 test_callback_fn startup
;
121 /* This called on a test if the test requires a flush call (the bool is from test_st) */
122 test_callback_fn flush
;
125 These are run before/after the test. If implemented. Their execution is not controlled
128 test_callback_fn pre_run
;
129 test_callback_fn post_run
;
132 If an error occurs during the test, this is called.
134 test_callback_error_fn on_error
;
138 /* This is called a the beginning of any collection run. */
139 test_callback_fn startup
;
141 /* This is called at the end of any collection run. */
142 test_callback_fn shutdown
;
147 Runner represents the callers for the tests. If not implemented we will use
148 a set of default implementations.
150 world_runner_st
*runner
;
156 @note world_stats_st is a simple structure for tracking test successes.
159 uint32_t collection_success
;
160 uint32_t collection_skipped
;
161 uint32_t collection_failed
;
162 uint32_t collection_total
;
173 /* Help function for use with gettimeofday() */
175 long int timedif(struct timeval a
, struct timeval b
);
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_fail(A) \
194 fprintf(stderr, "\nFailed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
196 return TEST_FAILURE; \
200 #define test_true(A) \
204 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
206 return TEST_FAILURE; \
210 #define test_true_got(A,B) \
214 fprintf(stderr, "\nAssertion failed at %s:%d: \"%s\" received \"%s\"\n", __FILE__, __LINE__, #A, (B));\
216 return TEST_FAILURE; \
220 #define test_false(A) \
224 fprintf(stderr, "\nAssertion failed at %s:%d: %s\n", __FILE__, __LINE__, #A);\
226 return TEST_FAILURE; \
230 #define test_false_with(A,B) \
234 fprintf(stderr, "\nAssertion failed at %s:%d: %s with %s\n", __FILE__, __LINE__, #A, (B));\
236 return TEST_FAILURE; \
240 #define test_strcmp(A,B) \
243 if (strcmp((A), (B))) \
245 fprintf(stderr, "\n%s:%d: %s -> %s\n", __FILE__, __LINE__, (A), (B)); \
247 return TEST_FAILURE; \
252 #define STRINGIFY(x) #x
253 #define TOSTRING(x) STRINGIFY(x)
254 #define AT __FILE__ ":" TOSTRING(__LINE__)
257 #define STRING_WITH_LEN(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
259 #define STRING_WITH_LEN(X) (X), ((size_t)((sizeof(X) - 1)))
263 #define STRING_PARAM_WITH_LEN(X) X, static_cast<size_t>(sizeof(X) - 1)
265 #define STRING_PARAM_WITH_LEN(X) X, (size_t)((sizeof(X) - 1))