Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplifications.
[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 #include <string>
14 #include <vector>
15
16 namespace {
17 void test_split_quoted(const std::string& name, const char* input, const std::vector<std::string>& expected)
18 {
19   INFO(name);
20   xbt_dynar_t a = xbt_str_split_quoted(input);
21   REQUIRE(xbt_dynar_length(a) == expected.size());
22   unsigned i;
23   char* token;
24   xbt_dynar_foreach (a, i, token)
25     REQUIRE(token == expected[i]);
26   xbt_dynar_free(&a);
27 }
28
29 template <typename F> void test_parse_error(F function, const std::string& name, const char* str)
30 {
31   INFO(name);
32   REQUIRE_THROWS_MATCHES(function(str, "Parse error"), xbt_ex,
33                          Catch::Matchers::Predicate<xbt_ex>([](xbt_ex const& e) { return e.category == arg_error; },
34                                                             "category arg_error"));
35 }
36
37 template <typename F, typename T> void test_parse_ok(F function, const std::string& name, const char* str, T value)
38 {
39   INFO(name);
40   T variable = static_cast<T>(-9999);
41   REQUIRE_NOTHROW(variable = function(str, "Parse error"));
42   REQUIRE(variable == value); /* Fail to parse str */
43 }
44 }
45
46 TEST_CASE("xbt::str: String Handling", "xbt_str")
47 {
48
49   SECTION("Test the function xbt_str_split_quoted")
50   {
51     test_split_quoted("Empty", "", {});
52     test_split_quoted("Basic test", "toto tutu", {"toto", "tutu"});
53     test_split_quoted("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", {"toto", "tutu"});
54     test_split_quoted("Protected space", "toto\\ tutu", {"toto tutu"});
55     test_split_quoted("Several spaces", "toto   tutu", {"toto", "tutu"});
56     test_split_quoted("LTriming", "  toto tatu", {"toto", "tatu"});
57     test_split_quoted("Triming", "  toto   tutu  ", {"toto", "tutu"});
58     test_split_quoted("Single quotes", "'toto tutu' tata", {"toto tutu", "tata"});
59     test_split_quoted("Double quotes", "\"toto tutu\" tata", {"toto tutu", "tata"});
60     test_split_quoted("Mixed quotes", "\"toto' 'tutu\" tata", {"toto' 'tutu", "tata"});
61     test_split_quoted("Backslashed quotes", "\\'toto tutu\\' tata", {"'toto", "tutu'", "tata"});
62     test_split_quoted("Backslashed quotes + quotes", "'toto \\'tutu' tata", {"toto 'tutu", "tata"});
63   }
64
65   SECTION("Test the parsing functions")
66   {
67     test_parse_ok(xbt_str_parse_int, "Parse int", "42", 42);
68     test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", "0", 0);
69     test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", "-1", -1);
70
71     test_parse_error(xbt_str_parse_int, "Parse int + noise", "342 cruft");
72     test_parse_error(xbt_str_parse_int, "Parse nullptr as an int", nullptr);
73     test_parse_error(xbt_str_parse_int, "Parse '' as an int", "");
74     test_parse_error(xbt_str_parse_int, "Parse cruft as an int", "cruft");
75
76     test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", "42", 42);
77     test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", "42.5", 42.5);
78     test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", "0", 0);
79     test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", "-1", -1);
80
81     test_parse_error(xbt_str_parse_double, "Parse double + noise", "342 cruft");
82     test_parse_error(xbt_str_parse_double, "Parse nullptr as a double", nullptr);
83     test_parse_error(xbt_str_parse_double, "Parse '' as a double", "");
84     test_parse_error(xbt_str_parse_double, "Parse cruft as a double", "cruft");
85   }
86 }