Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4ca449fb6f90a2c8e6a136bec975a2f501d5ae8a
[simgrid.git] / src / xbt / cunit.cpp
1 /* cunit - A little C Unit facility                                         */
2
3 /* Copyright (c) 2005-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 /* This is partially inspirated from the OSSP ts (Test Suite Library)       */
10 /* At some point we should use https://github.com/google/googletest instead */
11
12 #include "src/internal_config.h"
13 #include <stdio.h>
14
15 #include <xbt/ex.hpp>
16 #include "xbt/sysdep.h"         /* bvprintf */
17 #include "xbt/cunit.h"
18 #include "xbt/dynar.h"
19
20 /* collection of all suites */
21 static xbt_dynar_t _xbt_test_suites = NULL;
22 /* global statistics */
23 static int _xbt_test_nb_tests = 0;
24 static int _xbt_test_test_failed = 0;
25 static int _xbt_test_test_ignore = 0;
26 static int _xbt_test_test_expect = 0;
27
28 static int _xbt_test_nb_units = 0;
29 static int _xbt_test_unit_failed = 0;
30 static int _xbt_test_unit_ignore = 0;
31 static int _xbt_test_unit_disabled = 0;
32
33 static int _xbt_test_nb_suites = 0;
34 static int _xbt_test_suite_failed = 0;
35 static int _xbt_test_suite_ignore = 0;
36 static int _xbt_test_suite_disabled = 0;
37
38 /* Context */
39 xbt_test_unit_t _xbt_test_current_unit = NULL;
40
41 /* test suite test log */
42 typedef struct s_xbt_test_log {
43   char *text;
44   const char *file;
45   int line;
46 } *xbt_test_log_t;
47
48 static void xbt_test_log_dump(xbt_test_log_t log)
49 {
50   if (log)
51     fprintf(stderr, "      log %p(%s:%d)=%s\n", log, log->file, log->line, log->text);
52   else
53     fprintf(stderr, "      log=NULL\n");
54 }
55
56 /* test suite test check */
57 typedef struct s_xbt_test_test {
58   char *title;
59   int failed;
60   int expected_failure;
61   int ignored;
62   const char *file;
63   int line;
64   xbt_dynar_t logs;
65 } *xbt_test_test_t;
66
67 static void xbt_test_test_dump(xbt_test_test_t test)
68 {
69   if (test) {
70     xbt_test_log_t log;
71     unsigned int it_log;
72     fprintf(stderr, "    test %p(%s:%d)=%s (%s)\n", 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);
76   } else
77     fprintf(stderr, "    test=NULL\n");
78 }
79
80 /* test suite test unit */
81 struct s_xbt_test_unit {
82   int enabled;
83   char *name;
84   char *title;
85   ts_test_cb_t func;
86   const char *file;
87   int line;
88   xbt_dynar_t tests;            /* of xbt_test_test_t */
89
90   int nb_tests;
91   int test_failed, test_ignore, test_expect;
92 };
93
94 static void xbt_test_unit_dump(xbt_test_unit_t unit)
95 {
96   if (unit) {
97     xbt_test_test_t test;
98     unsigned int it_test;
99     fprintf(stderr, "  UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
100     if (unit->enabled)
101       xbt_dynar_foreach(unit->tests, it_test, test)
102           xbt_test_test_dump(test);
103   } else {
104     fprintf(stderr, "  unit=NULL\n");
105   }
106 }
107
108 /* test suite */
109 struct s_xbt_test_suite {
110   int enabled;
111   const char *name;
112   char *title;
113   xbt_dynar_t units;            /* of xbt_test_unit_t */
114
115   int nb_tests, nb_units;
116   int test_failed, test_ignore, test_expect;
117   int unit_failed, unit_ignore, unit_disabled;
118 };
119
120 /* destroy test suite */
121 static void xbt_test_suite_free(void *s)
122 {
123   xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
124
125   if (suite == NULL)
126     return;
127   xbt_dynar_free(&suite->units);
128   free(suite->title);
129   free(suite);
130 }
131
132 static void xbt_test_unit_free(void *unit)
133 {
134   xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
135   /* name is static */
136   free(u->title);
137   xbt_dynar_free(&u->tests);
138   free(u);
139 }
140
141 static void xbt_test_test_free(void *test)
142 {
143   xbt_test_test_t t = *(xbt_test_test_t *) test;
144   free(t->title);
145   xbt_dynar_free(&(t->logs));
146   free(t);
147 }
148
149 static void xbt_test_log_free(void *log)
150 {
151   xbt_test_log_t l = *(xbt_test_log_t *) log;
152   free(l->text);
153   free(l);
154 }
155
156 /** @brief create test suite */
157 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
158 {
159   xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
160   va_list ap;
161
162   if (!_xbt_test_suites)
163     _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
164
165   va_start(ap, fmt);
166   suite->title = bvprintf(fmt, ap);
167   suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
168   va_end(ap);
169   suite->name = name;
170   suite->enabled = 1;
171
172   xbt_dynar_push(_xbt_test_suites, &suite);
173
174   return suite;
175 }
176
177 /** @brief retrieve a testsuite from name, or create a new one */
178 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
179 {
180   xbt_test_suite_t suite;
181   unsigned int it_suite;
182
183   char *bufname;
184   va_list ap;
185
186   if (_xbt_test_suites)
187     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
188         if (!strcmp(suite->name, name))
189       return suite;
190
191   va_start(ap, fmt);
192   bufname = bvprintf(fmt, ap);
193   va_end(ap);
194   suite = xbt_test_suite_new(name, bufname, NULL);
195   free(bufname);
196
197   return suite;
198 }
199
200 void xbt_test_suite_dump(xbt_test_suite_t suite)
201 {
202   if (suite) {
203     xbt_test_unit_t unit;
204     unsigned int it_unit;
205     fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
206     if (suite->enabled)
207       xbt_dynar_foreach(suite->units, it_unit, unit)
208           xbt_test_unit_dump(unit);
209   } else {
210     fprintf(stderr, "TESTSUITE IS NULL!\n");
211   }
212 }
213
214 /* add test case to test suite */
215 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
216 {
217   xbt_test_unit_t unit;
218   va_list ap;
219
220   xbt_assert(suite);
221   xbt_assert(func);
222   xbt_assert(fmt);
223
224   unit = xbt_new0(struct s_xbt_test_unit, 1);
225   va_start(ap, fmt);
226   unit->title = bvprintf(fmt, ap);
227   va_end(ap);
228   unit->name = (char *) name;
229   unit->func = func;
230   unit->file = NULL;
231   unit->line = 0;
232   unit->enabled = 1;
233   unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
234
235   xbt_dynar_push(suite->units, &unit);
236   return;
237 }
238
239 /* run test one suite */
240 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
241 {
242   xbt_test_unit_t unit;
243   xbt_test_test_t test;
244   xbt_test_log_t log;
245
246   const char *file;
247   int line;
248   char *cp;
249   unsigned int it_unit, it_test, it_log;
250
251   int first = 1;                /* for result pretty printing */
252
253   if (suite == NULL)
254     return 0;
255
256   /* suite title pretty-printing */
257   {
258     char suite_title[81];
259     int suite_len = strlen(suite->title);
260     int i;
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 (i = 1; i < 80; i++)
266       suite_title[i] = '=';
267     suite_title[i++] = '\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 (!suite->enabled)
273       snprintf(suite_title + 70, 11, " DISABLED ");
274     fprintf(stderr, "\n%s\n", suite_title);
275   }
276
277   if (suite->enabled) {
278     /* iterate through all tests */
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       cp = bprintf(" Unit: %s ......................................"
288                    "......................................", unit->title);
289       cp[70] = '\0';
290       fprintf(stderr, "%s", cp);
291       free(cp);
292
293       /* run the test case function */
294       _xbt_test_current_unit = unit;
295       if (unit->enabled)
296         unit->func();
297
298       /* iterate through all performed tests to determine status */
299       xbt_dynar_foreach(unit->tests, it_test, test) {
300         if (test->ignored) {
301           unit->test_ignore++;
302         } else {
303           unit->nb_tests++;
304
305           if (test->failed && !test->expected_failure)
306             unit->test_failed++;
307           if (!test->failed && test->expected_failure)
308             unit->test_failed++;
309           if (test->expected_failure)
310             unit->test_expect++;
311         }
312       }
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 */
320         } else {
321           fprintf(stderr, ".... skip\n");       /* shouldn't happen, but I'm a bit lost with this logic */
322         }
323         xbt_dynar_foreach(unit->tests, it_test, test) {
324           file = (test->file != NULL ? test->file : unit->file);
325           line = (test->line != 0 ? test->line : unit->line);
326           fprintf(stderr, "      %s: %s [%s:%d]\n", (test->ignored ? " SKIP" : (test->expected_failure
327                   ? (test-> failed ? "EFAIL" : "EPASS") : (test->failed ? " FAIL" : " PASS"))),test->title, file, line);
328
329           if ((test->expected_failure && !test->failed) || (!test->expected_failure && test->failed)) {
330             xbt_dynar_foreach(test->logs, it_log, log) {
331               file = (log->file != NULL ? log->file : file);
332               line = (log->line != 0 ? log->line : line);
333               fprintf(stderr, "             %s:%d: %s\n", file, line, log->text);
334             }
335           }
336         }
337         fprintf(stderr, "    Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
338         if (unit->test_ignore) {
339           fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
340         } else {
341           fprintf(stderr, "\n");
342         }
343       } else if (!unit->enabled) {
344         fprintf(stderr, " disabled\n"); /* no test were run */
345       } else if (unit->nb_tests) {
346         fprintf(stderr, "...... ok\n"); /* successful */
347       } else {
348         fprintf(stderr, ".... skip\n"); /* no test were run */
349       }
350
351       /* Accumulate test counts into the suite */
352       suite->nb_tests += unit->nb_tests;
353       suite->test_failed += unit->test_failed;
354       suite->test_ignore += unit->test_ignore;
355       suite->test_expect += unit->test_expect;
356
357       _xbt_test_nb_tests += unit->nb_tests;
358       _xbt_test_test_failed += unit->test_failed;
359       _xbt_test_test_ignore += unit->test_ignore;
360       _xbt_test_test_expect += unit->test_expect;
361
362       /* What's the conclusion of this test anyway? */
363       if (unit->nb_tests) {
364         suite->nb_units++;
365         if (unit->test_failed)
366           suite->unit_failed++;
367       } else if (!unit->enabled) {
368         suite->unit_disabled++;
369       } else {
370         suite->unit_ignore++;
371       }
372     }
373   }
374   _xbt_test_nb_units += suite->nb_units;
375   _xbt_test_unit_failed += suite->unit_failed;
376   _xbt_test_unit_ignore += suite->unit_ignore;
377   _xbt_test_unit_disabled += suite->unit_disabled;
378
379   if (suite->nb_units) {
380     _xbt_test_nb_suites++;
381     if (suite->test_failed)
382       _xbt_test_suite_failed++;
383   } else if (!suite->enabled) {
384     _xbt_test_suite_disabled++;
385   } else {
386     _xbt_test_suite_ignore++;
387   }
388
389   /* print test suite summary */
390   if (suite->enabled) {
391     fprintf(stderr," =====================================================================%s\n",
392             (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
393                                (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
394     fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
395             ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
396
397     if (suite->nb_units != suite->unit_failed) {
398       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
399       first = 0;
400     }
401     if (suite->unit_failed) {
402       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
403       first = 0;
404     }
405     if (suite->unit_ignore) {
406       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
407       first = 0;
408     }
409     if (suite->unit_disabled) {
410       fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
411     }
412     fprintf(stderr, ")\n          Tests: %.0f%% ok (%d tests: ", suite->nb_tests
413             ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
414
415     first = 1;
416     if (suite->nb_tests != suite->test_failed) {
417       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
418       first = 0;
419     }
420     if (suite->test_failed) {
421       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
422       first = 0;
423     }
424     if (suite->test_ignore) {
425       fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
426       first = 0;
427     }
428     if (suite->test_expect) {
429       fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
430     }
431     fprintf(stderr, ")\n");
432   }
433   return suite->unit_failed;
434 }
435
436 static void apply_selection(char *selection)
437 {
438   /* for the parsing */
439   char *sel = selection;
440   int done = 0;
441   char dir[1024];               /* the directive */
442   /* iterators */
443   unsigned int it_suite;
444   xbt_test_suite_t suite;
445   xbt_test_unit_t unit;
446   unsigned int it_unit;
447
448   char suitename[512];
449   char unitname[512];
450
451   if (!selection || selection[0] == '\0')
452     return;
453
454   /*printf("Test selection: %s\n", selection); */
455
456   /* First apply the selection */
457   while (!done) {
458     int enabling = 1;
459
460     char *p = strchr(sel, ',');
461     if (p) {
462       strncpy(dir, sel, p - sel);
463       dir[p - sel] = '\0';
464       sel = p + 1;
465     } else {
466       strncpy(dir, sel,1024);
467       done = 1;
468     }
469
470     if (dir[0] == '-') {
471       enabling = 0;
472       memmove(dir, dir + 1, strlen(dir));
473     }
474     if (dir[0] == '+') {
475       enabling = 1;
476       memmove(dir, dir + 1, strlen(dir));
477     }
478
479     p = strchr(dir, ':');
480     if (p) {
481       strncpy(unitname, p + 1,512);
482       strncpy(suitename, dir, p - dir);
483       suitename[p - dir] = '\0';
484     } else {
485       strncpy(suitename, dir,512);
486       unitname[0] = '\0';
487     }
488
489     /* Deal with the specific case of 'all' pseudo serie */
490     if (!strcmp("all", suitename)) {
491       xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
492
493       xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
494         xbt_dynar_foreach(suite->units, it_unit, unit) {
495           unit->enabled = enabling;
496         }
497         suite->enabled = enabling;
498       }
499     } else {
500       unsigned int it;
501       for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
502         xbt_test_suite_t thissuite =
503             xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
504         if (!strcmp(suitename, thissuite->name)) {
505           /* Do not disable the whole suite when we just want to disable a child */
506           if (enabling || (unitname[0] == '\0'))
507             thissuite->enabled = enabling;
508
509           if (unitname[0] == '\0') {
510             xbt_dynar_foreach(thissuite->units, it_unit, unit) {
511               unit->enabled = enabling;
512             }
513           } else {              /* act on one child only */
514             unsigned int it2_unit;
515             /* search it, first (we won't reuse it for external loop which gets broken) */
516             for (it2_unit = 0;
517                  it2_unit < xbt_dynar_length(thissuite->units);
518                  it2_unit++) {
519               xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
520               if (!strcmp(thisunit->name, unitname)) {
521                 thisunit->enabled = enabling;
522                 break;
523               }
524             }                   /* search relevant unit */
525             xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
526                 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
527           }                     /* act on childs (either all or one) */
528
529           break;                /* found the relevant serie. We are happy */
530         }
531       }                         /* search relevant series */
532       xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
533                  "No suite of name '%s' found. Cannot apply the selection\n", suitename);
534     }
535   }
536 }
537
538 void xbt_test_dump(char *selection)
539 {
540   apply_selection(selection);
541
542   if (_xbt_test_suites) {
543     unsigned int it_suite;
544     xbt_test_suite_t suite;
545
546     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
547         xbt_test_suite_dump(suite);
548   } else {
549     printf(" No suite defined.");
550   }
551 }
552
553 int xbt_test_run(char *selection, int verbosity)
554 {
555   apply_selection(selection);
556
557   if (_xbt_test_suites) {
558     unsigned int it_suite;
559     xbt_test_suite_t suite;
560     int first = 1;
561
562     /* Run all the suites */
563     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
564       xbt_test_suite_run(suite, verbosity);
565
566     /* Display some more statistics */
567     fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
568             ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
569             : 100.0, _xbt_test_nb_suites);
570     if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
571       fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
572       first = 0;
573     }
574     if (_xbt_test_suite_failed) {
575       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
576       first = 0;
577     }
578
579     if (_xbt_test_suite_ignore) {
580       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
581     }
582     fprintf(stderr, ")\n        Units:  %.0f%% ok (%d units: ", _xbt_test_nb_units
583             ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
584     first = 1;
585     if (_xbt_test_nb_units != _xbt_test_unit_failed) {
586       fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_units - _xbt_test_unit_failed);
587       first = 0;
588     }
589     if (_xbt_test_unit_failed) {
590       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
591       first = 0;
592     }
593     if (_xbt_test_unit_ignore) {
594       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
595     }
596     fprintf(stderr, ")\n        Tests:  %.0f%% ok (%d tests: ", _xbt_test_nb_tests
597             ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
598     first = 1;
599     if (_xbt_test_nb_tests != _xbt_test_test_failed) {
600       fprintf(stderr, "%s%d ok", (first ? "" : ", "), _xbt_test_nb_tests - _xbt_test_test_failed);
601       first = 0;
602     }
603     if (_xbt_test_test_failed) {
604       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
605       first = 0;
606     }
607     if (_xbt_test_test_ignore) {
608       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
609       first = 0;
610     }
611     if (_xbt_test_test_expect) {
612       fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
613     }
614
615     fprintf(stderr, ")\n");
616   } else {
617     fprintf(stderr, "No unit to run!\n");
618     _xbt_test_unit_failed++;
619   }
620   return _xbt_test_unit_failed;
621 }
622
623 void xbt_test_exit(void)
624 {
625   xbt_dynar_free(&_xbt_test_suites);
626 }
627
628 /* annotate test case with test */
629 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
630 {
631   xbt_test_unit_t unit = _xbt_test_current_unit;
632   xbt_assert(unit);
633
634   va_list ap;
635   xbt_test_test_t test = xbt_new0(struct s_xbt_test_test, 1);
636   va_start(ap, fmt);
637   test->title = bvprintf(fmt, ap);
638   va_end(ap);
639   test->failed = 0;
640   test->expected_failure = 0;
641   test->ignored = 0;
642   test->file = file;
643   test->line = line;
644   test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
645   xbt_dynar_push(unit->tests, &test);
646   return;
647 }
648
649 /* annotate test case with log message and failure */
650 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
651 {
652   xbt_test_unit_t unit = _xbt_test_current_unit;
653   xbt_assert(unit);
654   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
655       "Test failed even before being declared (broken unit: %s)", unit->title);
656
657   va_list ap;
658   xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
659   va_start(ap, fmt);
660   log->text = bvprintf(fmt, ap);
661   va_end(ap);
662   log->file = file;
663   log->line = line;
664
665   xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
666   xbt_dynar_push(test->logs, &log);
667
668   test->failed = 1;
669 }
670
671 void xbt_test_exception(xbt_ex_t e)
672 {
673   _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
674 }
675
676 void xbt_test_expect_failure(void)
677 {
678   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
679       "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
680   xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
681   test->expected_failure = 1;
682 }
683
684 void xbt_test_skip(void)
685 {
686   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
687       "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
688   xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
689   test->ignored = 1;
690 }
691
692 /* annotate test case with log message only */
693 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
694 {
695   xbt_test_unit_t unit = _xbt_test_current_unit;
696   xbt_assert(unit);
697   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
698       "Test logged into even before being declared (broken test unit: %s)", unit->title);
699
700   va_list ap;
701   xbt_test_log_t log = xbt_new(struct s_xbt_test_log, 1);
702   va_start(ap, fmt);
703   log->text = bvprintf(fmt, ap);
704   va_end(ap);
705   log->file = file;
706   log->line = line;
707
708   xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
709   xbt_dynar_push(test->logs, &log);
710 }
711
712 #ifdef SIMGRID_TEST
713 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
714
715 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
716 {
717   xbt_test_add("Skipped test");
718   xbt_test_skip();
719
720   xbt_test_add("%s %s", "EXPECTED", "FAILURE");
721   xbt_test_expect_failure();
722   xbt_test_log("%s %s", "Test", "log");
723   xbt_test_fail("EXPECTED FAILURE");
724 }
725 #endif                          /* SIMGRID_TEST */