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) */
13 #include "xbt/sysdep.h" /* bvprintf */
14 #include "xbt/cunit.h"
15 #include "xbt/dynar.h"
17 /* collection of all suites */
18 static xbt_dynar_t _xbt_test_suites = NULL;
19 /* global statistics */
20 static int _xbt_test_nb_tests = 0;
21 static int _xbt_test_test_failed = 0;
22 static int _xbt_test_test_ignore = 0;
23 static int _xbt_test_test_expect = 0;
25 static int _xbt_test_nb_units = 0;
26 static int _xbt_test_unit_failed = 0;
27 static int _xbt_test_unit_ignore = 0;
28 static int _xbt_test_unit_disabled = 0;
30 static int _xbt_test_nb_suites = 0;
31 static int _xbt_test_suite_failed = 0;
32 static int _xbt_test_suite_ignore = 0;
33 static int _xbt_test_suite_disabled = 0;
36 xbt_test_unit_t _xbt_test_current_unit = NULL;
39 /* test suite test log */
40 typedef struct s_xbt_test_log {
46 static void xbt_test_log_dump(xbt_test_log_t log)
49 fprintf(stderr, " log %p(%s:%d)=%s\n", log, log->file, log->line,
52 fprintf(stderr, " log=NULL\n");
55 /* test suite test check */
56 typedef struct s_xbt_test_test {
66 static void xbt_test_test_dump(xbt_test_test_t test)
71 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n",
72 test, test->file, test->line, test->title,
73 test->failed ? "failed" : "not failed");
74 xbt_dynar_foreach(test->logs, it_log, log)
75 xbt_test_log_dump(log);
77 fprintf(stderr, " test=NULL\n");
80 /* test suite test unit */
81 struct s_xbt_test_unit {
88 xbt_dynar_t tests; /* of xbt_test_test_t */
91 int test_failed, test_ignore, test_expect;
94 static void xbt_test_unit_dump(xbt_test_unit_t unit)
99 fprintf(stderr, " UNIT %s: %s (%s)\n",
100 unit->name, unit->title,
101 (unit->enabled ? "enabled" : "disabled"));
103 xbt_dynar_foreach(unit->tests, it_test, test)
104 xbt_test_test_dump(test);
106 fprintf(stderr, " unit=NULL\n");
111 struct s_xbt_test_suite {
115 xbt_dynar_t units; /* of xbt_test_unit_t */
117 int nb_tests, nb_units;
118 int test_failed, test_ignore, test_expect;
119 int unit_failed, unit_ignore, unit_disabled;
122 /* destroy test suite */
123 static void xbt_test_suite_free(void *s)
125 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
129 xbt_dynar_free(&suite->units);
134 static void xbt_test_unit_free(void *unit)
136 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
139 xbt_dynar_free(&u->tests);
143 static void xbt_test_test_free(void *test)
145 xbt_test_test_t t = *(xbt_test_test_t *) test;
147 xbt_dynar_free(&(t->logs));
151 static void xbt_test_log_free(void *log)
153 xbt_test_log_t l = *(xbt_test_log_t *) log;
158 /** @brief create test suite */
159 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
161 xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
164 if (!_xbt_test_suites)
166 xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
169 suite->title = bvprintf(fmt, ap);
171 xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
176 xbt_dynar_push(_xbt_test_suites, &suite);
181 /** @brief retrieve a testsuite from name, or create a new one */
182 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt,
185 xbt_test_suite_t suite;
186 unsigned int it_suite;
191 if (_xbt_test_suites)
192 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
193 if (!strcmp(suite->name, name))
197 bufname = bvprintf(fmt, ap);
199 suite = xbt_test_suite_new(name, bufname, NULL);
205 void xbt_test_suite_dump(xbt_test_suite_t suite)
208 xbt_test_unit_t unit;
209 unsigned int it_unit;
210 fprintf(stderr, "TESTSUITE %s: %s (%s)\n",
211 suite->name, suite->title,
212 suite->enabled ? "enabled" : "disabled");
214 xbt_dynar_foreach(suite->units, it_unit, unit)
215 xbt_test_unit_dump(unit);
217 fprintf(stderr, "TESTSUITE IS NULL!\n");
221 /* add test case to test suite */
222 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name,
223 ts_test_cb_t func, const char *fmt, ...)
225 xbt_test_unit_t unit;
232 unit = xbt_new0(struct s_xbt_test_unit, 1);
234 unit->title = bvprintf(fmt, ap);
236 unit->name = (char *) name;
241 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
243 xbt_dynar_push(suite->units, &unit);
247 /* run test one suite */
248 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
250 xbt_test_unit_t unit;
251 xbt_test_test_t test;
257 unsigned int it_unit, it_test, it_log;
259 int first = 1; /* for result pretty printing */
264 /* suite title pretty-printing */
266 char suite_title[81];
267 int suite_len = strlen(suite->title);
270 xbt_assert(suite_len < 68,
271 "suite title \"%s\" too long (%d should be less than 68",
272 suite->title, suite_len);
274 suite_title[0] = ' ';
275 for (i = 1; i < 80; i++)
276 suite_title[i] = '=';
277 suite_title[i++] = '\n';
278 suite_title[80] = '\0';
280 sprintf(suite_title + 40 - (suite_len + 4) / 2, "[ %s ]",
282 suite_title[40 + (suite_len + 5) / 2] = '=';
284 snprintf(suite_title + 70, 11, " DISABLED ");
285 fprintf(stderr, "\n%s\n", suite_title);
288 if (suite->enabled) {
289 /* iterate through all tests */
290 xbt_dynar_foreach(suite->units, it_unit, unit) {
291 /* init unit case counters */
293 unit->test_ignore = 0;
294 unit->test_failed = 0;
295 unit->test_expect = 0;
297 /* display unit title */
298 cp = bprintf(" Unit: %s ......................................"
299 "......................................", unit->title);
301 fprintf(stderr, "%s", cp);
304 /* run the test case function */
305 _xbt_test_current_unit = unit;
309 /* iterate through all performed tests to determine status */
310 xbt_dynar_foreach(unit->tests, it_test, test) {
316 if (test->failed && !test->expected_failure)
318 if (!test->failed && test->expected_failure)
320 if (test->expected_failure)
326 /* Display whether this unit went well */
327 if (unit->test_failed > 0 || unit->test_expect ||
328 (verbosity && unit->nb_tests > 0)) {
329 /* some tests failed (or were supposed to), so do detailed reporting of test case */
330 if (unit->test_failed > 0) {
331 fprintf(stderr, ".. failed\n");
332 } else if (unit->nb_tests) {
333 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
335 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
337 xbt_dynar_foreach(unit->tests, it_test, test) {
338 file = (test->file != NULL ? test->file : unit->file);
339 line = (test->line != 0 ? test->line : unit->line);
340 fprintf(stderr, " %s: %s [%s:%d]\n",
341 (test->ignored ? " SKIP"
342 : (test->expected_failure
344 failed ? "EFAIL" : "EPASS") : (test->failed ?
347 test->title, file, line);
349 if ((test->expected_failure && !test->failed)
350 || (!test->expected_failure && test->failed)) {
351 xbt_dynar_foreach(test->logs, it_log, log) {
352 file = (log->file != NULL ? log->file : file);
353 line = (log->line != 0 ? log->line : line);
354 fprintf(stderr, " %s:%d: %s\n",
355 file, line, log->text);
360 fprintf(stderr, " Summary: %d of %d tests failed",
361 unit->test_failed, unit->nb_tests);
362 if (unit->test_ignore) {
363 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
365 fprintf(stderr, "\n");
368 } else if (!unit->enabled) {
369 fprintf(stderr, " disabled\n"); /* no test were run */
370 } else if (unit->nb_tests) {
371 fprintf(stderr, "...... ok\n"); /* successful */
373 fprintf(stderr, ".... skip\n"); /* no test were run */
376 /* Accumulate test counts into the suite */
377 suite->nb_tests += unit->nb_tests;
378 suite->test_failed += unit->test_failed;
379 suite->test_ignore += unit->test_ignore;
380 suite->test_expect += unit->test_expect;
382 _xbt_test_nb_tests += unit->nb_tests;
383 _xbt_test_test_failed += unit->test_failed;
384 _xbt_test_test_ignore += unit->test_ignore;
385 _xbt_test_test_expect += unit->test_expect;
387 /* What's the conclusion of this test anyway? */
388 if (unit->nb_tests) {
390 if (unit->test_failed)
391 suite->unit_failed++;
392 } else if (!unit->enabled) {
393 suite->unit_disabled++;
395 suite->unit_ignore++;
399 _xbt_test_nb_units += suite->nb_units;
400 _xbt_test_unit_failed += suite->unit_failed;
401 _xbt_test_unit_ignore += suite->unit_ignore;
402 _xbt_test_unit_disabled += suite->unit_disabled;
404 if (suite->nb_units) {
405 _xbt_test_nb_suites++;
406 if (suite->test_failed)
407 _xbt_test_suite_failed++;
408 } else if (!suite->enabled) {
409 _xbt_test_suite_disabled++;
411 _xbt_test_suite_ignore++;
415 /* print test suite summary */
416 if (suite->enabled) {
419 " =====================================================================%s\n",
421 ? (suite->unit_failed ? "== FAILED" : "====== OK")
422 : (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
423 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ",
426 (double) suite->unit_failed / (double) suite->nb_units) *
427 100.0) : 100.0, suite->nb_units);
429 if (suite->nb_units != suite->unit_failed) {
430 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
431 suite->nb_units - suite->unit_failed);
434 if (suite->unit_failed) {
435 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
439 if (suite->unit_ignore) {
440 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
444 if (suite->unit_disabled) {
445 fprintf(stderr, "%s%d disabled", (first ? "" : ", "),
446 suite->unit_disabled);
448 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
451 (double) suite->test_failed / (double) suite->nb_tests) *
452 100.0) : 100.0, suite->nb_tests);
455 if (suite->nb_tests != suite->test_failed) {
456 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
457 suite->nb_tests - suite->test_failed);
460 if (suite->test_failed) {
461 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
465 if (suite->test_ignore) {
466 fprintf(stderr, "%s%d ignored", (first ? "" : "; "),
470 if (suite->test_expect) {
471 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "),
474 fprintf(stderr, ")\n");
476 return suite->unit_failed;
479 static void apply_selection(char *selection)
481 /* for the parsing */
482 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 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);
532 /*fprintf(stderr,"Seen %s (%s; suite=%s; unit=%s)\n",
533 dir,enabling?"enabling":"disabling", suitename, unitname); */
535 /* Deal with the specific case of 'all' pseudo serie */
536 if (!strcmp("all", suitename)) {
537 if (unitname[0] != '\0') {
539 "The 'all' pseudo-suite does not accept any unit specification\n");
543 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
544 xbt_dynar_foreach(suite->units, it_unit, unit) {
545 unit->enabled = enabling;
547 suite->enabled = enabling;
551 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
552 xbt_test_suite_t thissuite =
553 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
554 if (!strcmp(suitename, thissuite->name)) {
555 /* Do not disable the whole suite when we just want to disable a child */
556 if (enabling || (unitname[0] == '\0'))
557 thissuite->enabled = enabling;
559 if (unitname[0] == '\0') {
560 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
561 unit->enabled = enabling;
563 } else { /* act on one child only */
564 unsigned int it2_unit;
565 /* search it, first (we won't reuse it for external loop which gets broken) */
567 it2_unit < xbt_dynar_length(thissuite->units);
569 xbt_test_unit_t thisunit =
570 xbt_dynar_get_as(thissuite->units, it2_unit,
572 if (!strcmp(thisunit->name, unitname)) {
573 thisunit->enabled = enabling;
576 } /* search relevant unit */
577 if (it2_unit == xbt_dynar_length(thissuite->units)) {
579 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n",
580 suitename, unitname);
583 } /* act on childs (either all or one) */
585 break; /* found the relevant serie. We are happy */
587 } /* search relevant series */
588 if (it == xbt_dynar_length(_xbt_test_suites)) {
590 "No suite of name '%s' found. Cannot apply the selection\n",
599 void xbt_test_dump(char *selection)
601 apply_selection(selection);
603 if (_xbt_test_suites) {
604 unsigned int it_suite;
605 xbt_test_suite_t suite;
607 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
608 xbt_test_suite_dump(suite);
610 printf(" No suite defined.");
614 int xbt_test_run(char *selection, int verbosity)
616 apply_selection(selection);
618 if (_xbt_test_suites) {
619 unsigned int it_suite;
620 xbt_test_suite_t suite;
623 /* Run all the suites */
624 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
625 xbt_test_suite_run(suite, verbosity);
627 /* Display some more statistics */
628 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
631 (double) _xbt_test_suite_failed /
632 (double) _xbt_test_nb_suites) * 100.0)
633 : 100.0, _xbt_test_nb_suites);
634 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
635 fprintf(stderr, "%d ok",
636 _xbt_test_nb_suites - _xbt_test_suite_failed);
639 if (_xbt_test_suite_failed) {
640 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
641 _xbt_test_suite_failed);
645 if (_xbt_test_suite_ignore) {
646 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
647 _xbt_test_suite_ignore);
649 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
652 (double) _xbt_test_unit_failed /
653 (double) _xbt_test_nb_units) * 100.0) : 100.0,
656 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
657 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
658 _xbt_test_nb_units - _xbt_test_unit_failed);
661 if (_xbt_test_unit_failed) {
662 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
663 _xbt_test_unit_failed);
666 if (_xbt_test_unit_ignore) {
667 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
668 _xbt_test_unit_ignore);
670 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
673 (double) _xbt_test_test_failed /
674 (double) _xbt_test_nb_tests) * 100.0) : 100.0,
677 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
678 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
679 _xbt_test_nb_tests - _xbt_test_test_failed);
682 if (_xbt_test_test_failed) {
683 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
684 _xbt_test_test_failed);
687 if (_xbt_test_test_ignore) {
688 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
689 _xbt_test_test_ignore);
692 if (_xbt_test_test_expect) {
693 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "),
694 _xbt_test_test_expect);
697 fprintf(stderr, ")\n");
699 fprintf(stderr, "No unit to run!\n");
700 _xbt_test_unit_failed++;
702 return _xbt_test_unit_failed;
705 void xbt_test_exit(void)
707 xbt_dynar_free(&_xbt_test_suites);
710 /* annotate test case with test */
711 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
713 xbt_test_unit_t unit = _xbt_test_current_unit;
714 xbt_test_test_t test;
720 test = xbt_new0(struct s_xbt_test_test, 1);
722 test->title = bvprintf(fmt, ap);
725 test->expected_failure = 0;
729 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
730 xbt_dynar_push(unit->tests, &test);
734 /* annotate test case with log message and failure */
735 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
737 xbt_test_unit_t unit = _xbt_test_current_unit;
738 xbt_test_test_t test;
745 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
746 "Test failed even before being declared (broken unit: %s)",
749 log = xbt_new(struct s_xbt_test_log, 1);
751 log->text = bvprintf(fmt, ap);
756 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
757 xbt_dynar_push(test->logs, &log);
762 void xbt_test_exception(xbt_ex_t e)
764 _xbt_test_fail(e.file, e.line, "Exception %s raised: %s",
765 xbt_ex_catname(e.category), e.msg);
768 void xbt_test_expect_failure(void)
770 xbt_test_test_t test;
771 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
772 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
773 _xbt_test_current_unit->title);
775 xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
776 test->expected_failure = 1;
779 void xbt_test_skip(void)
781 xbt_test_test_t test;
783 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
784 "Test skiped even before being declared (broken unit: %s)",
785 _xbt_test_current_unit->title);
788 xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
792 /* annotate test case with log message only */
793 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
795 xbt_test_unit_t unit = _xbt_test_current_unit;
796 xbt_test_test_t test;
803 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
804 "Test logged into even before being declared (broken test unit: %s)",
807 log = xbt_new(struct s_xbt_test_log, 1);
809 log->text = bvprintf(fmt, ap);
814 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
815 xbt_dynar_push(test->logs, &log);
823 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
825 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
827 xbt_test_add("Skipped test");
830 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
831 xbt_test_expect_failure();
832 xbt_test_log("%s %s", "Test", "log");
833 xbt_test_fail("EXPECTED FAILURE");
836 #endif /* SIMGRID_TEST */