Removed more then a handfull of memset() calls.
[awesomized/libmemcached] / tests / test.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <time.h>
13
14 #include "test.h"
15
16 long int timedif(struct timeval a, struct timeval b)
17 {
18 register int us, s;
19
20 us = a.tv_usec - b.tv_usec;
21 us /= 1000;
22 s = a.tv_sec - b.tv_sec;
23 s *= 1000;
24 return s + us;
25 }
26
27 int main(int argc, char *argv[])
28 {
29 unsigned int x;
30 char *server_list;
31 char *collection_to_run= NULL;
32 char *wildcard= NULL;
33 memcached_server_st *servers;
34 collection_st *collection;
35 uint8_t failed;
36
37 collection= gets_collections();
38
39
40 if (argc > 1)
41 collection_to_run= argv[1];
42
43 if (argc == 3)
44 wildcard= argv[2];
45
46 if (!(server_list= getenv("MEMCACHED_SERVERS")))
47 server_list= "localhost";
48
49 printf("servers %s\n", server_list);
50 srandom(time(NULL));
51
52 servers= memcached_servers_parse(server_list);
53 assert(servers);
54
55 for (x= 0; x < memcached_server_list_count(servers); x++)
56 {
57 printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
58 assert(servers[x].stack_responses == 0);
59 assert(servers[x].fd == -1);
60 assert(servers[x].cursor_active == 0);
61 }
62
63 printf("\n");
64
65 collection_st *next;
66 for (next= collection; next->name; next++)
67 {
68 test_st *run;
69
70 run= next->tests;
71 if (collection_to_run && strcmp(collection_to_run, next->name))
72 continue;
73
74 fprintf(stderr, "\n%s\n\n", next->name);
75
76 for (x= 0; run->name; run++)
77 {
78 if (wildcard && strcmp(wildcard, run->name))
79 continue;
80
81 fprintf(stderr, "Testing %s", run->name);
82
83 memcached_st *memc;
84 memcached_return rc;
85 struct timeval start_time, end_time;
86
87 memc= memcached_create(NULL);
88 assert(memc);
89
90 if (run->requires_flush)
91 memcached_flush(memc, 0);
92
93 rc= memcached_server_push(memc, servers);
94 assert(rc == MEMCACHED_SUCCESS);
95
96 unsigned int loop;
97 for (loop= 0; loop < memcached_server_list_count(servers); loop++)
98 {
99 assert(memc->hosts[loop].stack_responses == 0);
100 assert(memc->hosts[loop].fd == -1);
101 assert(memc->hosts[loop].cursor_active == 0);
102 }
103
104 if (next->pre)
105 {
106 memcached_return rc;
107 rc= next->pre(memc);
108
109 if (rc != MEMCACHED_SUCCESS)
110 {
111 fprintf(stderr, "\t\t\t\t\t [ skipping ]\n");
112 goto error;
113 }
114 }
115
116 gettimeofday(&start_time, NULL);
117 failed= run->function(memc);
118 gettimeofday(&end_time, NULL);
119 long int load_time= timedif(end_time, start_time);
120 if (failed)
121 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000,
122 load_time % 1000);
123 else
124 fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000,
125 load_time % 1000);
126
127 if (next->post)
128 (void)next->post(memc);
129
130 assert(memc);
131 error:
132 memcached_free(memc);
133 }
134 }
135
136 fprintf(stderr, "All tests completed successfully\n\n");
137
138 memcached_server_list_free(servers);
139
140 return 0;
141 }