1 /* xbt_str.cpp - various helping functions to deal with strings */
3 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
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. */
10 #include "simgrid/Exception.hpp"
14 #define mytest(name, input, expected) \
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)); \
20 for (int i = 0; a[i] != nullptr; i++) \
24 #define test_parse_error(function, name, variable, str) \
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")); \
32 #define test_parse_ok(function, name, variable, str, value) \
35 REQUIRE_NOTHROW(variable = function(str, "Parse error")); \
36 REQUIRE(variable == value); /* Fail to parse str */ \
39 TEST_CASE("xbt::str: String Handling", "xbt_str")
42 SECTION("Test the function xbt_str_split_quoted")
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");
61 SECTION("Test the parsing functions")
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);
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");
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);
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");