1 /* cunit - A little C Unit facility */
3 /* Copyright (c) 2005-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 /* This is partially inspirated from the OSSP ts (Test Suite Library) */
9 /* At some point we should use https://github.com/google/googletest instead */
11 #include "src/internal_config.h"
17 #include <xbt/cunit.h>
19 #include <xbt/string.hpp>
21 #include "xbt/sysdep.h" /* bvprintf */
22 #include "xbt/dynar.h"
26 /* collection of all suites */
27 static xbt_dynar_t _xbt_test_suites = nullptr;
28 /* global statistics */
29 static int _xbt_test_nb_tests = 0;
30 static int _xbt_test_test_failed = 0;
31 static int _xbt_test_test_ignore = 0;
32 static int _xbt_test_test_expect = 0;
34 static int _xbt_test_nb_units = 0;
35 static int _xbt_test_unit_failed = 0;
36 static int _xbt_test_unit_ignore = 0;
37 static int _xbt_test_unit_disabled = 0;
39 static int _xbt_test_nb_suites = 0;
40 static int _xbt_test_suite_failed = 0;
41 static int _xbt_test_suite_ignore = 0;
42 static int _xbt_test_suite_disabled = 0;
45 xbt_test_unit_t _xbt_test_current_unit = nullptr;
47 /* test suite test log */
48 class s_xbt_test_log {
50 s_xbt_test_log(std::string text, std::string file, int line)
51 : text_(std::move(text)), file_(std::move(file)), line_(line)
61 void s_xbt_test_log::dump() const
63 fprintf(stderr, " log %p(%s:%d)=%s\n", this, this->file_.c_str(), this->line_, this->text_.c_str());
66 /* test suite test check */
67 struct s_xbt_test_test {
74 std::vector<s_xbt_test_log> logs;
76 typedef s_xbt_test_test* xbt_test_test_t;
78 static void xbt_test_test_dump(xbt_test_test_t test)
81 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n", test, test->file, test->line, test->title,
82 test->failed ? "failed" : "not failed");
83 for (s_xbt_test_log const& log : test->logs)
86 fprintf(stderr, " test=nullptr\n");
89 /* test suite test unit */
90 struct s_xbt_test_unit {
97 xbt_dynar_t tests; /* of xbt_test_test_t */
105 static void xbt_test_unit_dump(xbt_test_unit_t unit)
108 fprintf(stderr, " UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
110 xbt_test_test_t test;
111 unsigned int it_test;
112 xbt_dynar_foreach(unit->tests, it_test, test)
113 xbt_test_test_dump(test);
116 fprintf(stderr, " unit=nullptr\n");
121 struct s_xbt_test_suite {
125 xbt_dynar_t units; /* of xbt_test_unit_t */
137 /* destroy test suite */
138 static void xbt_test_suite_free(void *s)
140 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
142 if (suite == nullptr)
144 xbt_dynar_free(&suite->units);
149 static void xbt_test_unit_free(void *unit)
151 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
154 xbt_dynar_free(&u->tests);
158 static void xbt_test_test_free(void *test)
160 xbt_test_test_t t = *(xbt_test_test_t *) test;
165 /** @brief retrieve a testsuite from name, or create a new one */
166 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
168 if (_xbt_test_suites == nullptr) {
169 _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
171 xbt_test_suite_t suite;
172 unsigned int it_suite;
173 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
174 if (not strcmp(suite->name, name))
178 xbt_test_suite_t suite = xbt_new0(s_xbt_test_suite, 1);
181 suite->title = bvprintf(fmt, ap);
182 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
187 xbt_dynar_push(_xbt_test_suites, &suite);
192 static void xbt_test_suite_dump(xbt_test_suite_t suite)
195 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
196 if (suite->enabled) {
197 xbt_test_unit_t unit;
198 unsigned int it_unit;
199 xbt_dynar_foreach(suite->units, it_unit, unit)
200 xbt_test_unit_dump(unit);
203 fprintf(stderr, "TESTSUITE IS NULL!\n");
207 /* add test case to test suite */
208 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
210 xbt_test_unit_t unit;
217 unit = xbt_new0(s_xbt_test_unit, 1);
219 unit->title = bvprintf(fmt, ap);
221 unit->name = (char *) name;
223 unit->file = nullptr;
226 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
228 xbt_dynar_push(suite->units, &unit);
231 /* run test one suite */
232 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
234 xbt_test_unit_t unit;
235 xbt_test_test_t test;
237 if (suite == nullptr)
240 /* suite title pretty-printing */
241 char suite_title[81];
242 int suite_len = strlen(suite->title);
244 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
246 suite_title[0] = ' ';
247 for (int i = 1; i < 79; i++)
248 suite_title[i] = '=';
249 suite_title[79] = '\n';
250 suite_title[80] = '\0';
252 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
253 suite_title[40 + (suite_len + 5) / 2] = '=';
254 if (not suite->enabled)
255 snprintf(suite_title + 70, 11, " DISABLED ");
256 fprintf(stderr, "\n%s\n", suite_title);
258 if (suite->enabled) {
259 /* iterate through all tests */
260 unsigned int it_unit;
261 xbt_dynar_foreach(suite->units, it_unit, unit) {
262 /* init unit case counters */
264 unit->test_ignore = 0;
265 unit->test_failed = 0;
266 unit->test_expect = 0;
268 /* display unit title */
269 char* cp = bprintf(" Unit: %s ......................................"
270 "......................................",
273 fprintf(stderr, "%s", cp);
276 /* run the test case function */
277 _xbt_test_current_unit = unit;
281 /* iterate through all performed tests to determine status */
282 unsigned int it_test;
283 xbt_dynar_foreach(unit->tests, it_test, test) {
289 if (test->failed && not test->expected_failure)
291 if (not test->failed && test->expected_failure)
293 if (test->expected_failure)
297 /* Display whether this unit went well */
298 if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
299 /* some tests failed (or were supposed to), so do detailed reporting of test case */
300 if (unit->test_failed > 0) {
301 fprintf(stderr, ".. failed\n");
302 } else if (unit->nb_tests) {
303 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
305 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
307 xbt_dynar_foreach(unit->tests, it_test, test) {
308 const char* file = (test->file != nullptr ? test->file : unit->file);
309 int line = (test->line != 0 ? test->line : unit->line);
313 else if (test->expected_failure) {
324 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test->title, file, line);
326 if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
327 for (s_xbt_test_log const& log : test->logs) {
328 file = (log.file_.empty() ? file : log.file_.c_str());
329 line = (log.line_ == 0 ? line : log.line_);
330 fprintf(stderr, " %s:%d: %s\n", file, line, log.text_.c_str());
334 fprintf(stderr, " Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
335 if (unit->test_ignore) {
336 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
338 fprintf(stderr, "\n");
340 } else if (not unit->enabled) {
341 fprintf(stderr, " disabled\n"); /* no test were run */
342 } else if (unit->nb_tests) {
343 fprintf(stderr, "...... ok\n"); /* successful */
345 fprintf(stderr, ".... skip\n"); /* no test were run */
348 /* Accumulate test counts into the suite */
349 suite->nb_tests += unit->nb_tests;
350 suite->test_failed += unit->test_failed;
351 suite->test_ignore += unit->test_ignore;
352 suite->test_expect += unit->test_expect;
354 _xbt_test_nb_tests += unit->nb_tests;
355 _xbt_test_test_failed += unit->test_failed;
356 _xbt_test_test_ignore += unit->test_ignore;
357 _xbt_test_test_expect += unit->test_expect;
359 /* What's the conclusion of this test anyway? */
360 if (unit->nb_tests) {
362 if (unit->test_failed)
363 suite->unit_failed++;
364 } else if (not unit->enabled) {
365 suite->unit_disabled++;
367 suite->unit_ignore++;
371 _xbt_test_nb_units += suite->nb_units;
372 _xbt_test_unit_failed += suite->unit_failed;
373 _xbt_test_unit_ignore += suite->unit_ignore;
374 _xbt_test_unit_disabled += suite->unit_disabled;
376 if (suite->nb_units) {
377 _xbt_test_nb_suites++;
378 if (suite->test_failed)
379 _xbt_test_suite_failed++;
380 } else if (not suite->enabled) {
381 _xbt_test_suite_disabled++;
383 _xbt_test_suite_ignore++;
386 /* print test suite summary */
387 if (suite->enabled) {
388 int first = 1; /* for result pretty printing */
390 fprintf(stderr," =====================================================================%s\n",
391 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
392 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
393 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
394 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
396 if (suite->nb_units != suite->unit_failed) {
397 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
400 if (suite->unit_failed) {
401 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
404 if (suite->unit_ignore) {
405 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
408 if (suite->unit_disabled) {
409 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
411 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
412 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
415 if (suite->nb_tests != suite->test_failed) {
416 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
419 if (suite->test_failed) {
420 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
423 if (suite->test_ignore) {
424 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
427 if (suite->test_expect) {
428 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
430 fprintf(stderr, ")\n");
432 return suite->unit_failed;
435 static void apply_selection(char *selection)
437 /* for the parsing */
438 char *sel = selection;
440 char dir[STRLEN]; /* the directive */
442 unsigned int it_suite;
443 xbt_test_suite_t suite;
444 xbt_test_unit_t unit;
445 unsigned int it_unit;
447 char suitename[STRLEN];
448 char unitname[STRLEN];
450 if (not selection || selection[0] == '\0')
453 /* First apply the selection */
457 char *p = strchr(sel, ',');
459 snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
462 snprintf(dir, STRLEN, "%s", sel);
468 memmove(dir, dir + 1, strlen(dir));
472 memmove(dir, dir + 1, strlen(dir));
475 p = strchr(dir, ':');
477 snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
478 snprintf(unitname, STRLEN, "%s", p + 1);
480 snprintf(suitename, STRLEN, "%s", dir);
484 /* Deal with the specific case of 'all' pseudo serie */
485 if (not strcmp("all", suitename)) {
486 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
488 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
489 xbt_dynar_foreach(suite->units, it_unit, unit) {
490 unit->enabled = enabling;
492 suite->enabled = enabling;
496 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
497 xbt_test_suite_t thissuite =
498 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
499 if (not strcmp(suitename, thissuite->name)) {
500 /* Do not disable the whole suite when we just want to disable a child */
501 if (enabling || (unitname[0] == '\0'))
502 thissuite->enabled = enabling;
504 if (unitname[0] == '\0') {
505 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
506 unit->enabled = enabling;
508 } else { /* act on one child only */
509 unsigned int it2_unit;
510 /* search it, first (we won't reuse it for external loop which gets broken) */
512 it2_unit < xbt_dynar_length(thissuite->units);
514 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
515 if (not strcmp(thisunit->name, unitname)) {
516 thisunit->enabled = enabling;
519 } /* search relevant unit */
520 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
521 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
522 } /* act on childs (either all or one) */
524 break; /* found the relevant serie. We are happy */
526 } /* search relevant series */
527 xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
528 "No suite of name '%s' found. Cannot apply the selection\n", suitename);
533 void xbt_test_dump(char *selection)
535 apply_selection(selection);
537 if (_xbt_test_suites) {
538 unsigned int it_suite;
539 xbt_test_suite_t suite;
541 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
542 xbt_test_suite_dump(suite);
544 printf(" No suite defined.");
548 int xbt_test_run(char *selection, int verbosity)
550 apply_selection(selection);
552 if (_xbt_test_suites) {
553 unsigned int it_suite;
554 xbt_test_suite_t suite;
557 /* Run all the suites */
558 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
559 xbt_test_suite_run(suite, verbosity);
561 /* Display some more statistics */
562 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
563 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
564 : 100.0, _xbt_test_nb_suites);
565 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
566 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
569 if (_xbt_test_suite_failed) {
570 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
574 if (_xbt_test_suite_ignore) {
575 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
577 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
578 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
580 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
581 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
584 if (_xbt_test_unit_failed) {
585 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
588 if (_xbt_test_unit_ignore) {
589 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
591 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
592 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
594 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
595 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
598 if (_xbt_test_test_failed) {
599 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
602 if (_xbt_test_test_ignore) {
603 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
606 if (_xbt_test_test_expect) {
607 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
610 fprintf(stderr, ")\n");
612 fprintf(stderr, "No unit to run!\n");
613 _xbt_test_unit_failed++;
615 return _xbt_test_unit_failed;
620 xbt_dynar_free(&_xbt_test_suites);
623 /* annotate test case with test */
624 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
626 xbt_test_unit_t unit = _xbt_test_current_unit;
630 xbt_test_test_t test = new s_xbt_test_test{};
632 test->title = bvprintf(fmt, ap);
635 test->expected_failure = 0;
639 xbt_dynar_push(unit->tests, &test);
642 /* annotate test case with log message and failure */
643 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
645 xbt_test_unit_t unit = _xbt_test_current_unit;
647 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
648 "Test failed even before being declared (broken unit: %s)", unit->title);
650 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
653 test->logs.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
659 void xbt_test_exception(xbt_ex_t e)
661 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
664 void xbt_test_expect_failure()
666 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
667 "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
668 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
669 test->expected_failure = 1;
674 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
675 "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
676 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
680 /* annotate test case with log message only */
681 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
683 xbt_test_unit_t unit = _xbt_test_current_unit;
685 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
686 "Test logged into even before being declared (broken test unit: %s)", unit->title);
688 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
691 test->logs.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
696 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
698 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
700 xbt_test_add("Skipped test");
703 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
704 xbt_test_expect_failure();
705 xbt_test_log("%s %s", "Test", "log");
706 xbt_test_fail("EXPECTED FAILURE");
708 #endif /* SIMGRID_TEST */