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
= {
108 static test_return_t
_default_callback(void *p
)
115 static inline void set_default_fn(test_callback_fn
*fn
)
119 *fn
= _default_callback
;
123 static collection_st
*init_world(world_st
*world
)
127 world
->runner
= &defualt_runners
;
130 set_default_fn(&world
->collection
.startup
);
131 set_default_fn(&world
->collection
.shutdown
);
133 return world
->collections
;
137 int main(int argc
, char *argv
[])
139 test_return_t return_code
;
141 char *collection_to_run
= NULL
;
142 char *wildcard
= NULL
;
144 collection_st
*collection
;
148 world_stats_st stats
;
150 memset(&stats
, 0, sizeof(stats
));
151 memset(&world
, 0, sizeof(world
));
154 collection
= init_world(&world
);
159 world_ptr
= world
.create(&error
);
160 if (error
!= TEST_SUCCESS
)
169 collection_to_run
= argv
[1];
174 for (next
= collection
; next
->name
; next
++)
176 test_return_t collection_rc
= TEST_SUCCESS
;
182 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
185 stats
.collection_total
++;
187 collection_rc
= world
.collection
.startup(world_ptr
);
189 switch (collection_rc
)
192 fprintf(stderr
, "\n%s\n\n", next
->name
);
195 fprintf(stderr
, "\n%s [ failed ]\n\n", next
->name
);
199 fprintf(stderr
, "\n%s [ skipping ]\n\n", next
->name
);
202 case TEST_MEMORY_ALLOCATION_FAILURE
:
203 case TEST_MAXIMUM_RETURN
:
209 for (x
= 0; run
->name
; run
++)
211 struct timeval start_time
, end_time
;
212 long int load_time
= 0;
214 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
217 fprintf(stderr
, "Testing %s", run
->name
);
219 if (world
.test
.startup
)
221 world
.test
.startup(world_ptr
);
224 if (run
->requires_flush
&& world
.test
.flush
)
226 world
.test
.flush(world_ptr
);
229 if (world
.test
.pre_run
)
231 world
.test
.pre_run(world_ptr
);
237 if (next
->pre
&& world
.runner
->pre
)
239 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
241 if (return_code
!= TEST_SUCCESS
)
247 gettimeofday(&start_time
, NULL
);
248 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
249 gettimeofday(&end_time
, NULL
);
250 load_time
= timedif(end_time
, start_time
);
252 if (next
->post
&& world
.runner
->post
)
254 (void) world
.runner
->post(next
->post
, world_ptr
);
258 if (world
.test
.post_run
)
260 world
.test
.post_run(world_ptr
);
266 fprintf(stderr
, "\t\t\t\t\t");
271 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
282 case TEST_MEMORY_ALLOCATION_FAILURE
:
283 fprintf(stderr
, "Exhausted memory, quitting\n");
285 case TEST_MAXIMUM_RETURN
:
287 assert(0); // Coding error.
291 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
293 if (world
.test
.on_error
)
296 rc
= world
.test
.on_error(return_code
, world_ptr
);
298 if (rc
!= TEST_SUCCESS
)
305 stats
.collection_failed
++;
310 stats
.collection_skipped
++;
313 if (! failed
&& ! skipped
)
315 stats
.collection_success
++;
318 world
.collection
.shutdown(world_ptr
);
321 if (stats
.collection_failed
|| stats
.collection_skipped
)
323 fprintf(stderr
, "Some test failures and/or skipped test occurred.\n\n");
327 fprintf(stderr
, "All tests completed successfully\n\n");
333 error
= world
.destroy(world_ptr
);
335 if (error
!= TEST_SUCCESS
)
337 fprintf(stderr
, "Failure during shutdown.\n");
338 stats
.failed
++; // We do this to make our exit code return 1
342 world_stats_print(&stats
);
344 return stats
.failed
== 0 ? 0 : 1;