Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Display what's going on as the unit is running, not afterward (some of them are a...
[simgrid.git] / src / xbt / cunit.c
index 6f8faf5..035f1e1 100644 (file)
@@ -15,8 +15,6 @@
 #include "xbt/cunit.h"
 #include "xbt/dynar.h"
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(testsuite,xbt,"Test infrastructure");
-
 /* collection of all suites */
 static xbt_dynar_t _xbt_test_suites = NULL; 
 /* global statistics */
@@ -34,6 +32,10 @@ static int _xbt_test_suite_failed = 0;
 static int _xbt_test_suite_ignore = 0;
 
 
+/* Context */
+xbt_test_unit_t _xbt_test_current_unit = NULL;
+
+
 /* test suite test log */
 typedef struct s_xbt_test_log {
   char              *text;
@@ -216,63 +218,6 @@ static int xbt_test_suite_run(xbt_test_suite_t suite) {
   if (suite == NULL)
     return 0;
 
-  /* iterate through all tests to see how much failed */
-  xbt_dynar_foreach(suite->units, it_unit, unit) {
-    /* init unit case counters */
-    unit->nb_tests = 0;
-    unit->test_ignore = 0;
-    unit->test_failed = 0;
-    unit->test_expect = 0;
-
-    /* run the test case function */
-    unit->func(unit);
-  
-    /* iterate through all performed tests to determine status */
-    xbt_dynar_foreach(unit->tests,it_test, test) {
-      if (test->ignored) {
-       unit->test_ignore++;
-      } else {
-       unit->nb_tests++;
-
-       if ( test->failed && !test->expected_failure) unit->test_failed++;
-       if (!test->failed &&  test->expected_failure) unit->test_failed++;
-       if (test->expected_failure)
-         unit->test_expect++;
-      }
-    }
-    
-    /* Accumulate test counts into the suite */
-    suite->nb_tests    += unit->nb_tests;
-    suite->test_failed += unit->test_failed;
-    suite->test_ignore += unit->test_ignore;
-    suite->test_expect += unit->test_expect;
-
-    _xbt_test_nb_tests    += unit->nb_tests;
-    _xbt_test_test_failed += unit->test_failed;
-    _xbt_test_test_ignore += unit->test_ignore;
-    _xbt_test_test_expect += unit->test_expect;
-    
-    /* What's the conclusion of this test anyway? */
-    if (unit->nb_tests) {
-      suite->nb_units++;
-      if (unit->test_failed)
-       suite->unit_failed++;
-    } else {
-      suite->unit_ignore++;
-    }
-  }
-  _xbt_test_nb_units    += suite->nb_units;
-  _xbt_test_unit_failed += suite->unit_failed;
-  _xbt_test_unit_ignore += suite->unit_ignore;
-
-  if (suite->nb_units) {
-    _xbt_test_nb_suites++;
-    if (suite->test_failed)
-      _xbt_test_suite_failed++;
-  } else {
-    _xbt_test_suite_ignore++;
-  }
-
   /* suite title pretty-printing */
   {
     char suite_title[80];
@@ -288,21 +233,44 @@ static int xbt_test_suite_run(xbt_test_suite_t suite) {
     suite_title[i]='\0';
 
     sprintf(suite_title + 40 - (suite_len+4)/2, "[ %s ]", suite->title);
-    suite_title[40 + (suite_len+4)/2] = '=';
-
-    fprintf(stderr, "\n%s  %s\n",suite_title,
-           (suite->nb_units?(suite->unit_failed?"FAILED":"OK"):"SKIP"));
-    
+    suite_title[40 + (suite_len+5)/2] = '=';
+    fprintf(stderr, "\n%s\n",suite_title);
   }
-  
-  /* iterate through all test cases to display details */
+
+  /* iterate through all tests */
   xbt_dynar_foreach(suite->units, it_unit, unit) {
+    /* init unit case counters */
+    unit->nb_tests = 0;
+    unit->test_ignore = 0;
+    unit->test_failed = 0;
+    unit->test_expect = 0;
+
+    /* display unit title */
     asprintf(&cp," Unit: %s ........................................"
             "........................................", unit->title);
     cp[72] = '\0';
     fprintf(stderr, "%s", cp);
     free(cp);
+
+    /* run the test case function */
+    _xbt_test_current_unit = unit;
+    unit->func();
     
+    /* iterate through all performed tests to determine status */
+    xbt_dynar_foreach(unit->tests,it_test, test) {
+      if (test->ignored) {
+       unit->test_ignore++;
+      } else {
+       unit->nb_tests++;
+
+       if ( test->failed && !test->expected_failure) unit->test_failed++;
+       if (!test->failed &&  test->expected_failure) unit->test_failed++;
+       if (test->expected_failure)
+         unit->test_expect++;
+      }
+    }
+
+    /* Display whether this unit went well */
     if (unit->test_failed > 0 || unit->test_expect) {
       /* some tests failed (or were supposed to), so do detailed reporting of test case */
       if (unit->test_failed > 0) {
@@ -339,10 +307,47 @@ static int xbt_test_suite_run(xbt_test_suite_t suite) {
     } else {
       fprintf(stderr, ".. skip\n"); /* no test were run */
     }
+
+
+    
+
+    /* Accumulate test counts into the suite */
+    suite->nb_tests    += unit->nb_tests;
+    suite->test_failed += unit->test_failed;
+    suite->test_ignore += unit->test_ignore;
+    suite->test_expect += unit->test_expect;
+
+    _xbt_test_nb_tests    += unit->nb_tests;
+    _xbt_test_test_failed += unit->test_failed;
+    _xbt_test_test_ignore += unit->test_ignore;
+    _xbt_test_test_expect += unit->test_expect;
+    
+    /* What's the conclusion of this test anyway? */
+    if (unit->nb_tests) {
+      suite->nb_units++;
+      if (unit->test_failed)
+       suite->unit_failed++;
+    } else {
+      suite->unit_ignore++;
+    }
   }
+  _xbt_test_nb_units    += suite->nb_units;
+  _xbt_test_unit_failed += suite->unit_failed;
+  _xbt_test_unit_ignore += suite->unit_ignore;
+
+  if (suite->nb_units) {
+    _xbt_test_nb_suites++;
+    if (suite->test_failed)
+      _xbt_test_suite_failed++;
+  } else {
+    _xbt_test_suite_ignore++;
+  }
+
   
   /* print test suite summary */
-  fprintf(stderr, " ==============================================================================\n");
+  fprintf(stderr,
+         " =======================================================================%s\n",
+         (suite->nb_units?(suite->unit_failed?" FAILED":"==== OK"):"== SKIP"));
   fprintf(stderr, " Summary: Units: %.0f%% ok (%d units: ", 
          suite->nb_units?((1-(double)suite->unit_failed/(double)suite->nb_units)*100.0):100.0,
          suite->nb_units);
@@ -415,7 +420,7 @@ int xbt_test_run(void) {
       fprintf(stderr, "%s%d ignored",(first?"":", "),_xbt_test_suite_ignore);
       first = 0;
     }
-    fprintf(stderr,")\n        Units:  %.0f%% ok (%d units:  ",
+    fprintf(stderr,")\n        Units:  %.0f%% ok (%d units: ",
            _xbt_test_nb_units?((1-(double)_xbt_test_unit_failed/(double)_xbt_test_nb_units)*100.0):100.0,
            _xbt_test_nb_units);
     first=1;
@@ -431,7 +436,7 @@ int xbt_test_run(void) {
       fprintf(stderr, "%s%d ignored",(first?"":", "),_xbt_test_unit_ignore);
       first = 0;
     }
-    fprintf(stderr,")\n        Tests:  %.0f%% ok (%d tests:  ",
+    fprintf(stderr,")\n        Tests:  %.0f%% ok (%d tests: ",
            _xbt_test_nb_tests?((1-(double)_xbt_test_test_failed/(double)_xbt_test_nb_tests)*100.0):100.0,
            _xbt_test_nb_tests);
     first=1;
@@ -461,7 +466,8 @@ int xbt_test_run(void) {
 
 
 /* annotate test case with test */
-void _xbt_test_add(xbt_test_unit_t unit, const char*file,int line, const char *fmt, ...) {
+void _xbt_test_add(const char*file,int line, const char *fmt, ...) {
+  xbt_test_unit_t unit=_xbt_test_current_unit;
   xbt_test_test_t test;
   va_list ap;
   
@@ -483,7 +489,8 @@ void _xbt_test_add(xbt_test_unit_t unit, const char*file,int line, const char *f
 }
 
 /* annotate test case with log message and failure */
-void _xbt_test_fail(xbt_test_unit_t unit,  const char*file,int line,const char *fmt, ...) {
+void _xbt_test_fail(const char*file,int line,const char *fmt, ...) {
+  xbt_test_unit_t unit = _xbt_test_current_unit;
   xbt_test_test_t test;
   xbt_test_log_t log;
   va_list ap;
@@ -491,6 +498,10 @@ void _xbt_test_fail(xbt_test_unit_t unit,  const char*file,int line,const char *
   xbt_assert(unit);
   xbt_assert(fmt);
 
+  xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
+             "Test failed even before being declared (broken unit: %s)",
+             unit->title);
+
   log = xbt_new(struct s_xbt_test_log,1);
   va_start(ap, fmt);
   vasprintf(&log->text,fmt, ap);
@@ -504,17 +515,33 @@ void _xbt_test_fail(xbt_test_unit_t unit,  const char*file,int line,const char *
   test->failed = 1;
 }
 
-void _xbt_test_expect_failure(xbt_test_unit_t unit) {
-  xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests,xbt_test_test_t);
+void xbt_test_exception(xbt_ex_t e) {
+  _xbt_test_fail(e.file,e.line,"Exception %s raised: %s",xbt_ex_catname(e.category),e.msg);
+}
+
+void xbt_test_expect_failure(void) {
+  xbt_test_test_t test;
+  xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
+             "Cannot expect the failure of a test before declaring it (broken unit: %s)",
+             _xbt_test_current_unit->title);
+  test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests,xbt_test_test_t);
   test->expected_failure = 1;
 }
-void _xbt_test_skip(xbt_test_unit_t unit) {
-  xbt_test_test_t test = xbt_dynar_getlast_as(unit->tests,xbt_test_test_t);
+void xbt_test_skip(void) {
+  xbt_test_test_t test;
+
+  xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
+             "Test skiped even before being declared (broken unit: %s)",
+             _xbt_test_current_unit->title);
+
+  test = xbt_dynar_getlast_as(_xbt_test_current_unit->tests,
+                             xbt_test_test_t);
   test->ignored = 1;
 }
 
 /* annotate test case with log message only */
-void _xbt_test_log(xbt_test_unit_t unit, const char*file,int line,const char *fmt, ...) {
+void _xbt_test_log(const char*file,int line,const char *fmt, ...) {
+  xbt_test_unit_t unit=_xbt_test_current_unit;
   xbt_test_test_t test;
   xbt_test_log_t log;
   va_list ap;
@@ -522,6 +549,9 @@ void _xbt_test_log(xbt_test_unit_t unit, const char*file,int line,const char *fm
   xbt_assert(unit);
   xbt_assert(fmt);
 
+  xbt_assert1(xbt_dynar_length(_xbt_test_current_unit->tests),
+             "Test logged into even before being declared (broken test unit: %s)",unit->title);
+
   log = xbt_new(struct s_xbt_test_log,1);
   va_start(ap, fmt);
   vasprintf(&log->text, fmt, ap);