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>
25 static void world_stats_print(world_stats_st
*stats
)
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
);
33 static long int timedif(struct timeval a
, struct timeval b
)
37 us
= (int)(a
.tv_usec
- b
.tv_usec
);
39 s
= (int)(a
.tv_sec
- b
.tv_sec
);
44 const char *test_strerror(test_return_t code
)
51 case TEST_MEMORY_ALLOCATION_FAILURE
:
52 return "memory allocation";
55 case TEST_MAXIMUM_RETURN
:
57 fprintf(stderr
, "Unknown return value\n");
63 void create_core(void)
65 if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL
)
75 while (waitpid(pid
, NULL
, 0) != pid
)
84 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
96 static world_runner_st defualt_runners
= {
103 int main(int argc
, char *argv
[])
105 test_return_t return_code
;
107 char *collection_to_run
= NULL
;
108 char *wildcard
= NULL
;
110 collection_st
*collection
;
114 world_stats_st stats
;
116 memset(&stats
, 0, sizeof(stats
));
117 memset(&world
, 0, sizeof(world
));
122 world
.runner
= &defualt_runners
;
125 collection
= world
.collections
;
128 world_ptr
= world
.create();
133 collection_to_run
= argv
[1];
138 for (next
= collection
; next
->name
; next
++)
143 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
146 fprintf(stderr
, "\n%s\n\n", next
->name
);
148 for (x
= 0; run
->name
; run
++)
150 struct timeval start_time
, end_time
;
151 long int load_time
= 0;
153 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
156 fprintf(stderr
, "Testing %s", run
->name
);
158 if (world
.collection_startup
)
160 world
.collection_startup(world_ptr
);
163 if (run
->requires_flush
&& world
.flush
)
165 world
.flush(world_ptr
);
170 world
.pre_run(world_ptr
);
174 if (next
->pre
&& world
.runner
->pre
)
176 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
178 if (return_code
!= TEST_SUCCESS
)
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
);
189 if (next
->post
&& world
.runner
->post
)
191 (void) world
.runner
->post(next
->post
, world_ptr
);
196 world
.post_run(world_ptr
);
202 fprintf(stderr
, "\t\t\t\t\t");
207 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
216 case TEST_MEMORY_ALLOCATION_FAILURE
:
217 case TEST_MAXIMUM_RETURN
:
222 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
227 rc
= world
.on_error(return_code
, world_ptr
);
229 if (rc
!= TEST_SUCCESS
)
235 fprintf(stderr
, "All tests completed successfully\n\n");
238 world
.destroy(world_ptr
);
240 world_stats_print(&stats
);
242 return stats
.failed
== 0 ? 0 : 1;