3 /* cunit - A little C Unit facility */
5 /* Copyright (c) 2005 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 /* This is partially inspirated from the OSSP ts (Test Suite Library) */
14 #include "xbt/sysdep.h" /* vasprintf */
15 #include "xbt/cunit.h"
16 #include "xbt/dynar.h"
18 /* collection of all suites */
19 static xbt_dynar_t _xbt_test_suites = NULL;
20 /* global statistics */
21 static int _xbt_test_nb_tests = 0;
22 static int _xbt_test_test_failed = 0;
23 static int _xbt_test_test_ignore = 0;
24 static int _xbt_test_test_expect = 0;
26 static int _xbt_test_nb_units = 0;
27 static int _xbt_test_unit_failed = 0;
28 static int _xbt_test_unit_ignore = 0;
29 static int _xbt_test_unit_disabled = 0;
31 static int _xbt_test_nb_suites = 0;
32 static int _xbt_test_suite_failed = 0;
33 static int _xbt_test_suite_ignore = 0;
34 static int _xbt_test_suite_disabled = 0;
37 xbt_test_unit_t _xbt_test_current_unit = NULL;
40 /* test suite test log */
41 typedef struct s_xbt_test_log {
47 static void xbt_test_log_dump(xbt_test_log_t log)
50 fprintf(stderr, " log %p(%s:%d)=%s\n", log, log->file, log->line,
53 fprintf(stderr, " log=NULL\n");
56 /* test suite test check */
57 typedef struct s_xbt_test_test {
67 static void xbt_test_test_dump(xbt_test_test_t test)
72 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n",
73 test, test->file, test->line, test->title,
74 test->failed ? "failed" : "not failed");
75 xbt_dynar_foreach(test->logs, it_log, log)
76 xbt_test_log_dump(log);
78 fprintf(stderr, " test=NULL\n");
81 /* test suite test unit */
82 struct s_xbt_test_unit {
89 xbt_dynar_t tests; /* of xbt_test_test_t */
92 int test_failed, test_ignore, test_expect;
95 static void xbt_test_unit_dump(xbt_test_unit_t unit)
100 fprintf(stderr, " UNIT %s: %s (%s)\n",
101 unit->name, unit->title,
102 (unit->enabled ? "enabled" : "disabled"));
104 xbt_dynar_foreach(unit->tests, it_test, test)
105 xbt_test_test_dump(test);
107 fprintf(stderr, " unit=NULL\n");
112 struct s_xbt_test_suite {
116 xbt_dynar_t units; /* of xbt_test_unit_t */
118 int nb_tests, nb_units;
119 int test_failed, test_ignore, test_expect;
120 int unit_failed, unit_ignore, unit_disabled;
123 /* destroy test suite */
124 static void xbt_test_suite_free(void *s)
126 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
130 xbt_dynar_free(&suite->units);
135 static void xbt_test_unit_free(void *unit)
137 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
140 xbt_dynar_free(&u->tests);
144 static void xbt_test_test_free(void *test)
146 xbt_test_test_t t = *(xbt_test_test_t *) test;
148 xbt_dynar_free(&(t->logs));
152 static void xbt_test_log_free(void *log)
154 xbt_test_log_t l = *(xbt_test_log_t *) log;
159 /** @brief create test suite */
160 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
162 xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
166 if (!_xbt_test_suites)
168 xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
171 vres = vasprintf(&suite->title, fmt, ap);
172 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
177 xbt_dynar_push(_xbt_test_suites, &suite);
182 /** @brief retrieve a testsuite from name, or create a new one */
183 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt,
186 xbt_test_suite_t suite;
187 unsigned int it_suite;
193 if (_xbt_test_suites)
194 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
195 if (!strcmp(suite->name, name))
199 vres = vasprintf(&bufname, fmt, ap);
201 suite = xbt_test_suite_new(name, bufname, NULL);
207 void xbt_test_suite_dump(xbt_test_suite_t suite)
210 xbt_test_unit_t unit;
211 unsigned int it_unit;
212 fprintf(stderr, "TESTSUITE %s: %s (%s)\n",
213 suite->name, suite->title,
214 suite->enabled ? "enabled" : "disabled");
216 xbt_dynar_foreach(suite->units, it_unit, unit)
217 xbt_test_unit_dump(unit);
219 fprintf(stderr, "TESTSUITE IS NULL!\n");
223 /* add test case to test suite */
224 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name,
225 ts_test_cb_t func, const char *fmt, ...)
227 xbt_test_unit_t unit;
235 unit = xbt_new0(struct s_xbt_test_unit, 1);
237 vres = vasprintf(&unit->title, fmt, ap);
239 unit->name = (char *) name;
244 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
246 xbt_dynar_push(suite->units, &unit);
250 /* run test one suite */
251 static int xbt_test_suite_run(xbt_test_suite_t suite)
253 xbt_test_unit_t unit;
254 xbt_test_test_t test;
260 unsigned int it_unit, it_test, it_log;
262 int first = 1; /* for result pretty printing */
268 /* suite title pretty-printing */
270 char suite_title[81];
271 int suite_len = strlen(suite->title);
274 xbt_assert2(suite_len < 68,
275 "suite title \"%s\" too long (%d should be less than 68",
276 suite->title, suite_len);
278 suite_title[0] = ' ';
279 for (i = 1; i < 80; i++)
280 suite_title[i] = '=';
281 suite_title[i++] = '\n';
282 suite_title[80] = '\0';
284 sprintf(suite_title + 40 - (suite_len + 4) / 2, "[ %s ]", suite->title);
285 suite_title[40 + (suite_len + 5) / 2] = '=';
287 snprintf(suite_title + 70, 11, " DISABLED ");
288 fprintf(stderr, "\n%s\n", suite_title);
291 if (suite->enabled) {
292 /* iterate through all tests */
293 xbt_dynar_foreach(suite->units, it_unit, unit) {
294 /* init unit case counters */
296 unit->test_ignore = 0;
297 unit->test_failed = 0;
298 unit->test_expect = 0;
300 /* display unit title */
301 vres = asprintf(&cp, " Unit: %s ......................................"
302 "......................................", unit->title);
304 fprintf(stderr, "%s", cp);
307 /* run the test case function */
308 _xbt_test_current_unit = unit;
312 /* iterate through all performed tests to determine status */
313 xbt_dynar_foreach(unit->tests, it_test, test) {
319 if (test->failed && !test->expected_failure)
321 if (!test->failed && test->expected_failure)
323 if (test->expected_failure)
329 /* Display whether this unit went well */
330 if (unit->test_failed > 0 || unit->test_expect) {
331 /* some tests failed (or were supposed to), so do detailed reporting of test case */
332 if (unit->test_failed > 0) {
333 fprintf(stderr, ".. failed\n");
334 } else if (unit->nb_tests) {
335 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
337 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
339 xbt_dynar_foreach(unit->tests, it_test, test) {
340 file = (test->file != NULL ? test->file : unit->file);
341 line = (test->line != 0 ? test->line : unit->line);
342 fprintf(stderr, " %s: %s [%s:%d]\n",
343 (test->ignored ? " SKIP"
344 : (test->expected_failure
345 ? (test->failed ? "EFAIL" : "EPASS") : (test->failed ?
348 test->title, file, line);
350 if ((test->expected_failure && !test->failed)
351 || (!test->expected_failure && test->failed)) {
352 xbt_dynar_foreach(test->logs, it_log, log) {
353 file = (log->file != NULL ? log->file : file);
354 line = (log->line != 0 ? log->line : line);
355 fprintf(stderr, " %s:%d: %s\n",
356 file, line, log->text);
361 fprintf(stderr, " Summary: %d of %d tests failed",
362 unit->test_failed, unit->nb_tests);
363 if (unit->test_ignore) {
364 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
366 fprintf(stderr, "\n");
369 } else if (!unit->enabled) {
370 fprintf(stderr, " disabled\n"); /* no test were run */
371 } else if (unit->nb_tests) {
372 fprintf(stderr, "...... ok\n"); /* successful */
374 fprintf(stderr, ".... skip\n"); /* no test were run */
377 /* Accumulate test counts into the suite */
378 suite->nb_tests += unit->nb_tests;
379 suite->test_failed += unit->test_failed;
380 suite->test_ignore += unit->test_ignore;
381 suite->test_expect += unit->test_expect;
383 _xbt_test_nb_tests += unit->nb_tests;
384 _xbt_test_test_failed += unit->test_failed;
385 _xbt_test_test_ignore += unit->test_ignore;
386 _xbt_test_test_expect += unit->test_expect;
388 /* What's the conclusion of this test anyway? */
389 if (unit->nb_tests) {
391 if (unit->test_failed)
392 suite->unit_failed++;
393 } else if (!unit->enabled) {
394 suite->unit_disabled++;
396 suite->unit_ignore++;
400 _xbt_test_nb_units += suite->nb_units;
401 _xbt_test_unit_failed += suite->unit_failed;
402 _xbt_test_unit_ignore += suite->unit_ignore;
403 _xbt_test_unit_disabled += suite->unit_disabled;
405 if (suite->nb_units) {
406 _xbt_test_nb_suites++;
407 if (suite->test_failed)
408 _xbt_test_suite_failed++;
409 } else if (!suite->enabled) {
410 _xbt_test_suite_disabled++;
412 _xbt_test_suite_ignore++;
416 /* print test suite summary */
417 if (suite->enabled) {
420 " =====================================================================%s\n",
422 ? (suite->unit_failed ? "== FAILED" : "====== OK")
423 : (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
424 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ",
427 (double) suite->unit_failed / (double) suite->nb_units) *
428 100.0) : 100.0, suite->nb_units);
430 if (suite->nb_units != suite->unit_failed) {
431 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
432 suite->nb_units - suite->unit_failed);
435 if (suite->unit_failed) {
436 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
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 ? "" : ", "), suite->test_failed);
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) */
567 for (it2_unit = 0; it2_unit < xbt_dynar_length(thissuite->units);
569 xbt_test_unit_t thisunit =
570 xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
571 if (!strcmp(thisunit->name, unitname)) {
572 thisunit->enabled = enabling;
575 } /* search relevant unit */
576 if (it2_unit == xbt_dynar_length(thissuite->units)) {
578 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n",
579 suitename, unitname);
582 } /* act on childs (either all or one) */
584 break; /* found the relevant serie. We are happy */
586 } /* search relevant series */
587 if (it == xbt_dynar_length(_xbt_test_suites)) {
589 "No suite of name '%s' found. Cannot apply the selection\n",
598 void xbt_test_dump(char *selection)
600 apply_selection(selection);
602 if (_xbt_test_suites) {
603 unsigned int it_suite;
604 xbt_test_suite_t suite;
606 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
607 xbt_test_suite_dump(suite);
609 printf(" No suite defined.");
613 int xbt_test_run(char *selection)
615 apply_selection(selection);
617 if (_xbt_test_suites) {
618 unsigned int it_suite;
619 xbt_test_suite_t suite;
622 /* Run all the suites */
623 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
624 xbt_test_suite_run(suite);
626 /* Display some more statistics */
627 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
630 (double) _xbt_test_suite_failed /
631 (double) _xbt_test_nb_suites) * 100.0)
632 : 100.0, _xbt_test_nb_suites);
633 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
634 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
637 if (_xbt_test_suite_failed) {
638 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
639 _xbt_test_suite_failed);
643 if (_xbt_test_suite_ignore) {
644 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
645 _xbt_test_suite_ignore);
648 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
651 (double) _xbt_test_unit_failed /
652 (double) _xbt_test_nb_units) * 100.0) : 100.0,
655 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
656 fprintf(stderr, "%s%d ok", (first ? "" : ", "),
657 _xbt_test_nb_units - _xbt_test_unit_failed);
660 if (_xbt_test_unit_failed) {
661 fprintf(stderr, "%s%d failed", (first ? "" : ", "),
662 _xbt_test_unit_failed);
665 if (_xbt_test_unit_ignore) {
666 fprintf(stderr, "%s%d ignored", (first ? "" : ", "),
667 _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;
721 test = xbt_new0(struct s_xbt_test_test, 1);
723 vres = vasprintf(&test->title, fmt, ap);
726 test->expected_failure = 0;
730 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
731 xbt_dynar_push(unit->tests, &test);
735 /* annotate test case with log message and failure */
736 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
738 xbt_test_unit_t unit = _xbt_test_current_unit;
739 xbt_test_test_t test;
747 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
748 "Test failed even before being declared (broken unit: %s)",
751 log = xbt_new(struct s_xbt_test_log, 1);
753 vres = vasprintf(&log->text, fmt, ap);
758 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
759 xbt_dynar_push(test->logs, &log);
764 void xbt_test_exception(xbt_ex_t e)
766 _xbt_test_fail(e.file, e.line, "Exception %s raised: %s",
767 xbt_ex_catname(e.category), e.msg);
770 void xbt_test_expect_failure(void)
772 xbt_test_test_t test;
773 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
774 "Cannot expect the failure of a test before declaring it (broken unit: %s)",
775 _xbt_test_current_unit->title);
776 test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
777 test->expected_failure = 1;
780 void xbt_test_skip(void)
782 xbt_test_test_t test;
784 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
785 "Test skiped even before being declared (broken unit: %s)",
786 _xbt_test_current_unit->title);
788 test = 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;
804 xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
805 "Test logged into even before being declared (broken test unit: %s)",
808 log = xbt_new(struct s_xbt_test_log, 1);
810 vres = vasprintf(&log->text, fmt, ap);
815 test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
816 xbt_dynar_push(test->logs, &log);
824 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
826 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
828 xbt_test_add0("Skipped test");
831 xbt_test_add2("%s %s", "EXPECTED", "FAILURE");
832 xbt_test_expect_failure();
833 xbt_test_log2("%s %s", "Test", "log");
834 xbt_test_fail0("EXPECTED FAILURE");
837 #endif /* SIMGRID_TEST */