Updating interface.
[m6w6/libmemcached] / tests / test.c
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 Sample test application.
11 */
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <time.h>
20 #include <fnmatch.h>
21 #include "server.h"
22
23 #include "test.h"
24
25 static void world_stats_print(world_stats_st *stats)
26 {
27 fprintf(stderr, "Total\t\t\t\t%u\n", stats->total);
28 fprintf(stderr, "\tFailed\t\t\t%u\n", stats->failed);
29 fprintf(stderr, "\tSkipped\t\t\t%u\n", stats->skipped);
30 fprintf(stderr, "\tSucceeded\t\t%u\n", stats->success);
31 }
32
33 static long int timedif(struct timeval a, struct timeval b)
34 {
35 register int us, s;
36
37 us = (int)(a.tv_usec - b.tv_usec);
38 us /= 1000;
39 s = (int)(a.tv_sec - b.tv_sec);
40 s *= 1000;
41 return s + us;
42 }
43
44 static const char *test_strerror(test_return_t code)
45 {
46 switch (code) {
47 case TEST_SUCCESS:
48 return "ok";
49 case TEST_FAILURE:
50 return "failed";
51 case TEST_MEMORY_ALLOCATION_FAILURE:
52 return "memory allocation";
53 case TEST_SKIPPED:
54 return "skipped";
55 case TEST_MAXIMUM_RETURN:
56 default:
57 fprintf(stderr, "Unknown return value\n");
58 abort();
59 }
60
61 }
62
63 void create_core(void)
64 {
65 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL && fork() == 0)
66 abort();
67 }
68
69
70 static test_return_t _runner_default(test_callback_fn func, void *p)
71 {
72 if (func)
73 {
74 return func(p);
75 }
76 else
77 {
78 return TEST_SUCCESS;
79 }
80 }
81
82 static world_runner_st defualt_runners= {
83 _runner_default,
84 _runner_default,
85 _runner_default
86 };
87
88
89 int main(int argc, char *argv[])
90 {
91 test_return_t return_code;
92 unsigned int x;
93 char *collection_to_run= NULL;
94 char *wildcard= NULL;
95 world_st world;
96 collection_st *collection;
97 collection_st *next;
98 void *world_ptr;
99
100 world_stats_st stats;
101
102 memset(&stats, 0, sizeof(stats));
103 memset(&world, 0, sizeof(world));
104 get_world(&world);
105
106 if (! world.runner)
107 {
108 world.runner= &defualt_runners;
109 }
110
111 collection= world.collections;
112
113 if (world.create)
114 world_ptr= world.create();
115 else
116 world_ptr= NULL;
117
118 if (argc > 1)
119 collection_to_run= argv[1];
120
121 if (argc == 3)
122 wildcard= argv[2];
123
124 for (next= collection; next->name; next++)
125 {
126 test_st *run;
127
128 run= next->tests;
129 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
130 continue;
131
132 fprintf(stderr, "\n%s\n\n", next->name);
133
134 for (x= 0; run->name; run++)
135 {
136 struct timeval start_time, end_time;
137 long int load_time= 0;
138
139 if (wildcard && fnmatch(wildcard, run->name, 0))
140 continue;
141
142 fprintf(stderr, "Testing %s", run->name);
143
144 if (world.collection_startup)
145 {
146 world.collection_startup(world_ptr);
147 }
148
149 if (run->requires_flush && world.flush)
150 {
151 world.flush(world_ptr);
152 }
153
154 if (world.pre_run)
155 {
156 world.pre_run(world_ptr);
157 }
158
159
160 if (next->pre)
161 {
162 return_code= world.runner->pre(next->pre, world_ptr);
163
164 if (return_code != TEST_SUCCESS)
165 {
166 goto error;
167 }
168 }
169
170 gettimeofday(&start_time, NULL);
171 return_code= world.runner->run(run->test_fn, world_ptr);
172 gettimeofday(&end_time, NULL);
173 load_time= timedif(end_time, start_time);
174
175 if (next->post)
176 {
177 (void) world.runner->pre(next->pre, world_ptr);
178 }
179
180 if (world.post_run)
181 {
182 world.post_run(world_ptr);
183 }
184
185 error:
186 stats.total++;
187
188 fprintf(stderr, "\t\t\t\t\t");
189
190 switch (return_code)
191 {
192 case TEST_SUCCESS:
193 fprintf(stderr, "%ld.%03ld ", load_time / 1000, load_time % 1000);
194 stats.success++;
195 break;
196 case TEST_FAILURE:
197 stats.failed++;
198 break;
199 case TEST_SKIPPED:
200 stats.skipped++;
201 break;
202 case TEST_MEMORY_ALLOCATION_FAILURE:
203 case TEST_MAXIMUM_RETURN:
204 default:
205 break;
206 }
207
208 fprintf(stderr, "[ %s ]\n", test_strerror(return_code));
209
210 if (world.on_error)
211 {
212 test_return_t rc;
213 rc= world.on_error(return_code, world_ptr);
214
215 if (rc != TEST_SUCCESS)
216 break;
217 }
218 }
219 }
220
221 fprintf(stderr, "All tests completed successfully\n\n");
222
223 if (world.destroy)
224 world.destroy(world_ptr);
225
226 world_stats_print(&stats);
227
228 return stats.failed == 0 ? 0 : 1;
229 }