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 unsigned int it_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 xbt_dynar_foreach(unit->tests, it_test, test) {
306 if (test->failed && not test->expected_failure)
308 if (not test->failed && test->expected_failure)
310 if (test->expected_failure)
314 /* Display whether this unit went well */
315 if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
316 /* some tests failed (or were supposed to), so do detailed reporting of test case */
317 if (unit->test_failed > 0) {
318 fprintf(stderr, ".. failed\n");
319 } else if (unit->nb_tests) {
320 fprintf(stderr, "...... ok\n"); /* successful, but show about expected */
322 fprintf(stderr, ".... skip\n"); /* shouldn't happen, but I'm a bit lost with this logic */
324 xbt_dynar_foreach(unit->tests, it_test, test) {
325 const char* file = (test->file != nullptr ? test->file : unit->file);
326 int line = (test->line != 0 ? test->line : unit->line);
330 else if (test->expected_failure) {
341 fprintf(stderr, " %s: %s [%s:%d]\n", resname, test->title, file, line);
343 if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
345 xbt_dynar_foreach(test->logs, it_log, log) {
346 file = (log->file != nullptr ? log->file : file);
347 line = (log->line != 0 ? log->line : line);
348 fprintf(stderr, " %s:%d: %s\n", file, line, log->text);
352 fprintf(stderr, " Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
353 if (unit->test_ignore) {
354 fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
356 fprintf(stderr, "\n");
358 } else if (not unit->enabled) {
359 fprintf(stderr, " disabled\n"); /* no test were run */
360 } else if (unit->nb_tests) {
361 fprintf(stderr, "...... ok\n"); /* successful */
363 fprintf(stderr, ".... skip\n"); /* no test were run */
366 /* Accumulate test counts into the suite */
367 suite->nb_tests += unit->nb_tests;
368 suite->test_failed += unit->test_failed;
369 suite->test_ignore += unit->test_ignore;
370 suite->test_expect += unit->test_expect;
372 _xbt_test_nb_tests += unit->nb_tests;
373 _xbt_test_test_failed += unit->test_failed;
374 _xbt_test_test_ignore += unit->test_ignore;
375 _xbt_test_test_expect += unit->test_expect;
377 /* What's the conclusion of this test anyway? */
378 if (unit->nb_tests) {
380 if (unit->test_failed)
381 suite->unit_failed++;
382 } else if (not unit->enabled) {
383 suite->unit_disabled++;
385 suite->unit_ignore++;
389 _xbt_test_nb_units += suite->nb_units;
390 _xbt_test_unit_failed += suite->unit_failed;
391 _xbt_test_unit_ignore += suite->unit_ignore;
392 _xbt_test_unit_disabled += suite->unit_disabled;
394 if (suite->nb_units) {
395 _xbt_test_nb_suites++;
396 if (suite->test_failed)
397 _xbt_test_suite_failed++;
398 } else if (not suite->enabled) {
399 _xbt_test_suite_disabled++;
401 _xbt_test_suite_ignore++;
404 /* print test suite summary */
405 if (suite->enabled) {
406 int first = 1; /* for result pretty printing */
408 fprintf(stderr," =====================================================================%s\n",
409 (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
410 (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
411 fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
412 ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
414 if (suite->nb_units != suite->unit_failed) {
415 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
418 if (suite->unit_failed) {
419 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
422 if (suite->unit_ignore) {
423 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
426 if (suite->unit_disabled) {
427 fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
429 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", suite->nb_tests
430 ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
433 if (suite->nb_tests != suite->test_failed) {
434 fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
437 if (suite->test_failed) {
438 fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
441 if (suite->test_ignore) {
442 fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
445 if (suite->test_expect) {
446 fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
448 fprintf(stderr, ")\n");
450 return suite->unit_failed;
453 static void apply_selection(char *selection)
455 /* for the parsing */
456 char *sel = selection;
458 char dir[1024]; /* the directive */
460 unsigned int it_suite;
461 xbt_test_suite_t suite;
462 xbt_test_unit_t unit;
463 unsigned int it_unit;
468 if (not selection || selection[0] == '\0')
471 /*printf("Test selection: %s\n", selection); */
473 /* First apply the selection */
477 char *p = strchr(sel, ',');
479 strncpy(dir, sel, p - sel);
483 strncpy(dir, sel,1024);
489 memmove(dir, dir + 1, strlen(dir));
493 memmove(dir, dir + 1, strlen(dir));
496 p = strchr(dir, ':');
498 strncpy(unitname, p + 1,512);
499 strncpy(suitename, dir, p - dir);
500 suitename[p - dir] = '\0';
502 strncpy(suitename, dir,512);
506 /* Deal with the specific case of 'all' pseudo serie */
507 if (not strcmp("all", suitename)) {
508 xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
510 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
511 xbt_dynar_foreach(suite->units, it_unit, unit) {
512 unit->enabled = enabling;
514 suite->enabled = enabling;
518 for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
519 xbt_test_suite_t thissuite =
520 xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
521 if (not strcmp(suitename, thissuite->name)) {
522 /* Do not disable the whole suite when we just want to disable a child */
523 if (enabling || (unitname[0] == '\0'))
524 thissuite->enabled = enabling;
526 if (unitname[0] == '\0') {
527 xbt_dynar_foreach(thissuite->units, it_unit, unit) {
528 unit->enabled = enabling;
530 } else { /* act on one child only */
531 unsigned int it2_unit;
532 /* search it, first (we won't reuse it for external loop which gets broken) */
534 it2_unit < xbt_dynar_length(thissuite->units);
536 xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
537 if (not strcmp(thisunit->name, unitname)) {
538 thisunit->enabled = enabling;
541 } /* search relevant unit */
542 xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
543 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
544 } /* act on childs (either all or one) */
546 break; /* found the relevant serie. We are happy */
548 } /* search relevant series */
549 xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
550 "No suite of name '%s' found. Cannot apply the selection\n", suitename);
555 void xbt_test_dump(char *selection)
557 apply_selection(selection);
559 if (_xbt_test_suites) {
560 unsigned int it_suite;
561 xbt_test_suite_t suite;
563 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
564 xbt_test_suite_dump(suite);
566 printf(" No suite defined.");
570 int xbt_test_run(char *selection, int verbosity)
572 apply_selection(selection);
574 if (_xbt_test_suites) {
575 unsigned int it_suite;
576 xbt_test_suite_t suite;
579 /* Run all the suites */
580 xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
581 xbt_test_suite_run(suite, verbosity);
583 /* Display some more statistics */
584 fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
585 ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
586 : 100.0, _xbt_test_nb_suites);
587 if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
588 fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
591 if (_xbt_test_suite_failed) {
592 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
596 if (_xbt_test_suite_ignore) {
597 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
599 fprintf(stderr, ")\n Units: %.0f%% ok (%d units: ", _xbt_test_nb_units
600 ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
602 if (_xbt_test_nb_units != _xbt_test_unit_failed) {
603 fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
606 if (_xbt_test_unit_failed) {
607 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
610 if (_xbt_test_unit_ignore) {
611 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
613 fprintf(stderr, ")\n Tests: %.0f%% ok (%d tests: ", _xbt_test_nb_tests
614 ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
616 if (_xbt_test_nb_tests != _xbt_test_test_failed) {
617 fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
620 if (_xbt_test_test_failed) {
621 fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
624 if (_xbt_test_test_ignore) {
625 fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
628 if (_xbt_test_test_expect) {
629 fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
632 fprintf(stderr, ")\n");
634 fprintf(stderr, "No unit to run!\n");
635 _xbt_test_unit_failed++;
637 return _xbt_test_unit_failed;
642 xbt_dynar_free(&_xbt_test_suites);
645 /* annotate test case with test */
646 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
648 xbt_test_unit_t unit = _xbt_test_current_unit;
652 xbt_test_test_t test = xbt_new0(struct s_xbt_test_test, 1);
654 test->title = bvprintf(fmt, ap);
657 test->expected_failure = 0;
661 test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
662 xbt_dynar_push(unit->tests, &test);
665 /* annotate test case with log message and failure */
666 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
668 xbt_test_unit_t unit = _xbt_test_current_unit;
670 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
671 "Test failed even before being declared (broken unit: %s)", unit->title);
674 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
676 log->text = bvprintf(fmt, ap);
681 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
682 xbt_dynar_push(test->logs, &log);
687 void xbt_test_exception(xbt_ex_t e)
689 _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
692 void xbt_test_expect_failure()
694 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
695 "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
696 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
697 test->expected_failure = 1;
702 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
703 "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
704 xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
708 /* annotate test case with log message only */
709 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
711 xbt_test_unit_t unit = _xbt_test_current_unit;
713 xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
714 "Test logged into even before being declared (broken test unit: %s)", unit->title);
717 xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
719 log->text = bvprintf(fmt, ap);
724 xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
725 xbt_dynar_push(test->logs, &log);
729 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
731 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
733 xbt_test_add("Skipped test");
736 xbt_test_add("%s %s", "EXPECTED", "FAILURE");
737 xbt_test_expect_failure();
738 xbt_test_log("%s %s", "Test", "log");
739 xbt_test_fail("EXPECTED FAILURE");
741 #endif /* SIMGRID_TEST */