Update for OSX Lion build.
[m6w6/libmemcached] / libtest / test.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <libtest/common.h>
23
24 #include <cassert>
25 #include <cstdlib>
26 #include <cstring>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32 #include <ctime>
33 #include <fnmatch.h>
34 #include <iostream>
35
36 #include <signal.h>
37
38 #ifndef __INTEL_COMPILER
39 #pragma GCC diagnostic ignored "-Wold-style-cast"
40 #endif
41
42 using namespace libtest;
43
44 static void stats_print(Stats *stats)
45 {
46 if (stats->collection_failed == 0 and stats->collection_success == 0)
47 {
48 return;
49 }
50
51 Out << "\tTotal Collections\t\t\t\t" << stats->collection_total;
52 Out << "\tFailed Collections\t\t\t\t" << stats->collection_failed;
53 Out << "\tSkipped Collections\t\t\t\t" << stats->collection_skipped;
54 Out << "\tSucceeded Collections\t\t\t\t" << stats->collection_success;
55 Outn();
56 Out << "Total\t\t\t\t" << stats->total;
57 Out << "\tFailed\t\t\t" << stats->failed;
58 Out << "\tSkipped\t\t\t" << stats->skipped;
59 Out << "\tSucceeded\t\t" << stats->success;
60 }
61
62 static long int timedif(struct timeval a, struct timeval b)
63 {
64 long us, s;
65
66 us = (long)(a.tv_usec - b.tv_usec);
67 us /= 1000;
68 s = (long)(a.tv_sec - b.tv_sec);
69 s *= 1000;
70 return s + us;
71 }
72
73 static Framework *world= NULL;
74 int main(int argc, char *argv[])
75 {
76 srandom((unsigned int)time(NULL));
77
78 if (getenv("LIBTEST_QUIET"))
79 {
80 close(STDOUT_FILENO);
81 }
82
83 char buffer[1024];
84 if (getenv("LIBTEST_TMP"))
85 {
86 snprintf(buffer, sizeof(buffer), "%s", getenv("LIBTEST_TMP"));
87 }
88 else
89 {
90 snprintf(buffer, sizeof(buffer), "%s", LIBTEST_TEMP);
91 }
92
93 if (chdir(buffer) == -1)
94 {
95 char getcwd_buffer[1024];
96 char *dir= getcwd(getcwd_buffer, sizeof(getcwd_buffer));
97
98 Error << "Unable to chdir() from " << dir << " to " << buffer << " errno:" << strerror(errno);
99 return EXIT_FAILURE;
100 }
101
102 if (libtest::libtool() == NULL)
103 {
104 Error << "Failed to locate libtool";
105 return EXIT_FAILURE;
106 }
107
108 world= new Framework();
109
110 if (world == NULL)
111 {
112 Error << "Failed to create Framework()";
113 return EXIT_FAILURE;
114 }
115
116 libtest::SignalThread signal;
117 if (not signal.setup())
118 {
119 return EXIT_FAILURE;
120 }
121
122 Stats stats;
123
124 get_world(world);
125
126 test_return_t error;
127 void *creators_ptr= world->create(error);
128
129 switch (error)
130 {
131 case TEST_SUCCESS:
132 break;
133
134 case TEST_SKIPPED:
135 Out << "SKIP " << argv[0];
136 delete world;
137 return EXIT_SUCCESS;
138
139 case TEST_FATAL:
140 case TEST_FAILURE:
141 case TEST_MEMORY_ALLOCATION_FAILURE:
142 Error << argv[0] << " failed in Framework::create()";
143 delete world;
144 return EXIT_FAILURE;
145 }
146
147 char *collection_to_run= NULL;
148 if (argc > 1)
149 {
150 collection_to_run= argv[1];
151 }
152 else if (getenv("TEST_COLLECTION"))
153 {
154 collection_to_run= getenv("TEST_COLLECTION");
155 }
156
157 if (collection_to_run)
158 {
159 Out << "Only testing " << collection_to_run;
160 }
161
162 char *wildcard= NULL;
163 if (argc == 3)
164 {
165 wildcard= argv[2];
166 }
167
168 for (collection_st *next= world->collections; next->name and (not signal.is_shutdown()); next++)
169 {
170 test_return_t collection_rc= TEST_SUCCESS;
171 bool failed= false;
172 bool skipped= false;
173
174 if (collection_to_run && fnmatch(collection_to_run, next->name, 0))
175 continue;
176
177 stats.collection_total++;
178
179 collection_rc= world->startup(creators_ptr);
180
181 if (collection_rc == TEST_SUCCESS and next->pre)
182 {
183 collection_rc= world->runner()->pre(next->pre, creators_ptr);
184 }
185
186 switch (collection_rc)
187 {
188 case TEST_SUCCESS:
189 break;
190
191 case TEST_FATAL:
192 case TEST_FAILURE:
193 Out << next->name << " [ failed ]";
194 failed= true;
195 signal.set_shutdown(SHUTDOWN_GRACEFUL);
196 goto cleanup;
197
198 case TEST_SKIPPED:
199 Out << next->name << " [ skipping ]";
200 skipped= true;
201 goto cleanup;
202
203 case TEST_MEMORY_ALLOCATION_FAILURE:
204 test_assert(0, "Allocation failure, or unknown return");
205 }
206
207 Out << "Collection: " << next->name;
208
209 for (test_st *run= next->tests; run->name; run++)
210 {
211 struct timeval start_time, end_time;
212 long int load_time= 0;
213
214 if (wildcard && fnmatch(wildcard, run->name, 0))
215 {
216 continue;
217 }
218
219 test_return_t return_code;
220 if (test_success(return_code= world->item.startup(creators_ptr)))
221 {
222 if (test_success(return_code= world->item.flush(creators_ptr, run)))
223 {
224 // @note pre will fail is SKIPPED is returned
225 if (test_success(return_code= world->item.pre(creators_ptr)))
226 {
227 { // Runner Code
228 gettimeofday(&start_time, NULL);
229 assert(world->runner());
230 assert(run->test_fn);
231 return_code= world->runner()->run(run->test_fn, creators_ptr);
232 gettimeofday(&end_time, NULL);
233 load_time= timedif(end_time, start_time);
234 }
235 }
236
237 // @todo do something if post fails
238 (void)world->item.post(creators_ptr);
239 }
240 else if (return_code == TEST_SKIPPED)
241 { }
242 else if (return_code == TEST_FAILURE)
243 {
244 Error << " item.flush(failure)";
245 signal.set_shutdown(SHUTDOWN_GRACEFUL);
246 }
247 }
248 else if (return_code == TEST_SKIPPED)
249 { }
250 else if (return_code == TEST_FAILURE)
251 {
252 Error << " item.startup(failure)";
253 signal.set_shutdown(SHUTDOWN_GRACEFUL);
254 }
255
256 stats.total++;
257
258 switch (return_code)
259 {
260 case TEST_SUCCESS:
261 Out << "\tTesting " << run->name << "\t\t\t\t\t" << load_time / 1000 << "." << load_time % 1000 << "[ " << test_strerror(return_code) << " ]";
262 stats.success++;
263 break;
264
265 case TEST_FATAL:
266 case TEST_FAILURE:
267 stats.failed++;
268 failed= true;
269 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
270 break;
271
272 case TEST_SKIPPED:
273 stats.skipped++;
274 skipped= true;
275 Out << "\tTesting " << run->name << "\t\t\t\t\t" << "[ " << test_strerror(return_code) << " ]";
276 break;
277
278 case TEST_MEMORY_ALLOCATION_FAILURE:
279 test_assert(0, "Memory Allocation Error");
280 }
281
282 if (test_failed(world->on_error(return_code, creators_ptr)))
283 {
284 Error << "Failed while running on_error()";
285 signal.set_shutdown(SHUTDOWN_GRACEFUL);
286 break;
287 }
288 }
289
290 (void) world->runner()->post(next->post, creators_ptr);
291
292 cleanup:
293 if (failed == false and skipped == false)
294 {
295 stats.collection_success++;
296 }
297
298 if (failed)
299 {
300 stats.collection_failed++;
301 }
302
303 if (skipped)
304 {
305 stats.collection_skipped++;
306 }
307
308 world->shutdown(creators_ptr);
309 Outn();
310 }
311
312 if (not signal.is_shutdown())
313 {
314 signal.set_shutdown(SHUTDOWN_GRACEFUL);
315 }
316
317 int exit_code= EXIT_SUCCESS;
318 shutdown_t status= signal.get_shutdown();
319 if (status == SHUTDOWN_FORCED)
320 {
321 Out << "Tests were aborted.";
322 exit_code= EXIT_FAILURE;
323 }
324 else if (stats.collection_failed)
325 {
326 Out << "Some test failed.";
327 exit_code= EXIT_FAILURE;
328 }
329 else if (stats.collection_skipped and stats.collection_failed and stats.collection_success)
330 {
331 Out << "Some tests were skipped.";
332 }
333 else if (stats.collection_success and stats.collection_failed == 0)
334 {
335 Out << "All tests completed successfully.";
336 }
337
338 stats_print(&stats);
339
340 delete world;
341
342 Outn(); // Generate a blank to break up the messages if make check/test has been run
343
344 return exit_code;
345 }