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 create test suite */
168 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
170 xbt_test_suite_t suite = xbt_new0(s_xbt_test_suite, 1);
173 if (_xbt_test_suites == nullptr)
174 _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
177 suite->title = bvprintf(fmt, ap);
178 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
183 xbt_dynar_push(_xbt_test_suites, &suite);
188 /** @brief retrieve a testsuite from name, or create a new one */
189 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
191 xbt_test_suite_t suite;
195 if (_xbt_test_suites) {
196 unsigned int it_suite;
197 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
198 if (not strcmp(suite->name, name))
203 bufname = bvprintf(fmt, ap);
205 suite = xbt_test_suite_new(name, bufname, nullptr);
211 void xbt_test_suite_dump(xbt_test_suite_t suite)
214 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
215 if (suite->enabled) {
216 xbt_test_unit_t unit;
217 unsigned int it_unit;
218 xbt_dynar_foreach(suite->units, it_unit, unit)
219 xbt_test_unit_dump(unit);
222 fprintf(stderr, "TESTSUITE IS NULL!\n");
226 /* add test case to test suite */
227 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
229 xbt_test_unit_t unit;
236 unit = xbt_new0(s_xbt_test_unit, 1);
238 unit->title = bvprintf(fmt, ap);
240 unit->name = (char *) name;
242 unit->file = nullptr;
245 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
247 xbt_dynar_push(suite->units, &unit);
250 /* run test one suite */
251 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
253 xbt_test_unit_t unit;
254 xbt_test_test_t test;
257 if (suite == nullptr)
260 /* suite title pretty-printing */
261 char suite_title[81];
262 int suite_len = strlen(suite->title);
264 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
266 suite_title[0] = ' ';
267 for (int i = 1; i < 79; i++)
268 suite_title[i] = '=';
269 suite_title[79] = '\n';
270 suite_title[80] = '\0';
272 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
273 suite_title[40 + (suite_len + 5) / 2] = '=';
274 if (not suite->enabled)
275 snprintf(suite_title + 70, 11, " DISABLED ");
276 fprintf(stderr, "\n%s\n", suite_title);
278 if (suite->enabled) {
279 /* iterate through all tests */
280 unsigned int it_unit;
281 xbt_dynar_foreach(suite->units, it_unit, unit) {
282 /* init unit case counters */
284 unit->test_ignore = 0;
285 unit->test_failed = 0;
286 unit->test_expect = 0;
288 /* display unit title */
289 char* cp = bprintf(" Unit: %s ......................................"
290 "......................................",
293 fprintf(stderr, "%s", cp);
296 /* run the test case function */
297 _xbt_test_current_unit = unit;
301 /* iterate through all performed tests to determine status */
302 unsigned int it_test;
303 xbt_dynar_foreach(unit->tests, it_test, test) {
309 if (test->failed && not test->expected_failure)
311 if (not test->failed && test->expected_failure)
313 if (test->expected_failure)
317 /* Display whether this unit went well */
318 if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
319 /* some tests failed (or were supposed to), so do detailed reporting of test case */
320 if (unit->test_failed > 0) {
321 fprintf(stderr, ".. failed\n");
322 } else if (unit->nb_tests) {
323 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
325 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
327 xbt_dynar_foreach(unit->tests, it_test, test) {
328 const char* file = (test->file != nullptr ? test->file : unit->file);
329 int line = (test->line != 0 ? test->line : unit->line);
333 else if (test->expected_failure) {
344 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test->title, file, line);
346 if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
348 xbt_dynar_foreach(test->logs, it_log, log) {
349 file = (log->file != nullptr ? log->file : file);
350 line = (log->line != 0 ? log->line : line);
351 fprintf(stderr, " %s:%d: %s\n", file, line, log->text);
355 fprintf(stderr, " Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
356 if (unit->test_ignore) {
357 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
359 fprintf(stderr, "\n");
361 } else if (not unit->enabled) {
362 fprintf(stderr, " disabled\n"); /* no test were run */
363 } else if (unit->nb_tests) {
364 fprintf(stderr, "...... ok\n"); /* successful */
366 fprintf(stderr, ".... skip\n"); /* no test were run */
369 /* Accumulate test counts into the suite */
370 suite->nb_tests += unit->nb_tests;
371 suite->test_failed += unit->test_failed;
372 suite->test_ignore += unit->test_ignore;
373 suite->test_expect += unit->test_expect;
375 _xbt_test_nb_tests += unit->nb_tests;
376 _xbt_test_test_failed += unit->test_failed;
377 _xbt_test_test_ignore += unit->test_ignore;
378 _xbt_test_test_expect += unit->test_expect;
380 /* What's the conclusion of this test anyway? */
381 if (unit->nb_tests) {
383 if (unit->test_failed)
384 suite->unit_failed++;
385 } else if (not unit->enabled) {
386 suite->unit_disabled++;
388 suite->unit_ignore++;
392 _xbt_test_nb_units += suite->nb_units;
393 _xbt_test_unit_failed += suite->unit_failed;
394 _xbt_test_unit_ignore += suite->unit_ignore;
395 _xbt_test_unit_disabled += suite->unit_disabled;
397 if (suite->nb_units) {
398 _xbt_test_nb_suites++;
399 if (suite->test_failed)
400 _xbt_test_suite_failed++;
401 } else if (not suite->enabled) {
402 _xbt_test_suite_disabled++;
404 _xbt_test_suite_ignore++;
407 /* print test suite summary */
408 if (suite->enabled) {
409 int first = 1; /* for result pretty printing */
411 fprintf(stderr," =====================================================================%s\n",
412 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
413 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
414 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
415 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
417 if (suite->nb_units != suite->unit_failed) {
418 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
421 if (suite->unit_failed) {
422 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
425 if (suite->unit_ignore) {
426 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
429 if (suite->unit_disabled) {
430 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
432 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
433 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
436 if (suite->nb_tests != suite->test_failed) {
437 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
440 if (suite->test_failed) {
441 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
444 if (suite->test_ignore) {
445 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
448 if (suite->test_expect) {
449 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
451 fprintf(stderr, ")\n");
453 return suite->unit_failed;
456 static void apply_selection(char *selection)
458 /* for the parsing */
459 char *sel = selection;
461 char dir[STRLEN]; /* the directive */
463 unsigned int it_suite;
464 xbt_test_suite_t suite;
465 xbt_test_unit_t unit;
466 unsigned int it_unit;
468 char suitename[STRLEN];
469 char unitname[STRLEN];
471 if (not selection || selection[0] == '\0')
474 /* First apply the selection */
478 char *p = strchr(sel, ',');
480 snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
483 snprintf(dir, STRLEN, "%s", sel);
489 memmove(dir, dir + 1, strlen(dir));
493 memmove(dir, dir + 1, strlen(dir));
496 p = strchr(dir, ':');
498 snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
499 snprintf(unitname, STRLEN, "%s", p + 1);
501 snprintf(suitename, STRLEN, "%s", dir);
505 /* Deal with the specific case of 'all' pseudo serie */
506 if (not strcmp("all", suitename)) {
507 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
509 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
510 xbt_dynar_foreach(suite->units, it_unit, unit) {
511 unit->enabled = enabling;
513 suite->enabled = enabling;
517 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
518 xbt_test_suite_t thissuite =
519 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
520 if (not strcmp(suitename, thissuite->name)) {
521 /* Do not disable the whole suite when we just want to disable a child */
522 if (enabling || (unitname[0] == '\0'))
523 thissuite->enabled = enabling;
525 if (unitname[0] == '\0') {
526 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
527 unit->enabled = enabling;
529 } else { /* act on one child only */
530 unsigned int it2_unit;
531 /* search it, first (we won't reuse it for external loop which gets broken) */
533 it2_unit < xbt_dynar_length(thissuite->units);
535 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
536 if (not strcmp(thisunit->name, unitname)) {
537 thisunit->enabled = enabling;
540 } /* search relevant unit */
541 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
542 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
543 } /* act on childs (either all or one) */
545 break; /* found the relevant serie. We are happy */
547 } /* search relevant series */
548 xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
549 "No suite of name '%s' found. Cannot apply the selection\n", suitename);
554 void xbt_test_dump(char *selection)
556 apply_selection(selection);
558 if (_xbt_test_suites) {
559 unsigned int it_suite;
560 xbt_test_suite_t suite;
562 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
563 xbt_test_suite_dump(suite);
565 printf(" No suite defined.");
569 int xbt_test_run(char *selection, int verbosity)
571 apply_selection(selection);
573 if (_xbt_test_suites) {
574 unsigned int it_suite;
575 xbt_test_suite_t suite;
578 /* Run all the suites */
579 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
580 xbt_test_suite_run(suite, verbosity);
582 /* Display some more statistics */
583 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
584 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
585 : 100.0, _xbt_test_nb_suites);
586 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
587 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
590 if (_xbt_test_suite_failed) {
591 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
595 if (_xbt_test_suite_ignore) {
596 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
598 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
599 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
601 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
602 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
605 if (_xbt_test_unit_failed) {
606 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
609 if (_xbt_test_unit_ignore) {
610 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
612 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
613 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
615 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
616 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
619 if (_xbt_test_test_failed) {
620 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
623 if (_xbt_test_test_ignore) {
624 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
627 if (_xbt_test_test_expect) {
628 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
631 fprintf(stderr, ")\n");
633 fprintf(stderr, "No unit to run!\n");
634 _xbt_test_unit_failed++;
636 return _xbt_test_unit_failed;
641 xbt_dynar_free(&_xbt_test_suites);
644 /* annotate test case with test */
645 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
647 xbt_test_unit_t unit = _xbt_test_current_unit;
651 xbt_test_test_t test = xbt_new0(s_xbt_test_test, 1);
653 test->title = bvprintf(fmt, ap);
656 test->expected_failure = 0;
660 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
661 xbt_dynar_push(unit->tests, &test);
664 /* annotate test case with log message and failure */
665 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
667 xbt_test_unit_t unit = _xbt_test_current_unit;
669 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
670 "Test failed even before being declared (broken unit: %s)", unit->title);
673 xbt_test_log_t log = xbt_new(s_xbt_test_log, 1);
675 log->text = bvprintf(fmt, ap);
680 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
681 xbt_dynar_push(test->logs, &log);
686 void xbt_test_exception(xbt_ex_t e)
688 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
691 void xbt_test_expect_failure()
693 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
694 "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
695 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
696 test->expected_failure = 1;
701 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
702 "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
703 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
707 /* annotate test case with log message only */
708 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
710 xbt_test_unit_t unit = _xbt_test_current_unit;
712 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
713 "Test logged into even before being declared (broken test unit: %s)", unit->title);
716 xbt_test_log_t log = xbt_new(s_xbt_test_log, 1);
718 log->text = bvprintf(fmt, ap);
723 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
724 xbt_dynar_push(test->logs, &log);
728 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
730 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
732 xbt_test_add("Skipped test");
735 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
736 xbt_test_expect_failure();
737 xbt_test_log("%s %s", "Test", "log");
738 xbt_test_fail("EXPECTED FAILURE");
740 #endif /* SIMGRID_TEST */