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
)
28 fprintf(stderr
, "Total\t\t\t\t%u\n", stats
->total
);
29 fprintf(stderr
, "\tFailed\t\t\t%u\n", stats
->failed
);
30 fprintf(stderr
, "\tSkipped\t\t\t%u\n", stats
->skipped
);
31 fprintf(stderr
, "\tSucceeded\t\t%u\n", stats
->success
);
34 static long int timedif(struct timeval a
, struct timeval b
)
38 us
= (int)(a
.tv_usec
- b
.tv_usec
);
40 s
= (int)(a
.tv_sec
- b
.tv_sec
);
45 const char *test_strerror(test_return_t code
)
52 case TEST_MEMORY_ALLOCATION_FAILURE
:
53 return "memory allocation";
56 case TEST_MAXIMUM_RETURN
:
58 fprintf(stderr
, "Unknown return value\n");
64 void create_core(void)
66 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
76 while (waitpid(pid
, NULL
, 0) != pid
)
85 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
97 static world_runner_st defualt_runners
= {
104 int main(int argc
, char *argv
[])
106 test_return_t return_code
;
108 char *collection_to_run
= NULL
;
109 char *wildcard
= NULL
;
111 collection_st
*collection
;
115 world_stats_st stats
;
117 memset(&stats
, 0, sizeof(stats
));
118 memset(&world
, 0, sizeof(world
));
123 world
.runner
= &defualt_runners
;
126 collection
= world
.collections
;
129 world_ptr
= world
.create();
134 collection_to_run
= argv
[1];
139 for (next
= collection
; next
->name
; next
++)
144 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
147 fprintf(stderr
, "\n%s\n\n", next
->name
);
149 for (x
= 0; run
->name
; run
++)
151 struct timeval start_time
, end_time
;
152 long int load_time
= 0;
154 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
157 fprintf(stderr
, "Testing %s", run
->name
);
159 if (world
.collection_startup
)
161 world
.collection_startup(world_ptr
);
164 if (run
->requires_flush
&& world
.flush
)
166 world
.flush(world_ptr
);
171 world
.pre_run(world_ptr
);
177 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
179 if (return_code
!= TEST_SUCCESS
)
185 gettimeofday(&start_time
, NULL
);
186 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
187 gettimeofday(&end_time
, NULL
);
188 load_time
= timedif(end_time
, start_time
);
192 (void) world
.runner
->pre(next
->pre
, world_ptr
);
197 world
.post_run(world_ptr
);
203 fprintf(stderr
, "\t\t\t\t\t");
208 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
217 case TEST_MEMORY_ALLOCATION_FAILURE
:
218 case TEST_MAXIMUM_RETURN
:
223 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
228 rc
= world
.on_error(return_code
, world_ptr
);
230 if (rc
!= TEST_SUCCESS
)
236 fprintf(stderr
, "All tests completed successfully\n\n");
239 world
.destroy(world_ptr
);
241 world_stats_print(&stats
);
243 return stats
.failed
== 0 ? 0 : 1;