a10cc47655097d86383328880d5257e421c8d02b
[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 <sys/wait.h>
19 #include <unistd.h>
20 #include <time.h>
21 #include <fnmatch.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 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)
66 {
67 pid_t pid= fork();
68
69 if (pid == 0)
70 {
71 abort();
72 }
73 else
74 {
75 while (waitpid(pid, NULL, 0) != pid)
76 {
77 ;
78 }
79 }
80 }
81 }
82
83
84 static test_return_t _runner_default(test_callback_fn func, void *p)
85 {
86 if (func)
87 {
88 return func(p);
89 }
90 else
91 {
92 return TEST_SUCCESS;
93 }
94 }
95
96 static world_runner_st defualt_runners= {
97 _runner_default,
98 _runner_default,
99 _runner_default
100 };
101
102
103 int main(int argc, char *argv[])
104 {
105 test_return_t return_code;
106 unsigned int x;
107 char *collection_to_run= NULL;
108 char *wildcard= NULL;
109 world_st world;
110 collection_st *collection;
111 collection_st *next;
112 void *world_ptr;
113
114 world_stats_st stats;
115
116 memset(&stats, 0, sizeof(stats));
117 memset(&world, 0, sizeof(world));
118 get_world(&world);
119
120 if (! world.runner)
121 {
122 world.runner= &defualt_runners;
123 }
124
125 collection= world.collections;
126
127 if (world.create)
128 world_ptr= world.create();
129 else
130 world_ptr= NULL;
131
132 if (argc > 1)
133 collection_to_run= argv[1];
134
135 if (argc == 3)
136 wildcard= argv[2];
137
138 for (next= collection; next->name; next++)
139 {
140 test_st *run;
141
142 run= next->tests;
143 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
144 continue;
145
146 fprintf(stderr, "\n%s\n\n", next->name);
147
148 for (x= 0; run->name; run++)
149 {
150 struct timeval start_time, end_time;
151 long int load_time= 0;
152
153 if (wildcard && fnmatch(wildcard, run->name, 0))
154 continue;
155
156 fprintf(stderr, "Testing %s", run->name);
157
158 if (world.collection_startup)
159 {
160 world.collection_startup(world_ptr);
161 }
162
163 if (run->requires_flush && world.flush)
164 {
165 world.flush(world_ptr);
166 }
167
168 if (world.pre_run)
169 {
170 world.pre_run(world_ptr);
171 }
172
173
174 if (next->pre && world.runner->pre)
175 {
176 return_code= world.runner->pre(next->pre, world_ptr);
177
178 if (return_code != TEST_SUCCESS)
179 {
180 goto error;
181 }
182 }
183
184 gettimeofday(&start_time, NULL);
185 return_code= world.runner->run(run->test_fn, world_ptr);
186 gettimeofday(&end_time, NULL);
187 load_time= timedif(end_time, start_time);
188
189 if (next->post && world.runner->post)
190 {
191 (void) world.runner->post(next->post, world_ptr);
192 }
193
194 if (world.post_run)
195 {
196 world.post_run(world_ptr);
197 }
198
199 error:
200 stats.total++;
201
202 fprintf(stderr, "\t\t\t\t\t");
203
204 switch (return_code)
205 {
206 case TEST_SUCCESS:
207 fprintf(stderr, "%ld.%03ld ", load_time / 1000, load_time % 1000);
208 stats.success++;
209 break;
210 case TEST_FAILURE:
211 stats.failed++;
212 break;
213 case TEST_SKIPPED:
214 stats.skipped++;
215 break;
216 case TEST_MEMORY_ALLOCATION_FAILURE:
217 case TEST_MAXIMUM_RETURN:
218 default:
219 break;
220 }
221
222 fprintf(stderr, "[ %s ]\n", test_strerror(return_code));
223
224 if (world.on_error)
225 {
226 test_return_t rc;
227 rc= world.on_error(return_code, world_ptr);
228
229 if (rc != TEST_SUCCESS)
230 break;
231 }
232 }
233 }
234
235 fprintf(stderr, "All tests completed successfully\n\n");
236
237 if (world.destroy)
238 world.destroy(world_ptr);
239
240 world_stats_print(&stats);
241
242 return stats.failed == 0 ? 0 : 1;
243 }