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"
19 /* collection of all suites */
20 static xbt_dynar_t _xbt_test_suites = nullptr;
21 /* global statistics */
22 static int _xbt_test_nb_tests = 0;
23 static int _xbt_test_test_failed = 0;
24 static int _xbt_test_test_ignore = 0;
25 static int _xbt_test_test_expect = 0;
27 static int _xbt_test_nb_units = 0;
28 static int _xbt_test_unit_failed = 0;
29 static int _xbt_test_unit_ignore = 0;
30 static int _xbt_test_unit_disabled = 0;
32 static int _xbt_test_nb_suites = 0;
33 static int _xbt_test_suite_failed = 0;
34 static int _xbt_test_suite_ignore = 0;
35 static int _xbt_test_suite_disabled = 0;
38 xbt_test_unit_t _xbt_test_current_unit = nullptr;
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, log->text);
52 fprintf(stderr, " log=nullptr\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", test, test->file, test->line, test->title,
72 test->failed ? "failed" : "not failed");
73 xbt_dynar_foreach(test->logs, it_log, log)
74 xbt_test_log_dump(log);
76 fprintf(stderr, " test=nullptr\n");
79 /* test suite test unit */
80 struct s_xbt_test_unit {
87 xbt_dynar_t tests; /* of xbt_test_test_t */
95 static void xbt_test_unit_dump(xbt_test_unit_t unit)
98 fprintf(stderr, " UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
100 xbt_test_test_t test;
101 unsigned int it_test;
102 xbt_dynar_foreach(unit->tests, it_test, test)
103 xbt_test_test_dump(test);
106 fprintf(stderr, " unit=nullptr\n");
111 struct s_xbt_test_suite {
115 xbt_dynar_t units; /* of xbt_test_unit_t */
127 /* destroy test suite */
128 static void xbt_test_suite_free(void *s)
130 xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
132 if (suite == nullptr)
134 xbt_dynar_free(&suite->units);
139 static void xbt_test_unit_free(void *unit)
141 xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
144 xbt_dynar_free(&u->tests);
148 static void xbt_test_test_free(void *test)
150 xbt_test_test_t t = *(xbt_test_test_t *) test;
152 xbt_dynar_free(&(t->logs));
156 static void xbt_test_log_free(void *log)
158 xbt_test_log_t l = *(xbt_test_log_t *) log;
163 /** @brief create test suite */
164 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
166 xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
169 if (_xbt_test_suites == nullptr)
170 _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
173 suite->title = bvprintf(fmt, ap);
174 suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
179 xbt_dynar_push(_xbt_test_suites, &suite);
184 /** @brief retrieve a testsuite from name, or create a new one */
185 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
187 xbt_test_suite_t suite;
191 if (_xbt_test_suites) {
192 unsigned int it_suite;
193 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
194 if (not strcmp(suite->name, name))
199 bufname = bvprintf(fmt, ap);
201 suite = xbt_test_suite_new(name, bufname, nullptr);
207 void xbt_test_suite_dump(xbt_test_suite_t suite)
210 fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
211 if (suite->enabled) {
212 xbt_test_unit_t unit;
213 unsigned int it_unit;
214 xbt_dynar_foreach(suite->units, it_unit, unit)
215 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, 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;
238 unit->file = nullptr;
241 unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
243 xbt_dynar_push(suite->units, &unit);
246 /* run test one suite */
247 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
249 xbt_test_unit_t unit;
250 xbt_test_test_t test;
253 if (suite == nullptr)
256 /* suite title pretty-printing */
257 char suite_title[81];
258 int suite_len = strlen(suite->title);
260 xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
262 suite_title[0] = ' ';
263 for (int i = 1; i < 79; i++)
264 suite_title[i] = '=';
265 suite_title[79] = '\n';
266 suite_title[80] = '\0';
268 snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
269 suite_title[40 + (suite_len + 5) / 2] = '=';
270 if (not suite->enabled)
271 snprintf(suite_title + 70, 11, " DISABLED ");
272 fprintf(stderr, "\n%s\n", suite_title);
274 if (suite->enabled) {
275 /* iterate through all tests */
276 unsigned int it_unit;
277 xbt_dynar_foreach(suite->units, it_unit, unit) {
278 /* init unit case counters */
280 unit->test_ignore = 0;
281 unit->test_failed = 0;
282 unit->test_expect = 0;
284 /* display unit title */
285 char* cp = bprintf(" Unit: %s ......................................"
286 "......................................",
289 fprintf(stderr, "%s", cp);
292 /* run the test case function */
293 _xbt_test_current_unit = unit;
297 /* iterate through all performed tests to determine status */
298 unsigned int it_test;
299 xbt_dynar_foreach(unit->tests, it_test, test) {
305 if (test->failed && not test->expected_failure)
307 if (not test->failed && test->expected_failure)
309 if (test->expected_failure)
313 /* Display whether this unit went well */
314 if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
315 /* some tests failed (or were supposed to), so do detailed reporting of test case */
316 if (unit->test_failed > 0) {
317 fprintf(stderr, ".. failed\n");
318 } else if (unit->nb_tests) {
319 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
321 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
323 xbt_dynar_foreach(unit->tests, it_test, test) {
324 const char* file = (test->file != nullptr ? test->file : unit->file);
325 int line = (test->line != 0 ? test->line : unit->line);
329 else if (test->expected_failure) {
340 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test->title, file, line);
342 if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
344 xbt_dynar_foreach(test->logs, it_log, log) {
345 file = (log->file != nullptr ? log->file : file);
346 line = (log->line != 0 ? log->line : line);
347 fprintf(stderr, " %s:%d: %s\n", file, line, log->text);
351 fprintf(stderr, " Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
352 if (unit->test_ignore) {
353 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
355 fprintf(stderr, "\n");
357 } else if (not unit->enabled) {
358 fprintf(stderr, " disabled\n"); /* no test were run */
359 } else if (unit->nb_tests) {
360 fprintf(stderr, "...... ok\n"); /* successful */
362 fprintf(stderr, ".... skip\n"); /* no test were run */
365 /* Accumulate test counts into the suite */
366 suite->nb_tests += unit->nb_tests;
367 suite->test_failed += unit->test_failed;
368 suite->test_ignore += unit->test_ignore;
369 suite->test_expect += unit->test_expect;
371 _xbt_test_nb_tests += unit->nb_tests;
372 _xbt_test_test_failed += unit->test_failed;
373 _xbt_test_test_ignore += unit->test_ignore;
374 _xbt_test_test_expect += unit->test_expect;
376 /* What's the conclusion of this test anyway? */
377 if (unit->nb_tests) {
379 if (unit->test_failed)
380 suite->unit_failed++;
381 } else if (not unit->enabled) {
382 suite->unit_disabled++;
384 suite->unit_ignore++;
388 _xbt_test_nb_units += suite->nb_units;
389 _xbt_test_unit_failed += suite->unit_failed;
390 _xbt_test_unit_ignore += suite->unit_ignore;
391 _xbt_test_unit_disabled += suite->unit_disabled;
393 if (suite->nb_units) {
394 _xbt_test_nb_suites++;
395 if (suite->test_failed)
396 _xbt_test_suite_failed++;
397 } else if (not suite->enabled) {
398 _xbt_test_suite_disabled++;
400 _xbt_test_suite_ignore++;
403 /* print test suite summary */
404 if (suite->enabled) {
405 int first = 1; /* for result pretty printing */
407 fprintf(stderr," =====================================================================%s\n",
408 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
409 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
410 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
411 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
413 if (suite->nb_units != suite->unit_failed) {
414 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
417 if (suite->unit_failed) {
418 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
421 if (suite->unit_ignore) {
422 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
425 if (suite->unit_disabled) {
426 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
428 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
429 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
432 if (suite->nb_tests != suite->test_failed) {
433 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
436 if (suite->test_failed) {
437 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
440 if (suite->test_ignore) {
441 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
444 if (suite->test_expect) {
445 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
447 fprintf(stderr, ")\n");
449 return suite->unit_failed;
452 static void apply_selection(char *selection)
454 /* for the parsing */
455 char *sel = selection;
457 char dir[1024]; /* the directive */
459 unsigned int it_suite;
460 xbt_test_suite_t suite;
461 xbt_test_unit_t unit;
462 unsigned int it_unit;
467 if (not selection || selection[0] == '\0')
470 /*printf("Test selection: %s\n", selection); */
472 /* First apply the selection */
476 char *p = strchr(sel, ',');
478 strncpy(dir, sel, p - sel);
482 strncpy(dir, sel,1024);
488 memmove(dir, dir + 1, strlen(dir));
492 memmove(dir, dir + 1, strlen(dir));
495 p = strchr(dir, ':');
497 strncpy(unitname, p + 1,512);
498 strncpy(suitename, dir, p - dir);
499 suitename[p - dir] = '\0';
501 strncpy(suitename, dir,512);
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 */