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_repeat
= false;
80 bool opt_quiet
= false;
81 std::string collection_to_run
;
87 OPT_LIBYATL_MATCH_COLLECTION
,
92 static struct option long_options
[]=
94 { "version", no_argument
, NULL
, OPT_LIBYATL_VERSION
},
95 { "quiet", no_argument
, NULL
, OPT_LIBYATL_QUIET
},
96 { "repeat", no_argument
, NULL
, OPT_LIBYATL_REPEAT
},
97 { "collection", required_argument
, NULL
, OPT_LIBYATL_MATCH_COLLECTION
},
104 int option_rv
= getopt_long(argc
, argv
, "", long_options
, &option_index
);
112 case OPT_LIBYATL_VERSION
:
115 case OPT_LIBYATL_QUIET
:
119 case OPT_LIBYATL_REPEAT
:
123 case OPT_LIBYATL_MATCH_COLLECTION
:
124 collection_to_run
= optarg
;
128 /* getopt_long already printed an error message. */
129 Error
<< "unknown option to getopt_long()";
138 srandom((unsigned int)time(NULL
));
141 if (bool(getenv("YATL_REPEAT")) and (repeat
= atoi(getenv("YATL_REPEAT"))))
146 if ((getenv("YATL_QUIET") and strcmp(getenv("YATL_QUIET"), "0") == 0) or opt_quiet
)
150 else if (getenv("JENKINS_URL"))
152 if (getenv("YATL_QUIET") and strcmp(getenv("YATL_QUIET"), "1") == 0)
162 close(STDOUT_FILENO
);
166 if (getenv("LIBTEST_TMP"))
168 snprintf(buffer
, sizeof(buffer
), "%s", getenv("LIBTEST_TMP"));
172 snprintf(buffer
, sizeof(buffer
), "%s", LIBTEST_TEMP
);
175 if (chdir(buffer
) == -1)
177 char getcwd_buffer
[1024];
178 char *dir
= getcwd(getcwd_buffer
, sizeof(getcwd_buffer
));
180 Error
<< "Unable to chdir() from " << dir
<< " to " << buffer
<< " errno:" << strerror(errno
);
184 if (libtest::libtool() == NULL
)
186 Error
<< "Failed to locate libtool";
194 exit_code
= EXIT_SUCCESS
;
197 fatal_assert(sigignore(SIGPIPE
) == 0);
199 libtest::SignalThread signal
;
200 if (signal
.setup() == false)
202 Error
<< "Failed to setup signals";
211 void *creators_ptr
= world
.create(error
);
219 Out
<< "SKIP " << argv
[0];
226 if (getenv("TEST_COLLECTION"))
228 if (strlen(getenv("TEST_COLLECTION")))
230 collection_to_run
= getenv("TEST_COLLECTION");
234 if (collection_to_run
.empty() == false)
236 Out
<< "Only testing " << collection_to_run
;
239 char *wildcard
= NULL
;
245 for (collection_st
*next
= world
.collections
; next
and next
->name
and (not signal
.is_shutdown()); next
++)
250 if (collection_to_run
.empty() == false and fnmatch(collection_to_run
.c_str(), next
->name
, 0))
255 stats
.collection_total
++;
257 test_return_t collection_rc
= world
.startup(creators_ptr
);
259 if (collection_rc
== TEST_SUCCESS
and next
->pre
)
261 collection_rc
= world
.runner()->pre(next
->pre
, creators_ptr
);
264 switch (collection_rc
)
270 Out
<< next
->name
<< " [ failed ]";
272 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
276 Out
<< next
->name
<< " [ skipping ]";
281 throw fatal_message("invalid return code");
284 Out
<< "Collection: " << next
->name
;
286 for (test_st
*run
= next
->tests
; run
->name
; run
++)
288 struct timeval start_time
, end_time
;
289 long int load_time
= 0;
291 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
296 test_return_t return_code
;
298 if (test_success(return_code
= world
.item
.startup(creators_ptr
)))
300 if (test_success(return_code
= world
.item
.flush(creators_ptr
, run
)))
302 // @note pre will fail is SKIPPED is returned
303 if (test_success(return_code
= world
.item
.pre(creators_ptr
)))
306 gettimeofday(&start_time
, NULL
);
307 assert(world
.runner());
308 assert(run
->test_fn
);
311 return_code
= world
.runner()->run(run
->test_fn
, creators_ptr
);
313 // Special case where check for the testing of the exception
315 catch (libtest::fatal
&e
)
317 if (fatal::is_disabled())
319 fatal::increment_disabled_counter();
320 return_code
= TEST_SUCCESS
;
328 gettimeofday(&end_time
, NULL
);
329 load_time
= timedif(end_time
, start_time
);
333 // @todo do something if post fails
334 (void)world
.item
.post(creators_ptr
);
336 else if (return_code
== TEST_SKIPPED
)
338 else if (return_code
== TEST_FAILURE
)
340 Error
<< " item.flush(failure)";
341 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
344 else if (return_code
== TEST_SKIPPED
)
346 else if (return_code
== TEST_FAILURE
)
348 Error
<< " item.startup(failure)";
349 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
353 catch (std::exception
&e
)
355 Error
<< "Exception was thrown: " << e
.what();
356 return_code
= TEST_FAILURE
;
360 Error
<< "Unknown exception occurred";
361 return_code
= TEST_FAILURE
;
369 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << load_time
/ 1000 << "." << load_time
% 1000 << "[ " << test_strerror(return_code
) << " ]";
376 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
382 Out
<< "\tTesting " << run
->name
<< "\t\t\t\t\t" << "[ " << test_strerror(return_code
) << " ]";
386 throw fatal_message("invalid return code");
389 if (test_failed(world
.on_error(return_code
, creators_ptr
)))
391 Error
<< "Failed while running on_error()";
392 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
397 (void) world
.runner()->post(next
->post
, creators_ptr
);
400 if (failed
== false and skipped
== false)
402 stats
.collection_success
++;
407 stats
.collection_failed
++;
412 stats
.collection_skipped
++;
415 world
.shutdown(creators_ptr
);
419 if (not signal
.is_shutdown())
421 signal
.set_shutdown(SHUTDOWN_GRACEFUL
);
424 shutdown_t status
= signal
.get_shutdown();
425 if (status
== SHUTDOWN_FORCED
)
427 Out
<< "Tests were aborted.";
428 exit_code
= EXIT_FAILURE
;
430 else if (stats
.collection_failed
)
432 Out
<< "Some test failed.";
433 exit_code
= EXIT_FAILURE
;
435 else if (stats
.collection_skipped
and stats
.collection_failed
and stats
.collection_success
)
437 Out
<< "Some tests were skipped.";
439 else if (stats
.collection_success
and stats
.collection_failed
== 0)
441 Out
<< "All tests completed successfully.";
446 Outn(); // Generate a blank to break up the messages if make check/test has been run
447 } while (exit_code
== EXIT_SUCCESS
and opt_repeat
);
449 catch (libtest::fatal
& e
)
451 std::cerr
<< e
.what() << std::endl
;
453 catch (std::exception
& e
)
455 std::cerr
<< e
.what() << std::endl
;
459 std::cerr
<< "Unknown exception halted execution." << std::endl
;