Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cunit: remove unused functions.
[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 retrieve a testsuite from name, or create a new one */
168 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
169 {
170   if (_xbt_test_suites == nullptr) {
171     _xbt_test_suites = xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
172   } else {
173     xbt_test_suite_t suite;
174     unsigned int it_suite;
175     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
176       if (not strcmp(suite->name, name))
177         return suite;
178   }
179
180   xbt_test_suite_t suite = xbt_new0(s_xbt_test_suite, 1);
181   va_list ap;
182   va_start(ap, fmt);
183   suite->title = bvprintf(fmt, ap);
184   suite->units = xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
185   va_end(ap);
186   suite->name    = name;
187   suite->enabled = 1;
188
189   xbt_dynar_push(_xbt_test_suites, &suite);
190
191   return suite;
192 }
193
194 static void xbt_test_suite_dump(xbt_test_suite_t suite)
195 {
196   if (suite) {
197     fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
198     if (suite->enabled) {
199       xbt_test_unit_t unit;
200       unsigned int it_unit;
201       xbt_dynar_foreach(suite->units, it_unit, unit)
202           xbt_test_unit_dump(unit);
203     }
204   } else {
205     fprintf(stderr, "TESTSUITE IS NULL!\n");
206   }
207 }
208
209 /* add test case to test suite */
210 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
211 {
212   xbt_test_unit_t unit;
213   va_list ap;
214
215   xbt_assert(suite);
216   xbt_assert(func);
217   xbt_assert(fmt);
218
219   unit = xbt_new0(s_xbt_test_unit, 1);
220   va_start(ap, fmt);
221   unit->title = bvprintf(fmt, ap);
222   va_end(ap);
223   unit->name = (char *) name;
224   unit->func = func;
225   unit->file = nullptr;
226   unit->line = 0;
227   unit->enabled = 1;
228   unit->tests = xbt_dynar_new(sizeof(xbt_test_test_t), xbt_test_test_free);
229
230   xbt_dynar_push(suite->units, &unit);
231 }
232
233 /* run test one suite */
234 static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
235 {
236   xbt_test_unit_t unit;
237   xbt_test_test_t test;
238   xbt_test_log_t log;
239
240   if (suite == nullptr)
241     return 0;
242
243   /* suite title pretty-printing */
244   char suite_title[81];
245   int suite_len = strlen(suite->title);
246
247   xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", suite->title, suite_len);
248
249   suite_title[0] = ' ';
250   for (int i = 1; i < 79; i++)
251     suite_title[i] = '=';
252   suite_title[79]  = '\n';
253   suite_title[80]  = '\0';
254
255   snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", suite->title);
256   suite_title[40 + (suite_len + 5) / 2] = '=';
257   if (not suite->enabled)
258     snprintf(suite_title + 70, 11, " DISABLED ");
259   fprintf(stderr, "\n%s\n", suite_title);
260
261   if (suite->enabled) {
262     /* iterate through all tests */
263     unsigned int it_unit;
264     xbt_dynar_foreach(suite->units, it_unit, unit) {
265       /* init unit case counters */
266       unit->nb_tests = 0;
267       unit->test_ignore = 0;
268       unit->test_failed = 0;
269       unit->test_expect = 0;
270
271       /* display unit title */
272       char* cp = bprintf(" Unit: %s ......................................"
273                          "......................................",
274                          unit->title);
275       cp[70] = '\0';
276       fprintf(stderr, "%s", cp);
277       free(cp);
278
279       /* run the test case function */
280       _xbt_test_current_unit = unit;
281       if (unit->enabled)
282         unit->func();
283
284       /* iterate through all performed tests to determine status */
285       unsigned int it_test;
286       xbt_dynar_foreach(unit->tests, it_test, test) {
287         if (test->ignored) {
288           unit->test_ignore++;
289         } else {
290           unit->nb_tests++;
291
292           if (test->failed && not test->expected_failure)
293             unit->test_failed++;
294           if (not test->failed && test->expected_failure)
295             unit->test_failed++;
296           if (test->expected_failure)
297             unit->test_expect++;
298         }
299       }
300       /* Display whether this unit went well */
301       if (unit->test_failed > 0 || unit->test_expect || (verbosity && unit->nb_tests > 0)) {
302         /* some tests failed (or were supposed to), so do detailed reporting of test case */
303         if (unit->test_failed > 0) {
304           fprintf(stderr, ".. failed\n");
305         } else if (unit->nb_tests) {
306           fprintf(stderr, "...... ok\n");       /* successful, but show about expected */
307         } else {
308           fprintf(stderr, ".... skip\n");       /* shouldn't happen, but I'm a bit lost with this logic */
309         }
310         xbt_dynar_foreach(unit->tests, it_test, test) {
311           const char* file = (test->file != nullptr ? test->file : unit->file);
312           int line         = (test->line != 0 ? test->line : unit->line);
313           const char* resname;
314           if (test->ignored)
315             resname = " SKIP";
316           else if (test->expected_failure) {
317             if (test->failed)
318               resname = "EFAIL";
319             else
320               resname = "EPASS";
321           } else {
322             if (test->failed)
323               resname = " FAIL";
324             else
325               resname = " PASS";
326           }
327           fprintf(stderr, "      %s: %s [%s:%d]\n", resname, test->title, file, line);
328
329           if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
330             unsigned int it_log;
331             xbt_dynar_foreach(test->logs, it_log, log) {
332               file = (log->file != nullptr ? log->file : file);
333               line = (log->line != 0 ? log->line : line);
334               fprintf(stderr, "             %s:%d: %s\n", file, line, log->text);
335             }
336           }
337         }
338         fprintf(stderr, "    Summary: %d of %d tests failed", unit->test_failed, unit->nb_tests);
339         if (unit->test_ignore) {
340           fprintf(stderr, " (%d tests ignored)\n", unit->test_ignore);
341         } else {
342           fprintf(stderr, "\n");
343         }
344       } else if (not unit->enabled) {
345         fprintf(stderr, " disabled\n"); /* no test were run */
346       } else if (unit->nb_tests) {
347         fprintf(stderr, "...... ok\n"); /* successful */
348       } else {
349         fprintf(stderr, ".... skip\n"); /* no test were run */
350       }
351
352       /* Accumulate test counts into the suite */
353       suite->nb_tests += unit->nb_tests;
354       suite->test_failed += unit->test_failed;
355       suite->test_ignore += unit->test_ignore;
356       suite->test_expect += unit->test_expect;
357
358       _xbt_test_nb_tests += unit->nb_tests;
359       _xbt_test_test_failed += unit->test_failed;
360       _xbt_test_test_ignore += unit->test_ignore;
361       _xbt_test_test_expect += unit->test_expect;
362
363       /* What's the conclusion of this test anyway? */
364       if (unit->nb_tests) {
365         suite->nb_units++;
366         if (unit->test_failed)
367           suite->unit_failed++;
368       } else if (not unit->enabled) {
369         suite->unit_disabled++;
370       } else {
371         suite->unit_ignore++;
372       }
373     }
374   }
375   _xbt_test_nb_units += suite->nb_units;
376   _xbt_test_unit_failed += suite->unit_failed;
377   _xbt_test_unit_ignore += suite->unit_ignore;
378   _xbt_test_unit_disabled += suite->unit_disabled;
379
380   if (suite->nb_units) {
381     _xbt_test_nb_suites++;
382     if (suite->test_failed)
383       _xbt_test_suite_failed++;
384   } else if (not suite->enabled) {
385     _xbt_test_suite_disabled++;
386   } else {
387     _xbt_test_suite_ignore++;
388   }
389
390   /* print test suite summary */
391   if (suite->enabled) {
392     int first = 1; /* for result pretty printing */
393
394     fprintf(stderr," =====================================================================%s\n",
395             (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
396                                (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
397     fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", suite->nb_units
398             ? ((1 - (double) suite->unit_failed / (double) suite->nb_units) * 100.0) : 100.0, suite->nb_units);
399
400     if (suite->nb_units != suite->unit_failed) {
401       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_units - suite->unit_failed);
402       first = 0;
403     }
404     if (suite->unit_failed) {
405       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->unit_failed);
406       first = 0;
407     }
408     if (suite->unit_ignore) {
409       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), suite->unit_ignore);
410       first = 0;
411     }
412     if (suite->unit_disabled) {
413       fprintf(stderr, "%s%d disabled", (first ? "" : ", "), suite->unit_disabled);
414     }
415     fprintf(stderr, ")\n          Tests: %.0f%% ok (%d tests: ", suite->nb_tests
416             ? ((1 - (double) suite->test_failed / (double) suite->nb_tests) * 100.0) : 100.0, suite->nb_tests);
417
418     first = 1;
419     if (suite->nb_tests != suite->test_failed) {
420       fprintf(stderr, "%s%d ok", (first ? "" : ", "), suite->nb_tests - suite->test_failed);
421       first = 0;
422     }
423     if (suite->test_failed) {
424       fprintf(stderr, "%s%d failed", (first ? "" : ", "), suite->test_failed);
425       first = 0;
426     }
427     if (suite->test_ignore) {
428       fprintf(stderr, "%s%d ignored", (first ? "" : "; "), suite->test_ignore);
429       first = 0;
430     }
431     if (suite->test_expect) {
432       fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), suite->test_expect);
433     }
434     fprintf(stderr, ")\n");
435   }
436   return suite->unit_failed;
437 }
438
439 static void apply_selection(char *selection)
440 {
441   /* for the parsing */
442   char *sel = selection;
443   int done = 0;
444   char dir[STRLEN]; /* the directive */
445   /* iterators */
446   unsigned int it_suite;
447   xbt_test_suite_t suite;
448   xbt_test_unit_t unit;
449   unsigned int it_unit;
450
451   char suitename[STRLEN];
452   char unitname[STRLEN];
453
454   if (not selection || selection[0] == '\0')
455     return;
456
457   /* First apply the selection */
458   while (not done) {
459     int enabling = 1;
460
461     char *p = strchr(sel, ',');
462     if (p) {
463       snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
464       sel = p + 1;
465     } else {
466       snprintf(dir, STRLEN, "%s", sel);
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       snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
482       snprintf(unitname, STRLEN, "%s", p + 1);
483     } else {
484       snprintf(suitename, STRLEN, "%s", dir);
485       unitname[0] = '\0';
486     }
487
488     /* Deal with the specific case of 'all' pseudo serie */
489     if (not strcmp("all", suitename)) {
490       xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
491
492       xbt_dynar_foreach(_xbt_test_suites, it_suite, suite) {
493         xbt_dynar_foreach(suite->units, it_unit, unit) {
494           unit->enabled = enabling;
495         }
496         suite->enabled = enabling;
497       }
498     } else {
499       unsigned int it;
500       for (it = 0; it < xbt_dynar_length(_xbt_test_suites); it++) {
501         xbt_test_suite_t thissuite =
502             xbt_dynar_get_as(_xbt_test_suites, it, xbt_test_suite_t);
503         if (not strcmp(suitename, thissuite->name)) {
504           /* Do not disable the whole suite when we just want to disable a child */
505           if (enabling || (unitname[0] == '\0'))
506             thissuite->enabled = enabling;
507
508           if (unitname[0] == '\0') {
509             xbt_dynar_foreach(thissuite->units, it_unit, unit) {
510               unit->enabled = enabling;
511             }
512           } else {              /* act on one child only */
513             unsigned int it2_unit;
514             /* search it, first (we won't reuse it for external loop which gets broken) */
515             for (it2_unit = 0;
516                  it2_unit < xbt_dynar_length(thissuite->units);
517                  it2_unit++) {
518               xbt_test_unit_t thisunit = xbt_dynar_get_as(thissuite->units, it2_unit, xbt_test_unit_t);
519               if (not strcmp(thisunit->name, unitname)) {
520                 thisunit->enabled = enabling;
521                 break;
522               }
523             }                   /* search relevant unit */
524             xbt_assert(it2_unit != xbt_dynar_length(thissuite->units),
525                 "Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
526           }                     /* act on childs (either all or one) */
527
528           break;                /* found the relevant serie. We are happy */
529         }
530       }                         /* search relevant series */
531       xbt_assert(it != xbt_dynar_length(_xbt_test_suites),
532                  "No suite of name '%s' found. Cannot apply the selection\n", suitename);
533     }
534   }
535 }
536
537 void xbt_test_dump(char *selection)
538 {
539   apply_selection(selection);
540
541   if (_xbt_test_suites) {
542     unsigned int it_suite;
543     xbt_test_suite_t suite;
544
545     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
546         xbt_test_suite_dump(suite);
547   } else {
548     printf(" No suite defined.");
549   }
550 }
551
552 int xbt_test_run(char *selection, int verbosity)
553 {
554   apply_selection(selection);
555
556   if (_xbt_test_suites) {
557     unsigned int it_suite;
558     xbt_test_suite_t suite;
559     int first = 1;
560
561     /* Run all the suites */
562     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
563       xbt_test_suite_run(suite, verbosity);
564
565     /* Display some more statistics */
566     fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
567             ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
568             : 100.0, _xbt_test_nb_suites);
569     if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
570       fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
571       first = 0;
572     }
573     if (_xbt_test_suite_failed) {
574       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
575       first = 0;
576     }
577
578     if (_xbt_test_suite_ignore) {
579       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
580     }
581     fprintf(stderr, ")\n        Units:  %.0f%% ok (%d units: ", _xbt_test_nb_units
582             ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
583     first = 1;
584     if (_xbt_test_nb_units != _xbt_test_unit_failed) {
585       fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
586       first = 0;
587     }
588     if (_xbt_test_unit_failed) {
589       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
590       first = 0;
591     }
592     if (_xbt_test_unit_ignore) {
593       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
594     }
595     fprintf(stderr, ")\n        Tests:  %.0f%% ok (%d tests: ", _xbt_test_nb_tests
596             ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
597     first = 1;
598     if (_xbt_test_nb_tests != _xbt_test_test_failed) {
599       fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
600       first = 0;
601     }
602     if (_xbt_test_test_failed) {
603       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
604       first = 0;
605     }
606     if (_xbt_test_test_ignore) {
607       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
608       first = 0;
609     }
610     if (_xbt_test_test_expect) {
611       fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
612     }
613
614     fprintf(stderr, ")\n");
615   } else {
616     fprintf(stderr, "No unit to run!\n");
617     _xbt_test_unit_failed++;
618   }
619   return _xbt_test_unit_failed;
620 }
621
622 void xbt_test_exit()
623 {
624   xbt_dynar_free(&_xbt_test_suites);
625 }
626
627 /* annotate test case with test */
628 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
629 {
630   xbt_test_unit_t unit = _xbt_test_current_unit;
631   xbt_assert(unit);
632
633   va_list ap;
634   xbt_test_test_t test = xbt_new0(s_xbt_test_test, 1);
635   va_start(ap, fmt);
636   test->title = bvprintf(fmt, ap);
637   va_end(ap);
638   test->failed = 0;
639   test->expected_failure = 0;
640   test->ignored = 0;
641   test->file = file;
642   test->line = line;
643   test->logs = xbt_dynar_new(sizeof(xbt_test_log_t), xbt_test_log_free);
644   xbt_dynar_push(unit->tests, &test);
645 }
646
647 /* annotate test case with log message and failure */
648 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
649 {
650   xbt_test_unit_t unit = _xbt_test_current_unit;
651   xbt_assert(unit);
652   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
653       "Test failed even before being declared (broken unit: %s)", unit->title);
654
655   va_list ap;
656   xbt_test_log_t log = xbt_new(s_xbt_test_log, 1);
657   va_start(ap, fmt);
658   log->text = bvprintf(fmt, ap);
659   va_end(ap);
660   log->file = file;
661   log->line = line;
662
663   xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
664   xbt_dynar_push(test->logs, &log);
665
666   test->failed = 1;
667 }
668
669 void xbt_test_exception(xbt_ex_t e)
670 {
671   _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
672 }
673
674 void xbt_test_expect_failure()
675 {
676   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
677       "Cannot expect the failure of a test before declaring it (broken unit: %s)", _xbt_test_current_unit->title);
678   xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
679   test->expected_failure = 1;
680 }
681
682 void xbt_test_skip()
683 {
684   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
685       "Test skipped even before being declared (broken unit: %s)", _xbt_test_current_unit->title);
686   xbt_test_test_t test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests, xbt_test_test_t);
687   test->ignored = 1;
688 }
689
690 /* annotate test case with log message only */
691 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
692 {
693   xbt_test_unit_t unit = _xbt_test_current_unit;
694   xbt_assert(unit);
695   xbt_assert(xbt_dynar_length(_xbt_test_current_unit->tests),
696       "Test logged into even before being declared (broken test unit: %s)", unit->title);
697
698   va_list ap;
699   xbt_test_log_t log = xbt_new(s_xbt_test_log, 1);
700   va_start(ap, fmt);
701   log->text = bvprintf(fmt, ap);
702   va_end(ap);
703   log->file = file;
704   log->line = line;
705
706   xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests, xbt_test_test_t);
707   xbt_dynar_push(test->logs, &log);
708 }
709
710 #ifdef SIMGRID_TEST
711 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
712
713 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
714 {
715   xbt_test_add("Skipped test");
716   xbt_test_skip();
717
718   xbt_test_add("%s %s", "EXPECTED", "FAILURE");
719   xbt_test_expect_failure();
720   xbt_test_log("%s %s", "Test", "log");
721   xbt_test_fail("EXPECTED FAILURE");
722 }
723 #endif                          /* SIMGRID_TEST */