Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #193 from Takishipp/signals
[simgrid.git] / src / xbt / cunit.cpp
1 /* cunit - A little C Unit facility                                         */
2
3 /* Copyright (c) 2005-2017. The SimGrid Team. All rights reserved.          */
4
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. */
7
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 */
10
11 #include "src/internal_config.h"
12 #include <stdio.h>
13
14 #include <xbt/ex.hpp>
15 #include "xbt/sysdep.h"         /* bvprintf */
16 #include "xbt/cunit.h"
17 #include "xbt/dynar.h"
18
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;
26
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;
31
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;
36
37 /* Context */
38 xbt_test_unit_t _xbt_test_current_unit = nullptr;
39
40 /* test suite test log */
41 typedef struct s_xbt_test_log {
42   char *text;
43   const char *file;
44   int line;
45 } *xbt_test_log_t;
46
47 static void xbt_test_log_dump(xbt_test_log_t log)
48 {
49   if (log)
50     fprintf(stderr, "      log %p(%s:%d)=%s\n", log, log->file, log->line, log->text);
51   else
52     fprintf(stderr, "      log=nullptr\n");
53 }
54
55 /* test suite test check */
56 typedef struct s_xbt_test_test {
57   char *title;
58   int failed;
59   int expected_failure;
60   int ignored;
61   const char *file;
62   int line;
63   xbt_dynar_t logs;
64 } *xbt_test_test_t;
65
66 static void xbt_test_test_dump(xbt_test_test_t test)
67 {
68   if (test) {
69     xbt_test_log_t log;
70     unsigned int it_log;
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);
75   } else
76     fprintf(stderr, "    test=nullptr\n");
77 }
78
79 /* test suite test unit */
80 struct s_xbt_test_unit {
81   int enabled;
82   char *name;
83   char *title;
84   ts_test_cb_t func;
85   const char *file;
86   int line;
87   xbt_dynar_t tests;            /* of xbt_test_test_t */
88
89   int nb_tests;
90   int test_failed;
91   int test_ignore;
92   int test_expect;
93 };
94
95 static void xbt_test_unit_dump(xbt_test_unit_t unit)
96 {
97   if (unit) {
98     fprintf(stderr, "  UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
99     if (unit->enabled) {
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);
104     }
105   } else {
106     fprintf(stderr, "  unit=nullptr\n");
107   }
108 }
109
110 /* test suite */
111 struct s_xbt_test_suite {
112   int enabled;
113   const char *name;
114   char *title;
115   xbt_dynar_t units;            /* of xbt_test_unit_t */
116
117   int nb_tests;
118   int nb_units;
119   int test_failed;
120   int test_ignore;
121   int test_expect;
122   int unit_failed;
123   int unit_ignore;
124   int unit_disabled;
125 };
126
127 /* destroy test suite */
128 static void xbt_test_suite_free(void *s)
129 {
130   xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
131
132   if (suite == nullptr)
133     return;
134   xbt_dynar_free(&suite->units);
135   free(suite->title);
136   free(suite);
137 }
138
139 static void xbt_test_unit_free(void *unit)
140 {
141   xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
142   /* name is static */
143   free(u->title);
144   xbt_dynar_free(&u->tests);
145   free(u);
146 }
147
148 static void xbt_test_test_free(void *test)
149 {
150   xbt_test_test_t t = *(xbt_test_test_t *) test;
151   free(t->title);
152   xbt_dynar_free(&(t->logs));
153   free(t);
154 }
155
156 static void xbt_test_log_free(void *log)
157 {
158   xbt_test_log_t l = *(xbt_test_log_t *) log;
159   free(l->text);
160   free(l);
161 }
162
163 /** @brief create test suite */
164 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
165 {
166   xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
167   va_list ap;
168
169   if (_xbt_test_suites == nullptr)
170     _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
171
172   va_start(ap, fmt);
173   suite->title = bvprintf(fmt, ap);
174   suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
175   va_end(ap);
176   suite->name = name;
177   suite->enabled = 1;
178
179   xbt_dynar_push(_xbt_test_suites, &suite);
180
181   return suite;
182 }
183
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, ...)
186 {
187   xbt_test_suite_t suite;
188   char *bufname;
189   va_list ap;
190
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))
195         return suite;
196   }
197
198   va_start(ap, fmt);
199   bufname = bvprintf(fmt, ap);
200   va_end(ap);
201   suite = xbt_test_suite_new(name, bufname, nullptr);
202   free(bufname);
203
204   return suite;
205 }
206
207 void xbt_test_suite_dump(xbt_test_suite_t suite)
208 {
209   if (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);
216     }
217   } else {
218     fprintf(stderr, "TESTSUITE IS NULL!\n");
219   }
220 }
221
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, ...)
224 {
225   xbt_test_unit_t unit;
226   va_list ap;
227
228   xbt_assert(suite);
229   xbt_assert(func);
230   xbt_assert(fmt);
231
232   unit = xbt_new0(struct s_xbt_test_unit, 1);
233   va_start(ap, fmt);
234   unit->title = bvprintf(fmt, ap);
235   va_end(ap);
236   unit->name = (char *) name;
237   unit->func = func;
238   unit->file = nullptr;
239   unit->line = 0;
240   unit->enabled = 1;
241   unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
242
243   xbt_dynar_push(suite->units, &unit);
244 }
245
246 /* run test one suite */
247 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
248 {
249   xbt_test_unit_t unit;
250   xbt_test_test_t test;
251   xbt_test_log_t log;
252
253   unsigned int it_test;
254
255   if (suite == nullptr)
256     return 0;
257
258   /* suite title pretty-printing */
259   char suite_title[81];
260   int suite_len = strlen(suite->title);
261
262   xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
263
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';
269
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);
275
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 */
281       unit->nb_tests = 0;
282       unit->test_ignore = 0;
283       unit->test_failed = 0;
284       unit->test_expect = 0;
285
286       /* display unit title */
287       char* cp = bprintf(" Unit: %s ......................................"
288                          "......................................",
289                          unit->title);
290       cp[70] = '\0';
291       fprintf(stderr, "%s", cp);
292       free(cp);
293
294       /* run the test case function */
295       _xbt_test_current_unit = unit;
296       if (unit->enabled)
297         unit->func();
298
299       /* iterate through all performed tests to determine status */
300       xbt_dynar_foreach(unit->tests, it_test, test) {
301         if (test->ignored) {
302           unit->test_ignore++;
303         } else {
304           unit->nb_tests++;
305
306           if (test->failed && not test->expected_failure)
307             unit->test_failed++;
308           if (not test->failed && test->expected_failure)
309             unit->test_failed++;
310           if (test->expected_failure)
311             unit->test_expect++;
312         }
313       }
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 */
321         } else {
322           fprintf(stderr, ".... skip\n");       /* shouldn't happen, but I'm a bit lost with this logic */
323         }
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);
327           const char* resname;
328           if (test->ignored)
329             resname = " SKIP";
330           else if (test->expected_failure) {
331             if (test->failed)
332               resname = "EFAIL";
333             else
334               resname = "EPASS";
335           } else {
336             if (test->failed)
337               resname = " FAIL";
338             else
339               resname = " PASS";
340           }
341           fprintf(stderr, "      %s: %s [%s:%d]\n", resname, test->title, file, line);
342
343           if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
344             unsigned int it_log;
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);
349             }
350           }
351         }
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);
355         } else {
356           fprintf(stderr, "\n");
357         }
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 */
362       } else {
363         fprintf(stderr, ".... skip\n"); /* no test were run */
364       }
365
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;
371
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;
376
377       /* What's the conclusion of this test anyway? */
378       if (unit->nb_tests) {
379         suite->nb_units++;
380         if (unit->test_failed)
381           suite->unit_failed++;
382       } else if (not unit->enabled) {
383         suite->unit_disabled++;
384       } else {
385         suite->unit_ignore++;
386       }
387     }
388   }
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;
393
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++;
400   } else {
401     _xbt_test_suite_ignore++;
402   }
403
404   /* print test suite summary */
405   if (suite->enabled) {
406     int first = 1; /* for result pretty printing */
407
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);
413
414     if (suite->nb_units != suite->unit_failed) {
415       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
416       first = 0;
417     }
418     if (suite->unit_failed) {
419       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
420       first = 0;
421     }
422     if (suite->unit_ignore) {
423       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
424       first = 0;
425     }
426     if (suite->unit_disabled) {
427       fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
428     }
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);
431
432     first = 1;
433     if (suite->nb_tests != suite->test_failed) {
434       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
435       first = 0;
436     }
437     if (suite->test_failed) {
438       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
439       first = 0;
440     }
441     if (suite->test_ignore) {
442       fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
443       first = 0;
444     }
445     if (suite->test_expect) {
446       fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
447     }
448     fprintf(stderr, ")\n");
449   }
450   return suite->unit_failed;
451 }
452
453 static void apply_selection(char *selection)
454 {
455   /* for the parsing */
456   char *sel = selection;
457   int done = 0;
458   char dir[1024];               /* the directive */
459   /* iterators */
460   unsigned int it_suite;
461   xbt_test_suite_t suite;
462   xbt_test_unit_t unit;
463   unsigned int it_unit;
464
465   char suitename[512];
466   char unitname[512];
467
468   if (not selection || selection[0] == '\0')
469     return;
470
471   /*printf("Test selection: %s\n", selection); */
472
473   /* First apply the selection */
474   while (not done) {
475     int enabling = 1;
476
477     char *p = strchr(sel, ',');
478     if (p) {
479       strncpy(dir, sel, p - sel);
480       dir[p - sel] = '\0';
481       sel = p + 1;
482     } else {
483       strncpy(dir, sel,1024);
484       done = 1;
485     }
486
487     if (dir[0] == '-') {
488       enabling = 0;
489       memmove(dir, dir + 1, strlen(dir));
490     }
491     if (dir[0] == '+') {
492       enabling = 1;
493       memmove(dir, dir + 1, strlen(dir));
494     }
495
496     p = strchr(dir, ':');
497     if (p) {
498       strncpy(unitname, p + 1,512);
499       strncpy(suitename, dir, p - dir);
500       suitename[p - dir] = '\0';
501     } else {
502       strncpy(suitename, dir,512);
503       unitname[0] = '\0';
504     }
505
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");
509
510       xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
511         xbt_dynar_foreach(suite->units, it_unit, unit) {
512           unit->enabled = enabling;
513         }
514         suite->enabled = enabling;
515       }
516     } else {
517       unsigned int it;
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;
525
526           if (unitname[0] == '\0') {
527             xbt_dynar_foreach(thissuite->units, it_unit, unit) {
528               unit->enabled = enabling;
529             }
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) */
533             for (it2_unit = 0;
534                  it2_unit < xbt_dynar_length(thissuite->units);
535                  it2_unit++) {
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;
539                 break;
540               }
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) */
545
546           break;                /* found the relevant serie. We are happy */
547         }
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);
551     }
552   }
553 }
554
555 void xbt_test_dump(char *selection)
556 {
557   apply_selection(selection);
558
559   if (_xbt_test_suites) {
560     unsigned int it_suite;
561     xbt_test_suite_t suite;
562
563     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
564         xbt_test_suite_dump(suite);
565   } else {
566     printf(" No suite defined.");
567   }
568 }
569
570 int xbt_test_run(char *selection, int verbosity)
571 {
572   apply_selection(selection);
573
574   if (_xbt_test_suites) {
575     unsigned int it_suite;
576     xbt_test_suite_t suite;
577     int first = 1;
578
579     /* Run all the suites */
580     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
581       xbt_test_suite_run(suite, verbosity);
582
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);
589       first = 0;
590     }
591     if (_xbt_test_suite_failed) {
592       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
593       first = 0;
594     }
595
596     if (_xbt_test_suite_ignore) {
597       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
598     }
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);
601     first = 1;
602     if (_xbt_test_nb_units != _xbt_test_unit_failed) {
603       fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
604       first = 0;
605     }
606     if (_xbt_test_unit_failed) {
607       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
608       first = 0;
609     }
610     if (_xbt_test_unit_ignore) {
611       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
612     }
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);
615     first = 1;
616     if (_xbt_test_nb_tests != _xbt_test_test_failed) {
617       fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
618       first = 0;
619     }
620     if (_xbt_test_test_failed) {
621       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
622       first = 0;
623     }
624     if (_xbt_test_test_ignore) {
625       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
626       first = 0;
627     }
628     if (_xbt_test_test_expect) {
629       fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
630     }
631
632     fprintf(stderr, ")\n");
633   } else {
634     fprintf(stderr, "No unit to run!\n");
635     _xbt_test_unit_failed++;
636   }
637   return _xbt_test_unit_failed;
638 }
639
640 void xbt_test_exit()
641 {
642   xbt_dynar_free(&_xbt_test_suites);
643 }
644
645 /* annotate test case with test */
646 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
647 {
648   xbt_test_unit_t unit = _xbt_test_current_unit;
649   xbt_assert(unit);
650
651   va_list ap;
652   xbt_test_test_t test = xbt_new0(struct s_xbt_test_test, 1);
653   va_start(ap, fmt);
654   test->title = bvprintf(fmt, ap);
655   va_end(ap);
656   test->failed = 0;
657   test->expected_failure = 0;
658   test->ignored = 0;
659   test->file = file;
660   test->line = line;
661   test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
662   xbt_dynar_push(unit->tests, &test);
663 }
664
665 /* annotate test case with log message and failure */
666 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
667 {
668   xbt_test_unit_t unit = _xbt_test_current_unit;
669   xbt_assert(unit);
670   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
671       "Test failed even before being declared (broken unit: %s)", unit->title);
672
673   va_list ap;
674   xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
675   va_start(ap, fmt);
676   log->text = bvprintf(fmt, ap);
677   va_end(ap);
678   log->file = file;
679   log->line = line;
680
681   xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
682   xbt_dynar_push(test->logs, &log);
683
684   test->failed = 1;
685 }
686
687 void xbt_test_exception(xbt_ex_t e)
688 {
689   _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
690 }
691
692 void xbt_test_expect_failure()
693 {
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;
698 }
699
700 void xbt_test_skip()
701 {
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);
705   test->ignored = 1;
706 }
707
708 /* annotate test case with log message only */
709 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
710 {
711   xbt_test_unit_t unit = _xbt_test_current_unit;
712   xbt_assert(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);
715
716   va_list ap;
717   xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
718   va_start(ap, fmt);
719   log->text = bvprintf(fmt, ap);
720   va_end(ap);
721   log->file = file;
722   log->line = line;
723
724   xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
725   xbt_dynar_push(test->logs, &log);
726 }
727
728 #ifdef SIMGRID_TEST
729 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
730
731 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
732 {
733   xbt_test_add("Skipped test");
734   xbt_test_skip();
735
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");
740 }
741 #endif                          /* SIMGRID_TEST */