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>
23 /* collection of all suites */
24 static std::vector<xbt_test_suite_t> _xbt_test_suites;
25 /* global statistics */
26 static int _xbt_test_nb_tests = 0;
27 static int _xbt_test_test_failed = 0;
28 static int _xbt_test_test_ignore = 0;
29 static int _xbt_test_test_expect = 0;
31 static int _xbt_test_nb_units = 0;
32 static int _xbt_test_unit_failed = 0;
33 static int _xbt_test_unit_ignore = 0;
34 static int _xbt_test_unit_disabled = 0;
36 static int _xbt_test_nb_suites = 0;
37 static int _xbt_test_suite_failed = 0;
38 static int _xbt_test_suite_ignore = 0;
39 static int _xbt_test_suite_disabled = 0;
42 xbt_test_unit_t _xbt_test_current_unit = nullptr;
44 /* test suite test log */
45 class s_xbt_test_log {
47 s_xbt_test_log(std::string text, std::string file, int line)
48 : text_(std::move(text)), file_(std::move(file)), line_(line)
58 void s_xbt_test_log::dump() const
60 fprintf(stderr, " log %p(%s:%d)=%s\n", this, this->file_.c_str(), this->line_, this->text_.c_str());
63 /* test suite test check */
64 class s_xbt_test_test {
66 s_xbt_test_test(std::string title, std::string file, int line)
67 : title_(std::move(title)), file_(std::move(file)), line_(line)
74 bool expected_failure_ = false;
75 bool ignored_ = false;
78 std::vector<s_xbt_test_log> logs_;
81 void s_xbt_test_test::dump() const
83 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n", this, this->file_.c_str(), this->line_, this->title_.c_str(),
84 this->failed_ ? "failed" : "not failed");
85 for (s_xbt_test_log const& log : this->logs_)
89 /* test suite test unit */
90 class s_xbt_test_unit {
92 s_xbt_test_unit(std::string name, std::string title, ts_test_cb_t func)
93 : name_(std::move(name)), title_(std::move(title)), func_(func)
101 std::vector<s_xbt_test_test> tests_;
103 bool enabled_ = true;
105 int test_failed_ = 0;
106 int test_ignore_ = 0;
107 int test_expect_ = 0;
110 void s_xbt_test_unit::dump() const
112 fprintf(stderr, " UNIT %s: %s (%s)\n", this->name_.c_str(), this->title_.c_str(),
113 (this->enabled_ ? "enabled" : "disabled"));
114 if (this->enabled_) {
115 for (s_xbt_test_test const& test : this->tests_)
121 class s_xbt_test_suite {
123 s_xbt_test_suite(std::string name, std::string title) : name_(std::move(name)), title_(std::move(title)) {}
125 void push(s_xbt_test_unit unit) { units_.emplace_back(std::move(unit)); }
126 int run(int verbosity);
130 std::vector<s_xbt_test_unit> units_;
132 bool enabled_ = true;
135 int test_failed_ = 0;
136 int test_ignore_ = 0;
137 int test_expect_ = 0;
138 int unit_failed_ = 0;
139 int unit_ignore_ = 0;
140 int unit_disabled_ = 0;
143 /** @brief retrieve a testsuite from name, or create a new one */
144 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
146 auto res = std::find_if(begin(_xbt_test_suites), end(_xbt_test_suites),
147 [&name](xbt_test_suite_t const& suite) { return suite->name_ == name; });
148 if (res != end(_xbt_test_suites))
153 xbt_test_suite_t suite = new s_xbt_test_suite(name, simgrid::xbt::string_vprintf(fmt, ap));
156 _xbt_test_suites.push_back(suite);
161 void s_xbt_test_suite::dump() const
163 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", this->name_.c_str(), this->title_.c_str(),
164 this->enabled_ ? "enabled" : "disabled");
165 if (this->enabled_) {
166 for (s_xbt_test_unit const& unit : this->units_)
171 void xbt_test_suite_dump(xbt_test_suite_t suite)
176 /* add test case to test suite */
177 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
185 s_xbt_test_unit unit(name, simgrid::xbt::string_vprintf(fmt, ap), func);
190 /* run test one suite */
191 int s_xbt_test_suite::run(int verbosity)
193 /* suite title pretty-printing */
194 char suite_title[81];
195 int suite_len = this->title_.length();
197 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", this->title_.c_str(), suite_len);
199 suite_title[0] = ' ';
200 for (int i = 1; i < 79; i++)
201 suite_title[i] = '=';
202 suite_title[79] = '\n';
203 suite_title[80] = '\0';
205 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", this->title_.c_str());
206 suite_title[40 + (suite_len + 5) / 2] = '=';
207 if (not this->enabled_)
208 snprintf(suite_title + 70, 11, " DISABLED ");
209 fprintf(stderr, "\n%s\n", suite_title);
211 if (this->enabled_) {
212 /* iterate through all tests */
213 for (s_xbt_test_unit& unit : this->units_) {
214 /* init unit case counters */
216 unit.test_ignore_ = 0;
217 unit.test_failed_ = 0;
218 unit.test_expect_ = 0;
220 /* display unit title */
221 char* cp = bprintf(" Unit: %s ......................................"
222 "......................................",
223 unit.title_.c_str());
225 fprintf(stderr, "%s", cp);
228 /* run the test case function */
229 _xbt_test_current_unit = &unit;
233 /* iterate through all performed tests to determine status */
234 for (s_xbt_test_test const& test : unit.tests_) {
240 if ((test.failed_ && not test.expected_failure_) || (not test.failed_ && test.expected_failure_))
242 if (test.expected_failure_)
246 /* Display whether this unit went well */
247 if (unit.test_failed_ > 0 || unit.test_expect_ || (verbosity && unit.nb_tests_ > 0)) {
248 /* some tests failed (or were supposed to), so do detailed reporting of test case */
249 if (unit.test_failed_ > 0) {
250 fprintf(stderr, ".. failed\n");
251 } else if (unit.nb_tests_) {
252 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
254 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
256 for (s_xbt_test_test const& test : unit.tests_) {
257 std::string file = test.file_;
258 int line = test.line_;
262 else if (test.expected_failure_) {
273 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test.title_.c_str(), file.c_str(), line);
275 if ((test.expected_failure_ && not test.failed_) || (not test.expected_failure_ && test.failed_)) {
276 for (s_xbt_test_log const& log : test.logs_) {
277 file = (log.file_.empty() ? file : log.file_);
278 line = (log.line_ == 0 ? line : log.line_);
279 fprintf(stderr, " %s:%d: %s\n", file.c_str(), line, log.text_.c_str());
283 fprintf(stderr, " Summary: %d of %d tests failed", unit.test_failed_, unit.nb_tests_);
284 if (unit.test_ignore_) {
285 fprintf(stderr, " (%d tests ignored)\n", unit.test_ignore_);
287 fprintf(stderr, "\n");
289 } else if (not unit.enabled_) {
290 fprintf(stderr, " disabled\n"); /* no test were run */
291 } else if (unit.nb_tests_) {
292 fprintf(stderr, "...... ok\n"); /* successful */
294 fprintf(stderr, ".... skip\n"); /* no test were run */
297 /* Accumulate test counts into the suite */
298 this->nb_tests_ += unit.nb_tests_;
299 this->test_failed_ += unit.test_failed_;
300 this->test_ignore_ += unit.test_ignore_;
301 this->test_expect_ += unit.test_expect_;
303 _xbt_test_nb_tests += unit.nb_tests_;
304 _xbt_test_test_failed += unit.test_failed_;
305 _xbt_test_test_ignore += unit.test_ignore_;
306 _xbt_test_test_expect += unit.test_expect_;
308 /* What's the conclusion of this test anyway? */
309 if (unit.nb_tests_) {
311 if (unit.test_failed_)
312 this->unit_failed_++;
313 } else if (not unit.enabled_) {
314 this->unit_disabled_++;
316 this->unit_ignore_++;
320 _xbt_test_nb_units += this->nb_units_;
321 _xbt_test_unit_failed += this->unit_failed_;
322 _xbt_test_unit_ignore += this->unit_ignore_;
323 _xbt_test_unit_disabled += this->unit_disabled_;
325 if (this->nb_units_) {
326 _xbt_test_nb_suites++;
327 if (this->test_failed_)
328 _xbt_test_suite_failed++;
329 } else if (not this->enabled_) {
330 _xbt_test_suite_disabled++;
332 _xbt_test_suite_ignore++;
335 /* print test suite summary */
336 if (this->enabled_) {
337 int first = 1; /* for result pretty printing */
339 fprintf(stderr, " =====================================================================%s\n",
340 (this->nb_units_ ? (this->unit_failed_ ? "== FAILED" : "====== OK")
341 : (this->unit_disabled_ ? " DISABLED" : "==== SKIP")));
342 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ",
343 this->nb_units_ ? ((1 - (double)this->unit_failed_ / (double)this->nb_units_) * 100.0) : 100.0,
346 if (this->nb_units_ != this->unit_failed_) {
347 fprintf(stderr, "%s%d ok", (first ? "" : ", "), this->nb_units_ - this->unit_failed_);
350 if (this->unit_failed_) {
351 fprintf(stderr, "%s%d failed", (first ? "" : ", "), this->unit_failed_);
354 if (this->unit_ignore_) {
355 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), this->unit_ignore_);
358 if (this->unit_disabled_) {
359 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), this->unit_disabled_);
361 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
362 this->nb_tests_ ? ((1 - (double)this->test_failed_ / (double)this->nb_tests_) * 100.0) : 100.0,
366 if (this->nb_tests_ != this->test_failed_) {
367 fprintf(stderr, "%s%d ok", (first ? "" : ", "), this->nb_tests_ - this->test_failed_);
370 if (this->test_failed_) {
371 fprintf(stderr, "%s%d failed", (first ? "" : ", "), this->test_failed_);
374 if (this->test_ignore_) {
375 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), this->test_ignore_);
378 if (this->test_expect_) {
379 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), this->test_expect_);
381 fprintf(stderr, ")\n");
383 return this->unit_failed_;
386 static void apply_selection(char *selection)
388 /* for the parsing */
389 char *sel = selection;
391 char dir[STRLEN]; /* the directive */
393 char suitename[STRLEN];
394 char unitname[STRLEN];
396 if (not selection || selection[0] == '\0')
399 /* First apply the selection */
403 char *p = strchr(sel, ',');
405 snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
408 snprintf(dir, STRLEN, "%s", sel);
414 memmove(dir, dir + 1, strlen(dir));
418 memmove(dir, dir + 1, strlen(dir));
421 p = strchr(dir, ':');
423 snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
424 snprintf(unitname, STRLEN, "%s", p + 1);
426 snprintf(suitename, STRLEN, "%s", dir);
430 /* Deal with the specific case of 'all' pseudo serie */
431 if (not strcmp("all", suitename)) {
432 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
434 for (xbt_test_suite_t& suite : _xbt_test_suites) {
435 for (s_xbt_test_unit& unit : suite->units_) {
436 unit.enabled_ = enabling;
438 suite->enabled_ = enabling;
441 bool suitefound = false;
442 for (xbt_test_suite_t& thissuite : _xbt_test_suites) {
443 if (suitename == thissuite->name_) {
444 /* Do not disable the whole suite when we just want to disable a child */
445 if (enabling || (unitname[0] == '\0'))
446 thissuite->enabled_ = enabling;
448 if (unitname[0] == '\0') {
449 for (s_xbt_test_unit& unit : thissuite->units_) {
450 unit.enabled_ = enabling;
452 } else { /* act on one child only */
453 /* search relevant unit */
454 auto unit = std::find_if(begin(thissuite->units_), end(thissuite->units_),
455 [&unitname](s_xbt_test_unit const& unit) { return unit.name_ == unitname; });
456 if (unit == end(thissuite->units_))
457 xbt_die("Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
458 unit->enabled_ = enabling;
459 } /* act on childs (either all or one) */
461 break; /* found the relevant serie. We are happy */
463 } /* search relevant series */
464 xbt_assert(suitefound, "No suite of name '%s' found. Cannot apply the selection\n", suitename);
469 void xbt_test_dump(char *selection)
471 apply_selection(selection);
473 if (not _xbt_test_suites.empty()) {
474 for (xbt_test_suite_t suite : _xbt_test_suites)
477 printf(" No suite defined.");
481 int xbt_test_run(char *selection, int verbosity)
483 apply_selection(selection);
485 if (not _xbt_test_suites.empty()) {
488 /* Run all the suites */
489 for (xbt_test_suite_t& suite : _xbt_test_suites)
491 suite->run(verbosity);
493 /* Display some more statistics */
494 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
495 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
496 : 100.0, _xbt_test_nb_suites);
497 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
498 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
501 if (_xbt_test_suite_failed) {
502 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
506 if (_xbt_test_suite_ignore) {
507 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
509 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
510 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
512 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
513 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
516 if (_xbt_test_unit_failed) {
517 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
520 if (_xbt_test_unit_ignore) {
521 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
523 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
524 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
526 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
527 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
530 if (_xbt_test_test_failed) {
531 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
534 if (_xbt_test_test_ignore) {
535 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
538 if (_xbt_test_test_expect) {
539 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
542 fprintf(stderr, ")\n");
544 fprintf(stderr, "No unit to run!\n");
545 _xbt_test_unit_failed++;
547 return _xbt_test_unit_failed;
552 for (xbt_test_suite_t suite : _xbt_test_suites)
554 _xbt_test_suites.clear();
557 /* annotate test case with test */
558 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
560 xbt_test_unit_t unit = _xbt_test_current_unit;
565 unit->tests_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
569 /* annotate test case with log message and failure */
570 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
572 xbt_test_unit_t unit = _xbt_test_current_unit;
574 xbt_assert(not _xbt_test_current_unit->tests_.empty(), "Test failed even before being declared (broken unit: %s)",
575 unit->title_.c_str());
577 s_xbt_test_test& test = unit->tests_.back();
580 test.logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
586 void xbt_test_exception(xbt_ex_t e)
588 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
591 void xbt_test_expect_failure()
593 xbt_assert(not _xbt_test_current_unit->tests_.empty(),
594 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
595 _xbt_test_current_unit->title_.c_str());
596 _xbt_test_current_unit->tests_.back().expected_failure_ = true;
601 xbt_assert(not _xbt_test_current_unit->tests_.empty(), "Test skipped even before being declared (broken unit: %s)",
602 _xbt_test_current_unit->title_.c_str());
603 _xbt_test_current_unit->tests_.back().ignored_ = true;
606 /* annotate test case with log message only */
607 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
609 xbt_test_unit_t unit = _xbt_test_current_unit;
611 xbt_assert(not _xbt_test_current_unit->tests_.empty(),
612 "Test logged into even before being declared (broken test unit: %s)", unit->title_.c_str());
616 unit->tests_.back().logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
621 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
623 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
625 xbt_test_add("Skipped test");
628 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
629 xbt_test_expect_failure();
630 xbt_test_log("%s %s", "Test", "log");
631 xbt_test_fail("EXPECTED FAILURE");
633 #endif /* SIMGRID_TEST */