Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stringify a bit
[simgrid.git] / src / xbt / cunit.cpp
index ba3d860..87c869c 100644 (file)
@@ -1,7 +1,6 @@
 /* cunit - A little C Unit facility                                         */
 
-/* Copyright (c) 2005-2014. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2005-2017. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 /* At some point we should use https://github.com/google/googletest instead */
 
 #include "src/internal_config.h"
-#include <stdio.h>
+#include <cstdio>
 
 #include <xbt/ex.hpp>
 #include "xbt/sysdep.h"         /* bvprintf */
 #include "xbt/cunit.h"
 #include "xbt/dynar.h"
 
+#define STRLEN 1024
+
 /* collection of all suites */
 static xbt_dynar_t _xbt_test_suites = nullptr;
 /* global statistics */
@@ -96,12 +97,13 @@ struct s_xbt_test_unit {
 static void xbt_test_unit_dump(xbt_test_unit_t unit)
 {
   if (unit) {
-    xbt_test_test_t test;
-    unsigned int it_test;
     fprintf(stderr, "  UNIT %s: %s (%s)\n", unit->name, unit->title, (unit->enabled ? "enabled" : "disabled"));
-    if (unit->enabled)
+    if (unit->enabled) {
+      xbt_test_test_t test;
+      unsigned int it_test;
       xbt_dynar_foreach(unit->tests, it_test, test)
           xbt_test_test_dump(test);
+    }
   } else {
     fprintf(stderr, "  unit=nullptr\n");
   }
@@ -185,15 +187,15 @@ xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
 xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
 {
   xbt_test_suite_t suite;
-  unsigned int it_suite;
-
   char *bufname;
   va_list ap;
 
-  if (_xbt_test_suites)
+  if (_xbt_test_suites) {
+    unsigned int it_suite;
     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
       if (not strcmp(suite->name, name))
         return suite;
+  }
 
   va_start(ap, fmt);
   bufname = bvprintf(fmt, ap);
@@ -207,14 +209,15 @@ xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt, ...)
 void xbt_test_suite_dump(xbt_test_suite_t suite)
 {
   if (suite) {
-    xbt_test_unit_t unit;
-    unsigned int it_unit;
     fprintf(stderr, "TESTSUITE %s: %s (%s)\n", suite->name, suite->title, suite->enabled ? "enabled" : "disabled");
-    if (suite->enabled)
+    if (suite->enabled) {
+      xbt_test_unit_t unit;
+      unsigned int it_unit;
       xbt_dynar_foreach(suite->units, it_unit, unit)
           xbt_test_unit_dump(unit);
+    }
   } else {
-    fprintf(stderr, "TESTSUITE IS nullptr!\n");
+    fprintf(stderr, "TESTSUITE IS NULL!\n");
   }
 }
 
@@ -249,15 +252,6 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
   xbt_test_test_t test;
   xbt_test_log_t log;
 
-  const char *file;
-  int line;
-  char *cp;
-  unsigned int it_unit;
-  unsigned int it_test;
-  unsigned int it_log;
-
-  int first = 1;                /* for result pretty printing */
-
   if (suite == nullptr)
     return 0;
 
@@ -281,6 +275,7 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
 
   if (suite->enabled) {
     /* iterate through all tests */
+    unsigned int it_unit;
     xbt_dynar_foreach(suite->units, it_unit, unit) {
       /* init unit case counters */
       unit->nb_tests = 0;
@@ -289,8 +284,9 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
       unit->test_expect = 0;
 
       /* display unit title */
-      cp = bprintf(" Unit: %s ......................................"
-                   "......................................", unit->title);
+      char* cp = bprintf(" Unit: %s ......................................"
+                         "......................................",
+                         unit->title);
       cp[70] = '\0';
       fprintf(stderr, "%s", cp);
       free(cp);
@@ -301,6 +297,7 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
         unit->func();
 
       /* iterate through all performed tests to determine status */
+      unsigned int it_test;
       xbt_dynar_foreach(unit->tests, it_test, test) {
         if (test->ignored) {
           unit->test_ignore++;
@@ -326,8 +323,8 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
           fprintf(stderr, ".... skip\n");       /* shouldn't happen, but I'm a bit lost with this logic */
         }
         xbt_dynar_foreach(unit->tests, it_test, test) {
-          file = (test->file != nullptr ? test->file : unit->file);
-          line = (test->line != 0 ? test->line : unit->line);
+          const char* file = (test->file != nullptr ? test->file : unit->file);
+          int line         = (test->line != 0 ? test->line : unit->line);
           const char* resname;
           if (test->ignored)
             resname = " SKIP";
@@ -345,6 +342,7 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
           fprintf(stderr, "      %s: %s [%s:%d]\n", resname, test->title, file, line);
 
           if ((test->expected_failure && not test->failed) || (not test->expected_failure && test->failed)) {
+            unsigned int it_log;
             xbt_dynar_foreach(test->logs, it_log, log) {
               file = (log->file != nullptr ? log->file : file);
               line = (log->line != 0 ? log->line : line);
@@ -406,6 +404,8 @@ static int xbt_test_suite_run(xbt_test_suite_t suite, int verbosity)
 
   /* print test suite summary */
   if (suite->enabled) {
+    int first = 1; /* for result pretty printing */
+
     fprintf(stderr," =====================================================================%s\n",
             (suite->nb_units ? (suite->unit_failed ? "== FAILED" : "====== OK") :
                                (suite->unit_disabled ? " DISABLED" : "==== SKIP")));
@@ -456,32 +456,29 @@ static void apply_selection(char *selection)
   /* for the parsing */
   char *sel = selection;
   int done = 0;
-  char dir[1024];               /* the directive */
+  char dir[STRLEN]; /* the directive */
   /* iterators */
   unsigned int it_suite;
   xbt_test_suite_t suite;
   xbt_test_unit_t unit;
   unsigned int it_unit;
 
-  char suitename[512];
-  char unitname[512];
+  char suitename[STRLEN];
+  char unitname[STRLEN];
 
   if (not selection || selection[0] == '\0')
     return;
 
-  /*printf("Test selection: %s\n", selection); */
-
   /* First apply the selection */
   while (not done) {
     int enabling = 1;
 
     char *p = strchr(sel, ',');
     if (p) {
-      strncpy(dir, sel, p - sel);
-      dir[p - sel] = '\0';
+      snprintf(dir, STRLEN, "%.*s", (int)(p - sel), sel);
       sel = p + 1;
     } else {
-      strncpy(dir, sel,1024);
+      snprintf(dir, STRLEN, "%s", sel);
       done = 1;
     }
 
@@ -496,11 +493,10 @@ static void apply_selection(char *selection)
 
     p = strchr(dir, ':');
     if (p) {
-      strncpy(unitname, p + 1,512);
-      strncpy(suitename, dir, p - dir);
-      suitename[p - dir] = '\0';
+      snprintf(suitename, STRLEN, "%.*s", (int)(p - dir), dir);
+      snprintf(unitname, STRLEN, "%s", p + 1);
     } else {
-      strncpy(suitename, dir,512);
+      snprintf(suitename, STRLEN, "%s", dir);
       unitname[0] = '\0';
     }