X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1555ef43eee251e5892a2f7fd49bc96e99d68fef..bee4b1233e37aa018a71c68ce2be9ee2e019e4c7:/src/xbt/cunit.cpp diff --git a/src/xbt/cunit.cpp b/src/xbt/cunit.cpp index 4ca449fb6f..0983062623 100644 --- a/src/xbt/cunit.cpp +++ b/src/xbt/cunit.cpp @@ -1,7 +1,6 @@ /* cunit - A little C Unit facility */ -/* Copyright (c) 2005-2014. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2005-2017. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -18,7 +17,7 @@ #include "xbt/dynar.h" /* collection of all suites */ -static xbt_dynar_t _xbt_test_suites = NULL; +static xbt_dynar_t _xbt_test_suites = nullptr; /* global statistics */ static int _xbt_test_nb_tests = 0; static int _xbt_test_test_failed = 0; @@ -36,7 +35,7 @@ static int _xbt_test_suite_ignore = 0; static int _xbt_test_suite_disabled = 0; /* Context */ -xbt_test_unit_t _xbt_test_current_unit = NULL; +xbt_test_unit_t _xbt_test_current_unit = nullptr; /* test suite test log */ typedef struct s_xbt_test_log { @@ -50,7 +49,7 @@ static void xbt_test_log_dump(xbt_test_log_t log) if (log) fprintf(stderr, " log %p(%s:%d)=%s\n", log, log->file, log->line, log->text); else - fprintf(stderr, " log=NULL\n"); + fprintf(stderr, " log=nullptr\n"); } /* test suite test check */ @@ -74,7 +73,7 @@ static void xbt_test_test_dump(xbt_test_test_t test) xbt_dynar_foreach(test->logs, it_log, log) xbt_test_log_dump(log); } else - fprintf(stderr, " test=NULL\n"); + fprintf(stderr, " test=nullptr\n"); } /* test suite test unit */ @@ -88,20 +87,23 @@ struct s_xbt_test_unit { xbt_dynar_t tests; /* of xbt_test_test_t */ int nb_tests; - int test_failed, test_ignore, test_expect; + int test_failed; + int test_ignore; + int test_expect; }; static void xbt_test_unit_dump(xbt_test_unit_t unit) { if (unit) { - xbt_test_test_t test; - unsigned int it_test; fprintf(stderr, " UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled")); - if (unit->enabled) + if (unit->enabled) { + xbt_test_test_t test; + unsigned int it_test; xbt_dynar_foreach(unit->tests, it_test, test) xbt_test_test_dump(test); + } } else { - fprintf(stderr, " unit=NULL\n"); + fprintf(stderr, " unit=nullptr\n"); } } @@ -112,9 +114,14 @@ struct s_xbt_test_suite { char *title; xbt_dynar_t units; /* of xbt_test_unit_t */ - int nb_tests, nb_units; - int test_failed, test_ignore, test_expect; - int unit_failed, unit_ignore, unit_disabled; + int nb_tests; + int nb_units; + int test_failed; + int test_ignore; + int test_expect; + int unit_failed; + int unit_ignore; + int unit_disabled; }; /* destroy test suite */ @@ -122,7 +129,7 @@ static void xbt_test_suite_free(void *s) { xbt_test_suite_t suite = *(xbt_test_suite_t *) s; - if (suite == NULL) + if (suite == nullptr) return; xbt_dynar_free(&suite->units); free(suite->title); @@ -159,7 +166,7 @@ xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...) xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1); va_list ap; - if (!_xbt_test_suites) + if (_xbt_test_suites == nullptr) _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free); va_start(ap, fmt); @@ -178,20 +185,20 @@ xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...) xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...) { xbt_test_suite_t suite; - unsigned int it_suite; - char *bufname; va_list ap; - if (_xbt_test_suites) + if (_xbt_test_suites) { + unsigned int it_suite; xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) - if (!strcmp(suite->name, name)) - return suite; + if (not strcmp(suite->name, name)) + return suite; + } va_start(ap, fmt); bufname = bvprintf(fmt, ap); va_end(ap); - suite = xbt_test_suite_new(name, bufname, NULL); + suite = xbt_test_suite_new(name, bufname, nullptr); free(bufname); return suite; @@ -200,12 +207,13 @@ xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...) void xbt_test_suite_dump(xbt_test_suite_t suite) { if (suite) { - xbt_test_unit_t unit; - unsigned int it_unit; fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled"); - if (suite->enabled) + if (suite->enabled) { + xbt_test_unit_t unit; + unsigned int it_unit; xbt_dynar_foreach(suite->units, it_unit, unit) xbt_test_unit_dump(unit); + } } else { fprintf(stderr, "TESTSUITE IS NULL!\n"); } @@ -227,13 +235,12 @@ void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t va_end(ap); unit->name = (char *) name; unit->func = func; - unit->file = NULL; + unit->file = nullptr; unit->line = 0; unit->enabled = 1; unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free); xbt_dynar_push(suite->units, &unit); - return; } /* run test one suite */ @@ -243,36 +250,29 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) xbt_test_test_t test; xbt_test_log_t log; - const char *file; - int line; - char *cp; - unsigned int it_unit, it_test, it_log; - - int first = 1; /* for result pretty printing */ + unsigned int it_unit; + unsigned int it_test; - if (suite == NULL) + if (suite == nullptr) return 0; /* suite title pretty-printing */ - { - char suite_title[81]; - int suite_len = strlen(suite->title); - int i; - - xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len); - - suite_title[0] = ' '; - for (i = 1; i < 80; i++) - suite_title[i] = '='; - suite_title[i++] = '\n'; - suite_title[80] = '\0'; - - snprintf(suite_title + 40 - (suite_len + 4) / 2, 81-(40 - (suite_len + 4)/ 2), "[ %s ]", suite->title); - suite_title[40 + (suite_len + 5) / 2] = '='; - if (!suite->enabled) - snprintf(suite_title + 70, 11, " DISABLED "); - fprintf(stderr, "\n%s\n", suite_title); - } + char suite_title[81]; + int suite_len = strlen(suite->title); + + xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len); + + suite_title[0] = ' '; + for (int i = 1; i < 79; i++) + suite_title[i] = '='; + suite_title[79] = '\n'; + suite_title[80] = '\0'; + + snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title); + suite_title[40 + (suite_len + 5) / 2] = '='; + if (not suite->enabled) + snprintf(suite_title + 70, 11, " DISABLED "); + fprintf(stderr, "\n%s\n", suite_title); if (suite->enabled) { /* iterate through all tests */ @@ -284,8 +284,9 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) unit->test_expect = 0; /* display unit title */ - cp = bprintf(" Unit: %s ......................................" - "......................................", unit->title); + char* cp = bprintf(" Unit: %s ......................................" + "......................................", + unit->title); cp[70] = '\0'; fprintf(stderr, "%s", cp); free(cp); @@ -302,9 +303,9 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) } else { unit->nb_tests++; - if (test->failed && !test->expected_failure) + if (test->failed && not test->expected_failure) unit->test_failed++; - if (!test->failed && test->expected_failure) + if (not test->failed && test->expected_failure) unit->test_failed++; if (test->expected_failure) unit->test_expect++; @@ -321,14 +322,28 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */ } xbt_dynar_foreach(unit->tests, it_test, test) { - file = (test->file != NULL ? test->file : unit->file); - line = (test->line != 0 ? test->line : unit->line); - fprintf(stderr, " %s: %s [%s:%d]\n", (test->ignored ? " SKIP" : (test->expected_failure - ? (test-> failed ? "EFAIL" : "EPASS") : (test->failed ? " FAIL" : " PASS"))),test->title, file, line); + const char* file = (test->file != nullptr ? test->file : unit->file); + int line = (test->line != 0 ? test->line : unit->line); + const char* resname; + if (test->ignored) + resname = " SKIP"; + else if (test->expected_failure) { + if (test->failed) + resname = "EFAIL"; + else + resname = "EPASS"; + } else { + if (test->failed) + resname = " FAIL"; + else + resname = " PASS"; + } + fprintf(stderr, " %s: %s [%s:%d]\n", resname, test->title, file, line); - if ((test->expected_failure && !test->failed) || (!test->expected_failure && test->failed)) { + if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) { + unsigned int it_log; xbt_dynar_foreach(test->logs, it_log, log) { - file = (log->file != NULL ? log->file : file); + file = (log->file != nullptr ? log->file : file); line = (log->line != 0 ? log->line : line); fprintf(stderr, " %s:%d: %s\n", file, line, log->text); } @@ -340,7 +355,7 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) } else { fprintf(stderr, "\n"); } - } else if (!unit->enabled) { + } else if (not unit->enabled) { fprintf(stderr, " disabled\n"); /* no test were run */ } else if (unit->nb_tests) { fprintf(stderr, "...... ok\n"); /* successful */ @@ -364,7 +379,7 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) suite->nb_units++; if (unit->test_failed) suite->unit_failed++; - } else if (!unit->enabled) { + } else if (not unit->enabled) { suite->unit_disabled++; } else { suite->unit_ignore++; @@ -380,7 +395,7 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) _xbt_test_nb_suites++; if (suite->test_failed) _xbt_test_suite_failed++; - } else if (!suite->enabled) { + } else if (not suite->enabled) { _xbt_test_suite_disabled++; } else { _xbt_test_suite_ignore++; @@ -388,6 +403,8 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) /* print test suite summary */ if (suite->enabled) { + int first = 1; /* for result pretty printing */ + fprintf(stderr," =====================================================================%s\n", (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") : (suite->unit_disabled ? " DISABLED" : "==== SKIP"))); @@ -448,13 +465,13 @@ static void apply_selection(char *selection) char suitename[512]; char unitname[512]; - if (!selection || selection[0] == '\0') + if (not selection || selection[0] == '\0') return; /*printf("Test selection: %s\n", selection); */ /* First apply the selection */ - while (!done) { + while (not done) { int enabling = 1; char *p = strchr(sel, ','); @@ -487,7 +504,7 @@ static void apply_selection(char *selection) } /* Deal with the specific case of 'all' pseudo serie */ - if (!strcmp("all", suitename)) { + if (not strcmp("all", suitename)) { xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n"); xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) { @@ -501,7 +518,7 @@ static void apply_selection(char *selection) for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) { xbt_test_suite_t thissuite = xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t); - if (!strcmp(suitename, thissuite->name)) { + if (not strcmp(suitename, thissuite->name)) { /* Do not disable the whole suite when we just want to disable a child */ if (enabling || (unitname[0] == '\0')) thissuite->enabled = enabling; @@ -517,7 +534,7 @@ static void apply_selection(char *selection) it2_unit < xbt_dynar_length(thissuite->units); it2_unit++) { xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t); - if (!strcmp(thisunit->name, unitname)) { + if (not strcmp(thisunit->name, unitname)) { thisunit->enabled = enabling; break; } @@ -583,7 +600,7 @@ int xbt_test_run(char *selection, int verbosity) ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units); first = 1; if (_xbt_test_nb_units != _xbt_test_unit_failed) { - fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_units - _xbt_test_unit_failed); + fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed); first = 0; } if (_xbt_test_unit_failed) { @@ -597,7 +614,7 @@ int xbt_test_run(char *selection, int verbosity) ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests); first = 1; if (_xbt_test_nb_tests != _xbt_test_test_failed) { - fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_tests - _xbt_test_test_failed); + fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed); first = 0; } if (_xbt_test_test_failed) { @@ -620,7 +637,7 @@ int xbt_test_run(char *selection, int verbosity) return _xbt_test_unit_failed; } -void xbt_test_exit(void) +void xbt_test_exit() { xbt_dynar_free(&_xbt_test_suites); } @@ -643,7 +660,6 @@ void _xbt_test_add(const char *file, int line, const char *fmt, ...) test->line = line; test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free); xbt_dynar_push(unit->tests, &test); - return; } /* annotate test case with log message and failure */ @@ -670,10 +686,10 @@ void _xbt_test_fail(const char *file, int line, const char *fmt, ...) void xbt_test_exception(xbt_ex_t e) { - _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what()); + _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what()); } -void xbt_test_expect_failure(void) +void xbt_test_expect_failure() { xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests), "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title); @@ -681,7 +697,7 @@ void xbt_test_expect_failure(void) test->expected_failure = 1; } -void xbt_test_skip(void) +void xbt_test_skip() { xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests), "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);