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 class s_xbt_test_test {
69 s_xbt_test_test(std::string title, std::string file, int line)
70 : title_(std::move(title)), file_(std::move(file)), line_(line)
77 bool expected_failure_ = false;
78 bool ignored_ = false;
81 std::vector<s_xbt_test_log> logs_;
84 void s_xbt_test_test::dump() const
86 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n", this, this->file_.c_str(), this->line_, this->title_.c_str(),
87 this->failed_ ? "failed" : "not failed");
88 for (s_xbt_test_log const& log : this->logs_)
92 /* test suite test unit */
93 struct s_xbt_test_unit {
100 std::vector<s_xbt_test_test> tests;
108 static void xbt_test_unit_dump(xbt_test_unit_t unit)
111 fprintf(stderr, " UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
113 for (s_xbt_test_test const& test : unit->tests)
117 fprintf(stderr, " unit=nullptr\n");
122 struct s_xbt_test_suite {
126 xbt_dynar_t units; /* of xbt_test_unit_t */
138 /* destroy test suite */
139 static void xbt_test_suite_free(void *s)
141 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
143 if (suite == nullptr)
145 xbt_dynar_free(&suite->units);
150 static void xbt_test_unit_free(void *unit)
152 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
158 /** @brief retrieve a testsuite from name, or create a new one */
159 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
161 if (_xbt_test_suites == nullptr) {
162 _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
164 xbt_test_suite_t suite;
165 unsigned int it_suite;
166 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
167 if (not strcmp(suite->name, name))
171 xbt_test_suite_t suite = xbt_new0(s_xbt_test_suite, 1);
174 suite->title = bvprintf(fmt, ap);
175 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
180 xbt_dynar_push(_xbt_test_suites, &suite);
185 static void xbt_test_suite_dump(xbt_test_suite_t suite)
188 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
189 if (suite->enabled) {
190 xbt_test_unit_t unit;
191 unsigned int it_unit;
192 xbt_dynar_foreach(suite->units, it_unit, unit)
193 xbt_test_unit_dump(unit);
196 fprintf(stderr, "TESTSUITE IS NULL!\n");
200 /* add test case to test suite */
201 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
203 xbt_test_unit_t unit;
210 unit = xbt_new0(s_xbt_test_unit, 1);
212 unit->title = bvprintf(fmt, ap);
214 unit->name = (char *) name;
216 unit->file = nullptr;
220 xbt_dynar_push(suite->units, &unit);
223 /* run test one suite */
224 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
226 xbt_test_unit_t unit;
228 if (suite == nullptr)
231 /* suite title pretty-printing */
232 char suite_title[81];
233 int suite_len = strlen(suite->title);
235 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
237 suite_title[0] = ' ';
238 for (int i = 1; i < 79; i++)
239 suite_title[i] = '=';
240 suite_title[79] = '\n';
241 suite_title[80] = '\0';
243 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
244 suite_title[40 + (suite_len + 5) / 2] = '=';
245 if (not suite->enabled)
246 snprintf(suite_title + 70, 11, " DISABLED ");
247 fprintf(stderr, "\n%s\n", suite_title);
249 if (suite->enabled) {
250 /* iterate through all tests */
251 unsigned int it_unit;
252 xbt_dynar_foreach(suite->units, it_unit, unit) {
253 /* init unit case counters */
255 unit->test_ignore = 0;
256 unit->test_failed = 0;
257 unit->test_expect = 0;
259 /* display unit title */
260 char* cp = bprintf(" Unit: %s ......................................"
261 "......................................",
264 fprintf(stderr, "%s", cp);
267 /* run the test case function */
268 _xbt_test_current_unit = unit;
272 /* iterate through all performed tests to determine status */
273 for (s_xbt_test_test const& test : unit->tests) {
279 if ((test.failed_ && not test.expected_failure_) || (not test.failed_ && test.expected_failure_))
281 if (test.expected_failure_)
285 /* Display whether this unit went well */
286 if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
287 /* some tests failed (or were supposed to), so do detailed reporting of test case */
288 if (unit->test_failed > 0) {
289 fprintf(stderr, ".. failed\n");
290 } else if (unit->nb_tests) {
291 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
293 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
295 for (s_xbt_test_test const& test : unit->tests) {
296 std::string file = (test.file_.empty() ? unit->file : test.file_);
297 int line = (test.line_ == 0 ? unit->line : test.line_);
301 else if (test.expected_failure_) {
312 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test.title_.c_str(), file.c_str(), line);
314 if ((test.expected_failure_ && not test.failed_) || (not test.expected_failure_ && test.failed_)) {
315 for (s_xbt_test_log const& log : test.logs_) {
316 file = (log.file_.empty() ? file : log.file_);
317 line = (log.line_ == 0 ? line : log.line_);
318 fprintf(stderr, " %s:%d: %s\n", file.c_str(), line, log.text_.c_str());
322 fprintf(stderr, " Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
323 if (unit->test_ignore) {
324 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
326 fprintf(stderr, "\n");
328 } else if (not unit->enabled) {
329 fprintf(stderr, " disabled\n"); /* no test were run */
330 } else if (unit->nb_tests) {
331 fprintf(stderr, "...... ok\n"); /* successful */
333 fprintf(stderr, ".... skip\n"); /* no test were run */
336 /* Accumulate test counts into the suite */
337 suite->nb_tests += unit->nb_tests;
338 suite->test_failed += unit->test_failed;
339 suite->test_ignore += unit->test_ignore;
340 suite->test_expect += unit->test_expect;
342 _xbt_test_nb_tests += unit->nb_tests;
343 _xbt_test_test_failed += unit->test_failed;
344 _xbt_test_test_ignore += unit->test_ignore;
345 _xbt_test_test_expect += unit->test_expect;
347 /* What's the conclusion of this test anyway? */
348 if (unit->nb_tests) {
350 if (unit->test_failed)
351 suite->unit_failed++;
352 } else if (not unit->enabled) {
353 suite->unit_disabled++;
355 suite->unit_ignore++;
359 _xbt_test_nb_units += suite->nb_units;
360 _xbt_test_unit_failed += suite->unit_failed;
361 _xbt_test_unit_ignore += suite->unit_ignore;
362 _xbt_test_unit_disabled += suite->unit_disabled;
364 if (suite->nb_units) {
365 _xbt_test_nb_suites++;
366 if (suite->test_failed)
367 _xbt_test_suite_failed++;
368 } else if (not suite->enabled) {
369 _xbt_test_suite_disabled++;
371 _xbt_test_suite_ignore++;
374 /* print test suite summary */
375 if (suite->enabled) {
376 int first = 1; /* for result pretty printing */
378 fprintf(stderr," =====================================================================%s\n",
379 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
380 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
381 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
382 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
384 if (suite->nb_units != suite->unit_failed) {
385 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
388 if (suite->unit_failed) {
389 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
392 if (suite->unit_ignore) {
393 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
396 if (suite->unit_disabled) {
397 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
399 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
400 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
403 if (suite->nb_tests != suite->test_failed) {
404 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
407 if (suite->test_failed) {
408 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
411 if (suite->test_ignore) {
412 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
415 if (suite->test_expect) {
416 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
418 fprintf(stderr, ")\n");
420 return suite->unit_failed;
423 static void apply_selection(char *selection)
425 /* for the parsing */
426 char *sel = selection;
428 char dir[STRLEN]; /* the directive */
430 unsigned int it_suite;
431 xbt_test_suite_t suite;
432 xbt_test_unit_t unit;
433 unsigned int it_unit;
435 char suitename[STRLEN];
436 char unitname[STRLEN];
438 if (not selection || selection[0] == '\0')
441 /* First apply the selection */
445 char *p = strchr(sel, ',');
447 snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
450 snprintf(dir, STRLEN, "%s", sel);
456 memmove(dir, dir + 1, strlen(dir));
460 memmove(dir, dir + 1, strlen(dir));
463 p = strchr(dir, ':');
465 snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
466 snprintf(unitname, STRLEN, "%s", p + 1);
468 snprintf(suitename, STRLEN, "%s", dir);
472 /* Deal with the specific case of 'all' pseudo serie */
473 if (not strcmp("all", suitename)) {
474 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
476 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
477 xbt_dynar_foreach(suite->units, it_unit, unit) {
478 unit->enabled = enabling;
480 suite->enabled = enabling;
484 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
485 xbt_test_suite_t thissuite =
486 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
487 if (not strcmp(suitename, thissuite->name)) {
488 /* Do not disable the whole suite when we just want to disable a child */
489 if (enabling || (unitname[0] == '\0'))
490 thissuite->enabled = enabling;
492 if (unitname[0] == '\0') {
493 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
494 unit->enabled = enabling;
496 } else { /* act on one child only */
497 unsigned int it2_unit;
498 /* search it, first (we won't reuse it for external loop which gets broken) */
500 it2_unit < xbt_dynar_length(thissuite->units);
502 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
503 if (not strcmp(thisunit->name, unitname)) {
504 thisunit->enabled = enabling;
507 } /* search relevant unit */
508 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
509 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
510 } /* act on childs (either all or one) */
512 break; /* found the relevant serie. We are happy */
514 } /* search relevant series */
515 xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
516 "No suite of name '%s' found. Cannot apply the selection\n", suitename);
521 void xbt_test_dump(char *selection)
523 apply_selection(selection);
525 if (_xbt_test_suites) {
526 unsigned int it_suite;
527 xbt_test_suite_t suite;
529 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
530 xbt_test_suite_dump(suite);
532 printf(" No suite defined.");
536 int xbt_test_run(char *selection, int verbosity)
538 apply_selection(selection);
540 if (_xbt_test_suites) {
541 unsigned int it_suite;
542 xbt_test_suite_t suite;
545 /* Run all the suites */
546 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
547 xbt_test_suite_run(suite, verbosity);
549 /* Display some more statistics */
550 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
551 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
552 : 100.0, _xbt_test_nb_suites);
553 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
554 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
557 if (_xbt_test_suite_failed) {
558 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
562 if (_xbt_test_suite_ignore) {
563 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
565 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
566 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
568 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
569 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
572 if (_xbt_test_unit_failed) {
573 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
576 if (_xbt_test_unit_ignore) {
577 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
579 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
580 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
582 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
583 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
586 if (_xbt_test_test_failed) {
587 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
590 if (_xbt_test_test_ignore) {
591 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
594 if (_xbt_test_test_expect) {
595 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
598 fprintf(stderr, ")\n");
600 fprintf(stderr, "No unit to run!\n");
601 _xbt_test_unit_failed++;
603 return _xbt_test_unit_failed;
608 xbt_dynar_free(&_xbt_test_suites);
611 /* annotate test case with test */
612 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
614 xbt_test_unit_t unit = _xbt_test_current_unit;
619 unit->tests.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
623 /* annotate test case with log message and failure */
624 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
626 xbt_test_unit_t unit = _xbt_test_current_unit;
628 xbt_assert(not _xbt_test_current_unit->tests.empty(), "Test failed even before being declared (broken unit: %s)",
631 s_xbt_test_test& test = unit->tests.back();
634 test.logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
640 void xbt_test_exception(xbt_ex_t e)
642 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
645 void xbt_test_expect_failure()
647 xbt_assert(not _xbt_test_current_unit->tests.empty(),
648 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
649 _xbt_test_current_unit->title);
650 _xbt_test_current_unit->tests.back().expected_failure_ = true;
655 xbt_assert(not _xbt_test_current_unit->tests.empty(), "Test skipped even before being declared (broken unit: %s)",
656 _xbt_test_current_unit->title);
657 _xbt_test_current_unit->tests.back().ignored_ = true;
660 /* annotate test case with log message only */
661 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
663 xbt_test_unit_t unit = _xbt_test_current_unit;
665 xbt_assert(not _xbt_test_current_unit->tests.empty(),
666 "Test logged into even before being declared (broken test unit: %s)", unit->title);
670 unit->tests.back().logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
675 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
677 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
679 xbt_test_add("Skipped test");
682 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
683 xbt_test_expect_failure();
684 xbt_test_log("%s %s", "Test", "log");
685 xbt_test_fail("EXPECTED FAILURE");
687 #endif /* SIMGRID_TEST */