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, 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);
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 ? "" : "; "),
476 fprintf(stderr, ")\n");
478 return suite->unit_failed;
481 static void apply_selection(char *selection)
483 /* for the parsing */
484 char *sel = selection;
487 char dir[1024]; /* the directive */
489 unsigned int it_suite;
490 xbt_test_suite_t suite;
491 xbt_test_unit_t unit;
492 unsigned int it_unit;
497 if (!selection || selection[0] == '\0')
500 /*printf("Test selection: %s\n", selection); */
502 /* First apply the selection */
506 p = strchr(sel, ',');
508 strncpy(dir, sel, p - sel);
518 memmove(dir, dir + 1, strlen(dir));
522 memmove(dir, dir + 1, strlen(dir));
525 p = strchr(dir, ':');
527 strcpy(unitname, p + 1);
528 strncpy(suitename, dir, p - dir);
529 suitename[p - dir] = '\0';
531 strcpy(suitename, dir);
534 /*fprintf(stderr,"Seen %s (%s; suite=%s; unit=%s)\n",
535 dir,enabling?"enabling":"disabling", suitename, unitname); */
537 /* Deal with the specific case of 'all' pseudo serie */
538 if (!strcmp("all", suitename)) {
539 if (unitname[0] != '\0') {
541 "The 'all' pseudo-suite does not accept any unit specification\n");
545 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
546 xbt_dynar_foreach(suite->units, it_unit, unit) {
547 unit->enabled = enabling;
549 suite->enabled = enabling;
553 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
554 xbt_test_suite_t thissuite =
555 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
556 if (!strcmp(suitename, thissuite->name)) {
557 /* Do not disable the whole suite when we just want to disable a child */
558 if (enabling || (unitname[0] == '\0'))
559 thissuite->enabled = enabling;
561 if (unitname[0] == '\0') {
562 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
563 unit->enabled = enabling;
565 } else { /* act on one child only */
566 unsigned int it2_unit;
567 /* search it, first (we won't reuse it for external loop which gets broken) */
569 it2_unit < xbt_dynar_length(thissuite->units);
571 xbt_test_unit_t thisunit =
572 xbt_dynar_get_as(thissuite->units, it2_unit,
574 if (!strcmp(thisunit->name, unitname)) {
575 thisunit->enabled = enabling;
578 } /* search relevant unit */
579 if (it2_unit == xbt_dynar_length(thissuite->units)) {
581 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n",
582 suitename, unitname);
585 } /* act on childs (either all or one) */
587 break; /* found the relevant serie. We are happy */
589 } /* search relevant series */
590 if (it == xbt_dynar_length(_xbt_test_suites)) {
592 "No suite of name '%s' found. Cannot apply the selection\n",
601 void xbt_test_dump(char *selection)
603 apply_selection(selection);
605 if (_xbt_test_suites) {
606 unsigned int it_suite;
607 xbt_test_suite_t suite;
609 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
610 xbt_test_suite_dump(suite);
612 printf(" No suite defined.");
616 int xbt_test_run(char *selection, int verbosity)
618 apply_selection(selection);
620 if (_xbt_test_suites) {
621 unsigned int it_suite;
622 xbt_test_suite_t suite;
625 /* Run all the suites */
626 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
627 xbt_test_suite_run(suite, verbosity);
629 /* Display some more statistics */
630 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
633 (double) _xbt_test_suite_failed /
634 (double) _xbt_test_nb_suites) * 100.0)
635 : 100.0, _xbt_test_nb_suites);
636 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
637 fprintf(stderr, "%d ok",
638 _xbt_test_nb_suites - _xbt_test_suite_failed);
641 if (_xbt_test_suite_failed) {
642 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
643 _xbt_test_suite_failed);
647 if (_xbt_test_suite_ignore) {
648 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
649 _xbt_test_suite_ignore);
652 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
655 (double) _xbt_test_unit_failed /
656 (double) _xbt_test_nb_units) * 100.0) : 100.0,
659 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
660 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
661 _xbt_test_nb_units - _xbt_test_unit_failed);
664 if (_xbt_test_unit_failed) {
665 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
666 _xbt_test_unit_failed);
669 if (_xbt_test_unit_ignore) {
670 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
671 _xbt_test_unit_ignore);
674 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
677 (double) _xbt_test_test_failed /
678 (double) _xbt_test_nb_tests) * 100.0) : 100.0,
681 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
682 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
683 _xbt_test_nb_tests - _xbt_test_test_failed);
686 if (_xbt_test_test_failed) {
687 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
688 _xbt_test_test_failed);
691 if (_xbt_test_test_ignore) {
692 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
693 _xbt_test_test_ignore);
696 if (_xbt_test_test_expect) {
697 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "),
698 _xbt_test_test_expect);
701 fprintf(stderr, ")\n");
703 fprintf(stderr, "No unit to run!\n");
704 _xbt_test_unit_failed++;
706 return _xbt_test_unit_failed;
709 void xbt_test_exit(void)
711 xbt_dynar_free(&_xbt_test_suites);
714 /* annotate test case with test */
715 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
717 xbt_test_unit_t unit = _xbt_test_current_unit;
718 xbt_test_test_t test;
724 test = xbt_new0(struct s_xbt_test_test, 1);
726 test->title = bvprintf(fmt, ap);
729 test->expected_failure = 0;
733 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
734 xbt_dynar_push(unit->tests, &test);
738 /* annotate test case with log message and failure */
739 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
741 xbt_test_unit_t unit = _xbt_test_current_unit;
742 xbt_test_test_t test;
749 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
750 "Test failed even before being declared (broken unit: %s)",
753 log = xbt_new(struct s_xbt_test_log, 1);
755 log->text = bvprintf(fmt, ap);
760 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
761 xbt_dynar_push(test->logs, &log);
766 void xbt_test_exception(xbt_ex_t e)
768 _xbt_test_fail(e.file, e.line, "Exception %s raised: %s",
769 xbt_ex_catname(e.category), e.msg);
772 void xbt_test_expect_failure(void)
774 xbt_test_test_t test;
775 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
776 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
777 _xbt_test_current_unit->title);
779 xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
780 test->expected_failure = 1;
783 void xbt_test_skip(void)
785 xbt_test_test_t test;
787 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
788 "Test skiped even before being declared (broken unit: %s)",
789 _xbt_test_current_unit->title);
792 xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
796 /* annotate test case with log message only */
797 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
799 xbt_test_unit_t unit = _xbt_test_current_unit;
800 xbt_test_test_t test;
807 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
808 "Test logged into even before being declared (broken test unit: %s)",
811 log = xbt_new(struct s_xbt_test_log, 1);
813 log->text = bvprintf(fmt, ap);
818 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
819 xbt_dynar_push(test->logs, &log);
827 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
829 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
831 xbt_test_add("Skipped test");
834 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
835 xbt_test_expect_failure();
836 xbt_test_log("%s %s", "Test", "log");
837 xbt_test_fail("EXPECTED FAILURE");
840 #endif /* SIMGRID_TEST */