1 /* cunit - A little C Unit facility */
3 /* Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010. 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)
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 /* some tests failed (or were supposed to), so do detailed reporting of test case */
329 if (unit->test_failed > 0) {
330 fprintf(stderr, ".. failed\n");
331 } else if (unit->nb_tests) {
332 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
334 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
336 xbt_dynar_foreach(unit->tests, it_test, test) {
337 file = (test->file != NULL ? test->file : unit->file);
338 line = (test->line != 0 ? test->line : unit->line);
339 fprintf(stderr, " %s: %s [%s:%d]\n",
340 (test->ignored ? " SKIP"
341 : (test->expected_failure
343 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 ? "" : ", "),
438 if (suite->unit_ignore) {
439 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
443 if (suite->unit_disabled) {
444 fprintf(stderr, "%s%d disabled", (first ? "" : ", "),
445 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 ? "" : "; "),
475 fprintf(stderr, ")\n");
477 return suite->unit_failed;
480 static void apply_selection(char *selection)
482 /* for the parsing */
483 char *sel = selection;
486 char dir[1024]; /* the directive */
488 unsigned int it_suite;
489 xbt_test_suite_t suite;
490 xbt_test_unit_t unit;
491 unsigned int it_unit;
496 if (!selection || selection[0] == '\0')
499 /*printf("Test selection: %s\n", selection); */
501 /* First apply the selection */
505 p = strchr(sel, ',');
507 strncpy(dir, sel, p - sel);
517 memmove(dir, dir + 1, strlen(dir));
521 memmove(dir, dir + 1, strlen(dir));
524 p = strchr(dir, ':');
526 strcpy(unitname, p + 1);
527 strncpy(suitename, dir, p - dir);
528 suitename[p - dir] = '\0';
530 strcpy(suitename, dir);
533 /*fprintf(stderr,"Seen %s (%s; suite=%s; unit=%s)\n",
534 dir,enabling?"enabling":"disabling", suitename, unitname); */
536 /* Deal with the specific case of 'all' pseudo serie */
537 if (!strcmp("all", suitename)) {
538 if (unitname[0] != '\0') {
540 "The 'all' pseudo-suite does not accept any unit specification\n");
544 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
545 xbt_dynar_foreach(suite->units, it_unit, unit) {
546 unit->enabled = enabling;
548 suite->enabled = enabling;
552 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
553 xbt_test_suite_t thissuite =
554 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
555 if (!strcmp(suitename, thissuite->name)) {
556 /* Do not disable the whole suite when we just want to disable a child */
557 if (enabling || (unitname[0] == '\0'))
558 thissuite->enabled = enabling;
560 if (unitname[0] == '\0') {
561 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
562 unit->enabled = enabling;
564 } else { /* act on one child only */
565 unsigned int it2_unit;
566 /* search it, first (we won't reuse it for external loop which gets broken) */
568 it2_unit < xbt_dynar_length(thissuite->units);
570 xbt_test_unit_t thisunit =
571 xbt_dynar_get_as(thissuite->units, it2_unit,
573 if (!strcmp(thisunit->name, unitname)) {
574 thisunit->enabled = enabling;
577 } /* search relevant unit */
578 if (it2_unit == xbt_dynar_length(thissuite->units)) {
580 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n",
581 suitename, unitname);
584 } /* act on childs (either all or one) */
586 break; /* found the relevant serie. We are happy */
588 } /* search relevant series */
589 if (it == xbt_dynar_length(_xbt_test_suites)) {
591 "No suite of name '%s' found. Cannot apply the selection\n",
600 void xbt_test_dump(char *selection)
602 apply_selection(selection);
604 if (_xbt_test_suites) {
605 unsigned int it_suite;
606 xbt_test_suite_t suite;
608 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
609 xbt_test_suite_dump(suite);
611 printf(" No suite defined.");
615 int xbt_test_run(char *selection)
617 apply_selection(selection);
619 if (_xbt_test_suites) {
620 unsigned int it_suite;
621 xbt_test_suite_t suite;
624 /* Run all the suites */
625 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
626 xbt_test_suite_run(suite);
628 /* Display some more statistics */
629 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
632 (double) _xbt_test_suite_failed /
633 (double) _xbt_test_nb_suites) * 100.0)
634 : 100.0, _xbt_test_nb_suites);
635 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
636 fprintf(stderr, "%d ok",
637 _xbt_test_nb_suites - _xbt_test_suite_failed);
640 if (_xbt_test_suite_failed) {
641 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
642 _xbt_test_suite_failed);
646 if (_xbt_test_suite_ignore) {
647 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
648 _xbt_test_suite_ignore);
651 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
654 (double) _xbt_test_unit_failed /
655 (double) _xbt_test_nb_units) * 100.0) : 100.0,
658 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
659 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
660 _xbt_test_nb_units - _xbt_test_unit_failed);
663 if (_xbt_test_unit_failed) {
664 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
665 _xbt_test_unit_failed);
668 if (_xbt_test_unit_ignore) {
669 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
670 _xbt_test_unit_ignore);
673 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
676 (double) _xbt_test_test_failed /
677 (double) _xbt_test_nb_tests) * 100.0) : 100.0,
680 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
681 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
682 _xbt_test_nb_tests - _xbt_test_test_failed);
685 if (_xbt_test_test_failed) {
686 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
687 _xbt_test_test_failed);
690 if (_xbt_test_test_ignore) {
691 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
692 _xbt_test_test_ignore);
695 if (_xbt_test_test_expect) {
696 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "),
697 _xbt_test_test_expect);
700 fprintf(stderr, ")\n");
702 fprintf(stderr, "No unit to run!\n");
703 _xbt_test_unit_failed++;
705 return _xbt_test_unit_failed;
708 void xbt_test_exit(void)
710 xbt_dynar_free(&_xbt_test_suites);
713 /* annotate test case with test */
714 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
716 xbt_test_unit_t unit = _xbt_test_current_unit;
717 xbt_test_test_t test;
723 test = xbt_new0(struct s_xbt_test_test, 1);
725 test->title = bvprintf(fmt, ap);
728 test->expected_failure = 0;
732 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
733 xbt_dynar_push(unit->tests, &test);
737 /* annotate test case with log message and failure */
738 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
740 xbt_test_unit_t unit = _xbt_test_current_unit;
741 xbt_test_test_t test;
748 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
749 "Test failed even before being declared (broken unit: %s)",
752 log = xbt_new(struct s_xbt_test_log, 1);
754 log->text = bvprintf(fmt, ap);
759 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
760 xbt_dynar_push(test->logs, &log);
765 void xbt_test_exception(xbt_ex_t e)
767 _xbt_test_fail(e.file, e.line, "Exception %s raised: %s",
768 xbt_ex_catname(e.category), e.msg);
771 void xbt_test_expect_failure(void)
773 xbt_test_test_t test;
774 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
775 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
776 _xbt_test_current_unit->title);
778 xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
779 test->expected_failure = 1;
782 void xbt_test_skip(void)
784 xbt_test_test_t test;
786 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
787 "Test skiped even before being declared (broken unit: %s)",
788 _xbt_test_current_unit->title);
791 xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
795 /* annotate test case with log message only */
796 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
798 xbt_test_unit_t unit = _xbt_test_current_unit;
799 xbt_test_test_t test;
806 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
807 "Test logged into even before being declared (broken test unit: %s)",
810 log = xbt_new(struct s_xbt_test_log, 1);
812 log->text = bvprintf(fmt, ap);
817 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
818 xbt_dynar_push(test->logs, &log);
826 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
828 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
830 xbt_test_add("Skipped test");
833 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
834 xbt_test_expect_failure();
835 xbt_test_log("%s %s", "Test", "log");
836 xbt_test_fail("EXPECTED FAILURE");
839 #endif /* SIMGRID_TEST */