Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / xbt / xbt_str_test.cpp
1 /* xbt_str.cpp - various helping functions to deal with strings             */
2
3 /* Copyright (c) 2007-2023. 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 #include "xbt/parse_units.hpp"
9 #include "xbt/str.h"
10
11 #include "simgrid/Exception.hpp"
12
13 #include "src/3rd-party/catch.hpp"
14 #include <string>
15 #include <vector>
16
17 namespace {
18 template <typename F> void test_parse_error(F function, const std::string& name, const char* str)
19 {
20   INFO(name);
21   REQUIRE_THROWS_AS(function(str, "Parse error"), std::invalid_argument);
22 }
23
24 template <typename F, typename T> void test_parse_ok(F function, const std::string& name, const char* str, T value)
25 {
26   INFO(name);
27   T variable;
28   REQUIRE_NOTHROW(variable = function(str, "Parse error"));
29   REQUIRE(variable == value); /* Fail to parse str */
30 }
31 }
32
33 TEST_CASE("xbt::str: String Handling", "xbt_str")
34 {
35   SECTION("Test the parsing functions")
36   {
37     test_parse_ok(xbt_str_parse_int, "Parse int", "42", 42);
38     test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", "0", 0);
39     test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", "-1", -1);
40
41     test_parse_error(xbt_str_parse_int, "Parse int + noise", "342 cruft");
42     test_parse_error(xbt_str_parse_int, "Parse nullptr as an int", nullptr);
43     test_parse_error(xbt_str_parse_int, "Parse '' as an int", "");
44     test_parse_error(xbt_str_parse_int, "Parse cruft as an int", "cruft");
45
46     test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", "42", 42);
47     test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", "42.5", 42.5);
48     test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", "0", 0);
49     test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", "-1", -1);
50
51     test_parse_error(xbt_str_parse_double, "Parse double + noise", "342 cruft");
52     test_parse_error(xbt_str_parse_double, "Parse nullptr as a double", nullptr);
53     test_parse_error(xbt_str_parse_double, "Parse '' as a double", "");
54     test_parse_error(xbt_str_parse_double, "Parse cruft as a double", "cruft");
55   }
56
57   SECTION("Test the parsing-with-units functions")
58   {
59     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0", "") == std::vector<double>{1e0});
60     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0,2.0", "") == std::vector<double>{1e0, 2e0});
61     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0,2.0,3.0", "") == std::vector<double>{1e0, 2e0, 3e0});
62
63     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1", "") == std::vector<double>{1e0});
64     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1,2", "") == std::vector<double>{1.0, 2.0});
65     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1,2,3", "") == std::vector<double>{1.0, 2.0, 3.0});
66
67     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0f", "") == std::vector<double>{1e0});
68     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0kf,2.0Mf", "") == std::vector<double>{1e3, 2e6});
69     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0Gf,2.0Tf,3.0Pf", "") ==
70             std::vector<double>{1e9, 2e12, 3e15});
71
72     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1f", "") == std::vector<double>{1e0});
73     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1kf,2Gf", "") == std::vector<double>{1e3, 2e9});
74     REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1Ef,2Zf,3Yf", "") == std::vector<double>{1e18, 2e21, 3e24});
75   }
76 }