1 /* xbt_str.cpp - various helping functions to deal with strings */
3 /* Copyright (c) 2007-2021. 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"
17 template <typename F> void test_parse_error(F function, const std::string& name, const char* str)
20 REQUIRE_THROWS_AS(function(str, "Parse error"), std::invalid_argument);
23 template <typename F, typename T> void test_parse_ok(F function, const std::string& name, const char* str, T value)
27 REQUIRE_NOTHROW(variable = function(str, "Parse error"));
28 REQUIRE(variable == value); /* Fail to parse str */
32 TEST_CASE("xbt::str: String Handling", "xbt_str")
34 SECTION("Test the parsing functions")
36 test_parse_ok(xbt_str_parse_int, "Parse int", "42", 42);
37 test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", "0", 0);
38 test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", "-1", -1);
40 test_parse_error(xbt_str_parse_int, "Parse int + noise", "342 cruft");
41 test_parse_error(xbt_str_parse_int, "Parse nullptr as an int", nullptr);
42 test_parse_error(xbt_str_parse_int, "Parse '' as an int", "");
43 test_parse_error(xbt_str_parse_int, "Parse cruft as an int", "cruft");
45 test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", "42", 42);
46 test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", "42.5", 42.5);
47 test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", "0", 0);
48 test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", "-1", -1);
50 test_parse_error(xbt_str_parse_double, "Parse double + noise", "342 cruft");
51 test_parse_error(xbt_str_parse_double, "Parse nullptr as a double", nullptr);
52 test_parse_error(xbt_str_parse_double, "Parse '' as a double", "");
53 test_parse_error(xbt_str_parse_double, "Parse cruft as a double", "cruft");