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"
15 #include "xbt/sysdep.h" /* bvprintf */
16 #include "xbt/cunit.h"
17 #include "xbt/dynar.h"
21 /* collection of all suites */
22 static xbt_dynar_t _xbt_test_suites = nullptr;
23 /* global statistics */
24 static int _xbt_test_nb_tests = 0;
25 static int _xbt_test_test_failed = 0;
26 static int _xbt_test_test_ignore = 0;
27 static int _xbt_test_test_expect = 0;
29 static int _xbt_test_nb_units = 0;
30 static int _xbt_test_unit_failed = 0;
31 static int _xbt_test_unit_ignore = 0;
32 static int _xbt_test_unit_disabled = 0;
34 static int _xbt_test_nb_suites = 0;
35 static int _xbt_test_suite_failed = 0;
36 static int _xbt_test_suite_ignore = 0;
37 static int _xbt_test_suite_disabled = 0;
40 xbt_test_unit_t _xbt_test_current_unit = nullptr;
42 /* test suite test log */
43 struct s_xbt_test_log {
48 typedef s_xbt_test_log* xbt_test_log_t;
50 static void xbt_test_log_dump(xbt_test_log_t log)
53 fprintf(stderr, " log %p(%s:%d)=%s\n", log, log->file, log->line, log->text);
55 fprintf(stderr, " log=nullptr\n");
58 /* test suite test check */
59 struct s_xbt_test_test {
68 typedef s_xbt_test_test* xbt_test_test_t;
70 static void xbt_test_test_dump(xbt_test_test_t test)
75 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n", test, test->file, test->line, test->title,
76 test->failed ? "failed" : "not failed");
77 xbt_dynar_foreach(test->logs, it_log, log)
78 xbt_test_log_dump(log);
80 fprintf(stderr, " test=nullptr\n");
83 /* test suite test unit */
84 struct s_xbt_test_unit {
91 xbt_dynar_t tests; /* of xbt_test_test_t */
99 static void xbt_test_unit_dump(xbt_test_unit_t unit)
102 fprintf(stderr, " UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
104 xbt_test_test_t test;
105 unsigned int it_test;
106 xbt_dynar_foreach(unit->tests, it_test, test)
107 xbt_test_test_dump(test);
110 fprintf(stderr, " unit=nullptr\n");
115 struct s_xbt_test_suite {
119 xbt_dynar_t units; /* of xbt_test_unit_t */
131 /* destroy test suite */
132 static void xbt_test_suite_free(void *s)
134 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
136 if (suite == nullptr)
138 xbt_dynar_free(&suite->units);
143 static void xbt_test_unit_free(void *unit)
145 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
148 xbt_dynar_free(&u->tests);
152 static void xbt_test_test_free(void *test)
154 xbt_test_test_t t = *(xbt_test_test_t *) test;
156 xbt_dynar_free(&(t->logs));
160 static void xbt_test_log_free(void *log)
162 xbt_test_log_t l = *(xbt_test_log_t *) log;
167 /** @brief retrieve a testsuite from name, or create a new one */
168 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
170 if (_xbt_test_suites == nullptr) {
171 _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
173 xbt_test_suite_t suite;
174 unsigned int it_suite;
175 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
176 if (not strcmp(suite->name, name))
180 xbt_test_suite_t suite = xbt_new0(s_xbt_test_suite, 1);
183 suite->title = bvprintf(fmt, ap);
184 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
189 xbt_dynar_push(_xbt_test_suites, &suite);
194 static void xbt_test_suite_dump(xbt_test_suite_t suite)
197 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
198 if (suite->enabled) {
199 xbt_test_unit_t unit;
200 unsigned int it_unit;
201 xbt_dynar_foreach(suite->units, it_unit, unit)
202 xbt_test_unit_dump(unit);
205 fprintf(stderr, "TESTSUITE IS NULL!\n");
209 /* add test case to test suite */
210 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
212 xbt_test_unit_t unit;
219 unit = xbt_new0(s_xbt_test_unit, 1);
221 unit->title = bvprintf(fmt, ap);
223 unit->name = (char *) name;
225 unit->file = nullptr;
228 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
230 xbt_dynar_push(suite->units, &unit);
233 /* run test one suite */
234 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
236 xbt_test_unit_t unit;
237 xbt_test_test_t test;
240 if (suite == nullptr)
243 /* suite title pretty-printing */
244 char suite_title[81];
245 int suite_len = strlen(suite->title);
247 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
249 suite_title[0] = ' ';
250 for (int i = 1; i < 79; i++)
251 suite_title[i] = '=';
252 suite_title[79] = '\n';
253 suite_title[80] = '\0';
255 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
256 suite_title[40 + (suite_len + 5) / 2] = '=';
257 if (not suite->enabled)
258 snprintf(suite_title + 70, 11, " DISABLED ");
259 fprintf(stderr, "\n%s\n", suite_title);
261 if (suite->enabled) {
262 /* iterate through all tests */
263 unsigned int it_unit;
264 xbt_dynar_foreach(suite->units, it_unit, unit) {
265 /* init unit case counters */
267 unit->test_ignore = 0;
268 unit->test_failed = 0;
269 unit->test_expect = 0;
271 /* display unit title */
272 char* cp = bprintf(" Unit: %s ......................................"
273 "......................................",
276 fprintf(stderr, "%s", cp);
279 /* run the test case function */
280 _xbt_test_current_unit = unit;
284 /* iterate through all performed tests to determine status */
285 unsigned int it_test;
286 xbt_dynar_foreach(unit->tests, it_test, test) {
292 if (test->failed && not test->expected_failure)
294 if (not test->failed && test->expected_failure)
296 if (test->expected_failure)
300 /* Display whether this unit went well */
301 if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
302 /* some tests failed (or were supposed to), so do detailed reporting of test case */
303 if (unit->test_failed > 0) {
304 fprintf(stderr, ".. failed\n");
305 } else if (unit->nb_tests) {
306 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
308 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
310 xbt_dynar_foreach(unit->tests, it_test, test) {
311 const char* file = (test->file != nullptr ? test->file : unit->file);
312 int line = (test->line != 0 ? test->line : unit->line);
316 else if (test->expected_failure) {
327 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test->title, file, line);
329 if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
331 xbt_dynar_foreach(test->logs, it_log, log) {
332 file = (log->file != nullptr ? log->file : file);
333 line = (log->line != 0 ? log->line : line);
334 fprintf(stderr, " %s:%d: %s\n", file, line, log->text);
338 fprintf(stderr, " Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
339 if (unit->test_ignore) {
340 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
342 fprintf(stderr, "\n");
344 } else if (not unit->enabled) {
345 fprintf(stderr, " disabled\n"); /* no test were run */
346 } else if (unit->nb_tests) {
347 fprintf(stderr, "...... ok\n"); /* successful */
349 fprintf(stderr, ".... skip\n"); /* no test were run */
352 /* Accumulate test counts into the suite */
353 suite->nb_tests += unit->nb_tests;
354 suite->test_failed += unit->test_failed;
355 suite->test_ignore += unit->test_ignore;
356 suite->test_expect += unit->test_expect;
358 _xbt_test_nb_tests += unit->nb_tests;
359 _xbt_test_test_failed += unit->test_failed;
360 _xbt_test_test_ignore += unit->test_ignore;
361 _xbt_test_test_expect += unit->test_expect;
363 /* What's the conclusion of this test anyway? */
364 if (unit->nb_tests) {
366 if (unit->test_failed)
367 suite->unit_failed++;
368 } else if (not unit->enabled) {
369 suite->unit_disabled++;
371 suite->unit_ignore++;
375 _xbt_test_nb_units += suite->nb_units;
376 _xbt_test_unit_failed += suite->unit_failed;
377 _xbt_test_unit_ignore += suite->unit_ignore;
378 _xbt_test_unit_disabled += suite->unit_disabled;
380 if (suite->nb_units) {
381 _xbt_test_nb_suites++;
382 if (suite->test_failed)
383 _xbt_test_suite_failed++;
384 } else if (not suite->enabled) {
385 _xbt_test_suite_disabled++;
387 _xbt_test_suite_ignore++;
390 /* print test suite summary */
391 if (suite->enabled) {
392 int first = 1; /* for result pretty printing */
394 fprintf(stderr," =====================================================================%s\n",
395 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
396 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
397 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
398 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
400 if (suite->nb_units != suite->unit_failed) {
401 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
404 if (suite->unit_failed) {
405 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
408 if (suite->unit_ignore) {
409 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
412 if (suite->unit_disabled) {
413 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
415 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
416 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
419 if (suite->nb_tests != suite->test_failed) {
420 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
423 if (suite->test_failed) {
424 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
427 if (suite->test_ignore) {
428 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
431 if (suite->test_expect) {
432 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
434 fprintf(stderr, ")\n");
436 return suite->unit_failed;
439 static void apply_selection(char *selection)
441 /* for the parsing */
442 char *sel = selection;
444 char dir[STRLEN]; /* the directive */
446 unsigned int it_suite;
447 xbt_test_suite_t suite;
448 xbt_test_unit_t unit;
449 unsigned int it_unit;
451 char suitename[STRLEN];
452 char unitname[STRLEN];
454 if (not selection || selection[0] == '\0')
457 /* First apply the selection */
461 char *p = strchr(sel, ',');
463 snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
466 snprintf(dir, STRLEN, "%s", sel);
472 memmove(dir, dir + 1, strlen(dir));
476 memmove(dir, dir + 1, strlen(dir));
479 p = strchr(dir, ':');
481 snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
482 snprintf(unitname, STRLEN, "%s", p + 1);
484 snprintf(suitename, STRLEN, "%s", dir);
488 /* Deal with the specific case of 'all' pseudo serie */
489 if (not strcmp("all", suitename)) {
490 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
492 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
493 xbt_dynar_foreach(suite->units, it_unit, unit) {
494 unit->enabled = enabling;
496 suite->enabled = enabling;
500 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
501 xbt_test_suite_t thissuite =
502 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
503 if (not strcmp(suitename, thissuite->name)) {
504 /* Do not disable the whole suite when we just want to disable a child */
505 if (enabling || (unitname[0] == '\0'))
506 thissuite->enabled = enabling;
508 if (unitname[0] == '\0') {
509 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
510 unit->enabled = enabling;
512 } else { /* act on one child only */
513 unsigned int it2_unit;
514 /* search it, first (we won't reuse it for external loop which gets broken) */
516 it2_unit < xbt_dynar_length(thissuite->units);
518 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
519 if (not strcmp(thisunit->name, unitname)) {
520 thisunit->enabled = enabling;
523 } /* search relevant unit */
524 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
525 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
526 } /* act on childs (either all or one) */
528 break; /* found the relevant serie. We are happy */
530 } /* search relevant series */
531 xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
532 "No suite of name '%s' found. Cannot apply the selection\n", suitename);
537 void xbt_test_dump(char *selection)
539 apply_selection(selection);
541 if (_xbt_test_suites) {
542 unsigned int it_suite;
543 xbt_test_suite_t suite;
545 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
546 xbt_test_suite_dump(suite);
548 printf(" No suite defined.");
552 int xbt_test_run(char *selection, int verbosity)
554 apply_selection(selection);
556 if (_xbt_test_suites) {
557 unsigned int it_suite;
558 xbt_test_suite_t suite;
561 /* Run all the suites */
562 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
563 xbt_test_suite_run(suite, verbosity);
565 /* Display some more statistics */
566 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
567 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
568 : 100.0, _xbt_test_nb_suites);
569 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
570 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
573 if (_xbt_test_suite_failed) {
574 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
578 if (_xbt_test_suite_ignore) {
579 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
581 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
582 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
584 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
585 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
588 if (_xbt_test_unit_failed) {
589 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
592 if (_xbt_test_unit_ignore) {
593 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
595 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
596 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
598 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
599 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
602 if (_xbt_test_test_failed) {
603 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
606 if (_xbt_test_test_ignore) {
607 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
610 if (_xbt_test_test_expect) {
611 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
614 fprintf(stderr, ")\n");
616 fprintf(stderr, "No unit to run!\n");
617 _xbt_test_unit_failed++;
619 return _xbt_test_unit_failed;
624 xbt_dynar_free(&_xbt_test_suites);
627 /* annotate test case with test */
628 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
630 xbt_test_unit_t unit = _xbt_test_current_unit;
634 xbt_test_test_t test = xbt_new0(s_xbt_test_test, 1);
636 test->title = bvprintf(fmt, ap);
639 test->expected_failure = 0;
643 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
644 xbt_dynar_push(unit->tests, &test);
647 /* annotate test case with log message and failure */
648 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
650 xbt_test_unit_t unit = _xbt_test_current_unit;
652 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
653 "Test failed even before being declared (broken unit: %s)", unit->title);
656 xbt_test_log_t log = xbt_new(s_xbt_test_log, 1);
658 log->text = bvprintf(fmt, ap);
663 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
664 xbt_dynar_push(test->logs, &log);
669 void xbt_test_exception(xbt_ex_t e)
671 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
674 void xbt_test_expect_failure()
676 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
677 "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
678 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
679 test->expected_failure = 1;
684 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
685 "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
686 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
690 /* annotate test case with log message only */
691 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
693 xbt_test_unit_t unit = _xbt_test_current_unit;
695 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
696 "Test logged into even before being declared (broken test unit: %s)", unit->title);
699 xbt_test_log_t log = xbt_new(s_xbt_test_log, 1);
701 log->text = bvprintf(fmt, ap);
706 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
707 xbt_dynar_push(test->logs, &log);
711 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
713 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
715 xbt_test_add("Skipped test");
718 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
719 xbt_test_expect_failure();
720 xbt_test_log("%s %s", "Test", "log");
721 xbt_test_fail("EXPECTED FAILURE");
723 #endif /* SIMGRID_TEST */