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