Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix xbt_parse_get_all_speeds(), and add some unit tests.
[simgrid.git] / src / xbt / xbt_str_test.cpp
index d6eee1e..88b1d92 100644 (file)
@@ -1,84 +1,76 @@
 /* xbt_str.cpp - various helping functions to deal with strings             */
 
-/* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2021. 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. */
 
+#include "xbt/parse_units.hpp"
 #include "xbt/str.h"
 
 #include "simgrid/Exception.hpp"
 
 #include "catch.hpp"
+#include <string>
+#include <vector>
 
-#define mytest(name, input, expected)                                                                                  \
-  INFO(name);                                                                                                          \
-  a = static_cast<char**>(xbt_dynar_to_array(xbt_str_split_quoted(input)));                                            \
-  s = xbt_str_join_array(a, "XXX");                                                                                    \
-  REQUIRE(not strcmp(s, expected));                                                                                    \
-  xbt_free(s);                                                                                                         \
-  for (int i = 0; a[i] != nullptr; i++)                                                                                \
-    xbt_free(a[i]);                                                                                                    \
-  xbt_free(a);
-
-#define test_parse_error(function, name, variable, str)                                                                \
-  do {                                                                                                                 \
-    INFO(name);                                                                                                        \
-    REQUIRE_THROWS_MATCHES(variable = function(str, "Parse error"), xbt_ex,                                            \
-                           Catch::Matchers::Predicate<xbt_ex>([](xbt_ex const& e) { return e.category == arg_error; }, \
-                                                              "category arg_error"));                                  \
-  } while (0)
+namespace {
+template <typename F> void test_parse_error(F function, const std::string& name, const char* str)
+{
+  INFO(name);
+  REQUIRE_THROWS_AS(function(str, "Parse error"), std::invalid_argument);
+}
 
-#define test_parse_ok(function, name, variable, str, value)                                                            \
-  do {                                                                                                                 \
-    INFO(name);                                                                                                        \
-    REQUIRE_NOTHROW(variable = function(str, "Parse error"));                                                          \
-    REQUIRE(variable == value); /* Fail to parse str */                                                                \
-  } while (0)
+template <typename F, typename T> void test_parse_ok(F function, const std::string& name, const char* str, T value)
+{
+  INFO(name);
+  T variable;
+  REQUIRE_NOTHROW(variable = function(str, "Parse error"));
+  REQUIRE(variable == value); /* Fail to parse str */
+}
+}
 
 TEST_CASE("xbt::str: String Handling", "xbt_str")
 {
-
-  SECTION("Test the function xbt_str_split_quoted")
+  SECTION("Test the parsing functions")
   {
-    char** a;
-    char* s;
+    test_parse_ok(xbt_str_parse_int, "Parse int", "42", 42);
+    test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", "0", 0);
+    test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", "-1", -1);
+
+    test_parse_error(xbt_str_parse_int, "Parse int + noise", "342 cruft");
+    test_parse_error(xbt_str_parse_int, "Parse nullptr as an int", nullptr);
+    test_parse_error(xbt_str_parse_int, "Parse '' as an int", "");
+    test_parse_error(xbt_str_parse_int, "Parse cruft as an int", "cruft");
 
-    mytest("Empty", "", "");
-    mytest("Basic test", "toto tutu", "totoXXXtutu");
-    mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", "totoXXXtutu");
-    mytest("Protected space", "toto\\ tutu", "toto tutu");
-    mytest("Several spaces", "toto   tutu", "totoXXXtutu");
-    mytest("LTriming", "  toto tatu", "totoXXXtatu");
-    mytest("Triming", "  toto   tutu  ", "totoXXXtutu");
-    mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
-    mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
-    mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
-    mytest("Backslashed quotes", "\\'toto tutu\\' tata", "'totoXXXtutu'XXXtata");
-    mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata");
+    test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", "42", 42);
+    test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", "42.5", 42.5);
+    test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", "0", 0);
+    test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", "-1", -1);
+
+    test_parse_error(xbt_str_parse_double, "Parse double + noise", "342 cruft");
+    test_parse_error(xbt_str_parse_double, "Parse nullptr as a double", nullptr);
+    test_parse_error(xbt_str_parse_double, "Parse '' as a double", "");
+    test_parse_error(xbt_str_parse_double, "Parse cruft as a double", "cruft");
   }
 
-  SECTION("Test the parsing functions")
+  SECTION("Test the parsing-with-units functions")
   {
-    int rint = -9999;
-    test_parse_ok(xbt_str_parse_int, "Parse int", rint, "42", 42);
-    test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", rint, "0", 0);
-    test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", rint, "-1", -1);
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0", "") == std::vector<double>{1e0});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0,2.0", "") == std::vector<double>{1e0, 2e0});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0,2.0,3.0", "") == std::vector<double>{1e0, 2e0, 3e0});
 
-    test_parse_error(xbt_str_parse_int, "Parse int + noise", rint, "342 cruft");
-    test_parse_error(xbt_str_parse_int, "Parse nullptr as an int", rint, nullptr);
-    test_parse_error(xbt_str_parse_int, "Parse '' as an int", rint, "");
-    test_parse_error(xbt_str_parse_int, "Parse cruft as an int", rint, "cruft");
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1", "") == std::vector<double>{1e0});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1,2", "") == std::vector<double>{1.0, 2.0});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1,2,3", "") == std::vector<double>{1.0, 2.0, 3.0});
 
-    double rdouble = -9999;
-    test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", rdouble, "42", 42);
-    test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", rdouble, "42.5", 42.5);
-    test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", rdouble, "0", 0);
-    test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", rdouble, "-1", -1);
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0f", "") == std::vector<double>{1e0});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0kf,2.0Mf", "") == std::vector<double>{1e3, 2e6});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0Gf,2.0Tf,3.0Pf", "") ==
+            std::vector<double>{1e9, 2e12, 3e15});
 
-    test_parse_error(xbt_str_parse_double, "Parse double + noise", rdouble, "342 cruft");
-    test_parse_error(xbt_str_parse_double, "Parse nullptr as a double", rdouble, nullptr);
-    test_parse_error(xbt_str_parse_double, "Parse '' as a double", rdouble, "");
-    test_parse_error(xbt_str_parse_double, "Parse cruft as a double", rdouble, "cruft");
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1f", "") == std::vector<double>{1e0});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1kf,2Gf", "") == std::vector<double>{1e3, 2e9});
+    REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1Ef,2Zf,3Yf", "") == std::vector<double>{1e18, 2e21, 3e24});
   }
 }