Merge in code for C++ compiling of libmemcached.
[awesomized/libmemcached] / libtest / 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
13 #include <config.h>
14
15 #include <unistd.h>
16
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <time.h>
25 #include <stdint.h>
26
27 #include <libtest/test.h>
28 #include <libtest/failed.h>
29
30 static void world_stats_print(world_stats_st *stats)
31 {
32 fputc('\n', stderr);
33 fprintf(stderr, "Total Collections\t\t\t\t%u\n", stats->collection_total);
34 fprintf(stderr, "\tFailed Collections\t\t\t%u\n", stats->collection_failed);
35 fprintf(stderr, "\tSkipped Collections\t\t\t%u\n", stats->collection_skipped);
36 fprintf(stderr, "\tSucceeded Collections\t\t%u\n", stats->collection_success);
37 fputc('\n', stderr);
38 fprintf(stderr, "Total\t\t\t\t%u\n", stats->total);
39 fprintf(stderr, "\tFailed\t\t\t%u\n", stats->failed);
40 fprintf(stderr, "\tSkipped\t\t\t%u\n", stats->skipped);
41 fprintf(stderr, "\tSucceeded\t\t%u\n", stats->success);
42 }
43
44 long int timedif(struct timeval a, struct timeval b)
45 {
46 long us, s;
47
48 us = (int)(a.tv_usec - b.tv_usec);
49 us /= 1000;
50 s = (int)(a.tv_sec - b.tv_sec);
51 s *= 1000;
52 return s + us;
53 }
54
55 const char *test_strerror(test_return_t code)
56 {
57 switch (code) {
58 case TEST_SUCCESS:
59 return "ok";
60 case TEST_FAILURE:
61 return "failed";
62 case TEST_MEMORY_ALLOCATION_FAILURE:
63 return "memory allocation";
64 case TEST_SKIPPED:
65 return "skipped";
66 case TEST_MAXIMUM_RETURN:
67 default:
68 fprintf(stderr, "Unknown return value\n");
69 abort();
70 }
71 }
72
73 void create_core(void)
74 {
75 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL)
76 {
77 pid_t pid= fork();
78
79 if (pid == 0)
80 {
81 abort();
82 }
83 else
84 {
85 while (waitpid(pid, NULL, 0) != pid)
86 {
87 ;
88 }
89 }
90 }
91 }
92
93
94 static test_return_t _runner_default(test_callback_fn func, void *p)
95 {
96 if (func)
97 {
98 return func(p);
99 }
100 else
101 {
102 return TEST_SUCCESS;
103 }
104 }
105
106 static world_runner_st defualt_runners= {
107 _runner_default,
108 _runner_default,
109 _runner_default
110 };
111
112 static test_return_t _default_callback(void *p)
113 {
114 (void)p;
115
116 return TEST_SUCCESS;
117 }
118
119 static inline void set_default_fn(test_callback_fn *fn)
120 {
121 if (*fn == NULL)
122 {
123 *fn= _default_callback;
124 }
125 }
126
127 static collection_st *init_world(world_st *world)
128 {
129 if (! world->runner)
130 {
131 world->runner= &defualt_runners;
132 }
133
134 set_default_fn(&world->collection.startup);
135 set_default_fn(&world->collection.shutdown);
136
137 return world->collections;
138 }
139
140
141 int main(int argc, char *argv[])
142 {
143 test_return_t return_code;
144 unsigned int x;
145 char *collection_to_run= NULL;
146 char *wildcard= NULL;
147 world_st world;
148 collection_st *collection;
149 collection_st *next;
150 void *world_ptr;
151
152 world_stats_st stats;
153
154 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
155 if (sasl_client_init(NULL) != SASL_OK)
156 {
157 fprintf(stderr, "Failed to initialize sasl library!\n");
158 return EXIT_FAILURE;
159 }
160 #endif
161
162 memset(&stats, 0, sizeof(stats));
163 memset(&world, 0, sizeof(world));
164 get_world(&world);
165
166 collection= init_world(&world);
167
168 if (world.create)
169 {
170 test_return_t error;
171 world_ptr= world.create(&error);
172 if (error != TEST_SUCCESS)
173 exit(1);
174 }
175 else
176 {
177 world_ptr= NULL;
178 }
179
180 if (argc > 1)
181 collection_to_run= argv[1];
182
183 if (argc == 3)
184 wildcard= argv[2];
185
186 for (next= collection; next->name; next++)
187 {
188 test_return_t collection_rc= TEST_SUCCESS;
189 test_st *run;
190 bool failed= false;
191 bool skipped= false;
192
193 run= next->tests;
194 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
195 continue;
196
197 stats.collection_total++;
198
199 collection_rc= world.collection.startup(world_ptr);
200
201 if (collection_rc != TEST_SUCCESS)
202 goto skip_pre;
203
204 if (next->pre)
205 {
206 collection_rc= world.runner->pre(next->pre, world_ptr);
207 }
208
209 skip_pre:
210 switch (collection_rc)
211 {
212 case TEST_SUCCESS:
213 fprintf(stderr, "\n%s\n\n", next->name);
214 break;
215 case TEST_FAILURE:
216 fprintf(stderr, "\n%s [ failed ]\n\n", next->name);
217 stats.collection_failed++;
218 goto cleanup;
219 case TEST_SKIPPED:
220 fprintf(stderr, "\n%s [ skipping ]\n\n", next->name);
221 stats.collection_skipped++;
222 goto cleanup;
223 case TEST_MEMORY_ALLOCATION_FAILURE:
224 case TEST_MAXIMUM_RETURN:
225 default:
226 assert(0);
227 break;
228 }
229
230
231 for (x= 0; run->name; run++)
232 {
233 struct timeval start_time, end_time;
234 long int load_time= 0;
235
236 if (wildcard && fnmatch(wildcard, run->name, 0))
237 continue;
238
239 fprintf(stderr, "Testing %s", run->name);
240
241 if (world.test.startup)
242 {
243 world.test.startup(world_ptr);
244 }
245
246 if (run->requires_flush && world.test.flush)
247 {
248 world.test.flush(world_ptr);
249 }
250
251 if (world.test.pre_run)
252 {
253 world.test.pre_run(world_ptr);
254 }
255
256
257 // Runner code
258 {
259 #if 0
260 if (next->pre && world.runner->pre)
261 {
262 return_code= world.runner->pre(next->pre, world_ptr);
263
264 if (return_code != TEST_SUCCESS)
265 {
266 goto error;
267 }
268 }
269 #endif
270
271 gettimeofday(&start_time, NULL);
272 return_code= world.runner->run(run->test_fn, world_ptr);
273 gettimeofday(&end_time, NULL);
274 load_time= timedif(end_time, start_time);
275
276 #if 0
277 if (next->post && world.runner->post)
278 {
279 (void) world.runner->post(next->post, world_ptr);
280 }
281 #endif
282 }
283
284 if (world.test.post_run)
285 {
286 world.test.post_run(world_ptr);
287 }
288
289 stats.total++;
290
291 fprintf(stderr, "\t\t\t\t\t");
292
293 switch (return_code)
294 {
295 case TEST_SUCCESS:
296 fprintf(stderr, "%ld.%03ld ", load_time / 1000, load_time % 1000);
297 stats.success++;
298 break;
299 case TEST_FAILURE:
300 #if 0
301 push_failed_test(next->name, run->name);
302 #endif
303 stats.failed++;
304 failed= true;
305 break;
306 case TEST_SKIPPED:
307 stats.skipped++;
308 skipped= true;
309 break;
310 case TEST_MEMORY_ALLOCATION_FAILURE:
311 fprintf(stderr, "Exhausted memory, quitting\n");
312 abort();
313 case TEST_MAXIMUM_RETURN:
314 default:
315 assert(0); // Coding error.
316 break;
317 }
318
319 fprintf(stderr, "[ %s ]\n", test_strerror(return_code));
320
321 if (world.test.on_error)
322 {
323 test_return_t rc;
324 rc= world.test.on_error(return_code, world_ptr);
325
326 if (rc != TEST_SUCCESS)
327 break;
328 }
329 }
330
331 if (next->post && world.runner->post)
332 {
333 (void) world.runner->post(next->post, world_ptr);
334 }
335
336 if (! failed && ! skipped)
337 {
338 stats.collection_success++;
339 }
340 cleanup:
341
342 world.collection.shutdown(world_ptr);
343 }
344
345 if (stats.collection_failed || stats.collection_skipped)
346 {
347 fprintf(stderr, "Some test failures and/or skipped test occurred.\n\n");
348 #if 0
349 print_failed_test();
350 #endif
351 }
352 else
353 {
354 fprintf(stderr, "All tests completed successfully\n\n");
355 }
356
357 if (world.destroy)
358 {
359 test_return_t error;
360 error= world.destroy(world_ptr);
361
362 if (error != TEST_SUCCESS)
363 {
364 fprintf(stderr, "Failure during shutdown.\n");
365 stats.failed++; // We do this to make our exit code return EXIT_FAILURE
366 }
367 }
368
369 world_stats_print(&stats);
370
371 return stats.failed == 0 ? 0 : 1;
372 }