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