1 /* cunit - A little C Unit facility */
3 /* Copyright (c) 2005-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 /* This is partially inspirated from the OSSP ts (Test Suite Library) */
9 /* At some point we should use https://github.com/google/googletest instead */
11 #include "src/internal_config.h"
15 #include "xbt/sysdep.h" /* bvprintf */
16 #include "xbt/cunit.h"
17 #include "xbt/dynar.h"
21 /* collection of all suites */
22 static xbt_dynar_t _xbt_test_suites = nullptr;
23 /* global statistics */
24 static int _xbt_test_nb_tests = 0;
25 static int _xbt_test_test_failed = 0;
26 static int _xbt_test_test_ignore = 0;
27 static int _xbt_test_test_expect = 0;
29 static int _xbt_test_nb_units = 0;
30 static int _xbt_test_unit_failed = 0;
31 static int _xbt_test_unit_ignore = 0;
32 static int _xbt_test_unit_disabled = 0;
34 static int _xbt_test_nb_suites = 0;
35 static int _xbt_test_suite_failed = 0;
36 static int _xbt_test_suite_ignore = 0;
37 static int _xbt_test_suite_disabled = 0;
40 xbt_test_unit_t _xbt_test_current_unit = nullptr;
42 /* test suite test log */
43 typedef struct s_xbt_test_log {
49 static void xbt_test_log_dump(xbt_test_log_t log)
52 fprintf(stderr, " log %p(%s:%d)=%s\n", log, log->file, log->line, log->text);
54 fprintf(stderr, " log=nullptr\n");
57 /* test suite test check */
58 typedef struct s_xbt_test_test {
68 static void xbt_test_test_dump(xbt_test_test_t test)
73 fprintf(stderr, " test %p(%s:%d)=%s (%s)\n", 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=nullptr\n");
81 /* test suite test unit */
82 struct s_xbt_test_unit {
89 xbt_dynar_t tests; /* of xbt_test_test_t */
97 static void xbt_test_unit_dump(xbt_test_unit_t unit)
100 fprintf(stderr, " UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
102 xbt_test_test_t test;
103 unsigned int it_test;
104 xbt_dynar_foreach(unit->tests, it_test, test)
105 xbt_test_test_dump(test);
108 fprintf(stderr, " unit=nullptr\n");
113 struct s_xbt_test_suite {
117 xbt_dynar_t units; /* of xbt_test_unit_t */
129 /* destroy test suite */
130 static void xbt_test_suite_free(void *s)
132 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
134 if (suite == nullptr)
136 xbt_dynar_free(&suite->units);
141 static void xbt_test_unit_free(void *unit)
143 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
146 xbt_dynar_free(&u->tests);
150 static void xbt_test_test_free(void *test)
152 xbt_test_test_t t = *(xbt_test_test_t *) test;
154 xbt_dynar_free(&(t->logs));
158 static void xbt_test_log_free(void *log)
160 xbt_test_log_t l = *(xbt_test_log_t *) log;
165 /** @brief create test suite */
166 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
168 xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
171 if (_xbt_test_suites == nullptr)
172 _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
175 suite->title = bvprintf(fmt, ap);
176 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
181 xbt_dynar_push(_xbt_test_suites, &suite);
186 /** @brief retrieve a testsuite from name, or create a new one */
187 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
189 xbt_test_suite_t suite;
193 if (_xbt_test_suites) {
194 unsigned int it_suite;
195 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
196 if (not strcmp(suite->name, name))
201 bufname = bvprintf(fmt, ap);
203 suite = xbt_test_suite_new(name, bufname, nullptr);
209 void xbt_test_suite_dump(xbt_test_suite_t suite)
212 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
213 if (suite->enabled) {
214 xbt_test_unit_t unit;
215 unsigned int it_unit;
216 xbt_dynar_foreach(suite->units, it_unit, unit)
217 xbt_test_unit_dump(unit);
220 fprintf(stderr, "TESTSUITE IS NULL!\n");
224 /* add test case to test suite */
225 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
227 xbt_test_unit_t unit;
234 unit = xbt_new0(struct s_xbt_test_unit, 1);
236 unit->title = bvprintf(fmt, ap);
238 unit->name = (char *) name;
240 unit->file = nullptr;
243 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
245 xbt_dynar_push(suite->units, &unit);
248 /* run test one suite */
249 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
251 xbt_test_unit_t unit;
252 xbt_test_test_t test;
255 if (suite == nullptr)
258 /* suite title pretty-printing */
259 char suite_title[81];
260 int suite_len = strlen(suite->title);
262 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
264 suite_title[0] = ' ';
265 for (int i = 1; i < 79; i++)
266 suite_title[i] = '=';
267 suite_title[79] = '\n';
268 suite_title[80] = '\0';
270 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
271 suite_title[40 + (suite_len + 5) / 2] = '=';
272 if (not suite->enabled)
273 snprintf(suite_title + 70, 11, " DISABLED ");
274 fprintf(stderr, "\n%s\n", suite_title);
276 if (suite->enabled) {
277 /* iterate through all tests */
278 unsigned int it_unit;
279 xbt_dynar_foreach(suite->units, it_unit, unit) {
280 /* init unit case counters */
282 unit->test_ignore = 0;
283 unit->test_failed = 0;
284 unit->test_expect = 0;
286 /* display unit title */
287 char* cp = bprintf(" Unit: %s ......................................"
288 "......................................",
291 fprintf(stderr, "%s", cp);
294 /* run the test case function */
295 _xbt_test_current_unit = unit;
299 /* iterate through all performed tests to determine status */
300 unsigned int it_test;
301 xbt_dynar_foreach(unit->tests, it_test, test) {
307 if (test->failed && not test->expected_failure)
309 if (not test->failed && test->expected_failure)
311 if (test->expected_failure)
315 /* Display whether this unit went well */
316 if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
317 /* some tests failed (or were supposed to), so do detailed reporting of test case */
318 if (unit->test_failed > 0) {
319 fprintf(stderr, ".. failed\n");
320 } else if (unit->nb_tests) {
321 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
323 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
325 xbt_dynar_foreach(unit->tests, it_test, test) {
326 const char* file = (test->file != nullptr ? test->file : unit->file);
327 int line = (test->line != 0 ? test->line : unit->line);
331 else if (test->expected_failure) {
342 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test->title, file, line);
344 if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
346 xbt_dynar_foreach(test->logs, it_log, log) {
347 file = (log->file != nullptr ? log->file : file);
348 line = (log->line != 0 ? log->line : line);
349 fprintf(stderr, " %s:%d: %s\n", file, line, log->text);
353 fprintf(stderr, " Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
354 if (unit->test_ignore) {
355 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
357 fprintf(stderr, "\n");
359 } else if (not unit->enabled) {
360 fprintf(stderr, " disabled\n"); /* no test were run */
361 } else if (unit->nb_tests) {
362 fprintf(stderr, "...... ok\n"); /* successful */
364 fprintf(stderr, ".... skip\n"); /* no test were run */
367 /* Accumulate test counts into the suite */
368 suite->nb_tests += unit->nb_tests;
369 suite->test_failed += unit->test_failed;
370 suite->test_ignore += unit->test_ignore;
371 suite->test_expect += unit->test_expect;
373 _xbt_test_nb_tests += unit->nb_tests;
374 _xbt_test_test_failed += unit->test_failed;
375 _xbt_test_test_ignore += unit->test_ignore;
376 _xbt_test_test_expect += unit->test_expect;
378 /* What's the conclusion of this test anyway? */
379 if (unit->nb_tests) {
381 if (unit->test_failed)
382 suite->unit_failed++;
383 } else if (not unit->enabled) {
384 suite->unit_disabled++;
386 suite->unit_ignore++;
390 _xbt_test_nb_units += suite->nb_units;
391 _xbt_test_unit_failed += suite->unit_failed;
392 _xbt_test_unit_ignore += suite->unit_ignore;
393 _xbt_test_unit_disabled += suite->unit_disabled;
395 if (suite->nb_units) {
396 _xbt_test_nb_suites++;
397 if (suite->test_failed)
398 _xbt_test_suite_failed++;
399 } else if (not suite->enabled) {
400 _xbt_test_suite_disabled++;
402 _xbt_test_suite_ignore++;
405 /* print test suite summary */
406 if (suite->enabled) {
407 int first = 1; /* for result pretty printing */
409 fprintf(stderr," =====================================================================%s\n",
410 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
411 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
412 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
413 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
415 if (suite->nb_units != suite->unit_failed) {
416 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
419 if (suite->unit_failed) {
420 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
423 if (suite->unit_ignore) {
424 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
427 if (suite->unit_disabled) {
428 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
430 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
431 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
434 if (suite->nb_tests != suite->test_failed) {
435 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
438 if (suite->test_failed) {
439 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
442 if (suite->test_ignore) {
443 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
446 if (suite->test_expect) {
447 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
449 fprintf(stderr, ")\n");
451 return suite->unit_failed;
454 static void apply_selection(char *selection)
456 /* for the parsing */
457 char *sel = selection;
459 char dir[STRLEN]; /* the directive */
461 unsigned int it_suite;
462 xbt_test_suite_t suite;
463 xbt_test_unit_t unit;
464 unsigned int it_unit;
466 char suitename[STRLEN];
467 char unitname[STRLEN];
469 if (not selection || selection[0] == '\0')
472 /*printf("Test selection: %s\n", selection); */
474 /* First apply the selection */
478 char *p = strchr(sel, ',');
480 snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
483 snprintf(dir, STRLEN, "%s", sel);
489 memmove(dir, dir + 1, strlen(dir));
493 memmove(dir, dir + 1, strlen(dir));
496 p = strchr(dir, ':');
498 snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
499 snprintf(unitname, STRLEN, "%s", p + 1);
501 snprintf(suitename, STRLEN, "%s", dir);
505 /* Deal with the specific case of 'all' pseudo serie */
506 if (not strcmp("all", suitename)) {
507 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
509 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
510 xbt_dynar_foreach(suite->units, it_unit, unit) {
511 unit->enabled = enabling;
513 suite->enabled = enabling;
517 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
518 xbt_test_suite_t thissuite =
519 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
520 if (not strcmp(suitename, thissuite->name)) {
521 /* Do not disable the whole suite when we just want to disable a child */
522 if (enabling || (unitname[0] == '\0'))
523 thissuite->enabled = enabling;
525 if (unitname[0] == '\0') {
526 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
527 unit->enabled = enabling;
529 } else { /* act on one child only */
530 unsigned int it2_unit;
531 /* search it, first (we won't reuse it for external loop which gets broken) */
533 it2_unit < xbt_dynar_length(thissuite->units);
535 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
536 if (not strcmp(thisunit->name, unitname)) {
537 thisunit->enabled = enabling;
540 } /* search relevant unit */
541 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
542 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
543 } /* act on childs (either all or one) */
545 break; /* found the relevant serie. We are happy */
547 } /* search relevant series */
548 xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
549 "No suite of name '%s' found. Cannot apply the selection\n", suitename);
554 void xbt_test_dump(char *selection)
556 apply_selection(selection);
558 if (_xbt_test_suites) {
559 unsigned int it_suite;
560 xbt_test_suite_t suite;
562 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
563 xbt_test_suite_dump(suite);
565 printf(" No suite defined.");
569 int xbt_test_run(char *selection, int verbosity)
571 apply_selection(selection);
573 if (_xbt_test_suites) {
574 unsigned int it_suite;
575 xbt_test_suite_t suite;
578 /* Run all the suites */
579 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
580 xbt_test_suite_run(suite, verbosity);
582 /* Display some more statistics */
583 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
584 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
585 : 100.0, _xbt_test_nb_suites);
586 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
587 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
590 if (_xbt_test_suite_failed) {
591 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
595 if (_xbt_test_suite_ignore) {
596 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
598 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
599 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
601 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
602 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
605 if (_xbt_test_unit_failed) {
606 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
609 if (_xbt_test_unit_ignore) {
610 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
612 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
613 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
615 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
616 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
619 if (_xbt_test_test_failed) {
620 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
623 if (_xbt_test_test_ignore) {
624 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
627 if (_xbt_test_test_expect) {
628 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
631 fprintf(stderr, ")\n");
633 fprintf(stderr, "No unit to run!\n");
634 _xbt_test_unit_failed++;
636 return _xbt_test_unit_failed;
641 xbt_dynar_free(&_xbt_test_suites);
644 /* annotate test case with test */
645 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
647 xbt_test_unit_t unit = _xbt_test_current_unit;
651 xbt_test_test_t test = xbt_new0(struct s_xbt_test_test, 1);
653 test->title = bvprintf(fmt, ap);
656 test->expected_failure = 0;
660 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
661 xbt_dynar_push(unit->tests, &test);
664 /* annotate test case with log message and failure */
665 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
667 xbt_test_unit_t unit = _xbt_test_current_unit;
669 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
670 "Test failed even before being declared (broken unit: %s)", unit->title);
673 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
675 log->text = bvprintf(fmt, ap);
680 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
681 xbt_dynar_push(test->logs, &log);
686 void xbt_test_exception(xbt_ex_t e)
688 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
691 void xbt_test_expect_failure()
693 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
694 "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
695 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
696 test->expected_failure = 1;
701 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
702 "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
703 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
707 /* annotate test case with log message only */
708 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
710 xbt_test_unit_t unit = _xbt_test_current_unit;
712 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
713 "Test logged into even before being declared (broken test unit: %s)", unit->title);
716 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
718 log->text = bvprintf(fmt, ap);
723 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
724 xbt_dynar_push(test->logs, &log);
728 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
730 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
732 xbt_test_add("Skipped test");
735 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
736 xbt_test_expect_failure();
737 xbt_test_log("%s %s", "Test", "log");
738 xbt_test_fail("EXPECTED FAILURE");
740 #endif /* SIMGRID_TEST */