Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into actor-priority
[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 struct s_xbt_test_log {
44   char *text;
45   const char *file;
46   int line;
47 };
48 typedef s_xbt_test_log* xbt_test_log_t;
49
50 static void xbt_test_log_dump(xbt_test_log_t log)
51 {
52   if (log)
53     fprintf(stderr, "      log %p(%s:%d)=%s\n", log, log->file, log->line, log->text);
54   else
55     fprintf(stderr, "      log=nullptr\n");
56 }
57
58 /* test suite test check */
59 struct s_xbt_test_test {
60   char *title;
61   int failed;
62   int expected_failure;
63   int ignored;
64   const char *file;
65   int line;
66   xbt_dynar_t logs;
67 };
68 typedef s_xbt_test_test* xbt_test_test_t;
69
70 static void xbt_test_test_dump(xbt_test_test_t test)
71 {
72   if (test) {
73     xbt_test_log_t log;
74     unsigned int it_log;
75     fprintf(stderr, "    test %p(%s:%d)=%s (%s)\n", test, test->file, test->line, test->title,
76             test->failed ? "failed" : "not failed");
77     xbt_dynar_foreach(test->logs, it_log, log)
78         xbt_test_log_dump(log);
79   } else
80     fprintf(stderr, "    test=nullptr\n");
81 }
82
83 /* test suite test unit */
84 struct s_xbt_test_unit {
85   int enabled;
86   char *name;
87   char *title;
88   ts_test_cb_t func;
89   const char *file;
90   int line;
91   xbt_dynar_t tests;            /* of xbt_test_test_t */
92
93   int nb_tests;
94   int test_failed;
95   int test_ignore;
96   int test_expect;
97 };
98
99 static void xbt_test_unit_dump(xbt_test_unit_t unit)
100 {
101   if (unit) {
102     fprintf(stderr, "  UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
103     if (unit->enabled) {
104       xbt_test_test_t test;
105       unsigned int it_test;
106       xbt_dynar_foreach(unit->tests, it_test, test)
107           xbt_test_test_dump(test);
108     }
109   } else {
110     fprintf(stderr, "  unit=nullptr\n");
111   }
112 }
113
114 /* test suite */
115 struct s_xbt_test_suite {
116   int enabled;
117   const char *name;
118   char *title;
119   xbt_dynar_t units;            /* of xbt_test_unit_t */
120
121   int nb_tests;
122   int nb_units;
123   int test_failed;
124   int test_ignore;
125   int test_expect;
126   int unit_failed;
127   int unit_ignore;
128   int unit_disabled;
129 };
130
131 /* destroy test suite */
132 static void xbt_test_suite_free(void *s)
133 {
134   xbt_test_suite_t suite = *(xbt_test_suite_t *) s;
135
136   if (suite == nullptr)
137     return;
138   xbt_dynar_free(&suite->units);
139   free(suite->title);
140   free(suite);
141 }
142
143 static void xbt_test_unit_free(void *unit)
144 {
145   xbt_test_unit_t u = *(xbt_test_unit_t *) unit;
146   /* name is static */
147   free(u->title);
148   xbt_dynar_free(&u->tests);
149   free(u);
150 }
151
152 static void xbt_test_test_free(void *test)
153 {
154   xbt_test_test_t t = *(xbt_test_test_t *) test;
155   free(t->title);
156   xbt_dynar_free(&(t->logs));
157   free(t);
158 }
159
160 static void xbt_test_log_free(void *log)
161 {
162   xbt_test_log_t l = *(xbt_test_log_t *) log;
163   free(l->text);
164   free(l);
165 }
166
167 /** @brief create test suite */
168 xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
169 {
170   xbt_test_suite_t suite = xbt_new0(s_xbt_test_suite, 1);
171   va_list ap;
172
173   if (_xbt_test_suites == nullptr)
174     _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
175
176   va_start(ap, fmt);
177   suite->title = bvprintf(fmt, ap);
178   suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
179   va_end(ap);
180   suite->name = name;
181   suite->enabled = 1;
182
183   xbt_dynar_push(_xbt_test_suites, &suite);
184
185   return suite;
186 }
187
188 /** @brief retrieve a testsuite from name, or create a new one */
189 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
190 {
191   xbt_test_suite_t suite;
192   char *bufname;
193   va_list ap;
194
195   if (_xbt_test_suites) {
196     unsigned int it_suite;
197     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
198       if (not strcmp(suite->name, name))
199         return suite;
200   }
201
202   va_start(ap, fmt);
203   bufname = bvprintf(fmt, ap);
204   va_end(ap);
205   suite = xbt_test_suite_new(name, bufname, nullptr);
206   free(bufname);
207
208   return suite;
209 }
210
211 void xbt_test_suite_dump(xbt_test_suite_t suite)
212 {
213   if (suite) {
214     fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
215     if (suite->enabled) {
216       xbt_test_unit_t unit;
217       unsigned int it_unit;
218       xbt_dynar_foreach(suite->units, it_unit, unit)
219           xbt_test_unit_dump(unit);
220     }
221   } else {
222     fprintf(stderr, "TESTSUITE IS NULL!\n");
223   }
224 }
225
226 /* add test case to test suite */
227 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
228 {
229   xbt_test_unit_t unit;
230   va_list ap;
231
232   xbt_assert(suite);
233   xbt_assert(func);
234   xbt_assert(fmt);
235
236   unit = xbt_new0(s_xbt_test_unit, 1);
237   va_start(ap, fmt);
238   unit->title = bvprintf(fmt, ap);
239   va_end(ap);
240   unit->name = (char *) name;
241   unit->func = func;
242   unit->file = nullptr;
243   unit->line = 0;
244   unit->enabled = 1;
245   unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
246
247   xbt_dynar_push(suite->units, &unit);
248 }
249
250 /* run test one suite */
251 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
252 {
253   xbt_test_unit_t unit;
254   xbt_test_test_t test;
255   xbt_test_log_t log;
256
257   if (suite == nullptr)
258     return 0;
259
260   /* suite title pretty-printing */
261   char suite_title[81];
262   int suite_len = strlen(suite->title);
263
264   xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
265
266   suite_title[0] = ' ';
267   for (int i = 1; i < 79; i++)
268     suite_title[i] = '=';
269   suite_title[79]  = '\n';
270   suite_title[80]  = '\0';
271
272   snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
273   suite_title[40 + (suite_len + 5) / 2] = '=';
274   if (not suite->enabled)
275     snprintf(suite_title + 70, 11, " DISABLED ");
276   fprintf(stderr, "\n%s\n", suite_title);
277
278   if (suite->enabled) {
279     /* iterate through all tests */
280     unsigned int it_unit;
281     xbt_dynar_foreach(suite->units, it_unit, unit) {
282       /* init unit case counters */
283       unit->nb_tests = 0;
284       unit->test_ignore = 0;
285       unit->test_failed = 0;
286       unit->test_expect = 0;
287
288       /* display unit title */
289       char* cp = bprintf(" Unit: %s ......................................"
290                          "......................................",
291                          unit->title);
292       cp[70] = '\0';
293       fprintf(stderr, "%s", cp);
294       free(cp);
295
296       /* run the test case function */
297       _xbt_test_current_unit = unit;
298       if (unit->enabled)
299         unit->func();
300
301       /* iterate through all performed tests to determine status */
302       unsigned int it_test;
303       xbt_dynar_foreach(unit->tests, it_test, test) {
304         if (test->ignored) {
305           unit->test_ignore++;
306         } else {
307           unit->nb_tests++;
308
309           if (test->failed && not test->expected_failure)
310             unit->test_failed++;
311           if (not test->failed && test->expected_failure)
312             unit->test_failed++;
313           if (test->expected_failure)
314             unit->test_expect++;
315         }
316       }
317       /* Display whether this unit went well */
318       if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
319         /* some tests failed (or were supposed to), so do detailed reporting of test case */
320         if (unit->test_failed > 0) {
321           fprintf(stderr, ".. failed\n");
322         } else if (unit->nb_tests) {
323           fprintf(stderr, "...... ok\n");       /* successful, but show about expected */
324         } else {
325           fprintf(stderr, ".... skip\n");       /* shouldn't happen, but I'm a bit lost with this logic */
326         }
327         xbt_dynar_foreach(unit->tests, it_test, test) {
328           const char* file = (test->file != nullptr ? test->file : unit->file);
329           int line         = (test->line != 0 ? test->line : unit->line);
330           const char* resname;
331           if (test->ignored)
332             resname = " SKIP";
333           else if (test->expected_failure) {
334             if (test->failed)
335               resname = "EFAIL";
336             else
337               resname = "EPASS";
338           } else {
339             if (test->failed)
340               resname = " FAIL";
341             else
342               resname = " PASS";
343           }
344           fprintf(stderr, "      %s: %s [%s:%d]\n", resname, test->title, file, line);
345
346           if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
347             unsigned int it_log;
348             xbt_dynar_foreach(test->logs, it_log, log) {
349               file = (log->file != nullptr ? log->file : file);
350               line = (log->line != 0 ? log->line : line);
351               fprintf(stderr, "             %s:%d: %s\n", file, line, log->text);
352             }
353           }
354         }
355         fprintf(stderr, "    Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
356         if (unit->test_ignore) {
357           fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
358         } else {
359           fprintf(stderr, "\n");
360         }
361       } else if (not unit->enabled) {
362         fprintf(stderr, " disabled\n"); /* no test were run */
363       } else if (unit->nb_tests) {
364         fprintf(stderr, "...... ok\n"); /* successful */
365       } else {
366         fprintf(stderr, ".... skip\n"); /* no test were run */
367       }
368
369       /* Accumulate test counts into the suite */
370       suite->nb_tests += unit->nb_tests;
371       suite->test_failed += unit->test_failed;
372       suite->test_ignore += unit->test_ignore;
373       suite->test_expect += unit->test_expect;
374
375       _xbt_test_nb_tests += unit->nb_tests;
376       _xbt_test_test_failed += unit->test_failed;
377       _xbt_test_test_ignore += unit->test_ignore;
378       _xbt_test_test_expect += unit->test_expect;
379
380       /* What's the conclusion of this test anyway? */
381       if (unit->nb_tests) {
382         suite->nb_units++;
383         if (unit->test_failed)
384           suite->unit_failed++;
385       } else if (not unit->enabled) {
386         suite->unit_disabled++;
387       } else {
388         suite->unit_ignore++;
389       }
390     }
391   }
392   _xbt_test_nb_units += suite->nb_units;
393   _xbt_test_unit_failed += suite->unit_failed;
394   _xbt_test_unit_ignore += suite->unit_ignore;
395   _xbt_test_unit_disabled += suite->unit_disabled;
396
397   if (suite->nb_units) {
398     _xbt_test_nb_suites++;
399     if (suite->test_failed)
400       _xbt_test_suite_failed++;
401   } else if (not suite->enabled) {
402     _xbt_test_suite_disabled++;
403   } else {
404     _xbt_test_suite_ignore++;
405   }
406
407   /* print test suite summary */
408   if (suite->enabled) {
409     int first = 1; /* for result pretty printing */
410
411     fprintf(stderr," =====================================================================%s\n",
412             (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
413                                (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
414     fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
415             ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
416
417     if (suite->nb_units != suite->unit_failed) {
418       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
419       first = 0;
420     }
421     if (suite->unit_failed) {
422       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
423       first = 0;
424     }
425     if (suite->unit_ignore) {
426       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
427       first = 0;
428     }
429     if (suite->unit_disabled) {
430       fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
431     }
432     fprintf(stderr, ")\n          Tests: %.0f%% ok (%d tests: ", suite->nb_tests
433             ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
434
435     first = 1;
436     if (suite->nb_tests != suite->test_failed) {
437       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
438       first = 0;
439     }
440     if (suite->test_failed) {
441       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
442       first = 0;
443     }
444     if (suite->test_ignore) {
445       fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
446       first = 0;
447     }
448     if (suite->test_expect) {
449       fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
450     }
451     fprintf(stderr, ")\n");
452   }
453   return suite->unit_failed;
454 }
455
456 static void apply_selection(char *selection)
457 {
458   /* for the parsing */
459   char *sel = selection;
460   int done = 0;
461   char dir[STRLEN]; /* the directive */
462   /* iterators */
463   unsigned int it_suite;
464   xbt_test_suite_t suite;
465   xbt_test_unit_t unit;
466   unsigned int it_unit;
467
468   char suitename[STRLEN];
469   char unitname[STRLEN];
470
471   if (not selection || selection[0] == '\0')
472     return;
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(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(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(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 */