Merge from Mikki
[m6w6/libmemcached] / tests / test.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/time.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <time.h>
12 #include <fnmatch.h>
13 #include "server.h"
14
15 #include "test.h"
16
17 static long int timedif(struct timeval a, struct timeval b)
18 {
19 register int us, s;
20
21 us = (int)(a.tv_usec - b.tv_usec);
22 us /= 1000;
23 s = (int)(a.tv_sec - b.tv_sec);
24 s *= 1000;
25 return s + us;
26 }
27
28 static const char *test_strerror(test_return_t code)
29 {
30 switch (code) {
31 case TEST_SUCCESS:
32 return "ok";
33 case TEST_FAILURE:
34 return "failed";
35 case TEST_MEMORY_ALLOCATION_FAILURE:
36 return "memory allocation";
37 case TEST_SKIPPED:
38 return "skipped";
39 case TEST_MAXIMUM_RETURN:
40 default:
41 fprintf(stderr, "Unknown return value\n");
42 abort();
43 }
44
45 }
46
47 int main(int argc, char *argv[])
48 {
49 test_return_t failed;
50 unsigned int x;
51 char *collection_to_run= NULL;
52 char *wildcard= NULL;
53 server_startup_st *startup_ptr;
54 memcached_server_st *servers;
55 world_st world;
56 collection_st *collection;
57 collection_st *next;
58 void *world_ptr;
59
60 memset(&world, 0, sizeof(world_st));
61 get_world(&world);
62 collection= world.collections;
63
64 if (world.create)
65 world_ptr= world.create();
66 else
67 world_ptr= NULL;
68
69 startup_ptr= (server_startup_st *)world_ptr;
70 servers= (memcached_server_st *)startup_ptr->servers;
71
72 if (argc > 1)
73 collection_to_run= argv[1];
74
75 if (argc == 3)
76 wildcard= argv[2];
77
78 for (next= collection; next->name; next++)
79 {
80 test_st *run;
81
82 run= next->tests;
83 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
84 continue;
85
86 fprintf(stderr, "\n%s\n\n", next->name);
87
88 for (x= 0; run->name; run++)
89 {
90 unsigned int loop;
91 memcached_st *memc;
92 memcached_return rc;
93 struct timeval start_time, end_time;
94 long int load_time;
95
96 if (wildcard && fnmatch(wildcard, run->name, 0))
97 continue;
98
99 fprintf(stderr, "Testing %s", run->name);
100
101 memc= memcached_create(NULL);
102 test_truth(memc);
103
104 rc= memcached_server_push(memc, servers);
105 test_truth(rc == MEMCACHED_SUCCESS);
106
107 if (run->requires_flush)
108 {
109 memcached_flush(memc, 0);
110 memcached_quit(memc);
111 }
112
113 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
114 {
115 test_truth(memc->hosts[loop].fd == -1);
116 test_truth(memc->hosts[loop].cursor_active == 0);
117 }
118
119 if (next->pre)
120 {
121 rc= next->pre(memc);
122
123 if (rc != MEMCACHED_SUCCESS)
124 {
125 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
126 goto error;
127 }
128 }
129
130 gettimeofday(&start_time, NULL);
131 failed= run->function(memc);
132 gettimeofday(&end_time, NULL);
133 load_time= timedif(end_time, start_time);
134
135 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ %s ]\n", load_time / 1000,
136 load_time % 1000, test_strerror(failed));
137
138 if (next->post)
139 (void)next->post(memc);
140
141 test_truth(memc);
142 error:
143 memcached_free(memc);
144 }
145 }
146
147 fprintf(stderr, "All tests completed successfully\n\n");
148
149 if (world.destroy)
150 world.destroy(world_ptr);
151
152 return 0;
153 }