Absorb test bits into anonymous structure.
[m6w6/libmemcached] / tests / test.h
1 /* uTest
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 /*
10 Structures for generic tests.
11 */
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21
22 typedef struct world_st world_st;
23 typedef struct collection_st collection_st;
24 typedef struct test_st test_st;
25
26 typedef enum {
27 TEST_SUCCESS= 0, /* Backwards compatibility */
28 TEST_FAILURE,
29 TEST_MEMORY_ALLOCATION_FAILURE,
30 TEST_SKIPPED,
31 TEST_MAXIMUM_RETURN /* Always add new error code before */
32 } test_return_t;
33
34 typedef void *(*test_callback_create_fn)(test_return_t *error);
35 typedef test_return_t (*test_callback_fn)(void *);
36 typedef test_return_t (*test_callback_runner_fn)(test_callback_fn, void *);
37 typedef test_return_t (*test_callback_error_fn)(test_return_t, void *);
38
39
40 /**
41 A structure describing the test case.
42 */
43 struct test_st {
44 const char *name;
45 bool requires_flush;
46 test_callback_fn test_fn;
47 };
48
49
50 /**
51 A structure which describes a collection of test cases.
52 */
53 struct collection_st {
54 const char *name;
55 test_callback_fn pre;
56 test_callback_fn post;
57 test_st *tests;
58 };
59
60
61 /**
62 Structure which houses the actual callers for the test cases contained in
63 the collections.
64 */
65 typedef struct {
66 test_callback_runner_fn pre;
67 test_callback_runner_fn run;
68 test_callback_runner_fn post;
69 } world_runner_st;
70
71
72 /**
73 world_st is the structure which is passed to the test implementation to be filled.
74 This must be implemented in order for the test framework to load the tests. We call
75 get_world() in order to fill this structure.
76 */
77
78 struct world_st {
79 collection_st *collections;
80
81 /* These methods are called outside of any collection call. */
82 test_callback_create_fn create;
83 test_callback_fn destroy;
84
85 struct {
86 /* This is called a the beginning of any test run. */
87 test_callback_fn startup;
88
89 /* This called on a test if the test requires a flush call (the bool is from test_st) */
90 test_callback_fn flush;
91
92 /**
93 These are run before/after the test. If implemented. Their execution is not controlled
94 by the test.
95 */
96 test_callback_fn pre_run;
97 test_callback_fn post_run;
98
99 /**
100 If an error occurs during the test, this is called.
101 */
102 test_callback_error_fn on_error;
103 } test;
104
105 /* This is called a the beginning of any collection run. */
106 test_callback_fn collection_startup;
107
108 /* This is called a the beginning of any collection run. */
109 test_callback_fn collection_shutdown;
110
111
112 /**
113 Runner represents the callers for the tests. If not implemented we will use
114 a set of default implementations.
115 */
116 world_runner_st *runner;
117 };
118
119
120
121 /**
122 @note world_stats_st is a simple structure for tracking test successes.
123 */
124 typedef struct {
125 uint32_t collection_success;
126 uint32_t collection_skipped;
127 uint32_t collection_failed;
128 uint32_t collection_total;
129 uint32_t success;
130 uint32_t skipped;
131 uint32_t failed;
132 uint32_t total;
133 } world_stats_st;
134
135 /* How we make all of this work :) */
136 void get_world(world_st *world);
137
138 void create_core(void);
139
140 /**
141 @note Friendly print function for errors.
142 */
143 const char *test_strerror(test_return_t code);
144
145 #define test_truth(A) \
146 do \
147 { \
148 if (! (A)) { \
149 fprintf(stderr, "\nAssertion failed in %s:%d: %s\n", __FILE__, __LINE__, #A);\
150 create_core(); \
151 return TEST_FAILURE; \
152 } \
153 } while (0)
154
155 #define test_false(A) \
156 do \
157 { \
158 if ((A)) { \
159 fprintf(stderr, "\nAssertion failed in %s:%d: %s\n", __FILE__, __LINE__, #A);\
160 create_core(); \
161 return TEST_FAILURE; \
162 } \
163 } while (0)
164
165 #define test_strcmp(A,B) \
166 do \
167 { \
168 if (strcmp((A), (B))) \
169 { \
170 fprintf(stderr, "\n%s:%d: %s -> %s\n", __FILE__, __LINE__, (A), (B)); \
171 create_core(); \
172 return TEST_FAILURE; \
173 } \
174 } while (0)
175
176 #ifdef __cplusplus
177 }
178 #endif