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" /* vasprintf */
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);
165 if (!_xbt_test_suites)
167 xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
170 vres = vasprintf(&suite->title, fmt, ap);
171 suite->units = 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;
192 if (_xbt_test_suites)
193 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
194 if (!strcmp(suite->name, name))
198 vres = vasprintf(&bufname, 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;
234 unit = xbt_new0(struct s_xbt_test_unit, 1);
236 vres = vasprintf(&unit->title, fmt, ap);
238 unit->name = (char *) name;
243 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
245 xbt_dynar_push(suite->units, &unit);
249 /* run test one suite */
250 static int xbt_test_suite_run(xbt_test_suite_t suite)
252 xbt_test_unit_t unit;
253 xbt_test_test_t test;
259 unsigned int it_unit, it_test, it_log;
261 int first = 1; /* for result pretty printing */
267 /* suite title pretty-printing */
269 char suite_title[81];
270 int suite_len = strlen(suite->title);
273 xbt_assert2(suite_len < 68,
274 "suite title \"%s\" too long (%d should be less than 68",
275 suite->title, suite_len);
277 suite_title[0] = ' ';
278 for (i = 1; i < 80; i++)
279 suite_title[i] = '=';
280 suite_title[i++] = '\n';
281 suite_title[80] = '\0';
283 sprintf(suite_title + 40 - (suite_len + 4) / 2, "[ %s ]", suite->title);
284 suite_title[40 + (suite_len + 5) / 2] = '=';
286 snprintf(suite_title + 70, 11, " DISABLED ");
287 fprintf(stderr, "\n%s\n", suite_title);
290 if (suite->enabled) {
291 /* iterate through all tests */
292 xbt_dynar_foreach(suite->units, it_unit, unit) {
293 /* init unit case counters */
295 unit->test_ignore = 0;
296 unit->test_failed = 0;
297 unit->test_expect = 0;
299 /* display unit title */
300 vres = asprintf(&cp, " Unit: %s ......................................"
301 "......................................", unit->title);
303 fprintf(stderr, "%s", cp);
306 /* run the test case function */
307 _xbt_test_current_unit = unit;
311 /* iterate through all performed tests to determine status */
312 xbt_dynar_foreach(unit->tests, it_test, test) {
318 if (test->failed && !test->expected_failure)
320 if (!test->failed && test->expected_failure)
322 if (test->expected_failure)
328 /* Display whether this unit went well */
329 if (unit->test_failed > 0 || unit->test_expect) {
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
344 ? (test->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 ? "" : ", "), suite->unit_failed);
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 ? "" : ", "), suite->test_failed);
464 if (suite->test_ignore) {
465 fprintf(stderr, "%s%d ignored", (first ? "" : "; "),
469 if (suite->test_expect) {
470 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) */
566 for (it2_unit = 0; it2_unit < xbt_dynar_length(thissuite->units);
568 xbt_test_unit_t thisunit =
569 xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
570 if (!strcmp(thisunit->name, unitname)) {
571 thisunit->enabled = enabling;
574 } /* search relevant unit */
575 if (it2_unit == xbt_dynar_length(thissuite->units)) {
577 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n",
578 suitename, unitname);
581 } /* act on childs (either all or one) */
583 break; /* found the relevant serie. We are happy */
585 } /* search relevant series */
586 if (it == xbt_dynar_length(_xbt_test_suites)) {
588 "No suite of name '%s' found. Cannot apply the selection\n",
597 void xbt_test_dump(char *selection)
599 apply_selection(selection);
601 if (_xbt_test_suites) {
602 unsigned int it_suite;
603 xbt_test_suite_t suite;
605 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
606 xbt_test_suite_dump(suite);
608 printf(" No suite defined.");
612 int xbt_test_run(char *selection)
614 apply_selection(selection);
616 if (_xbt_test_suites) {
617 unsigned int it_suite;
618 xbt_test_suite_t suite;
621 /* Run all the suites */
622 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
623 xbt_test_suite_run(suite);
625 /* Display some more statistics */
626 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
629 (double) _xbt_test_suite_failed /
630 (double) _xbt_test_nb_suites) * 100.0)
631 : 100.0, _xbt_test_nb_suites);
632 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
633 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
636 if (_xbt_test_suite_failed) {
637 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
638 _xbt_test_suite_failed);
642 if (_xbt_test_suite_ignore) {
643 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
644 _xbt_test_suite_ignore);
647 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
650 (double) _xbt_test_unit_failed /
651 (double) _xbt_test_nb_units) * 100.0) : 100.0,
654 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
655 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
656 _xbt_test_nb_units - _xbt_test_unit_failed);
659 if (_xbt_test_unit_failed) {
660 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
661 _xbt_test_unit_failed);
664 if (_xbt_test_unit_ignore) {
665 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
666 _xbt_test_unit_ignore);
669 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
672 (double) _xbt_test_test_failed /
673 (double) _xbt_test_nb_tests) * 100.0) : 100.0,
676 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
677 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
678 _xbt_test_nb_tests - _xbt_test_test_failed);
681 if (_xbt_test_test_failed) {
682 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
683 _xbt_test_test_failed);
686 if (_xbt_test_test_ignore) {
687 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
688 _xbt_test_test_ignore);
691 if (_xbt_test_test_expect) {
692 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "),
693 _xbt_test_test_expect);
696 fprintf(stderr, ")\n");
698 fprintf(stderr, "No unit to run!\n");
699 _xbt_test_unit_failed++;
701 return _xbt_test_unit_failed;
704 void xbt_test_exit(void)
706 xbt_dynar_free(&_xbt_test_suites);
709 /* annotate test case with test */
710 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
712 xbt_test_unit_t unit = _xbt_test_current_unit;
713 xbt_test_test_t test;
720 test = xbt_new0(struct s_xbt_test_test, 1);
722 vres = vasprintf(&test->title, 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;
746 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
747 "Test failed even before being declared (broken unit: %s)",
750 log = xbt_new(struct s_xbt_test_log, 1);
752 vres = vasprintf(&log->text, fmt, ap);
757 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
758 xbt_dynar_push(test->logs, &log);
763 void xbt_test_exception(xbt_ex_t e)
765 _xbt_test_fail(e.file, e.line, "Exception %s raised: %s",
766 xbt_ex_catname(e.category), e.msg);
769 void xbt_test_expect_failure(void)
771 xbt_test_test_t test;
772 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
773 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
774 _xbt_test_current_unit->title);
775 test = 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_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
784 "Test skiped even before being declared (broken unit: %s)",
785 _xbt_test_current_unit->title);
787 test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
791 /* annotate test case with log message only */
792 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
794 xbt_test_unit_t unit = _xbt_test_current_unit;
795 xbt_test_test_t test;
803 xbt_assert1(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 vres = vasprintf(&log->text, 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_add0("Skipped test");
830 xbt_test_add2("%s %s", "EXPECTED", "FAILURE");
831 xbt_test_expect_failure();
832 xbt_test_log2("%s %s", "Test", "log");
833 xbt_test_fail0("EXPECTED FAILURE");
836 #endif /* SIMGRID_TEST */