1 /* cunit - A little C Unit facility */
3 /* Copyright (c) 2005 Martin Quinson. 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) */
12 #include "xbt/sysdep.h" /* vasprintf */
13 #include "xbt/cunit.h"
14 #include "xbt/dynar.h"
16 /* collection of all suites */
17 static xbt_dynar_t _xbt_test_suites = NULL;
18 /* global statistics */
19 static int _xbt_test_nb_tests = 0;
20 static int _xbt_test_test_failed = 0;
21 static int _xbt_test_test_ignore = 0;
22 static int _xbt_test_test_expect = 0;
24 static int _xbt_test_nb_units = 0;
25 static int _xbt_test_unit_failed = 0;
26 static int _xbt_test_unit_ignore = 0;
27 static int _xbt_test_unit_disabled = 0;
29 static int _xbt_test_nb_suites = 0;
30 static int _xbt_test_suite_failed = 0;
31 static int _xbt_test_suite_ignore = 0;
32 static int _xbt_test_suite_disabled = 0;
35 xbt_test_unit_t _xbt_test_current_unit = NULL;
38 /* test suite test log */
39 typedef struct s_xbt_test_log {
45 static void xbt_test_log_dump(xbt_test_log_t log)
48 fprintf(stderr, " log %p(%s:%d)=%s\n", log, log->file, log->line,
51 fprintf(stderr, " log=NULL\n");
54 /* test suite test check */
55 typedef struct s_xbt_test_test {
65 static void xbt_test_test_dump(xbt_test_test_t test)
70 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n",
71 test, test->file, test->line, test->title,
72 test->failed ? "failed" : "not failed");
73 xbt_dynar_foreach(test->logs, it_log, log)
74 xbt_test_log_dump(log);
76 fprintf(stderr, " test=NULL\n");
79 /* test suite test unit */
80 struct s_xbt_test_unit {
87 xbt_dynar_t tests; /* of xbt_test_test_t */
90 int test_failed, test_ignore, test_expect;
93 static void xbt_test_unit_dump(xbt_test_unit_t unit)
98 fprintf(stderr, " UNIT %s: %s (%s)\n",
99 unit->name, unit->title,
100 (unit->enabled ? "enabled" : "disabled"));
102 xbt_dynar_foreach(unit->tests, it_test, test)
103 xbt_test_test_dump(test);
105 fprintf(stderr, " unit=NULL\n");
110 struct s_xbt_test_suite {
114 xbt_dynar_t units; /* of xbt_test_unit_t */
116 int nb_tests, nb_units;
117 int test_failed, test_ignore, test_expect;
118 int unit_failed, unit_ignore, unit_disabled;
121 /* destroy test suite */
122 static void xbt_test_suite_free(void *s)
124 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
128 xbt_dynar_free(&suite->units);
133 static void xbt_test_unit_free(void *unit)
135 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
138 xbt_dynar_free(&u->tests);
142 static void xbt_test_test_free(void *test)
144 xbt_test_test_t t = *(xbt_test_test_t *) test;
146 xbt_dynar_free(&(t->logs));
150 static void xbt_test_log_free(void *log)
152 xbt_test_log_t l = *(xbt_test_log_t *) log;
157 /** @brief create test suite */
158 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
160 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 vres = vasprintf(&suite->title, fmt, ap);
170 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
175 xbt_dynar_push(_xbt_test_suites, &suite);
180 /** @brief retrieve a testsuite from name, or create a new one */
181 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt,
184 xbt_test_suite_t suite;
185 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 vres = vasprintf(&bufname, 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;
233 unit = xbt_new0(struct s_xbt_test_unit, 1);
235 vres = vasprintf(&unit->title, 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)
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 */
266 /* suite title pretty-printing */
268 char suite_title[81];
269 int suite_len = strlen(suite->title);
272 xbt_assert2(suite_len < 68,
273 "suite title \"%s\" too long (%d should be less than 68",
274 suite->title, suite_len);
276 suite_title[0] = ' ';
277 for (i = 1; i < 80; i++)
278 suite_title[i] = '=';
279 suite_title[i++] = '\n';
280 suite_title[80] = '\0';
282 sprintf(suite_title + 40 - (suite_len + 4) / 2, "[ %s ]", suite->title);
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 vres = asprintf(&cp, " 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 /* 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
343 ? (test->failed ? "EFAIL" : "EPASS") : (test->failed ?
346 test->title, file, line);
348 if ((test->expected_failure && !test->failed)
349 || (!test->expected_failure && test->failed)) {
350 xbt_dynar_foreach(test->logs, it_log, log) {
351 file = (log->file != NULL ? log->file : file);
352 line = (log->line != 0 ? log->line : line);
353 fprintf(stderr, " %s:%d: %s\n",
354 file, line, log->text);
359 fprintf(stderr, " Summary: %d of %d tests failed",
360 unit->test_failed, unit->nb_tests);
361 if (unit->test_ignore) {
362 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
364 fprintf(stderr, "\n");
367 } else if (!unit->enabled) {
368 fprintf(stderr, " disabled\n"); /* no test were run */
369 } else if (unit->nb_tests) {
370 fprintf(stderr, "...... ok\n"); /* successful */
372 fprintf(stderr, ".... skip\n"); /* no test were run */
375 /* Accumulate test counts into the suite */
376 suite->nb_tests += unit->nb_tests;
377 suite->test_failed += unit->test_failed;
378 suite->test_ignore += unit->test_ignore;
379 suite->test_expect += unit->test_expect;
381 _xbt_test_nb_tests += unit->nb_tests;
382 _xbt_test_test_failed += unit->test_failed;
383 _xbt_test_test_ignore += unit->test_ignore;
384 _xbt_test_test_expect += unit->test_expect;
386 /* What's the conclusion of this test anyway? */
387 if (unit->nb_tests) {
389 if (unit->test_failed)
390 suite->unit_failed++;
391 } else if (!unit->enabled) {
392 suite->unit_disabled++;
394 suite->unit_ignore++;
398 _xbt_test_nb_units += suite->nb_units;
399 _xbt_test_unit_failed += suite->unit_failed;
400 _xbt_test_unit_ignore += suite->unit_ignore;
401 _xbt_test_unit_disabled += suite->unit_disabled;
403 if (suite->nb_units) {
404 _xbt_test_nb_suites++;
405 if (suite->test_failed)
406 _xbt_test_suite_failed++;
407 } else if (!suite->enabled) {
408 _xbt_test_suite_disabled++;
410 _xbt_test_suite_ignore++;
414 /* print test suite summary */
415 if (suite->enabled) {
418 " =====================================================================%s\n",
420 ? (suite->unit_failed ? "== FAILED" : "====== OK")
421 : (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
422 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ",
425 (double) suite->unit_failed / (double) suite->nb_units) *
426 100.0) : 100.0, suite->nb_units);
428 if (suite->nb_units != suite->unit_failed) {
429 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
430 suite->nb_units - suite->unit_failed);
433 if (suite->unit_failed) {
434 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
437 if (suite->unit_ignore) {
438 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
442 if (suite->unit_disabled) {
443 fprintf(stderr, "%s%d disabled", (first ? "" : ", "),
444 suite->unit_disabled);
447 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
450 (double) suite->test_failed / (double) suite->nb_tests) *
451 100.0) : 100.0, suite->nb_tests);
454 if (suite->nb_tests != suite->test_failed) {
455 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
456 suite->nb_tests - suite->test_failed);
459 if (suite->test_failed) {
460 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
463 if (suite->test_ignore) {
464 fprintf(stderr, "%s%d ignored", (first ? "" : "; "),
468 if (suite->test_expect) {
469 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "),
473 fprintf(stderr, ")\n");
475 return suite->unit_failed;
478 static void apply_selection(char *selection)
480 /* for the parsing */
481 char *sel = selection;
484 char dir[1024]; /* the directive */
486 unsigned int it_suite;
487 xbt_test_suite_t suite;
488 xbt_test_unit_t unit;
489 unsigned int it_unit;
494 if (!selection || selection[0] == '\0')
497 /*printf("Test selection: %s\n", selection); */
499 /* First apply the selection */
503 p = strchr(sel, ',');
505 strncpy(dir, sel, p - sel);
515 memmove(dir, dir + 1, strlen(dir));
519 memmove(dir, dir + 1, strlen(dir));
522 p = strchr(dir, ':');
524 strcpy(unitname, p + 1);
525 strncpy(suitename, dir, p - dir);
526 suitename[p - dir] = '\0';
528 strcpy(suitename, dir);
531 /*fprintf(stderr,"Seen %s (%s; suite=%s; unit=%s)\n",
532 dir,enabling?"enabling":"disabling", suitename, unitname); */
534 /* Deal with the specific case of 'all' pseudo serie */
535 if (!strcmp("all", suitename)) {
536 if (unitname[0] != '\0') {
538 "The 'all' pseudo-suite does not accept any unit specification\n");
542 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
543 xbt_dynar_foreach(suite->units, it_unit, unit) {
544 unit->enabled = enabling;
546 suite->enabled = enabling;
550 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
551 xbt_test_suite_t thissuite =
552 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
553 if (!strcmp(suitename, thissuite->name)) {
554 /* Do not disable the whole suite when we just want to disable a child */
555 if (enabling || (unitname[0] == '\0'))
556 thissuite->enabled = enabling;
558 if (unitname[0] == '\0') {
559 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
560 unit->enabled = enabling;
562 } else { /* act on one child only */
563 unsigned int it2_unit;
564 /* search it, first (we won't reuse it for external loop which gets broken) */
565 for (it2_unit = 0; it2_unit < xbt_dynar_length(thissuite->units);
567 xbt_test_unit_t thisunit =
568 xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
569 if (!strcmp(thisunit->name, unitname)) {
570 thisunit->enabled = enabling;
573 } /* search relevant unit */
574 if (it2_unit == xbt_dynar_length(thissuite->units)) {
576 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n",
577 suitename, unitname);
580 } /* act on childs (either all or one) */
582 break; /* found the relevant serie. We are happy */
584 } /* search relevant series */
585 if (it == xbt_dynar_length(_xbt_test_suites)) {
587 "No suite of name '%s' found. Cannot apply the selection\n",
596 void xbt_test_dump(char *selection)
598 apply_selection(selection);
600 if (_xbt_test_suites) {
601 unsigned int it_suite;
602 xbt_test_suite_t suite;
604 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
605 xbt_test_suite_dump(suite);
607 printf(" No suite defined.");
611 int xbt_test_run(char *selection)
613 apply_selection(selection);
615 if (_xbt_test_suites) {
616 unsigned int it_suite;
617 xbt_test_suite_t suite;
620 /* Run all the suites */
621 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
622 xbt_test_suite_run(suite);
624 /* Display some more statistics */
625 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
628 (double) _xbt_test_suite_failed /
629 (double) _xbt_test_nb_suites) * 100.0)
630 : 100.0, _xbt_test_nb_suites);
631 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
632 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
635 if (_xbt_test_suite_failed) {
636 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
637 _xbt_test_suite_failed);
641 if (_xbt_test_suite_ignore) {
642 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
643 _xbt_test_suite_ignore);
646 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
649 (double) _xbt_test_unit_failed /
650 (double) _xbt_test_nb_units) * 100.0) : 100.0,
653 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
654 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
655 _xbt_test_nb_units - _xbt_test_unit_failed);
658 if (_xbt_test_unit_failed) {
659 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
660 _xbt_test_unit_failed);
663 if (_xbt_test_unit_ignore) {
664 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
665 _xbt_test_unit_ignore);
668 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
671 (double) _xbt_test_test_failed /
672 (double) _xbt_test_nb_tests) * 100.0) : 100.0,
675 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
676 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
677 _xbt_test_nb_tests - _xbt_test_test_failed);
680 if (_xbt_test_test_failed) {
681 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
682 _xbt_test_test_failed);
685 if (_xbt_test_test_ignore) {
686 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
687 _xbt_test_test_ignore);
690 if (_xbt_test_test_expect) {
691 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "),
692 _xbt_test_test_expect);
695 fprintf(stderr, ")\n");
697 fprintf(stderr, "No unit to run!\n");
698 _xbt_test_unit_failed++;
700 return _xbt_test_unit_failed;
703 void xbt_test_exit(void)
705 xbt_dynar_free(&_xbt_test_suites);
708 /* annotate test case with test */
709 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
711 xbt_test_unit_t unit = _xbt_test_current_unit;
712 xbt_test_test_t test;
719 test = xbt_new0(struct s_xbt_test_test, 1);
721 vres = vasprintf(&test->title, fmt, ap);
724 test->expected_failure = 0;
728 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
729 xbt_dynar_push(unit->tests, &test);
733 /* annotate test case with log message and failure */
734 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
736 xbt_test_unit_t unit = _xbt_test_current_unit;
737 xbt_test_test_t test;
745 xbt_assert1(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 vres = vasprintf(&log->text, 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_assert1(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);
774 test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
775 test->expected_failure = 1;
778 void xbt_test_skip(void)
780 xbt_test_test_t test;
782 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
783 "Test skiped even before being declared (broken unit: %s)",
784 _xbt_test_current_unit->title);
786 test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
790 /* annotate test case with log message only */
791 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
793 xbt_test_unit_t unit = _xbt_test_current_unit;
794 xbt_test_test_t test;
802 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
803 "Test logged into even before being declared (broken test unit: %s)",
806 log = xbt_new(struct s_xbt_test_log, 1);
808 vres = vasprintf(&log->text, fmt, ap);
813 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
814 xbt_dynar_push(test->logs, &log);
822 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
824 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
826 xbt_test_add0("Skipped test");
829 xbt_test_add2("%s %s", "EXPECTED", "FAILURE");
830 xbt_test_expect_failure();
831 xbt_test_log2("%s %s", "Test", "log");
832 xbt_test_fail0("EXPECTED FAILURE");
835 #endif /* SIMGRID_TEST */