From 89d6f2594bd77b1d2a52ef35d296c461e9b7f9a8 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Sat, 17 Jul 2021 17:54:20 +0200 Subject: [PATCH] Fix xbt_parse_get_all_speeds(), and add some unit tests. Fix failure to parse e.g. "1f,2f". --- include/xbt/parse_units.hpp | 1 + src/xbt/xbt_parse_units.cpp | 18 +++++++----------- src/xbt/xbt_str_test.cpp | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/include/xbt/parse_units.hpp b/include/xbt/parse_units.hpp index 6ced9416c1..c1e97f4baf 100644 --- a/include/xbt/parse_units.hpp +++ b/include/xbt/parse_units.hpp @@ -8,6 +8,7 @@ #define SIMGRID_XBT_PARSE_UNITS_HPP #include +#include double xbt_parse_get_time(const std::string& filename, int lineno, const std::string& string, const std::string& entity_kind); diff --git a/src/xbt/xbt_parse_units.cpp b/src/xbt/xbt_parse_units.cpp index 5b1d35d669..9439e832eb 100644 --- a/src/xbt/xbt_parse_units.cpp +++ b/src/xbt/xbt_parse_units.cpp @@ -146,19 +146,15 @@ std::vector xbt_parse_get_all_speeds(const std::string& filename, int li const std::string& entity_kind) { std::vector speed_per_pstate; + std::vector pstate_list; - if (speeds.find('.') == std::string::npos) { - double speed = xbt_parse_get_speed(filename, lineno, speeds, entity_kind); + boost::split(pstate_list, speeds, boost::is_any_of(",")); + for (auto speed_str : pstate_list) { + boost::trim(speed_str); + double speed = xbt_parse_get_speed(filename, lineno, speed_str, entity_kind); speed_per_pstate.push_back(speed); - } else { - std::vector pstate_list; - boost::split(pstate_list, speeds, boost::is_any_of(",")); - for (auto speed_str : pstate_list) { - boost::trim(speed_str); - double speed = xbt_parse_get_speed(filename, lineno, speed_str, entity_kind); - speed_per_pstate.push_back(speed); - XBT_DEBUG("Speed value: %f", speed); - } + XBT_DEBUG("Speed value: %f", speed); } + return speed_per_pstate; } diff --git a/src/xbt/xbt_str_test.cpp b/src/xbt/xbt_str_test.cpp index 22ee6f9194..88b1d92d64 100644 --- a/src/xbt/xbt_str_test.cpp +++ b/src/xbt/xbt_str_test.cpp @@ -5,6 +5,7 @@ /* 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" @@ -52,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}); + } } -- 2.20.1