1 /* cunit - A little C Unit facility */
3 /* Copyright (c) 2005-2014. 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) */
11 #include "src/portable.h"
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);
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 ? "" : "; "),
474 fprintf(stderr, ")\n");
476 return suite->unit_failed;
479 static void apply_selection(char *selection)
481 /* for the parsing */
482 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 char *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);
532 /* Deal with the specific case of 'all' pseudo serie */
533 if (!strcmp("all", suitename)) {
534 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
536 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
537 xbt_dynar_foreach(suite->units, it_unit, unit) {
538 unit->enabled = enabling;
540 suite->enabled = enabling;
544 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
545 xbt_test_suite_t thissuite =
546 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
547 if (!strcmp(suitename, thissuite->name)) {
548 /* Do not disable the whole suite when we just want to disable a child */
549 if (enabling || (unitname[0] == '\0'))
550 thissuite->enabled = enabling;
552 if (unitname[0] == '\0') {
553 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
554 unit->enabled = enabling;
556 } else { /* act on one child only */
557 unsigned int it2_unit;
558 /* search it, first (we won't reuse it for external loop which gets broken) */
560 it2_unit < xbt_dynar_length(thissuite->units);
562 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
563 if (!strcmp(thisunit->name, unitname)) {
564 thisunit->enabled = enabling;
567 } /* search relevant unit */
568 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
569 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
570 } /* act on childs (either all or one) */
572 break; /* found the relevant serie. We are happy */
574 } /* search relevant series */
575 xbt_assert(it != xbt_dynar_length(_xbt_test_suites), "No suite of name '%s' found. Cannot apply the selection\n", suitename);
581 void xbt_test_dump(char *selection)
583 apply_selection(selection);
585 if (_xbt_test_suites) {
586 unsigned int it_suite;
587 xbt_test_suite_t suite;
589 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
590 xbt_test_suite_dump(suite);
592 printf(" No suite defined.");
596 int xbt_test_run(char *selection, int verbosity)
598 apply_selection(selection);
600 if (_xbt_test_suites) {
601 unsigned int it_suite;
602 xbt_test_suite_t suite;
605 /* Run all the suites */
606 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
607 xbt_test_suite_run(suite, verbosity);
609 /* Display some more statistics */
610 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",
613 (double) _xbt_test_suite_failed /
614 (double) _xbt_test_nb_suites) * 100.0)
615 : 100.0, _xbt_test_nb_suites);
616 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
617 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
620 if (_xbt_test_suite_failed) {
621 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
625 if (_xbt_test_suite_ignore) {
626 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
628 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ",
631 (double) _xbt_test_unit_failed /
632 (double) _xbt_test_nb_units) * 100.0) : 100.0,
635 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
636 fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_units - _xbt_test_unit_failed);
639 if (_xbt_test_unit_failed) {
640 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
643 if (_xbt_test_unit_ignore) {
644 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
646 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ",
649 (double) _xbt_test_test_failed /
650 (double) _xbt_test_nb_tests) * 100.0) : 100.0,
653 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
654 fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_tests - _xbt_test_test_failed);
657 if (_xbt_test_test_failed) {
658 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
661 if (_xbt_test_test_ignore) {
662 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
665 if (_xbt_test_test_expect) {
666 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
669 fprintf(stderr, ")\n");
671 fprintf(stderr, "No unit to run!\n");
672 _xbt_test_unit_failed++;
674 return _xbt_test_unit_failed;
677 void xbt_test_exit(void)
679 xbt_dynar_free(&_xbt_test_suites);
682 /* annotate test case with test */
683 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
685 xbt_test_unit_t unit = _xbt_test_current_unit;
689 xbt_test_test_t test = xbt_new0(struct s_xbt_test_test, 1);
691 test->title = bvprintf(fmt, ap);
694 test->expected_failure = 0;
698 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
699 xbt_dynar_push(unit->tests, &test);
703 /* annotate test case with log message and failure */
704 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
706 xbt_test_unit_t unit = _xbt_test_current_unit;
708 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
709 "Test failed even before being declared (broken unit: %s)", unit->title);
712 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
714 log->text = bvprintf(fmt, ap);
719 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
720 xbt_dynar_push(test->logs, &log);
725 void xbt_test_exception(xbt_ex_t e)
727 _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.msg);
730 void xbt_test_expect_failure(void)
732 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
733 "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
734 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
735 test->expected_failure = 1;
738 void xbt_test_skip(void)
740 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
741 "Test skiped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
742 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
746 /* annotate test case with log message only */
747 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
749 xbt_test_unit_t unit = _xbt_test_current_unit;
751 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
752 "Test logged into even before being declared (broken test unit: %s)", unit->title);
755 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
757 log->text = bvprintf(fmt, ap);
762 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
763 xbt_dynar_push(test->logs, &log);
769 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
771 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
773 xbt_test_add("Skipped test");
776 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
777 xbt_test_expect_failure();
778 xbt_test_log("%s %s", "Test", "log");
779 xbt_test_fail("EXPECTED FAILURE");
782 #endif /* SIMGRID_TEST */