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 static 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
&& fork() == 0)
70 static test_return_t
_runner_default(test_callback_fn func
, void *p
)
82 static world_runner_st defualt_runners
= {
89 int main(int argc
, char *argv
[])
91 test_return_t return_code
;
93 char *collection_to_run
= NULL
;
96 collection_st
*collection
;
100 world_stats_st stats
;
102 memset(&stats
, 0, sizeof(stats
));
103 memset(&world
, 0, sizeof(world
));
108 world
.runner
= &defualt_runners
;
111 collection
= world
.collections
;
114 world_ptr
= world
.create();
119 collection_to_run
= argv
[1];
124 for (next
= collection
; next
->name
; next
++)
129 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
132 fprintf(stderr
, "\n%s\n\n", next
->name
);
134 for (x
= 0; run
->name
; run
++)
136 struct timeval start_time
, end_time
;
137 long int load_time
= 0;
139 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
142 fprintf(stderr
, "Testing %s", run
->name
);
144 if (world
.collection_startup
)
146 world
.collection_startup(world_ptr
);
149 if (run
->requires_flush
&& world
.flush
)
151 world
.flush(world_ptr
);
156 world
.pre_run(world_ptr
);
162 return_code
= world
.runner
->pre(next
->pre
, world_ptr
);
164 if (return_code
!= TEST_SUCCESS
)
170 gettimeofday(&start_time
, NULL
);
171 return_code
= world
.runner
->run(run
->test_fn
, world_ptr
);
172 gettimeofday(&end_time
, NULL
);
173 load_time
= timedif(end_time
, start_time
);
177 (void) world
.runner
->pre(next
->pre
, world_ptr
);
182 world
.post_run(world_ptr
);
188 fprintf(stderr
, "\t\t\t\t\t");
193 fprintf(stderr
, "%ld.%03ld ", load_time
/ 1000, load_time
% 1000);
202 case TEST_MEMORY_ALLOCATION_FAILURE
:
203 case TEST_MAXIMUM_RETURN
:
208 fprintf(stderr
, "[ %s ]\n", test_strerror(return_code
));
213 rc
= world
.on_error(return_code
, world_ptr
);
215 if (rc
!= TEST_SUCCESS
)
221 fprintf(stderr
, "All tests completed successfully\n\n");
224 world
.destroy(world_ptr
);
226 world_stats_print(&stats
);
228 return stats
.failed
== 0 ? 0 : 1;