Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused type definitions.
[simgrid.git] / src / xbt / xbt_str_test.cpp
1 /* xbt_str.cpp - various helping functions to deal with strings             */
2
3 /* Copyright (c) 2007-2019. 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/str.h"
9
10 #include "simgrid/Exception.hpp"
11
12 #include "catch.hpp"
13
14 #define mytest(name, input, expected)                                                                                  \
15   INFO(name);                                                                                                          \
16   a = static_cast<char**>(xbt_dynar_to_array(xbt_str_split_quoted(input)));                                            \
17   s = xbt_str_join_array(a, "XXX");                                                                                    \
18   REQUIRE(not strcmp(s, expected));                                                                                    \
19   xbt_free(s);                                                                                                         \
20   for (int i = 0; a[i] != nullptr; i++)                                                                                \
21     xbt_free(a[i]);                                                                                                    \
22   xbt_free(a);
23
24 #define test_parse_error(function, name, variable, str)                                                                \
25   do {                                                                                                                 \
26     INFO(name);                                                                                                        \
27     REQUIRE_THROWS_MATCHES(variable = function(str, "Parse error"), xbt_ex,                                            \
28                            Catch::Matchers::Predicate<xbt_ex>([](xbt_ex const& e) { return e.category == arg_error; }, \
29                                                               "category arg_error"));                                  \
30   } while (0)
31
32 #define test_parse_ok(function, name, variable, str, value)                                                            \
33   do {                                                                                                                 \
34     INFO(name);                                                                                                        \
35     REQUIRE_NOTHROW(variable = function(str, "Parse error"));                                                          \
36     REQUIRE(variable == value); /* Fail to parse str */                                                                \
37   } while (0)
38
39 TEST_CASE("xbt::str: String Handling", "xbt_str")
40 {
41
42   SECTION("Test the function xbt_str_split_quoted")
43   {
44     char** a;
45     char* s;
46
47     mytest("Empty", "", "");
48     mytest("Basic test", "toto tutu", "totoXXXtutu");
49     mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", "totoXXXtutu");
50     mytest("Protected space", "toto\\ tutu", "toto tutu");
51     mytest("Several spaces", "toto   tutu", "totoXXXtutu");
52     mytest("LTriming", "  toto tatu", "totoXXXtatu");
53     mytest("Triming", "  toto   tutu  ", "totoXXXtutu");
54     mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
55     mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
56     mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
57     mytest("Backslashed quotes", "\\'toto tutu\\' tata", "'totoXXXtutu'XXXtata");
58     mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata");
59   }
60
61   SECTION("Test the parsing functions")
62   {
63     int rint = -9999;
64     test_parse_ok(xbt_str_parse_int, "Parse int", rint, "42", 42);
65     test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", rint, "0", 0);
66     test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", rint, "-1", -1);
67
68     test_parse_error(xbt_str_parse_int, "Parse int + noise", rint, "342 cruft");
69     test_parse_error(xbt_str_parse_int, "Parse nullptr as an int", rint, nullptr);
70     test_parse_error(xbt_str_parse_int, "Parse '' as an int", rint, "");
71     test_parse_error(xbt_str_parse_int, "Parse cruft as an int", rint, "cruft");
72
73     double rdouble = -9999;
74     test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", rdouble, "42", 42);
75     test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", rdouble, "42.5", 42.5);
76     test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", rdouble, "0", 0);
77     test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", rdouble, "-1", -1);
78
79     test_parse_error(xbt_str_parse_double, "Parse double + noise", rdouble, "342 cruft");
80     test_parse_error(xbt_str_parse_double, "Parse nullptr as a double", rdouble, nullptr);
81     test_parse_error(xbt_str_parse_double, "Parse '' as a double", rdouble, "");
82     test_parse_error(xbt_str_parse_double, "Parse cruft as a double", rdouble, "cruft");
83   }
84 }