Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cunit: C++-ify s_xbt_test_suite.
[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 <algorithm>
13 #include <cstdio>
14 #include <string>
15 #include <vector>
16
17 #include <xbt/cunit.h>
18 #include <xbt/ex.hpp>
19 #include <xbt/string.hpp>
20
21 #define STRLEN 1024
22
23 /* collection of all suites */
24 static std::vector<xbt_test_suite_t> _xbt_test_suites;
25 /* global statistics */
26 static int _xbt_test_nb_tests = 0;
27 static int _xbt_test_test_failed = 0;
28 static int _xbt_test_test_ignore = 0;
29 static int _xbt_test_test_expect = 0;
30
31 static int _xbt_test_nb_units = 0;
32 static int _xbt_test_unit_failed = 0;
33 static int _xbt_test_unit_ignore = 0;
34 static int _xbt_test_unit_disabled = 0;
35
36 static int _xbt_test_nb_suites = 0;
37 static int _xbt_test_suite_failed = 0;
38 static int _xbt_test_suite_ignore = 0;
39 static int _xbt_test_suite_disabled = 0;
40
41 /* Context */
42 xbt_test_unit_t _xbt_test_current_unit = nullptr;
43
44 /* test suite test log */
45 class s_xbt_test_log {
46 public:
47   s_xbt_test_log(std::string text, std::string file, int line)
48       : text_(std::move(text)), file_(std::move(file)), line_(line)
49   {
50   }
51   void dump() const;
52
53   std::string text_;
54   std::string file_;
55   int line_;
56 };
57
58 void s_xbt_test_log::dump() const
59 {
60   fprintf(stderr, "      log %p(%s:%d)=%s\n", this, this->file_.c_str(), this->line_, this->text_.c_str());
61 }
62
63 /* test suite test check */
64 class s_xbt_test_test {
65 public:
66   s_xbt_test_test(std::string title, std::string file, int line)
67       : title_(std::move(title)), file_(std::move(file)), line_(line)
68   {
69   }
70   void dump() const;
71
72   std::string title_;
73   bool failed_           = false;
74   bool expected_failure_ = false;
75   bool ignored_          = false;
76   std::string file_;
77   int line_;
78   std::vector<s_xbt_test_log> logs_;
79 };
80
81 void s_xbt_test_test::dump() const
82 {
83   fprintf(stderr, "    test %p(%s:%d)=%s (%s)\n", this, this->file_.c_str(), this->line_, this->title_.c_str(),
84           this->failed_ ? "failed" : "not failed");
85   for (s_xbt_test_log const& log : this->logs_)
86     log.dump();
87 }
88
89 /* test suite test unit */
90 class s_xbt_test_unit {
91 public:
92   s_xbt_test_unit(std::string name, std::string title, ts_test_cb_t func)
93       : name_(std::move(name)), title_(std::move(title)), func_(func)
94   {
95   }
96   void dump() const;
97
98   std::string name_;
99   std::string title_;
100   ts_test_cb_t func_;
101   std::vector<s_xbt_test_test> tests_;
102
103   bool enabled_    = true;
104   int nb_tests_    = 0;
105   int test_failed_ = 0;
106   int test_ignore_ = 0;
107   int test_expect_ = 0;
108 };
109
110 void s_xbt_test_unit::dump() const
111 {
112   fprintf(stderr, "  UNIT %s: %s (%s)\n", this->name_.c_str(), this->title_.c_str(),
113           (this->enabled_ ? "enabled" : "disabled"));
114   if (this->enabled_) {
115     for (s_xbt_test_test const& test : this->tests_)
116       test.dump();
117   }
118 }
119
120 /* test suite */
121 class s_xbt_test_suite {
122 public:
123   s_xbt_test_suite(std::string name, std::string title) : name_(std::move(name)), title_(std::move(title)) {}
124   void dump() const;
125   void push(s_xbt_test_unit unit) { units_.emplace_back(std::move(unit)); }
126   int run(int verbosity);
127
128   std::string name_;
129   std::string title_;
130   std::vector<s_xbt_test_unit> units_;
131
132   bool enabled_      = true;
133   int nb_tests_      = 0;
134   int nb_units_      = 0;
135   int test_failed_   = 0;
136   int test_ignore_   = 0;
137   int test_expect_   = 0;
138   int unit_failed_   = 0;
139   int unit_ignore_   = 0;
140   int unit_disabled_ = 0;
141 };
142
143 /** @brief retrieve a testsuite from name, or create a new one */
144 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
145 {
146   auto res = std::find_if(begin(_xbt_test_suites), end(_xbt_test_suites),
147                           [&name](xbt_test_suite_t const& suite) { return suite->name_ == name; });
148   if (res != end(_xbt_test_suites))
149     return *res;
150
151   va_list ap;
152   va_start(ap, fmt);
153   xbt_test_suite_t suite = new s_xbt_test_suite(name, simgrid::xbt::string_vprintf(fmt, ap));
154   va_end(ap);
155
156   _xbt_test_suites.push_back(suite);
157
158   return suite;
159 }
160
161 void s_xbt_test_suite::dump() const
162 {
163   fprintf(stderr, "TESTSUITE %s: %s (%s)\n", this->name_.c_str(), this->title_.c_str(),
164           this->enabled_ ? "enabled" : "disabled");
165   if (this->enabled_) {
166     for (s_xbt_test_unit const& unit : this->units_)
167       unit.dump();
168   }
169 }
170
171 void xbt_test_suite_dump(xbt_test_suite_t suite)
172 {
173   suite->dump();
174 }
175
176 /* add test case to test suite */
177 void xbt_test_suite_push(xbt_test_suite_t suite, const char *name, ts_test_cb_t func, const char *fmt, ...)
178 {
179   xbt_assert(suite);
180   xbt_assert(func);
181   xbt_assert(fmt);
182
183   va_list ap;
184   va_start(ap, fmt);
185   s_xbt_test_unit unit(name, simgrid::xbt::string_vprintf(fmt, ap), func);
186   va_end(ap);
187   suite->push(unit);
188 }
189
190 /* run test one suite */
191 int s_xbt_test_suite::run(int verbosity)
192 {
193   /* suite title pretty-printing */
194   char suite_title[81];
195   int suite_len = this->title_.length();
196
197   xbt_assert(suite_len < 68, "suite title \"%s\" too long (%d should be less than 68", this->title_.c_str(), suite_len);
198
199   suite_title[0] = ' ';
200   for (int i = 1; i < 79; i++)
201     suite_title[i] = '=';
202   suite_title[79]  = '\n';
203   suite_title[80]  = '\0';
204
205   snprintf(suite_title + 40 - (suite_len + 4) / 2, 81 - (40 - (suite_len + 4) / 2), "[ %s ]", this->title_.c_str());
206   suite_title[40 + (suite_len + 5) / 2] = '=';
207   if (not this->enabled_)
208     snprintf(suite_title + 70, 11, " DISABLED ");
209   fprintf(stderr, "\n%s\n", suite_title);
210
211   if (this->enabled_) {
212     /* iterate through all tests */
213     for (s_xbt_test_unit& unit : this->units_) {
214       /* init unit case counters */
215       unit.nb_tests_    = 0;
216       unit.test_ignore_ = 0;
217       unit.test_failed_ = 0;
218       unit.test_expect_ = 0;
219
220       /* display unit title */
221       char* cp = bprintf(" Unit: %s ......................................"
222                          "......................................",
223                          unit.title_.c_str());
224       cp[70] = '\0';
225       fprintf(stderr, "%s", cp);
226       free(cp);
227
228       /* run the test case function */
229       _xbt_test_current_unit = &unit;
230       if (unit.enabled_)
231         unit.func_();
232
233       /* iterate through all performed tests to determine status */
234       for (s_xbt_test_test const& test : unit.tests_) {
235         if (test.ignored_) {
236           unit.test_ignore_++;
237         } else {
238           unit.nb_tests_++;
239
240           if ((test.failed_ && not test.expected_failure_) || (not test.failed_ && test.expected_failure_))
241             unit.test_failed_++;
242           if (test.expected_failure_)
243             unit.test_expect_++;
244         }
245       }
246       /* Display whether this unit went well */
247       if (unit.test_failed_ > 0 || unit.test_expect_ || (verbosity && unit.nb_tests_ > 0)) {
248         /* some tests failed (or were supposed to), so do detailed reporting of test case */
249         if (unit.test_failed_ > 0) {
250           fprintf(stderr, ".. failed\n");
251         } else if (unit.nb_tests_) {
252           fprintf(stderr, "...... ok\n");       /* successful, but show about expected */
253         } else {
254           fprintf(stderr, ".... skip\n");       /* shouldn't happen, but I'm a bit lost with this logic */
255         }
256         for (s_xbt_test_test const& test : unit.tests_) {
257           std::string file = test.file_;
258           int line         = test.line_;
259           const char* resname;
260           if (test.ignored_)
261             resname = " SKIP";
262           else if (test.expected_failure_) {
263             if (test.failed_)
264               resname = "EFAIL";
265             else
266               resname = "EPASS";
267           } else {
268             if (test.failed_)
269               resname = " FAIL";
270             else
271               resname = " PASS";
272           }
273           fprintf(stderr, "      %s: %s [%s:%d]\n", resname, test.title_.c_str(), file.c_str(), line);
274
275           if ((test.expected_failure_ && not test.failed_) || (not test.expected_failure_ && test.failed_)) {
276             for (s_xbt_test_log const& log : test.logs_) {
277               file = (log.file_.empty() ? file : log.file_);
278               line = (log.line_ == 0 ? line : log.line_);
279               fprintf(stderr, "             %s:%d: %s\n", file.c_str(), line, log.text_.c_str());
280             }
281           }
282         }
283         fprintf(stderr, "    Summary: %d of %d tests failed", unit.test_failed_, unit.nb_tests_);
284         if (unit.test_ignore_) {
285           fprintf(stderr, " (%d tests ignored)\n", unit.test_ignore_);
286         } else {
287           fprintf(stderr, "\n");
288         }
289       } else if (not unit.enabled_) {
290         fprintf(stderr, " disabled\n"); /* no test were run */
291       } else if (unit.nb_tests_) {
292         fprintf(stderr, "...... ok\n"); /* successful */
293       } else {
294         fprintf(stderr, ".... skip\n"); /* no test were run */
295       }
296
297       /* Accumulate test counts into the suite */
298       this->nb_tests_ += unit.nb_tests_;
299       this->test_failed_ += unit.test_failed_;
300       this->test_ignore_ += unit.test_ignore_;
301       this->test_expect_ += unit.test_expect_;
302
303       _xbt_test_nb_tests += unit.nb_tests_;
304       _xbt_test_test_failed += unit.test_failed_;
305       _xbt_test_test_ignore += unit.test_ignore_;
306       _xbt_test_test_expect += unit.test_expect_;
307
308       /* What's the conclusion of this test anyway? */
309       if (unit.nb_tests_) {
310         this->nb_units_++;
311         if (unit.test_failed_)
312           this->unit_failed_++;
313       } else if (not unit.enabled_) {
314         this->unit_disabled_++;
315       } else {
316         this->unit_ignore_++;
317       }
318     }
319   }
320   _xbt_test_nb_units += this->nb_units_;
321   _xbt_test_unit_failed += this->unit_failed_;
322   _xbt_test_unit_ignore += this->unit_ignore_;
323   _xbt_test_unit_disabled += this->unit_disabled_;
324
325   if (this->nb_units_) {
326     _xbt_test_nb_suites++;
327     if (this->test_failed_)
328       _xbt_test_suite_failed++;
329   } else if (not this->enabled_) {
330     _xbt_test_suite_disabled++;
331   } else {
332     _xbt_test_suite_ignore++;
333   }
334
335   /* print test suite summary */
336   if (this->enabled_) {
337     int first = 1; /* for result pretty printing */
338
339     fprintf(stderr, " =====================================================================%s\n",
340             (this->nb_units_ ? (this->unit_failed_ ? "== FAILED" : "====== OK")
341                              : (this->unit_disabled_ ? " DISABLED" : "==== SKIP")));
342     fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ",
343             this->nb_units_ ? ((1 - (double)this->unit_failed_ / (double)this->nb_units_) * 100.0) : 100.0,
344             this->nb_units_);
345
346     if (this->nb_units_ != this->unit_failed_) {
347       fprintf(stderr, "%s%d ok", (first ? "" : ", "), this->nb_units_ - this->unit_failed_);
348       first = 0;
349     }
350     if (this->unit_failed_) {
351       fprintf(stderr, "%s%d failed", (first ? "" : ", "), this->unit_failed_);
352       first = 0;
353     }
354     if (this->unit_ignore_) {
355       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), this->unit_ignore_);
356       first = 0;
357     }
358     if (this->unit_disabled_) {
359       fprintf(stderr, "%s%d disabled", (first ? "" : ", "), this->unit_disabled_);
360     }
361     fprintf(stderr, ")\n          Tests: %.0f%% ok (%d tests: ",
362             this->nb_tests_ ? ((1 - (double)this->test_failed_ / (double)this->nb_tests_) * 100.0) : 100.0,
363             this->nb_tests_);
364
365     first = 1;
366     if (this->nb_tests_ != this->test_failed_) {
367       fprintf(stderr, "%s%d ok", (first ? "" : ", "), this->nb_tests_ - this->test_failed_);
368       first = 0;
369     }
370     if (this->test_failed_) {
371       fprintf(stderr, "%s%d failed", (first ? "" : ", "), this->test_failed_);
372       first = 0;
373     }
374     if (this->test_ignore_) {
375       fprintf(stderr, "%s%d ignored", (first ? "" : "; "), this->test_ignore_);
376       first = 0;
377     }
378     if (this->test_expect_) {
379       fprintf(stderr, "%s%d expected to fail", (first ? "" : "; "), this->test_expect_);
380     }
381     fprintf(stderr, ")\n");
382   }
383   return this->unit_failed_;
384 }
385
386 static void apply_selection(char *selection)
387 {
388   /* for the parsing */
389   char *sel = selection;
390   int done = 0;
391   char dir[STRLEN]; /* the directive */
392
393   char suitename[STRLEN];
394   char unitname[STRLEN];
395
396   if (not selection || selection[0] == '\0')
397     return;
398
399   /* First apply the selection */
400   while (not done) {
401     int enabling = 1;
402
403     char *p = strchr(sel, ',');
404     if (p) {
405       snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
406       sel = p + 1;
407     } else {
408       snprintf(dir, STRLEN, "%s", sel);
409       done = 1;
410     }
411
412     if (dir[0] == '-') {
413       enabling = 0;
414       memmove(dir, dir + 1, strlen(dir));
415     }
416     if (dir[0] == '+') {
417       enabling = 1;
418       memmove(dir, dir + 1, strlen(dir));
419     }
420
421     p = strchr(dir, ':');
422     if (p) {
423       snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
424       snprintf(unitname, STRLEN, "%s", p + 1);
425     } else {
426       snprintf(suitename, STRLEN, "%s", dir);
427       unitname[0] = '\0';
428     }
429
430     /* Deal with the specific case of 'all' pseudo serie */
431     if (not strcmp("all", suitename)) {
432       xbt_assert(unitname[0] == '\0', "The 'all' pseudo-suite does not accept any unit specification\n");
433
434       for (xbt_test_suite_t& suite : _xbt_test_suites) {
435         for (s_xbt_test_unit& unit : suite->units_) {
436           unit.enabled_ = enabling;
437         }
438         suite->enabled_ = enabling;
439       }
440     } else {
441       bool suitefound = false;
442       for (xbt_test_suite_t& thissuite : _xbt_test_suites) {
443         if (suitename == thissuite->name_) {
444           /* Do not disable the whole suite when we just want to disable a child */
445           if (enabling || (unitname[0] == '\0'))
446             thissuite->enabled_ = enabling;
447
448           if (unitname[0] == '\0') {
449             for (s_xbt_test_unit& unit : thissuite->units_) {
450               unit.enabled_ = enabling;
451             }
452           } else {              /* act on one child only */
453             /* search relevant unit */
454             auto unit = std::find_if(begin(thissuite->units_), end(thissuite->units_),
455                                      [&unitname](s_xbt_test_unit const& unit) { return unit.name_ == unitname; });
456             if (unit == end(thissuite->units_))
457               xbt_die("Suite '%s' has no unit of name '%s'. Cannot apply the selection\n", suitename, unitname);
458             unit->enabled_ = enabling;
459           }                     /* act on childs (either all or one) */
460           suitefound = true;
461           break;                /* found the relevant serie. We are happy */
462         }
463       }                         /* search relevant series */
464       xbt_assert(suitefound, "No suite of name '%s' found. Cannot apply the selection\n", suitename);
465     }
466   }
467 }
468
469 void xbt_test_dump(char *selection)
470 {
471   apply_selection(selection);
472
473   if (not _xbt_test_suites.empty()) {
474     for (xbt_test_suite_t suite : _xbt_test_suites)
475       suite->dump();
476   } else {
477     printf(" No suite defined.");
478   }
479 }
480
481 int xbt_test_run(char *selection, int verbosity)
482 {
483   apply_selection(selection);
484
485   if (not _xbt_test_suites.empty()) {
486     int first = 1;
487
488     /* Run all the suites */
489     for (xbt_test_suite_t& suite : _xbt_test_suites)
490       if (suite)
491         suite->run(verbosity);
492
493     /* Display some more statistics */
494     fprintf(stderr, "\n\n TOTAL: Suites: %.0f%% ok (%d suites: ",_xbt_test_nb_suites
495             ? ((1 - (double) _xbt_test_suite_failed / (double) _xbt_test_nb_suites) * 100.0)
496             : 100.0, _xbt_test_nb_suites);
497     if (_xbt_test_nb_suites != _xbt_test_suite_failed) {
498       fprintf(stderr, "%d ok", _xbt_test_nb_suites - _xbt_test_suite_failed);
499       first = 0;
500     }
501     if (_xbt_test_suite_failed) {
502       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_suite_failed);
503       first = 0;
504     }
505
506     if (_xbt_test_suite_ignore) {
507       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_suite_ignore);
508     }
509     fprintf(stderr, ")\n        Units:  %.0f%% ok (%d units: ", _xbt_test_nb_units
510             ? ((1 - (double) _xbt_test_unit_failed / (double) _xbt_test_nb_units) * 100.0) : 100.0, _xbt_test_nb_units);
511     first = 1;
512     if (_xbt_test_nb_units != _xbt_test_unit_failed) {
513       fprintf(stderr, "%d ok", _xbt_test_nb_units - _xbt_test_unit_failed);
514       first = 0;
515     }
516     if (_xbt_test_unit_failed) {
517       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_unit_failed);
518       first = 0;
519     }
520     if (_xbt_test_unit_ignore) {
521       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_unit_ignore);
522     }
523     fprintf(stderr, ")\n        Tests:  %.0f%% ok (%d tests: ", _xbt_test_nb_tests
524             ? ((1 - (double) _xbt_test_test_failed / (double) _xbt_test_nb_tests) * 100.0) : 100.0, _xbt_test_nb_tests);
525     first = 1;
526     if (_xbt_test_nb_tests != _xbt_test_test_failed) {
527       fprintf(stderr, "%d ok", _xbt_test_nb_tests - _xbt_test_test_failed);
528       first = 0;
529     }
530     if (_xbt_test_test_failed) {
531       fprintf(stderr, "%s%d failed", (first ? "" : ", "), _xbt_test_test_failed);
532       first = 0;
533     }
534     if (_xbt_test_test_ignore) {
535       fprintf(stderr, "%s%d ignored", (first ? "" : ", "), _xbt_test_test_ignore);
536       first = 0;
537     }
538     if (_xbt_test_test_expect) {
539       fprintf(stderr, "%s%d expected to fail", (first ? "" : ", "), _xbt_test_test_expect);
540     }
541
542     fprintf(stderr, ")\n");
543   } else {
544     fprintf(stderr, "No unit to run!\n");
545     _xbt_test_unit_failed++;
546   }
547   return _xbt_test_unit_failed;
548 }
549
550 void xbt_test_exit()
551 {
552   for (xbt_test_suite_t suite : _xbt_test_suites)
553     delete suite;
554   _xbt_test_suites.clear();
555 }
556
557 /* annotate test case with test */
558 void _xbt_test_add(const char *file, int line, const char *fmt, ...)
559 {
560   xbt_test_unit_t unit = _xbt_test_current_unit;
561   xbt_assert(unit);
562
563   va_list ap;
564   va_start(ap, fmt);
565   unit->tests_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
566   va_end(ap);
567 }
568
569 /* annotate test case with log message and failure */
570 void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
571 {
572   xbt_test_unit_t unit = _xbt_test_current_unit;
573   xbt_assert(unit);
574   xbt_assert(not _xbt_test_current_unit->tests_.empty(), "Test failed even before being declared (broken unit: %s)",
575              unit->title_.c_str());
576
577   s_xbt_test_test& test = unit->tests_.back();
578   va_list ap;
579   va_start(ap, fmt);
580   test.logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
581   va_end(ap);
582
583   test.failed_ = true;
584 }
585
586 void xbt_test_exception(xbt_ex_t e)
587 {
588   _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
589 }
590
591 void xbt_test_expect_failure()
592 {
593   xbt_assert(not _xbt_test_current_unit->tests_.empty(),
594              "Cannot expect the failure of a test before declaring it (broken unit: %s)",
595              _xbt_test_current_unit->title_.c_str());
596   _xbt_test_current_unit->tests_.back().expected_failure_ = true;
597 }
598
599 void xbt_test_skip()
600 {
601   xbt_assert(not _xbt_test_current_unit->tests_.empty(), "Test skipped even before being declared (broken unit: %s)",
602              _xbt_test_current_unit->title_.c_str());
603   _xbt_test_current_unit->tests_.back().ignored_ = true;
604 }
605
606 /* annotate test case with log message only */
607 void _xbt_test_log(const char *file, int line, const char *fmt, ...)
608 {
609   xbt_test_unit_t unit = _xbt_test_current_unit;
610   xbt_assert(unit);
611   xbt_assert(not _xbt_test_current_unit->tests_.empty(),
612              "Test logged into even before being declared (broken test unit: %s)", unit->title_.c_str());
613
614   va_list ap;
615   va_start(ap, fmt);
616   unit->tests_.back().logs_.emplace_back(simgrid::xbt::string_vprintf(fmt, ap), file, line);
617   va_end(ap);
618 }
619
620 #ifdef SIMGRID_TEST
621 XBT_TEST_SUITE("cunit", "Testsuite mechanism autotest");
622
623 XBT_TEST_UNIT("expect", test_expected_failure, "expected failures")
624 {
625   xbt_test_add("Skipped test");
626   xbt_test_skip();
627
628   xbt_test_add("%s %s", "EXPECTED", "FAILURE");
629   xbt_test_expect_failure();
630   xbt_test_log("%s %s", "Test", "log");
631   xbt_test_fail("EXPECTED FAILURE");
632 }
633 #endif                          /* SIMGRID_TEST */