1 /* cunit - A little C Unit facility */
3 /* Copyright (c) 2005-2014. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 /* This is partially inspirated from the OSSP ts (Test Suite Library) */
10 /* At some point we should use https://github.com/google/googletest instead */
12 #include "src/portable.h"
14 #include "xbt/sysdep.h" /* bvprintf */
15 #include "xbt/cunit.h"
16 #include "xbt/dynar.h"
18 /* collection of all suites */
19 static xbt_dynar_t _xbt_test_suites = NULL;
20 /* global statistics */
21 static int _xbt_test_nb_tests = 0;
22 static int _xbt_test_test_failed = 0;
23 static int _xbt_test_test_ignore = 0;
24 static int _xbt_test_test_expect = 0;
26 static int _xbt_test_nb_units = 0;
27 static int _xbt_test_unit_failed = 0;
28 static int _xbt_test_unit_ignore = 0;
29 static int _xbt_test_unit_disabled = 0;
31 static int _xbt_test_nb_suites = 0;
32 static int _xbt_test_suite_failed = 0;
33 static int _xbt_test_suite_ignore = 0;
34 static int _xbt_test_suite_disabled = 0;
37 xbt_test_unit_t _xbt_test_current_unit = NULL;
40 /* test suite test log */
41 typedef struct s_xbt_test_log {
47 static void xbt_test_log_dump(xbt_test_log_t log)
50 fprintf(stderr, " log %p(%s:%d)=%s\n", log, log->file, log->line,
53 fprintf(stderr, " log=NULL\n");
56 /* test suite test check */
57 typedef struct s_xbt_test_test {
67 static void xbt_test_test_dump(xbt_test_test_t test)
72 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n",
73 test, test->file, test->line, test->title,
74 test->failed ? "failed" : "not failed");
75 xbt_dynar_foreach(test->logs, it_log, log)
76 xbt_test_log_dump(log);
78 fprintf(stderr, " test=NULL\n");
81 /* test suite test unit */
82 struct s_xbt_test_unit {
89 xbt_dynar_t tests; /* of xbt_test_test_t */
92 int test_failed, test_ignore, test_expect;
95 static void xbt_test_unit_dump(xbt_test_unit_t unit)
100 fprintf(stderr, " UNIT %s: %s (%s)\n",
101 unit->name, unit->title,
102 (unit->enabled ? "enabled" : "disabled"));
104 xbt_dynar_foreach(unit->tests, it_test, test)
105 xbt_test_test_dump(test);
107 fprintf(stderr, " unit=NULL\n");
112 struct s_xbt_test_suite {
116 xbt_dynar_t units; /* of xbt_test_unit_t */
118 int nb_tests, nb_units;
119 int test_failed, test_ignore, test_expect;
120 int unit_failed, unit_ignore, unit_disabled;
123 /* destroy test suite */
124 static void xbt_test_suite_free(void *s)
126 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
130 xbt_dynar_free(&suite->units);
135 static void xbt_test_unit_free(void *unit)
137 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
140 xbt_dynar_free(&u->tests);
144 static void xbt_test_test_free(void *test)
146 xbt_test_test_t t = *(xbt_test_test_t *) test;
148 xbt_dynar_free(&(t->logs));
152 static void xbt_test_log_free(void *log)
154 xbt_test_log_t l = *(xbt_test_log_t *) log;
159 /** @brief create test suite */
160 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
162 xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
165 if (!_xbt_test_suites)
167 xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
170 suite->title = bvprintf(fmt, ap);
172 xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
177 xbt_dynar_push(_xbt_test_suites, &suite);
182 /** @brief retrieve a testsuite from name, or create a new one */
183 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt,
186 xbt_test_suite_t suite;
187 unsigned int it_suite;
192 if (_xbt_test_suites)
193 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
194 if (!strcmp(suite->name, name))
198 bufname = bvprintf(fmt, ap);
200 suite = xbt_test_suite_new(name, bufname, NULL);
206 void xbt_test_suite_dump(xbt_test_suite_t suite)
209 xbt_test_unit_t unit;
210 unsigned int it_unit;
211 fprintf(stderr, "TESTSUITE %s: %s (%s)\n",
212 suite->name, suite->title,
213 suite->enabled ? "enabled" : "disabled");
215 xbt_dynar_foreach(suite->units, it_unit, unit)
216 xbt_test_unit_dump(unit);
218 fprintf(stderr, "TESTSUITE IS NULL!\n");
222 /* add test case to test suite */
223 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name,
224 ts_test_cb_t func, const char *fmt, ...)
226 xbt_test_unit_t unit;
233 unit = xbt_new0(struct s_xbt_test_unit, 1);
235 unit->title = bvprintf(fmt, ap);
237 unit->name = (char *) name;
242 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
244 xbt_dynar_push(suite->units, &unit);
248 /* run test one suite */
249 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
251 xbt_test_unit_t unit;
252 xbt_test_test_t test;
258 unsigned int it_unit, it_test, it_log;
260 int first = 1; /* for result pretty printing */
265 /* suite title pretty-printing */
267 char suite_title[81];
268 int suite_len = strlen(suite->title);
271 xbt_assert(suite_len < 68,
272 "suite title \"%s\" too long (%d should be less than 68",
273 suite->title, suite_len);
275 suite_title[0] = ' ';
276 for (i = 1; i < 80; i++)
277 suite_title[i] = '=';
278 suite_title[i++] = '\n';
279 suite_title[80] = '\0';
281 sprintf(suite_title + 40 - (suite_len + 4) / 2, "[ %s ]",
283 suite_title[40 + (suite_len + 5) / 2] = '=';
285 snprintf(suite_title + 70, 11, " DISABLED ");
286 fprintf(stderr, "\n%s\n", suite_title);
289 if (suite->enabled) {
290 /* iterate through all tests */
291 xbt_dynar_foreach(suite->units, it_unit, unit) {
292 /* init unit case counters */
294 unit->test_ignore = 0;
295 unit->test_failed = 0;
296 unit->test_expect = 0;
298 /* display unit title */
299 cp = bprintf(" Unit: %s ......................................"
300 "......................................", unit->title);
302 fprintf(stderr, "%s", cp);
305 /* run the test case function */
306 _xbt_test_current_unit = unit;
310 /* iterate through all performed tests to determine status */
311 xbt_dynar_foreach(unit->tests, it_test, test) {
317 if (test->failed && !test->expected_failure)
319 if (!test->failed && test->expected_failure)
321 if (test->expected_failure)
327 /* Display whether this unit went well */
328 if (unit->test_failed > 0 || unit->test_expect ||
329 (verbosity && unit->nb_tests > 0)) {
330 /* some tests failed (or were supposed to), so do detailed reporting of test case */
331 if (unit->test_failed > 0) {
332 fprintf(stderr, ".. failed\n");
333 } else if (unit->nb_tests) {
334 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
336 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
338 xbt_dynar_foreach(unit->tests, it_test, test) {
339 file = (test->file != NULL ? test->file : unit->file);
340 line = (test->line != 0 ? test->line : unit->line);
341 fprintf(stderr, " %s: %s [%s:%d]\n",
342 (test->ignored ? " SKIP"
343 : (test->expected_failure
345 failed ? "EFAIL" : "EPASS") : (test->failed ?
348 test->title, file, line);
350 if ((test->expected_failure && !test->failed)
351 || (!test->expected_failure && test->failed)) {
352 xbt_dynar_foreach(test->logs, it_log, log) {
353 file = (log->file != NULL ? log->file : file);
354 line = (log->line != 0 ? log->line : line);
355 fprintf(stderr, " %s:%d: %s\n",
356 file, line, log->text);
361 fprintf(stderr, " Summary: %d of %d tests failed",
362 unit->test_failed, unit->nb_tests);
363 if (unit->test_ignore) {
364 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
366 fprintf(stderr, "\n");
369 } else if (!unit->enabled) {
370 fprintf(stderr, " disabled\n"); /* no test were run */
371 } else if (unit->nb_tests) {
372 fprintf(stderr, "...... ok\n"); /* successful */
374 fprintf(stderr, ".... skip\n"); /* no test were run */
377 /* Accumulate test counts into the suite */
378 suite->nb_tests += unit->nb_tests;
379 suite->test_failed += unit->test_failed;
380 suite->test_ignore += unit->test_ignore;
381 suite->test_expect += unit->test_expect;
383 _xbt_test_nb_tests += unit->nb_tests;
384 _xbt_test_test_failed += unit->test_failed;
385 _xbt_test_test_ignore += unit->test_ignore;
386 _xbt_test_test_expect += unit->test_expect;
388 /* What's the conclusion of this test anyway? */
389 if (unit->nb_tests) {
391 if (unit->test_failed)
392 suite->unit_failed++;
393 } else if (!unit->enabled) {
394 suite->unit_disabled++;
396 suite->unit_ignore++;
400 _xbt_test_nb_units += suite->nb_units;
401 _xbt_test_unit_failed += suite->unit_failed;
402 _xbt_test_unit_ignore += suite->unit_ignore;
403 _xbt_test_unit_disabled += suite->unit_disabled;
405 if (suite->nb_units) {
406 _xbt_test_nb_suites++;
407 if (suite->test_failed)
408 _xbt_test_suite_failed++;
409 } else if (!suite->enabled) {
410 _xbt_test_suite_disabled++;
412 _xbt_test_suite_ignore++;
416 /* print test suite summary */
417 if (suite->enabled) {
420 " =====================================================================%s\n",
422 ? (suite->unit_failed ? "== FAILED" : "====== OK")
423 : (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
424 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ",
427 (double) suite->unit_failed / (double) suite->nb_units) *
428 100.0) : 100.0, suite->nb_units);
430 if (suite->nb_units != suite->unit_failed) {
431 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
432 suite->nb_units - suite->unit_failed);
435 if (suite->unit_failed) {
436 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
440 if (suite->unit_ignore) {
441 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
445 if (suite->unit_disabled) {
446 fprintf(stderr, "%s%d disabled", (first ? "" : ", "),
447 suite->unit_disabled);
449 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
452 (double) suite->test_failed / (double) suite->nb_tests) *
453 100.0) : 100.0, suite->nb_tests);
456 if (suite->nb_tests != suite->test_failed) {
457 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
458 suite->nb_tests - suite->test_failed);
461 if (suite->test_failed) {
462 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
466 if (suite->test_ignore) {
467 fprintf(stderr, "%s%d ignored", (first ? "" : "; "),
471 if (suite->test_expect) {
472 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "),
475 fprintf(stderr, ")\n");
477 return suite->unit_failed;
480 static void apply_selection(char *selection)
482 /* for the parsing */
483 char *sel = selection;
485 char dir[1024]; /* the directive */
487 unsigned int it_suite;
488 xbt_test_suite_t suite;
489 xbt_test_unit_t unit;
490 unsigned int it_unit;
495 if (!selection || selection[0] == '\0')
498 /*printf("Test selection: %s\n", selection); */
500 /* First apply the selection */
504 char *p = strchr(sel, ',');
506 strncpy(dir, sel, p - sel);
516 memmove(dir, dir + 1, strlen(dir));
520 memmove(dir, dir + 1, strlen(dir));
523 p = strchr(dir, ':');
525 strcpy(unitname, p + 1);
526 strncpy(suitename, dir, p - dir);
527 suitename[p - dir] = '\0';
529 strcpy(suitename, dir);
533 /* Deal with the specific case of 'all' pseudo serie */
534 if (!strcmp("all", suitename)) {
535 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
537 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
538 xbt_dynar_foreach(suite->units, it_unit, unit) {
539 unit->enabled = enabling;
541 suite->enabled = enabling;
545 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
546 xbt_test_suite_t thissuite =
547 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
548 if (!strcmp(suitename, thissuite->name)) {
549 /* Do not disable the whole suite when we just want to disable a child */
550 if (enabling || (unitname[0] == '\0'))
551 thissuite->enabled = enabling;
553 if (unitname[0] == '\0') {
554 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
555 unit->enabled = enabling;
557 } else { /* act on one child only */
558 unsigned int it2_unit;
559 /* search it, first (we won't reuse it for external loop which gets broken) */
561 it2_unit < xbt_dynar_length(thissuite->units);
563 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
564 if (!strcmp(thisunit->name, unitname)) {
565 thisunit->enabled = enabling;
568 } /* search relevant unit */
569 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
570 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
571 } /* act on childs (either all or one) */
573 break; /* found the relevant serie. We are happy */
575 } /* search relevant series */
576 xbt_assert(it != xbt_dynar_length(_xbt_test_suites), "No suite of name '%s' found. Cannot apply the selection\n", suitename);
582 void xbt_test_dump(char *selection)
584 apply_selection(selection);
586 if (_xbt_test_suites) {
587 unsigned int it_suite;
588 xbt_test_suite_t suite;
590 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
591 xbt_test_suite_dump(suite);
593 printf(" No suite defined.");
597 int xbt_test_run(char *selection, int verbosity)
599 apply_selection(selection);
601 if (_xbt_test_suites) {
602 unsigned int it_suite;
603 xbt_test_suite_t suite;
606 /* Run all the suites */
607 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
608 xbt_test_suite_run(suite, verbosity);
610 /* Display some more statistics */
611 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
614 (double) _xbt_test_suite_failed /
615 (double) _xbt_test_nb_suites) * 100.0)
616 : 100.0, _xbt_test_nb_suites);
617 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
618 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
621 if (_xbt_test_suite_failed) {
622 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
626 if (_xbt_test_suite_ignore) {
627 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
629 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
632 (double) _xbt_test_unit_failed /
633 (double) _xbt_test_nb_units) * 100.0) : 100.0,
636 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
637 fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_units - _xbt_test_unit_failed);
640 if (_xbt_test_unit_failed) {
641 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
644 if (_xbt_test_unit_ignore) {
645 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
647 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
650 (double) _xbt_test_test_failed /
651 (double) _xbt_test_nb_tests) * 100.0) : 100.0,
654 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
655 fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_tests - _xbt_test_test_failed);
658 if (_xbt_test_test_failed) {
659 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
662 if (_xbt_test_test_ignore) {
663 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
666 if (_xbt_test_test_expect) {
667 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
670 fprintf(stderr, ")\n");
672 fprintf(stderr, "No unit to run!\n");
673 _xbt_test_unit_failed++;
675 return _xbt_test_unit_failed;
678 void xbt_test_exit(void)
680 xbt_dynar_free(&_xbt_test_suites);
683 /* annotate test case with test */
684 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
686 xbt_test_unit_t unit = _xbt_test_current_unit;
690 xbt_test_test_t test = xbt_new0(struct s_xbt_test_test, 1);
692 test->title = bvprintf(fmt, ap);
695 test->expected_failure = 0;
699 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
700 xbt_dynar_push(unit->tests, &test);
704 /* annotate test case with log message and failure */
705 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
707 xbt_test_unit_t unit = _xbt_test_current_unit;
709 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
710 "Test failed even before being declared (broken unit: %s)", unit->title);
713 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
715 log->text = bvprintf(fmt, ap);
720 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
721 xbt_dynar_push(test->logs, &log);
726 void xbt_test_exception(xbt_ex_t e)
728 _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.msg);
731 void xbt_test_expect_failure(void)
733 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
734 "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
735 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
736 test->expected_failure = 1;
739 void xbt_test_skip(void)
741 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
742 "Test skiped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
743 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
747 /* annotate test case with log message only */
748 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
750 xbt_test_unit_t unit = _xbt_test_current_unit;
752 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
753 "Test logged into even before being declared (broken test unit: %s)", unit->title);
756 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
758 log->text = bvprintf(fmt, ap);
763 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
764 xbt_dynar_push(test->logs, &log);
770 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
772 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
774 xbt_test_add("Skipped test");
777 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
778 xbt_test_expect_failure();
779 xbt_test_log("%s %s", "Test", "log");
780 xbt_test_fail("EXPECTED FAILURE");
783 #endif /* SIMGRID_TEST */