diff --git a/server/src/utils/StringUtils.cpp b/server/src/utils/StringUtils.cpp index 1d809ce66..5ab684b7f 100644 --- a/server/src/utils/StringUtils.cpp +++ b/server/src/utils/StringUtils.cpp @@ -158,7 +158,7 @@ namespace StringUtils { } template<> - int stot<>(const std::string& s) { + int stot(const std::string& s) { return std::stoi(s); } template<> diff --git a/server/src/utils/StringUtils.h b/server/src/utils/StringUtils.h index 58fea6e26..0ba2de265 100644 --- a/server/src/utils/StringUtils.h +++ b/server/src/utils/StringUtils.h @@ -87,6 +87,18 @@ namespace StringUtils { T stot(const std::string&) { return T(); } + template<> int stot(const std::string& s); + template<> long stot(const std::string& s); + template<> long long stot(const std::string& s); + template<> unsigned int stot(const std::string& s); + template<> unsigned long stot(const std::string& s); + template<> unsigned long long stot(const std::string& s); + template<> float stot(const std::string& s); + template<> double stot(const std::string& s); + template<> long double stot(const std::string& s); + template<> bool stot(const std::string& s); + template<> __int128 stot(const std::string& s); + template<> unsigned __int128 stot(const std::string& s); std::string wrapQuotations(const std::string &s); } diff --git a/server/test/framework/Utils_Tests.cpp b/server/test/framework/Utils_Tests.cpp index fb728aa12..cfaeb9752 100644 --- a/server/test/framework/Utils_Tests.cpp +++ b/server/test/framework/Utils_Tests.cpp @@ -17,18 +17,21 @@ namespace { TEST(StringUtils_stotInt128, simple) { __int128 val = 42; - ASSERT_TRUE(val == StringUtils::stot<__int128>("42")); + auto res = StringUtils::stot<__int128>("42"); + EXPECT_EQ(val, res); } TEST(StringUtils_stotInt128, simple_unsigned) { unsigned __int128 val = 42; - ASSERT_TRUE(val == StringUtils::stot("42")); + auto res = StringUtils::stot("42"); + EXPECT_EQ(val, res); } TEST(StringUtils_stotInt128, INT128_MIN) { __int128 val = 1; val <<= 127; - ASSERT_TRUE(val == StringUtils::stot<__int128>("-170141183460469231731687303715884105728")); + auto res = StringUtils::stot<__int128>("-170141183460469231731687303715884105728"); + EXPECT_EQ(val, res); } TEST(ReadBytesAsValueTest, Unsigned1) { @@ -323,12 +326,12 @@ namespace { template void readBytesAsValueTestTemplate(T val) { - srand(42); + size_t const len = sizeof(T); + std::default_random_engine gen(42); for (size_t tcount = 0; tcount < 5; ++tcount) { - auto add = static_cast(rand() % 10); - auto start = static_cast(rand() % add); - size_t const len = sizeof(T); - std::vector bytes(len + add); + auto extra = static_cast(std::uniform_int_distribution(1, 10)(gen)); + auto start = static_cast(std::uniform_int_distribution(0, extra)(gen)); + std::vector bytes(len + extra); for (size_t i = 1; i <= len; ++i) { bytes[start + i - 1] = (val >> (CHAR_BIT * (i - 1))) & ((1 << CHAR_BIT) - 1); }