2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
10 Sample test application.
16 #include <sys/types.h>
26 static void world_stats_print(world_stats_st
*stats
)
29 fprintf(stderr
, "Total Collections\t\t\t\t%u\n", stats
->collection_total
);
30 fprintf(stderr
, "\tFailed Collections\t\t\t%u\n", stats
->collection_failed
);
31 fprintf(stderr
, "\tSkipped Collections\t\t\t%u\n", stats
->collection_skipped
);
32 fprintf(stderr
, "\tSucceeded Collections\t\t%u\n", stats
->collection_success
);
34 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
35 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
36 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
37 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
40 static long int timedif(struct timeval a
, struct timeval b
)
44 us
= (int)(a
.tv_usec
- b
.tv_usec
);
46 s
= (int)(a
.tv_sec
- b
.tv_sec
);
51 const char *test_strerror(test_return_t code
)
58 case TEST_MEMORY_ALLOCATION_FAILURE
:
59 return "memory allocation";
62 case TEST_MAXIMUM_RETURN
:
64 fprintf(stderr
, "Unknown return value\n");
69 void create_core(void)
71 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
81 while (waitpid(pid
, NULL
, 0) != pid
)
90 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
102 static world_runner_st defualt_runners
= {
109 int main(int argc
, char *argv
[])
111 test_return_t return_code
;
113 char *collection_to_run
= NULL
;
114 char *wildcard
= NULL
;
116 collection_st
*collection
;
120 world_stats_st stats
;
122 memset(&stats
, 0, sizeof(stats
));
123 memset(&world
, 0, sizeof(world
));
128 world
.runner
= &defualt_runners
;
131 collection
= world
.collections
;
136 world_ptr
= world
.create(&error
);
137 if (error
!= TEST_SUCCESS
)
146 collection_to_run
= argv
[1];
151 for (next
= collection
; next
->name
; next
++)
153 test_return_t collection_rc
= TEST_SUCCESS
;
159 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
162 stats
.collection_total
++;
164 if (world
.collection_startup
)
166 collection_rc
= world
.test_startup(world_ptr
);
169 switch (collection_rc
)
172 fprintf(stderr
, "\n%s\n\n", next
->name
);
175 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
179 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
182 case TEST_MEMORY_ALLOCATION_FAILURE
:
183 case TEST_MAXIMUM_RETURN
:
189 for (x
= 0; run
->name
; run
++)
191 struct timeval start_time
, end_time
;
192 long int load_time
= 0;
194 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
197 fprintf(stderr
, "Testing %s", run
->name
);
199 if (world
.test_startup
)
201 world
.test_startup(world_ptr
);
204 if (run
->requires_flush
&& world
.flush
)
206 world
.flush(world_ptr
);
211 world
.pre_run(world_ptr
);
215 if (next
->pre
&& world
.runner
->pre
)
217 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
219 if (return_code
!= TEST_SUCCESS
)
225 gettimeofday(&start_time
, NULL
);
226 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
227 gettimeofday(&end_time
, NULL
);
228 load_time
= timedif(end_time
, start_time
);
230 if (next
->post
&& world
.runner
->post
)
232 (void) world
.runner
->post(next
->post
, world_ptr
);
237 world
.post_run(world_ptr
);
243 fprintf(stderr
, "\t\t\t\t\t");
248 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
259 case TEST_MEMORY_ALLOCATION_FAILURE
:
260 fprintf(stderr
, "Exhausted memory, quitting\n");
262 case TEST_MAXIMUM_RETURN
:
264 assert(0); // Coding error.
268 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
273 rc
= world
.on_error(return_code
, world_ptr
);
275 if (rc
!= TEST_SUCCESS
)
282 stats
.collection_failed
++;
287 stats
.collection_skipped
++;
290 if (! failed
&& ! skipped
)
292 stats
.collection_success
++;
295 if (world
.collection_shutdown
)
297 world
.collection_shutdown(world_ptr
);
301 if (stats
.collection_failed
|| stats
.collection_skipped
)
303 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
307 fprintf(stderr
, "All tests completed successfully\n\n");
313 error
= world
.destroy(world_ptr
);
315 if (error
!= TEST_SUCCESS
)
317 fprintf(stderr
, "Failure during shutdown.\n");
318 stats
.failed
++; // We do this to make our exit code return 1
322 world_stats_print(&stats
);
324 return stats
.failed
== 0 ? 0 : 1;