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 class s_xbt_test_unit {
95 s_xbt_test_unit(std::string name, std::string title, ts_test_cb_t func)
96 : name_(std::move(name)), title_(std::move(title)), func_(func)
104 std::vector<s_xbt_test_test> tests_;
106 bool enabled_ = true;
108 int test_failed_ = 0;
109 int test_ignore_ = 0;
110 int test_expect_ = 0;
113 void s_xbt_test_unit::dump() const
115 fprintf(stderr, " UNIT %s: %s (%s)\n", this->name_.c_str(), this->title_.c_str(),
116 (this->enabled_ ? "enabled" : "disabled"));
117 if (this->enabled_) {
118 for (s_xbt_test_test const& test : this->tests_)
124 struct s_xbt_test_suite {
128 std::vector<s_xbt_test_unit> units;
140 /* destroy test suite */
141 static void xbt_test_suite_free(void *s)
143 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
145 if (suite == nullptr)
147 xbt_free(suite->title);
151 /** @brief retrieve a testsuite from name, or create a new one */
152 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
154 if (_xbt_test_suites == nullptr) {
155 _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
157 xbt_test_suite_t suite;
158 unsigned int it_suite;
159 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
160 if (not strcmp(suite->name, name))
164 xbt_test_suite_t suite = new s_xbt_test_suite{};
167 suite->title = bvprintf(fmt, ap);
172 xbt_dynar_push(_xbt_test_suites, &suite);
177 static void xbt_test_suite_dump(xbt_test_suite_t suite)
180 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
181 if (suite->enabled) {
182 for (s_xbt_test_unit const& unit : suite->units)
186 fprintf(stderr, "TESTSUITE IS NULL!\n");
190 /* add test case to test suite */
191 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
199 suite->units.emplace_back(name, simgrid::xbt::string_vprintf(fmt, ap), func);
203 /* run test one suite */
204 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
206 if (suite == nullptr)
209 /* suite title pretty-printing */
210 char suite_title[81];
211 int suite_len = strlen(suite->title);
213 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
215 suite_title[0] = ' ';
216 for (int i = 1; i < 79; i++)
217 suite_title[i] = '=';
218 suite_title[79] = '\n';
219 suite_title[80] = '\0';
221 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
222 suite_title[40 + (suite_len + 5) / 2] = '=';
223 if (not suite->enabled)
224 snprintf(suite_title + 70, 11, " DISABLED ");
225 fprintf(stderr, "\n%s\n", suite_title);
227 if (suite->enabled) {
228 /* iterate through all tests */
229 for (s_xbt_test_unit& unit : suite->units) {
230 /* init unit case counters */
232 unit.test_ignore_ = 0;
233 unit.test_failed_ = 0;
234 unit.test_expect_ = 0;
236 /* display unit title */
237 char* cp = bprintf(" Unit: %s ......................................"
238 "......................................",
239 unit.title_.c_str());
241 fprintf(stderr, "%s", cp);
244 /* run the test case function */
245 _xbt_test_current_unit = &unit;
249 /* iterate through all performed tests to determine status */
250 for (s_xbt_test_test const& test : unit.tests_) {
256 if ((test.failed_ && not test.expected_failure_) || (not test.failed_ && test.expected_failure_))
258 if (test.expected_failure_)
262 /* Display whether this unit went well */
263 if (unit.test_failed_ > 0 || unit.test_expect_ || (verbosity && unit.nb_tests_ > 0)) {
264 /* some tests failed (or were supposed to), so do detailed reporting of test case */
265 if (unit.test_failed_ > 0) {
266 fprintf(stderr, ".. failed\n");
267 } else if (unit.nb_tests_) {
268 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
270 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
272 for (s_xbt_test_test const& test : unit.tests_) {
273 std::string file = test.file_;
274 int line = test.line_;
278 else if (test.expected_failure_) {
289 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test.title_.c_str(), file.c_str(), line);
291 if ((test.expected_failure_ && not test.failed_) || (not test.expected_failure_ && test.failed_)) {
292 for (s_xbt_test_log const& log : test.logs_) {
293 file = (log.file_.empty() ? file : log.file_);
294 line = (log.line_ == 0 ? line : log.line_);
295 fprintf(stderr, " %s:%d: %s\n", file.c_str(), line, log.text_.c_str());
299 fprintf(stderr, " Summary: %d of %d tests failed", unit.test_failed_, unit.nb_tests_);
300 if (unit.test_ignore_) {
301 fprintf(stderr, " (%d tests ignored)\n", unit.test_ignore_);
303 fprintf(stderr, "\n");
305 } else if (not unit.enabled_) {
306 fprintf(stderr, " disabled\n"); /* no test were run */
307 } else if (unit.nb_tests_) {
308 fprintf(stderr, "...... ok\n"); /* successful */
310 fprintf(stderr, ".... skip\n"); /* no test were run */
313 /* Accumulate test counts into the suite */
314 suite->nb_tests += unit.nb_tests_;
315 suite->test_failed += unit.test_failed_;
316 suite->test_ignore += unit.test_ignore_;
317 suite->test_expect += unit.test_expect_;
319 _xbt_test_nb_tests += unit.nb_tests_;
320 _xbt_test_test_failed += unit.test_failed_;
321 _xbt_test_test_ignore += unit.test_ignore_;
322 _xbt_test_test_expect += unit.test_expect_;
324 /* What's the conclusion of this test anyway? */
325 if (unit.nb_tests_) {
327 if (unit.test_failed_)
328 suite->unit_failed++;
329 } else if (not unit.enabled_) {
330 suite->unit_disabled++;
332 suite->unit_ignore++;
336 _xbt_test_nb_units += suite->nb_units;
337 _xbt_test_unit_failed += suite->unit_failed;
338 _xbt_test_unit_ignore += suite->unit_ignore;
339 _xbt_test_unit_disabled += suite->unit_disabled;
341 if (suite->nb_units) {
342 _xbt_test_nb_suites++;
343 if (suite->test_failed)
344 _xbt_test_suite_failed++;
345 } else if (not suite->enabled) {
346 _xbt_test_suite_disabled++;
348 _xbt_test_suite_ignore++;
351 /* print test suite summary */
352 if (suite->enabled) {
353 int first = 1; /* for result pretty printing */
355 fprintf(stderr," =====================================================================%s\n",
356 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
357 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
358 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
359 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
361 if (suite->nb_units != suite->unit_failed) {
362 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
365 if (suite->unit_failed) {
366 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
369 if (suite->unit_ignore) {
370 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
373 if (suite->unit_disabled) {
374 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
376 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
377 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
380 if (suite->nb_tests != suite->test_failed) {
381 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
384 if (suite->test_failed) {
385 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
388 if (suite->test_ignore) {
389 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
392 if (suite->test_expect) {
393 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
395 fprintf(stderr, ")\n");
397 return suite->unit_failed;
400 static void apply_selection(char *selection)
402 /* for the parsing */
403 char *sel = selection;
405 char dir[STRLEN]; /* the directive */
407 unsigned int it_suite;
408 xbt_test_suite_t suite;
410 char suitename[STRLEN];
411 char unitname[STRLEN];
413 if (not selection || selection[0] == '\0')
416 /* First apply the selection */
420 char *p = strchr(sel, ',');
422 snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
425 snprintf(dir, STRLEN, "%s", sel);
431 memmove(dir, dir + 1, strlen(dir));
435 memmove(dir, dir + 1, strlen(dir));
438 p = strchr(dir, ':');
440 snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
441 snprintf(unitname, STRLEN, "%s", p + 1);
443 snprintf(suitename, STRLEN, "%s", dir);
447 /* Deal with the specific case of 'all' pseudo serie */
448 if (not strcmp("all", suitename)) {
449 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
451 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
452 for (s_xbt_test_unit& unit : suite->units) {
453 unit.enabled_ = enabling;
455 suite->enabled = enabling;
459 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
460 xbt_test_suite_t thissuite =
461 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
462 if (not strcmp(suitename, thissuite->name)) {
463 /* Do not disable the whole suite when we just want to disable a child */
464 if (enabling || (unitname[0] == '\0'))
465 thissuite->enabled = enabling;
467 if (unitname[0] == '\0') {
468 for (s_xbt_test_unit& unit : thissuite->units) {
469 unit.enabled_ = enabling;
471 } else { /* act on one child only */
472 /* search relevant unit */
473 auto unit = std::find_if(begin(thissuite->units), end(thissuite->units),
474 [&unitname](s_xbt_test_unit const& unit) { return unit.name_ == unitname; });
475 if (unit == end(thissuite->units))
476 xbt_die("Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
477 unit->enabled_ = enabling;
478 } /* act on childs (either all or one) */
480 break; /* found the relevant serie. We are happy */
482 } /* search relevant series */
483 xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
484 "No suite of name '%s' found. Cannot apply the selection\n", suitename);
489 void xbt_test_dump(char *selection)
491 apply_selection(selection);
493 if (_xbt_test_suites) {
494 unsigned int it_suite;
495 xbt_test_suite_t suite;
497 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
498 xbt_test_suite_dump(suite);
500 printf(" No suite defined.");
504 int xbt_test_run(char *selection, int verbosity)
506 apply_selection(selection);
508 if (_xbt_test_suites) {
509 unsigned int it_suite;
510 xbt_test_suite_t suite;
513 /* Run all the suites */
514 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
515 xbt_test_suite_run(suite, verbosity);
517 /* Display some more statistics */
518 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
519 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
520 : 100.0, _xbt_test_nb_suites);
521 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
522 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
525 if (_xbt_test_suite_failed) {
526 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
530 if (_xbt_test_suite_ignore) {
531 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
533 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
534 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
536 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
537 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
540 if (_xbt_test_unit_failed) {
541 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
544 if (_xbt_test_unit_ignore) {
545 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
547 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
548 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
550 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
551 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
554 if (_xbt_test_test_failed) {
555 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
558 if (_xbt_test_test_ignore) {
559 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
562 if (_xbt_test_test_expect) {
563 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
566 fprintf(stderr, ")\n");
568 fprintf(stderr, "No unit to run!\n");
569 _xbt_test_unit_failed++;
571 return _xbt_test_unit_failed;
576 xbt_dynar_free(&_xbt_test_suites);
579 /* annotate test case with test */
580 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
582 xbt_test_unit_t unit = _xbt_test_current_unit;
587 unit->tests_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
591 /* annotate test case with log message and failure */
592 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
594 xbt_test_unit_t unit = _xbt_test_current_unit;
596 xbt_assert(not _xbt_test_current_unit->tests_.empty(), "Test failed even before being declared (broken unit: %s)",
597 unit->title_.c_str());
599 s_xbt_test_test& test = unit->tests_.back();
602 test.logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
608 void xbt_test_exception(xbt_ex_t e)
610 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
613 void xbt_test_expect_failure()
615 xbt_assert(not _xbt_test_current_unit->tests_.empty(),
616 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
617 _xbt_test_current_unit->title_.c_str());
618 _xbt_test_current_unit->tests_.back().expected_failure_ = true;
623 xbt_assert(not _xbt_test_current_unit->tests_.empty(), "Test skipped even before being declared (broken unit: %s)",
624 _xbt_test_current_unit->title_.c_str());
625 _xbt_test_current_unit->tests_.back().ignored_ = true;
628 /* annotate test case with log message only */
629 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
631 xbt_test_unit_t unit = _xbt_test_current_unit;
633 xbt_assert(not _xbt_test_current_unit->tests_.empty(),
634 "Test logged into even before being declared (broken test unit: %s)", unit->title_.c_str());
638 unit->tests_.back().logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
643 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
645 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
647 xbt_test_add("Skipped test");
650 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
651 xbt_test_expect_failure();
652 xbt_test_log("%s %s", "Test", "log");
653 xbt_test_fail("EXPECTED FAILURE");
655 #endif /* SIMGRID_TEST */