X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/595e59c568ff5f8510de201bfd800951cdc2adcb..1873a02acdc52506c010f43b4c78a8b8400dc0de:/src/xbt/cunit.cpp diff --git a/src/xbt/cunit.cpp b/src/xbt/cunit.cpp index 94e16a6638..9da7c7ea5f 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. */ @@ -10,13 +9,15 @@ /* At some point we should use https://github.com/google/googletest instead */ #include "src/internal_config.h" -#include +#include #include #include "xbt/sysdep.h" /* bvprintf */ #include "xbt/cunit.h" #include "xbt/dynar.h" +#define STRLEN 1024 + /* collection of all suites */ static xbt_dynar_t _xbt_test_suites = nullptr; /* global statistics */ @@ -96,12 +97,13 @@ struct s_xbt_test_unit { 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=nullptr\n"); } @@ -166,7 +168,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); @@ -185,15 +187,15 @@ 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); @@ -207,14 +209,15 @@ 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 nullptr!\n"); + fprintf(stderr, "TESTSUITE IS NULL!\n"); } } @@ -249,15 +252,6 @@ 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; - unsigned int it_test; - unsigned int it_log; - - int first = 1; /* for result pretty printing */ - if (suite == nullptr) return 0; @@ -275,12 +269,13 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) 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) + if (not suite->enabled) snprintf(suite_title + 70, 11, " DISABLED "); fprintf(stderr, "\n%s\n", suite_title); if (suite->enabled) { /* iterate through all tests */ + unsigned int it_unit; xbt_dynar_foreach(suite->units, it_unit, unit) { /* init unit case counters */ unit->nb_tests = 0; @@ -289,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); @@ -301,15 +297,16 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) unit->func(); /* iterate through all performed tests to determine status */ + unsigned int it_test; xbt_dynar_foreach(unit->tests, it_test, test) { if (test->ignored) { unit->test_ignore++; } 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++; @@ -326,8 +323,8 @@ 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 != nullptr ? test->file : unit->file); - line = (test->line != 0 ? test->line : unit->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"; @@ -344,7 +341,8 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity) } 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 != nullptr ? log->file : file); line = (log->line != 0 ? log->line : line); @@ -358,7 +356,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 */ @@ -382,7 +380,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++; @@ -398,7 +396,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++; @@ -406,6 +404,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"))); @@ -456,32 +456,31 @@ static void apply_selection(char *selection) /* for the parsing */ char *sel = selection; int done = 0; - char dir[1024]; /* the directive */ + char dir[STRLEN]; /* the directive */ /* iterators */ unsigned int it_suite; xbt_test_suite_t suite; xbt_test_unit_t unit; unsigned int it_unit; - char suitename[512]; - char unitname[512]; + char suitename[STRLEN]; + char unitname[STRLEN]; - 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, ','); if (p) { - strncpy(dir, sel, p - sel); - dir[p - sel] = '\0'; + snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel); sel = p + 1; } else { - strncpy(dir, sel,1024); + snprintf(dir, STRLEN, "%s", sel); done = 1; } @@ -496,16 +495,15 @@ static void apply_selection(char *selection) p = strchr(dir, ':'); if (p) { - strncpy(unitname, p + 1,512); - strncpy(suitename, dir, p - dir); - suitename[p - dir] = '\0'; + snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir); + snprintf(unitname, STRLEN, "%s", p + 1); } else { - strncpy(suitename, dir,512); + snprintf(suitename, STRLEN, "%s", dir); unitname[0] = '\0'; } /* 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) { @@ -519,7 +517,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; @@ -535,7 +533,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; }