Skip to content

Commit ecb24ac

Browse files
committed
new acutest version
1 parent 433202c commit ecb24ac

File tree

2 files changed

+6741
-53
lines changed

2 files changed

+6741
-53
lines changed

include/acutest.h

+94-53
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Acutest -- Another C/C++ Unit Test facility
33
* <http://github.com/mity/acutest>
44
*
5-
* Copyright (c) 2013-2019 Martin Mitas
5+
* Copyright 2013-2020 Martin Mitas
6+
* Copyright 2019 Garrett D'Amore
67
*
78
* Permission is hereby granted, free of charge, to any person obtaining a
89
* copy of this software and associated documentation files (the "Software"),
@@ -178,7 +179,7 @@
178179
* the last test case after exiting the loop), you may use TEST_CASE(NULL).
179180
*/
180181
#define TEST_CASE_(...) test_case__(__VA_ARGS__)
181-
#define TEST_CASE(name) test_case__("%s", name);
182+
#define TEST_CASE(name) test_case__("%s", name)
182183

183184

184185
/* printf-like macro for outputting an extra information about a failure.
@@ -277,6 +278,13 @@
277278
#include <exception>
278279
#endif
279280

281+
/* Load valgrind.h, if available. This allows to detect valgrind's presence via RUNNING_ON_VALGRIND. */
282+
#ifdef __has_include
283+
#if __has_include(<valgrind.h>)
284+
#include <valgrind.h>
285+
#endif
286+
#endif
287+
280288

281289
/* Note our global private identifiers end with '__' to mitigate risk of clash
282290
* with the unit tests implementation. */
@@ -325,6 +333,7 @@ static int test_skip_mode__ = 0;
325333
static int test_worker__ = 0;
326334
static int test_worker_index__ = 0;
327335
static int test_cond_failed__ = 0;
336+
static int test_was_aborted__ = 0;
328337
static FILE *test_xml_output__ = NULL;
329338

330339
static int test_stat_failed_units__ = 0;
@@ -364,8 +373,8 @@ static jmp_buf test_abort_jmp_buf__;
364373
static double
365374
test_timer_diff__(LARGE_INTEGER start, LARGE_INTEGER end)
366375
{
367-
double duration = end.QuadPart - start.QuadPart;
368-
duration /= test_timer_freq__.QuadPart;
376+
double duration = (double)(end.QuadPart - start.QuadPart);
377+
duration /= (double)test_timer_freq__.QuadPart;
369378
return duration;
370379
}
371380

@@ -384,12 +393,7 @@ static jmp_buf test_abort_jmp_buf__;
384393
test_timer_init__(void)
385394
{
386395
if(test_timer__ == 1)
387-
#ifdef CLOCK_MONOTONIC_RAW
388-
/* linux specific; not subject of NTP adjustments or adjtime() */
389-
test_timer_id__ = CLOCK_MONOTONIC_RAW;
390-
#else
391396
test_timer_id__ = CLOCK_MONOTONIC;
392-
#endif
393397
else if(test_timer__ == 2)
394398
test_timer_id__ = CLOCK_PROCESS_CPUTIME_ID;
395399
}
@@ -403,11 +407,18 @@ static jmp_buf test_abort_jmp_buf__;
403407
static double
404408
test_timer_diff__(struct timespec start, struct timespec end)
405409
{
406-
return ((double) end.tv_sec +
407-
(double) end.tv_nsec * 10e-9)
408-
-
409-
((double) start.tv_sec +
410-
(double) start.tv_nsec * 10e-9);
410+
double endns;
411+
double startns;
412+
413+
endns = end.tv_sec;
414+
endns *= 1e9;
415+
endns += end.tv_nsec;
416+
417+
startns = start.tv_sec;
418+
startns *= 1e9;
419+
startns += start.tv_nsec;
420+
421+
return ((endns - startns)/ 1e9);
411422
}
412423

413424
static void
@@ -614,21 +625,19 @@ test_check__(int cond, const char* file, int line, const char* fmt, ...)
614625

615626
test_line_indent__(test_case_name__[0] ? 2 : 1);
616627
if(file != NULL) {
617-
if(test_verbose_level__ < 3) {
618628
#ifdef ACUTEST_WIN__
619-
const char* lastsep1 = strrchr(file, '\\');
620-
const char* lastsep2 = strrchr(file, '/');
621-
if(lastsep1 == NULL)
622-
lastsep1 = file-1;
623-
if(lastsep2 == NULL)
624-
lastsep2 = file-1;
625-
file = (lastsep1 > lastsep2 ? lastsep1 : lastsep2) + 1;
629+
const char* lastsep1 = strrchr(file, '\\');
630+
const char* lastsep2 = strrchr(file, '/');
631+
if(lastsep1 == NULL)
632+
lastsep1 = file-1;
633+
if(lastsep2 == NULL)
634+
lastsep2 = file-1;
635+
file = (lastsep1 > lastsep2 ? lastsep1 : lastsep2) + 1;
626636
#else
627-
const char* lastsep = strrchr(file, '/');
628-
if(lastsep != NULL)
629-
file = lastsep+1;
637+
const char* lastsep = strrchr(file, '/');
638+
if(lastsep != NULL)
639+
file = lastsep+1;
630640
#endif
631-
}
632641
printf("%s:%d: Check ", file, line);
633642
}
634643

@@ -881,16 +890,6 @@ test_error__(const char* fmt, ...)
881890
if(test_verbose_level__ == 0)
882891
return;
883892

884-
if(test_verbose_level__ <= 2 && !test_current_already_logged__ && test_current_unit__ != NULL) {
885-
if(test_tap__) {
886-
test_finish_test_line__(-1);
887-
} else {
888-
printf("[ ");
889-
test_print_in_color__(TEST_COLOR_RED_INTENSIVE__, "FAILED");
890-
printf(" ]\n");
891-
}
892-
}
893-
894893
if(test_verbose_level__ >= 2) {
895894
test_line_indent__(1);
896895
if(test_verbose_level__ >= 3)
@@ -910,6 +909,7 @@ test_error__(const char* fmt, ...)
910909
static int
911910
test_do_run__(const struct test__* test, int index)
912911
{
912+
test_was_aborted__ = 0;
913913
test_current_unit__ = test;
914914
test_current_index__ = index;
915915
test_current_failures__ = 0;
@@ -928,8 +928,10 @@ test_do_run__(const struct test__* test, int index)
928928

929929
if(!test_worker__) {
930930
test_abort_has_jmp_buf__ = 1;
931-
if(setjmp(test_abort_jmp_buf__) != 0)
931+
if(setjmp(test_abort_jmp_buf__) != 0) {
932+
test_was_aborted__ = 1;
932933
goto aborted;
934+
}
933935
}
934936

935937
test_timer_get_time__(&test_timer_start__);
@@ -952,10 +954,14 @@ test_do_run__(const struct test__* test, int index)
952954
}
953955
} else {
954956
test_print_in_color__(TEST_COLOR_RED_INTENSIVE__, "FAILED: ");
955-
printf("%d condition%s %s failed.\n",
956-
test_current_failures__,
957-
(test_current_failures__ == 1) ? "" : "s",
958-
(test_current_failures__ == 1) ? "has" : "have");
957+
if(!test_was_aborted__) {
958+
printf("%d condition%s %s failed.\n",
959+
test_current_failures__,
960+
(test_current_failures__ == 1) ? "" : "s",
961+
(test_current_failures__ == 1) ? "has" : "have");
962+
} else {
963+
printf("Aborted.\n");
964+
}
959965
}
960966
printf("\n");
961967
} else if(test_verbose_level__ >= 1 && test_current_failures__ == 0) {
@@ -972,9 +978,23 @@ test_do_run__(const struct test__* test, int index)
972978
test_check__(0, NULL, 0, "Threw std::exception");
973979
if(what != NULL)
974980
test_message__("std::exception::what(): %s", what);
981+
982+
if(test_verbose_level__ >= 3) {
983+
test_line_indent__(1);
984+
test_print_in_color__(TEST_COLOR_RED_INTENSIVE__, "FAILED: ");
985+
printf("C++ exception.\n\n");
986+
}
987+
975988
return -1;
976989
} catch(...) {
977990
test_check__(0, NULL, 0, "Threw an exception");
991+
992+
if(test_verbose_level__ >= 3) {
993+
test_line_indent__(1);
994+
test_print_in_color__(TEST_COLOR_RED_INTENSIVE__, "FAILED: ");
995+
printf("C++ exception.\n\n");
996+
}
997+
978998
return -1;
979999
}
9801000
#endif
@@ -1036,9 +1056,9 @@ test_run__(const struct test__* test, int index, int master_index)
10361056
case SIGTERM: signame = "SIGTERM"; break;
10371057
default: sprintf(tmp, "signal %d", WTERMSIG(exit_code)); signame = tmp; break;
10381058
}
1039-
test_error__("Test interrupted by %s", signame);
1059+
test_error__("Test interrupted by %s.", signame);
10401060
} else {
1041-
test_error__("Test ended in an unexpected way [%d]", exit_code);
1061+
test_error__("Test ended in an unexpected way [%d].", exit_code);
10421062
}
10431063
}
10441064

@@ -1053,7 +1073,7 @@ test_run__(const struct test__* test, int index, int master_index)
10531073
* through a command line arguments. */
10541074
_snprintf(buffer, sizeof(buffer)-1,
10551075
"%s --worker=%d %s --no-exec --no-summary %s --verbose=%d --color=%s -- \"%s\"",
1056-
test_argv0__, index, test_timer__ ? "--timer" : "",
1076+
test_argv0__, index, test_timer__ ? "--time" : "",
10571077
test_tap__ ? "--tap" : "", test_verbose_level__,
10581078
test_colorize__ ? "always" : "never",
10591079
test->name);
@@ -1065,6 +1085,13 @@ test_run__(const struct test__* test, int index, int master_index)
10651085
CloseHandle(processInfo.hThread);
10661086
CloseHandle(processInfo.hProcess);
10671087
failed = (exitCode != 0);
1088+
if(exitCode > 1) {
1089+
switch(exitCode) {
1090+
case 3: test_error__("Aborted."); break;
1091+
case 0xC0000005: test_error__("Access violation."); break;
1092+
default: test_error__("Test ended in an unexpected way [%lu].", exitCode); break;
1093+
}
1094+
}
10681095
} else {
10691096
test_error__("Cannot create unit test subprocess [%ld].", GetLastError());
10701097
failed = 1;
@@ -1281,14 +1308,14 @@ test_help__(void)
12811308
printf(" -s, --skip Execute all unit tests but the listed ones\n");
12821309
printf(" --exec[=WHEN] If supported, execute unit tests as child processes\n");
12831310
printf(" (WHEN is one of 'auto', 'always', 'never')\n");
1311+
printf(" -E, --no-exec Same as --exec=never\n");
12841312
#if defined ACUTEST_WIN__
1285-
printf(" -t, --timer Measure test duration\n");
1313+
printf(" -t, --time Measure test duration\n");
12861314
#elif defined ACUTEST_HAS_POSIX_TIMER__
1287-
printf(" -t, --timer Measure test duration (real time)\n");
1288-
printf(" --timer=TIMER Measure test duration, using given timer\n");
1315+
printf(" -t, --time Measure test duration (real time)\n");
1316+
printf(" --time=TIMER Measure test duration, using given timer\n");
12891317
printf(" (TIMER is one of 'real', 'cpu')\n");
12901318
#endif
1291-
printf(" -E, --no-exec Same as --exec=never\n");
12921319
printf(" --no-summary Suppress printing of test results summary\n");
12931320
printf(" --tap Produce TAP-compliant output\n");
12941321
printf(" (See https://testanything.org/)\n");
@@ -1300,6 +1327,7 @@ test_help__(void)
13001327
printf(" 1 ... Output one line per test (and summary)\n");
13011328
printf(" 2 ... As 1 and failed conditions (this is default)\n");
13021329
printf(" 3 ... As 1 and all conditions (and extended summary)\n");
1330+
printf(" -q, --quiet Same as --verbose=0\n");
13031331
printf(" --color[=WHEN] Enable colorized output\n");
13041332
printf(" (WHEN is one of 'auto', 'always', 'never')\n");
13051333
printf(" --no-color Same as --color=never\n");
@@ -1316,14 +1344,17 @@ static const TEST_CMDLINE_OPTION__ test_cmdline_options__[] = {
13161344
{ 0, "exec", 'e', TEST_CMDLINE_OPTFLAG_OPTIONALARG__ },
13171345
{ 'E', "no-exec", 'E', 0 },
13181346
#if defined ACUTEST_WIN__
1319-
{ 't', "timer", 't', 0 },
1347+
{ 't', "time", 't', 0 },
1348+
{ 0, "timer", 't', 0 }, /* kept for compatibility */
13201349
#elif defined ACUTEST_HAS_POSIX_TIMER__
1321-
{ 't', "timer", 't', TEST_CMDLINE_OPTFLAG_OPTIONALARG__ },
1350+
{ 't', "time", 't', TEST_CMDLINE_OPTFLAG_OPTIONALARG__ },
1351+
{ 0, "timer", 't', TEST_CMDLINE_OPTFLAG_OPTIONALARG__ }, /* kept for compatibility */
13221352
#endif
13231353
{ 0, "no-summary", 'S', 0 },
13241354
{ 0, "tap", 'T', 0 },
13251355
{ 'l', "list", 'l', 0 },
13261356
{ 'v', "verbose", 'v', TEST_CMDLINE_OPTFLAG_OPTIONALARG__ },
1357+
{ 'q', "quiet", 'q', 0 },
13271358
{ 0, "color", 'c', TEST_CMDLINE_OPTFLAG_OPTIONALARG__ },
13281359
{ 0, "no-color", 'C', 0 },
13291360
{ 'h', "help", 'h', 0 },
@@ -1367,7 +1398,7 @@ test_cmdline_callback__(int id, const char* arg)
13671398
test_timer__ = 2;
13681399
#endif
13691400
} else {
1370-
fprintf(stderr, "%s: Unrecognized argument '%s' for option --timer.\n", test_argv0__, arg);
1401+
fprintf(stderr, "%s: Unrecognized argument '%s' for option --time.\n", test_argv0__, arg);
13711402
fprintf(stderr, "Try '%s --help' for more information.\n", test_argv0__);
13721403
exit(2);
13731404
}
@@ -1390,6 +1421,10 @@ test_cmdline_callback__(int id, const char* arg)
13901421
test_verbose_level__ = (arg != NULL ? atoi(arg) : test_verbose_level__+1);
13911422
break;
13921423

1424+
case 'q':
1425+
test_verbose_level__ = 0;
1426+
break;
1427+
13931428
case 'c':
13941429
if(arg == NULL || strcmp(arg, "always") == 0) {
13951430
test_colorize__ = 1;
@@ -1509,8 +1544,6 @@ main(int argc, char** argv)
15091544
test_colorize__ = 0;
15101545
#endif
15111546

1512-
test_timer_init__();
1513-
15141547
/* Count all test units */
15151548
test_list_size__ = 0;
15161549
for(i = 0; test_list__[i].func != NULL; i++)
@@ -1525,6 +1558,9 @@ main(int argc, char** argv)
15251558
/* Parse options */
15261559
test_cmdline_read__(test_cmdline_options__, argc, argv, test_cmdline_callback__);
15271560

1561+
/* Initialize the proper timer. */
1562+
test_timer_init__();
1563+
15281564
#if defined(ACUTEST_WIN__)
15291565
SetUnhandledExceptionFilter(test_seh_exception_filter__);
15301566
#endif
@@ -1549,6 +1585,11 @@ main(int argc, char** argv)
15491585
#ifdef ACUTEST_LINUX__
15501586
if(test_is_tracer_present__())
15511587
test_no_exec__ = 1;
1588+
#endif
1589+
#ifdef RUNNING_ON_VALGRIND
1590+
/* RUNNING_ON_VALGRIND is provided by valgrind.h */
1591+
if(RUNNING_ON_VALGRIND)
1592+
test_no_exec__ = 1;
15521593
#endif
15531594
}
15541595
}

0 commit comments

Comments
 (0)