Logo AND Algorithmique Numérique Distribuée

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