X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b9625f82f86db0674e911887addce45dca31b57f..89d6f2594bd77b1d2a52ef35d296c461e9b7f9a8:/src/xbt/xbt_str_test.cpp?ds=sidebyside diff --git a/src/xbt/xbt_str_test.cpp b/src/xbt/xbt_str_test.cpp index 81ed1aa0da..88b1d92d64 100644 --- a/src/xbt/xbt_str_test.cpp +++ b/src/xbt/xbt_str_test.cpp @@ -1,10 +1,11 @@ /* xbt_str.cpp - various helping functions to deal with strings */ -/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ +#include "xbt/parse_units.hpp" #include "xbt/str.h" #include "simgrid/Exception.hpp" @@ -14,18 +15,6 @@ #include namespace { -void test_split_quoted(const std::string& name, const char* input, const std::vector& expected) -{ - INFO(name); - xbt_dynar_t a = xbt_str_split_quoted(input); - REQUIRE(xbt_dynar_length(a) == expected.size()); - unsigned i; - char* token; - xbt_dynar_foreach (a, i, token) - REQUIRE(token == expected[i]); - xbt_dynar_free(&a); -} - template void test_parse_error(F function, const std::string& name, const char* str) { INFO(name); @@ -35,7 +24,7 @@ template void test_parse_error(F function, const std::string& name, template void test_parse_ok(F function, const std::string& name, const char* str, T value) { INFO(name); - T variable = static_cast(-9999); + T variable; REQUIRE_NOTHROW(variable = function(str, "Parse error")); REQUIRE(variable == value); /* Fail to parse str */ } @@ -43,22 +32,6 @@ template void test_parse_ok(F function, const std::stri TEST_CASE("xbt::str: String Handling", "xbt_str") { - SECTION("Test the function xbt_str_split_quoted") - { - test_split_quoted("Empty", "", {}); - test_split_quoted("Basic test", "toto tutu", {"toto", "tutu"}); - test_split_quoted("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", {"toto", "tutu"}); - test_split_quoted("Protected space", "toto\\ tutu", {"toto tutu"}); - test_split_quoted("Several spaces", "toto tutu", {"toto", "tutu"}); - test_split_quoted("LTrimming", " toto tatu", {"toto", "tatu"}); - test_split_quoted("Trimming", " toto tutu ", {"toto", "tutu"}); - test_split_quoted("Single quotes", "'toto tutu' tata", {"toto tutu", "tata"}); - test_split_quoted("Double quotes", "\"toto tutu\" tata", {"toto tutu", "tata"}); - test_split_quoted("Mixed quotes", "\"toto' 'tutu\" tata", {"toto' 'tutu", "tata"}); - test_split_quoted("Backslashed quotes", "\\'toto tutu\\' tata", {"'toto", "tutu'", "tata"}); - test_split_quoted("Backslashed quotes + quotes", "'toto \\'tutu' tata", {"toto 'tutu", "tata"}); - } - SECTION("Test the parsing functions") { test_parse_ok(xbt_str_parse_int, "Parse int", "42", 42); @@ -80,4 +53,24 @@ TEST_CASE("xbt::str: String Handling", "xbt_str") test_parse_error(xbt_str_parse_double, "Parse '' as a double", ""); test_parse_error(xbt_str_parse_double, "Parse cruft as a double", "cruft"); } + + SECTION("Test the parsing-with-units functions") + { + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0", "") == std::vector{1e0}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0,2.0", "") == std::vector{1e0, 2e0}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0,2.0,3.0", "") == std::vector{1e0, 2e0, 3e0}); + + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1", "") == std::vector{1e0}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1,2", "") == std::vector{1.0, 2.0}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1,2,3", "") == std::vector{1.0, 2.0, 3.0}); + + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0f", "") == std::vector{1e0}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0kf,2.0Mf", "") == std::vector{1e3, 2e6}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1.0Gf,2.0Tf,3.0Pf", "") == + std::vector{1e9, 2e12, 3e15}); + + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1f", "") == std::vector{1e0}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1kf,2Gf", "") == std::vector{1e3, 2e9}); + REQUIRE(xbt_parse_get_all_speeds(__FILE__, __LINE__, "1Ef,2Zf,3Yf", "") == std::vector{1e18, 2e21, 3e24}); + } }