// License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2020 Intel Corporation. All Rights Reserved. //#cmake:dependencies rsutils #include "common.h" #include #include using namespace rsutils::string; template ::value>::type* = nullptr> // check if T is arithmetic during compile std::string to_str(const T& t) { /// The std::to_string will make the min float value equal zero, so another function is /// params: a value type T /// returns: a string of the given number std::ostringstream os; os << t; return os.str(); } template struct string_checker { std::string str; T value; bool expected_res; }; template void check_tests(std::vector>& tests) { T value; for (const auto test : tests) { CAPTURE(test.str); CAPTURE(test.expected_res); CHECK(string_to_value(test.str, value) == test.expected_res); if (test.expected_res) { CAPTURE(value); CAPTURE(test.value); if (std::is_integral::value) CHECK(value == test.value); else // floating-point { CHECK(value == Catch::Approx(test.value)); } } } } TEST_CASE("string to value", "[string]") { // int std::vector> int_tests; int_tests.push_back(string_checker{ "123", 123, true }); int_tests.push_back(string_checker{ "-123", -123, true }); int_tests.push_back(string_checker{ "-123456789", -123456789, true }); int_tests.push_back(string_checker{ std::to_string(std::numeric_limits::max())+"0", 0, false }); int_tests.push_back(string_checker{ std::to_string(std::numeric_limits::min())+"0", 0, false }); int_tests.push_back(string_checker{ "abc", 0, false }); check_tests(int_tests); // unsigned int std::vector> uint_tests; uint_tests.push_back(string_checker{ "123", 123, true }); uint_tests.push_back(string_checker{ "123456789", 123456789, true }); uint_tests.push_back(string_checker{ "-123456789", 0, false }); uint_tests.push_back(string_checker{ std::to_string(std::numeric_limits::max())+"0", 0, false }); uint_tests.push_back(string_checker{ "abc", 0, false }); check_tests(uint_tests); // short std::vector> s_tests; s_tests.push_back(string_checker{ "123", 123, true }); s_tests.push_back(string_checker{ "-123", -123, true }); s_tests.push_back(string_checker{ std::to_string(std::numeric_limits::max())+"0", 0, false }); s_tests.push_back(string_checker{ std::to_string(std::numeric_limits::min())+"0", 0, false }); s_tests.push_back(string_checker{ "abc", 0, false }); check_tests(s_tests); // unsigned short std::vector> us_tests; us_tests.push_back(string_checker{ "123", 123, true }); us_tests.push_back(string_checker{ "-123", 0, false }); us_tests.push_back(string_checker{std::to_string(std::numeric_limits::max())+"0", 0, false }); us_tests.push_back(string_checker{ "abc", 0, false }); check_tests(us_tests); // long std::vector> l_tests; l_tests.push_back(string_checker{ "123", 123, true }); l_tests.push_back(string_checker{ "-123", -123, true }); l_tests.push_back(string_checker{ "-123456789", -123456789, true }); l_tests.push_back(string_checker{ std::to_string(std::numeric_limits::max())+"0", 0, false }); l_tests.push_back(string_checker{ std::to_string(std::numeric_limits::min())+"0", 0, false }); l_tests.push_back(string_checker{ "abc", 0, false }); check_tests(l_tests); // long long std::vector> ll_tests; ll_tests.push_back(string_checker{ "123", 123, true }); ll_tests.push_back(string_checker{ "-123", -123, true }); ll_tests.push_back(string_checker{ "12345", 12345, true }); ll_tests.push_back(string_checker{ "-12345", -12345, true }); ll_tests.push_back(string_checker{ std::to_string(std::numeric_limits::max())+"0", 0, false }); ll_tests.push_back(string_checker{ std::to_string(std::numeric_limits::min()) + "0", 0, false }); ll_tests.push_back(string_checker{ "abc", 0, false }); check_tests(ll_tests); // ungined long long std::vector> ull_tests; ull_tests.push_back(string_checker{ "123", 123, true }); ull_tests.push_back(string_checker{ "123456789", 123456789, true }); ull_tests.push_back(string_checker{ "-123456789", 0, false }); ull_tests.push_back(string_checker{ std::to_string(std::numeric_limits::max())+"0", 0, false }); ull_tests.push_back(string_checker{ "abc", 0, false }); check_tests(ull_tests); // float std::vector> f_tests; f_tests.push_back(string_checker{ "1.23456789", 1.23456789f, true }); f_tests.push_back(string_checker{ "2.12121212", 2.12121212f, true }); f_tests.push_back(string_checker{ "-1.23456789", -1.23456789f, true }); f_tests.push_back(string_checker{ "-2.12121212", -2.12121212f, true }); f_tests.push_back(string_checker{ "INF", 0.f, false }); f_tests.push_back(string_checker{ "-INF", 0.f, false }); f_tests.push_back(string_checker{ to_str(std::numeric_limits::max()) + "0", 0.f, false }); f_tests.push_back(string_checker{ to_str(std::numeric_limits::min())+"0", 0.f, false }); f_tests.push_back(string_checker{ to_str(std::numeric_limits::lowest())+"0", 0.f, false }); f_tests.push_back(string_checker{ "NaN", 0.f, false }); f_tests.push_back(string_checker{ "abc", 0.f, false }); check_tests(f_tests); // double std::vector> d_tests; d_tests.push_back(string_checker{ "9.876543", 9.876543, true }); d_tests.push_back(string_checker{ "2.12121212", 2.12121212, true }); d_tests.push_back(string_checker{ "-1.234598765", -1.234598765, true }); d_tests.push_back(string_checker{ "-2.12121212", -2.12121212, true }); d_tests.push_back(string_checker{ "INF", 0., false }); d_tests.push_back(string_checker{ "-INF", 0., false }); d_tests.push_back(string_checker{ to_str(std::numeric_limits::max())+"0", 0., false }); d_tests.push_back(string_checker{ to_str(std::numeric_limits::min())+"0", 0., false }); d_tests.push_back(string_checker{ to_str(std::numeric_limits::lowest())+"0", 0., false }); d_tests.push_back(string_checker{ "NaN", 0., false }); d_tests.push_back(string_checker{ "abc", 0., false }); check_tests(d_tests); // long double std::vector> ld_tests; ld_tests.push_back(string_checker{ "12345.6789123456789", 12345.6789123456789, true }); ld_tests.push_back(string_checker{ "5432.123456789", 5432.123456789, true }); ld_tests.push_back(string_checker{ "-12345678.23456789", -12345678.23456789, true }); ld_tests.push_back(string_checker{ "INF", 0., false }); ld_tests.push_back(string_checker{ "-INF", 0., false }); ld_tests.push_back(string_checker{ to_str(std::numeric_limits::max())+"0", 0., false }); ld_tests.push_back(string_checker{ to_str(std::numeric_limits::min())+"0", 0., false }); ld_tests.push_back(string_checker{ to_str(std::numeric_limits::lowest())+"0", 0., false }); ld_tests.push_back(string_checker{ "NaN", 0., false }); ld_tests.push_back(string_checker{ "abc", 0., false }); check_tests(ld_tests); }