1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
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.
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.
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
23 #include <libtest/common.h>
29 #include <sys/types.h>
39 #ifndef __INTEL_COMPILER
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
43 using namespace libtest
;
45 static void stats_print(Stats
*stats
)
47 if (stats
->collection_failed
== 0 and stats
->collection_success
== 0)
52 Out
<< "\tTotal Collections\t\t\t\t" << stats
->collection_total
;
53 Out
<< "\tFailed Collections\t\t\t\t" << stats
->collection_failed
;
54 Out
<< "\tSkipped Collections\t\t\t\t" << stats
->collection_skipped
;
55 Out
<< "\tSucceeded Collections\t\t\t\t" << stats
->collection_success
;
57 Out
<< "Total\t\t\t\t" << stats
->total
;
58 Out
<< "\tFailed\t\t\t" << stats
->failed
;
59 Out
<< "\tSkipped\t\t\t" << stats
->skipped
;
60 Out
<< "\tSucceeded\t\t" << stats
->success
;
63 static long int timedif(struct timeval a
, struct timeval b
)
67 us
= (long)(a
.tv_usec
- b
.tv_usec
);
69 s
= (long)(a
.tv_sec
- b
.tv_sec
);
77 int main(int argc
, char *argv
[])
79 bool opt_massive
= false;
80 unsigned long int opt_repeat
= 1; // Run all tests once
81 bool opt_quiet
= false;
82 std::string collection_to_run
;
88 OPT_LIBYATL_MATCH_COLLECTION
,
94 static struct option long_options
[]=
96 { "version", no_argument
, NULL
, OPT_LIBYATL_VERSION
},
97 { "quiet", no_argument
, NULL
, OPT_LIBYATL_QUIET
},
98 { "repeat", no_argument
, NULL
, OPT_LIBYATL_REPEAT
},
99 { "collection", required_argument
, NULL
, OPT_LIBYATL_MATCH_COLLECTION
},
100 { "massive", no_argument
, NULL
, OPT_LIBYATL_MASSIVE
},
107 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
115 case OPT_LIBYATL_VERSION
:
118 case OPT_LIBYATL_QUIET
:
122 case OPT_LIBYATL_REPEAT
:
123 opt_repeat
= strtoul(optarg
, (char **) NULL
, 10);
126 case OPT_LIBYATL_MATCH_COLLECTION
:
127 collection_to_run
= optarg
;
130 case OPT_LIBYATL_MASSIVE
:
135 /* getopt_long already printed an error message. */
136 Error
<< "unknown option to getopt_long()";
145 srandom((unsigned int)time(NULL
));
147 if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL
, 10) > 1))
149 opt_repeat
= strtoul(getenv("YATL_REPEAT"), (char **) NULL
, 10);
152 if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet
)
156 else if (getenv("JENKINS_URL"))
158 if (bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "1") == 0))
168 close(STDOUT_FILENO
);
172 if (getenv("LIBTEST_TMP"))
174 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
178 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
181 if (chdir(buffer
) == -1)
183 char getcwd_buffer
[1024];
184 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
186 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
190 if (libtest::libtool() == NULL
)
192 Error
<< "Failed to locate libtool";
200 exit_code
= EXIT_SUCCESS
;
203 fatal_assert(sigignore(SIGPIPE
) == 0);
205 libtest::SignalThread signal
;
206 if (signal
.setup() == false)
208 Error
<< "Failed to setup signals";
217 void *creators_ptr
= world
.create(error
);
225 Out
<< "SKIP " << argv
[0];
232 if (getenv("TEST_COLLECTION"))
234 if (strlen(getenv("TEST_COLLECTION")))
236 collection_to_run
= getenv("TEST_COLLECTION");
240 if (collection_to_run
.empty() == false)
242 Out
<< "Only testing " << collection_to_run
;
245 char *wildcard
= NULL
;
251 for (collection_st
*next
= world
.collections
; next
and next
->name
and (not signal
.is_shutdown()); next
++)
256 if (collection_to_run
.empty() == false and fnmatch(collection_to_run
.c_str(), next
->name
, 0))
261 stats
.collection_total
++;
263 test_return_t collection_rc
= world
.startup(creators_ptr
);
265 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
267 collection_rc
= world
.runner()->pre(next
->pre
, creators_ptr
);
270 switch (collection_rc
)
276 Out
<< next
->name
<< " [ failed ]";
278 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
282 Out
<< next
->name
<< " [ skipping ]";
287 fatal_message("invalid return code");
290 Out
<< "Collection: " << next
->name
;
292 for (test_st
*run
= next
->tests
; run
->name
; run
++)
294 struct timeval start_time
, end_time
;
295 long int load_time
= 0;
297 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
302 test_return_t return_code
;
304 if (test_success(return_code
= world
.item
.startup(creators_ptr
)))
306 if (test_success(return_code
= world
.item
.flush(creators_ptr
, run
)))
308 // @note pre will fail is SKIPPED is returned
309 if (test_success(return_code
= world
.item
.pre(creators_ptr
)))
312 gettimeofday(&start_time
, NULL
);
313 assert(world
.runner());
314 assert(run
->test_fn
);
317 return_code
= world
.runner()->run(run
->test_fn
, creators_ptr
);
319 // Special case where check for the testing of the exception
321 catch (libtest::fatal
&e
)
323 if (fatal::is_disabled())
325 fatal::increment_disabled_counter();
326 return_code
= TEST_SUCCESS
;
334 gettimeofday(&end_time
, NULL
);
335 load_time
= timedif(end_time
, start_time
);
339 // @todo do something if post fails
340 (void)world
.item
.post(creators_ptr
);
342 else if (return_code
== TEST_SKIPPED
)
344 else if (return_code
== TEST_FAILURE
)
346 Error
<< " item.flush(failure)";
347 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
350 else if (return_code
== TEST_SKIPPED
)
352 else if (return_code
== TEST_FAILURE
)
354 Error
<< " item.startup(failure)";
355 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
359 catch (libtest::fatal
&e
)
361 Error
<< "Fatal exception was thrown: " << e
.what();
362 return_code
= TEST_FAILURE
;
364 catch (std::exception
&e
)
366 Error
<< "Exception was thrown: " << e
.what();
367 return_code
= TEST_FAILURE
;
371 Error
<< "Unknown exception occurred";
372 return_code
= TEST_FAILURE
;
380 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
387 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
393 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
397 fatal_message("invalid return code");
400 if (test_failed(world
.on_error(return_code
, creators_ptr
)))
402 Error
<< "Failed while running on_error()";
403 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
408 (void) world
.runner()->post(next
->post
, creators_ptr
);
411 if (failed
== false and skipped
== false)
413 stats
.collection_success
++;
418 stats
.collection_failed
++;
423 stats
.collection_skipped
++;
426 world
.shutdown(creators_ptr
);
430 if (not signal
.is_shutdown())
432 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
435 shutdown_t status
= signal
.get_shutdown();
436 if (status
== SHUTDOWN_FORCED
)
438 Out
<< "Tests were aborted.";
439 exit_code
= EXIT_FAILURE
;
441 else if (stats
.collection_failed
)
443 Out
<< "Some test failed.";
444 exit_code
= EXIT_FAILURE
;
446 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
448 Out
<< "Some tests were skipped.";
450 else if (stats
.collection_success
and stats
.collection_failed
== 0)
452 Out
<< "All tests completed successfully.";
457 Outn(); // Generate a blank to break up the messages if make check/test has been run
458 } while (exit_code
== EXIT_SUCCESS
and --opt_repeat
);
460 catch (libtest::fatal
& e
)
462 std::cerr
<< e
.what() << std::endl
;
464 catch (std::exception
& e
)
466 std::cerr
<< e
.what() << std::endl
;
470 std::cerr
<< "Unknown exception halted execution." << std::endl
;