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