From b94baa467eadb1dbcd67d1175383959f3ca332d0 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Fri, 5 Jan 2024 01:41:37 +0800 Subject: [PATCH 01/10] iox-#2055 Refactor existing edge case testing Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 118 ++++++++++++------ 1 file changed, 81 insertions(+), 37 deletions(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index c2cd543ec5..be006ca9c1 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -303,59 +303,103 @@ TEST_F(convert_test, fromString_LongInt_Fail) ASSERT_THAT(result.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_MinMaxShort) +/// EDGE CASES START +/// inc: increment, dec: decrement + +TEST_F(convert_test, fromString_EdgeCase_SignedShort) { ::testing::Test::RecordProperty("TEST_ID", "98e33efd-ba39-4b88-8307-358be30e4e73"); - std::string source = "32767"; - auto gg = iox::convert::from_string(source.c_str()); - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); - source = "32768"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); - source = "-32768"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); + + std::string source = "-32768"; + auto short_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(short_min.has_value(), Eq(true)); + EXPECT_THAT(short_min.value(), Eq(-32768)); + source = "-32769"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); -} + auto short_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(short_min_dec_1.has_value(), Eq(false)); -TEST_F(convert_test, fromString_MinMaxUNSIGNED_Short) -{ - ::testing::Test::RecordProperty("TEST_ID", "f9196939-ae5d-4c27-85bf-b3b084343261"); - std::string source = "65535"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); - source = "65536"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); - source = "0"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); - source = "-1"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); + source = "32767"; + auto short_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(short_max.has_value(), Eq(true)); + EXPECT_THAT(short_max.value(), Eq(32767)); + + source = "32768"; + auto short_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(short_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_MinMaxInt) +TEST_F(convert_test, fromString_EdgeCase_SignedInt) { ::testing::Test::RecordProperty("TEST_ID", "abf0fda5-044e-4f1b-bb1e-31b701578a3d"); - std::string source = "2147483647"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); - source = "2147483648"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); - source = "-2147483648"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); + + std::string source = "-2147483648"; + auto int_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(int_min.has_value(), Eq(true)); + EXPECT_THAT(int_min.value(), Eq(-2147483648)); + source = "-2147483649"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); + auto int_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(int_min_dec_1.has_value(), Eq(false)); + + source = "2147483647"; + auto int_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(int_max.has_value(), Eq(true)); + EXPECT_THAT(int_max.value(), Eq(2147483647)); + + source = "2147483648"; + auto int_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(int_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_MinMaxUNSIGNED_Int) +TEST_F(convert_test, fromString_EdgeCase_UnSignedShort) +{ + ::testing::Test::RecordProperty("TEST_ID", "f9196939-ae5d-4c27-85bf-b3b084343261"); + + std::string source = "0"; + auto unshort_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unshort_min.has_value(), Eq(true)); + EXPECT_THAT(unshort_min.value(), Eq(0)); + + source = "-1"; + auto unshort_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unshort_min_dec_1.has_value(), Eq(false)); + + source = "65535"; + auto unshort_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unshort_max.has_value(), Eq(true)); + EXPECT_THAT(unshort_max.value(), Eq(65535)); + + source = "65536"; + auto unshort_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unshort_max_inc_1.has_value(), Eq(false)); +} + +TEST_F(convert_test, fromString_EdgeCase_UnSignedInt) { ::testing::Test::RecordProperty("TEST_ID", "c2a832ef-3e86-4303-a98c-63c7b11ea789"); - std::string source = "4294967295"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); - source = "4294967296"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); - source = "0"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(true)); + + std::string source = "0"; + auto unint_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unint_min.has_value(), Eq(true)); + EXPECT_THAT(unint_min.value(), Eq(0)); + source = "-1"; - EXPECT_THAT(iox::convert::from_string(source.c_str()).has_value(), Eq(false)); + auto unint_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unint_min_dec_1.has_value(), Eq(false)); + + source = "4294967295"; + auto unint_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unint_max.has_value(), Eq(true)); + EXPECT_THAT(unint_max.value(), Eq(4294967295)); + + source = "4294967296"; + auto unint_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unint_max_inc_1.has_value(), Eq(false)); } +/// EDGE CASES END + TEST_F(convert_test, fromString_cxxString) { ::testing::Test::RecordProperty("TEST_ID", "dbf015bb-5f51-47e1-9d0e-0525f65e7803"); From 40a1224a92577c3c83e7d16cb1320323635aa9de Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Fri, 5 Jan 2024 04:11:40 +0800 Subject: [PATCH 02/10] iox-#2055 Add signed/unsigned intergral tests Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 161 +++++++++++++++++- 1 file changed, 155 insertions(+), 6 deletions(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index be006ca9c1..215ad7c208 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -303,9 +303,32 @@ TEST_F(convert_test, fromString_LongInt_Fail) ASSERT_THAT(result.has_value(), Eq(false)); } -/// EDGE CASES START +/// SINGED INTEGRAL EDGE CASES START /// inc: increment, dec: decrement +TEST_F(convert_test, fromString_EdgeCase_SignedChar) +{ + ::testing::Test::RecordProperty("TEST_ID", "3a70481e-ae86-4b96-92c4-44169700e93a"); + + std::string source = "-128"; + auto signed_char_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(signed_char_min.has_value(), Eq(true)); + EXPECT_THAT(signed_char_min.value(), Eq(static_cast(-128))); + + source = "-129"; + auto signed_char_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(signed_char_min_dec_1.has_value(), Eq(false)); + + source = "127"; + auto signed_char_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(signed_char_max.has_value(), Eq(true)); + EXPECT_THAT(signed_char_max.value(), Eq(static_cast(127))); + + source = "128"; + auto signed_char_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(signed_char_max_inc_1.has_value(), Eq(false)); +} + TEST_F(convert_test, fromString_EdgeCase_SignedShort) { ::testing::Test::RecordProperty("TEST_ID", "98e33efd-ba39-4b88-8307-358be30e4e73"); @@ -313,7 +336,7 @@ TEST_F(convert_test, fromString_EdgeCase_SignedShort) std::string source = "-32768"; auto short_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(short_min.has_value(), Eq(true)); - EXPECT_THAT(short_min.value(), Eq(-32768)); + EXPECT_THAT(short_min.value(), Eq(static_cast(-32768))); source = "-32769"; auto short_min_dec_1 = iox::convert::from_string(source.c_str()); @@ -322,7 +345,7 @@ TEST_F(convert_test, fromString_EdgeCase_SignedShort) source = "32767"; auto short_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(short_max.has_value(), Eq(true)); - EXPECT_THAT(short_max.value(), Eq(32767)); + EXPECT_THAT(short_max.value(), Eq(static_cast(32767))); source = "32768"; auto short_max_inc_1 = iox::convert::from_string(source.c_str()); @@ -352,6 +375,83 @@ TEST_F(convert_test, fromString_EdgeCase_SignedInt) ASSERT_THAT(int_max_inc_1.has_value(), Eq(false)); } +// platform dependent (32/64 bit system only) +TEST_F(convert_test, fromString_EdgeCase_SignedLong) +{ + ::testing::Test::RecordProperty("TEST_ID", "5dc4c773-6a51-42b6-ad94-7ec885263856"); + + constexpr bool IS_32_BIT{sizeof(long) != sizeof(long long)}; + + std::string source = std::to_string(std::numeric_limits::min()); + auto long_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_min.has_value(), Eq(true)); + EXPECT_THAT(long_min.value(), Eq(std::numeric_limits::min())); + + source = IS_32_BIT ? "-2147483649" : "-9223372036854775809"; + auto long_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_min_dec_1.has_value(), Eq(false)); + + source = std::to_string(std::numeric_limits::max()); + auto long_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_max.has_value(), Eq(true)); + EXPECT_THAT(long_max.value(), Eq(std::numeric_limits::max())); + + source = IS_32_BIT ? "2147483648" : "9223372036854775808"; + auto long_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_max_inc_1.has_value(), Eq(false)); +} + +TEST_F(convert_test, fromString_EdgeCase_SignedLongLong) +{ + ::testing::Test::RecordProperty("TEST_ID", "7c015ac0-06a7-407d-aa93-d39c50734951"); + + std::string source = "-9223372036854775808"; + auto long_long_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_long_min.has_value(), Eq(true)); + // we don't use -9223372036854775808LL here for the compiler will parse it in way we don't want + EXPECT_THAT(long_long_min.value(), Eq(std::numeric_limits::min())); + + source = "-9223372036854775809"; + auto long_long_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_long_min_dec_1.has_value(), Eq(false)); + + source = "9223372036854775807"; + auto long_long_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_long_max.has_value(), Eq(true)); + EXPECT_THAT(long_long_max.value(), Eq(9223372036854775807LL)); + + source = "9223372036854775808"; + auto long_long_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_long_max_inc_1.has_value(), Eq(false)); +} + +/// SINGED INTEGRAL EDGE CASES END + +/// UNSINGED INTEGRAL EDGE CASES START + +TEST_F(convert_test, fromString_EdgeCase_UnSignedChar) +{ + ::testing::Test::RecordProperty("TEST_ID", "c11d74a1-be55-41fc-952f-519546eb04fe"); + + std::string source = "0"; + auto unchar_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unchar_min.has_value(), Eq(true)); + EXPECT_THAT(unchar_min.value(), Eq(0)); + + source = "-1"; + auto unchar_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unchar_min_dec_1.has_value(), Eq(false)); + + source = "255"; + auto unchar_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unchar_max.has_value(), Eq(true)); + EXPECT_THAT(unchar_max.value(), Eq(static_cast(255))); + + source = "256"; + auto unchar_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unchar_max_inc_1.has_value(), Eq(false)); +} + TEST_F(convert_test, fromString_EdgeCase_UnSignedShort) { ::testing::Test::RecordProperty("TEST_ID", "f9196939-ae5d-4c27-85bf-b3b084343261"); @@ -368,7 +468,7 @@ TEST_F(convert_test, fromString_EdgeCase_UnSignedShort) source = "65535"; auto unshort_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unshort_max.has_value(), Eq(true)); - EXPECT_THAT(unshort_max.value(), Eq(65535)); + EXPECT_THAT(unshort_max.value(), Eq(static_cast(65535))); source = "65536"; auto unshort_max_inc_1 = iox::convert::from_string(source.c_str()); @@ -391,14 +491,63 @@ TEST_F(convert_test, fromString_EdgeCase_UnSignedInt) source = "4294967295"; auto unint_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unint_max.has_value(), Eq(true)); - EXPECT_THAT(unint_max.value(), Eq(4294967295)); + EXPECT_THAT(unint_max.value(), Eq(4294967295U)); source = "4294967296"; auto unint_max_inc_1 = iox::convert::from_string(source.c_str()); ASSERT_THAT(unint_max_inc_1.has_value(), Eq(false)); } -/// EDGE CASES END +// platform dependent (32/64 bit system only) +TEST_F(convert_test, fromString_EdgeCase_UnSignedLong) +{ + ::testing::Test::RecordProperty("TEST_ID", "6e74e284-7f13-4d77-8d3f-009df216828f"); + + constexpr bool IS_32_BIT{sizeof(long) != sizeof(long long)}; + + std::string source = "0"; + auto unlong_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_min.has_value(), Eq(true)); + EXPECT_THAT(unlong_min.value(), Eq(0)); + + source = "-1"; + auto unlong_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_min_dec_1.has_value(), Eq(false)); + + source = std::to_string(std::numeric_limits::max()); + auto unlong_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_max.has_value(), Eq(true)); + EXPECT_THAT(unlong_max.value(), Eq(std::numeric_limits::max())); + + source = IS_32_BIT ? "4294967296" : "18446744073709551616"; + auto unlong_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_max_inc_1.has_value(), Eq(false)); +} + +TEST_F(convert_test, fromString_EdgeCase_UnSignedLongLong) +{ + ::testing::Test::RecordProperty("TEST_ID", "96456d6f-2493-4db2-b5fa-f96f92ec64dd"); + + std::string source = "0"; + auto unlong_long_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_long_min.has_value(), Eq(true)); + EXPECT_THAT(unlong_long_min.value(), Eq(0)); + + source = "-1"; + auto unlong_long_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_long_min_dec_1.has_value(), Eq(false)); + + source = "18446744073709551615"; + auto unlong_long_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_long_max.has_value(), Eq(true)); + EXPECT_THAT(unlong_long_max.value(), Eq(18446744073709551615ULL)); + + source = "18446744073709551616"; + auto unlong_long_max_inc_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_long_max_inc_1.has_value(), Eq(false)); +} + +/// UNSINGED INTEGRAL EDGE CASES END TEST_F(convert_test, fromString_cxxString) { From 30e60cc5b7d0db3cc1248bd87bbe90de0693fe9b Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Fri, 5 Jan 2024 13:17:48 +0800 Subject: [PATCH 03/10] iox-#2055 Add edge test cases for floating point Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 183 +++++++++++++++++- 1 file changed, 182 insertions(+), 1 deletion(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index 215ad7c208..e0e63b4103 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -21,7 +21,7 @@ #include "iox/std_string_support.hpp" #include "test.hpp" - +#include #include namespace { @@ -38,6 +38,15 @@ class convert_test : public Test void TearDown() override { } + template + std::string fp_to_string(T value) + { + static_assert(std::is_floating_point::value, "fp_to_string requires floating point type"); + + std::ostringstream oss; + oss << std::scientific << std::setprecision(std::numeric_limits::max_digits10) << value; + return oss.str(); + } }; @@ -549,6 +558,178 @@ TEST_F(convert_test, fromString_EdgeCase_UnSignedLongLong) /// UNSINGED INTEGRAL EDGE CASES END +/// NORMAL FLOATING POINT TYPE EDGE CASES START + +TEST_F(convert_test, fromString_EdgeCase_Float) +{ + ::testing::Test::RecordProperty("TEST_ID", "68d4f096-a93c-406b-b081-fe50e4b1a2c9"); + + std::string source = fp_to_string(std::numeric_limits::min()); + auto float_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(float_min.has_value(), Eq(true)); + EXPECT_THAT(float_min.value(), FloatEq(std::numeric_limits::min())); + + // strtof will trigger ERANGE if the input is a subnormal float, resulting in a nullopt return value. + auto normal_float_min_eps = std::nextafter(std::numeric_limits::min(), 0.0F); + source = fp_to_string(std::numeric_limits::min() - normal_float_min_eps); + auto float_min_dec_eps = iox::convert::from_string(source.c_str()); + ASSERT_THAT(float_min_dec_eps.has_value(), Eq(false)); + + source = fp_to_string(std::numeric_limits::lowest()); + auto float_lowest = iox::convert::from_string(source.c_str()); + ASSERT_THAT(float_lowest.has_value(), Eq(true)); + EXPECT_THAT(float_lowest.value(), FloatEq(std::numeric_limits::lowest())); + + source = fp_to_string(std::numeric_limits::max()); + auto float_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(float_max.has_value(), Eq(true)); + EXPECT_THAT(float_max.value(), FloatEq(std::numeric_limits::max())); +} + +TEST_F(convert_test, fromString_EdgeCase_Double) +{ + ::testing::Test::RecordProperty("TEST_ID", "af7ca2e6-ba7e-41f7-a321-5f68617d3566"); + + std::string source = fp_to_string(std::numeric_limits::min()); + auto double_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(double_min.has_value(), Eq(true)); + EXPECT_THAT(double_min.value(), DoubleEq(std::numeric_limits::min())); + + auto normal_double_min_eps = std::nextafter(std::numeric_limits::min(), 0.0); + source = fp_to_string(std::numeric_limits::min() - normal_double_min_eps); + auto double_min_dec_eps = iox::convert::from_string(source.c_str()); + ASSERT_THAT(double_min_dec_eps.has_value(), Eq(false)); + + source = fp_to_string(std::numeric_limits::lowest()); + auto double_lowest = iox::convert::from_string(source.c_str()); + ASSERT_THAT(double_lowest.has_value(), Eq(true)); + EXPECT_THAT(double_lowest.value(), DoubleEq(std::numeric_limits::lowest())); + + source = fp_to_string(std::numeric_limits::max()); + auto double_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(double_max.has_value(), Eq(true)); + EXPECT_THAT(double_max.value(), DoubleEq(std::numeric_limits::max())); +} + +TEST_F(convert_test, fromString_EdgeCase_LongDouble) +{ + ::testing::Test::RecordProperty("TEST_ID", "fb96e526-8fb6-4af9-87f0-dfd4193237a5"); + + std::string source = fp_to_string(std::numeric_limits::min()); + auto long_double_min = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_double_min.has_value(), Eq(true)); + // There's no LongDoubleEq + EXPECT_THAT(long_double_min.value(), Eq(std::numeric_limits::min())); + + auto normal_long_double_min_eps = std::nextafter(std::numeric_limits::min(), 0.0L); + source = fp_to_string(std::numeric_limits::min() - normal_long_double_min_eps); + auto long_double_min_dec_eps = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_double_min_dec_eps.has_value(), Eq(false)); + + source = fp_to_string(std::numeric_limits::lowest()); + auto long_double_lowest = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_double_lowest.has_value(), Eq(true)); + EXPECT_THAT(long_double_lowest.value(), Eq(std::numeric_limits::lowest())); + + source = fp_to_string(std::numeric_limits::max()); + auto long_double_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_double_max.has_value(), Eq(true)); + EXPECT_THAT(long_double_max.value(), Eq(std::numeric_limits::max())); +} + +/// NORMAL FLOATING POINT TYPE EDGE CASES END + +/// SPECIAL FLOATING POINT TYPE EDGE CASES START + +TEST_F(convert_test, fromString_EdgeCase_Float_NaN) +{ + ::testing::Test::RecordProperty("TEST_ID", "772bcbc3-d55b-464f-873f-82754ad543f3"); + + std::vector nan_vec = {"NaN", "nan"}; + + for (const auto& v : nan_vec) + { + auto nan_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(nan_ret.has_value(), Eq(true)); + ASSERT_THAT(std::isnan(nan_ret.value()), Eq(true)); + } +} + +TEST_F(convert_test, fromString_EdgeCase_Double_NaN) +{ + ::testing::Test::RecordProperty("TEST_ID", "a27c8575-658c-465d-a1a2-4f2f6b9a723a"); + + std::vector nan_vec = {"NaN", "nan"}; + + for (const auto& v : nan_vec) + { + auto nan_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(nan_ret.has_value(), Eq(true)); + ASSERT_THAT(std::isnan(nan_ret.value()), Eq(true)); + } +} + +TEST_F(convert_test, fromString_EdgeCase_LongDouble_NaN) +{ + ::testing::Test::RecordProperty("TEST_ID", "486f4e78-6000-4401-bb66-62d26b1d0cce"); + + std::vector nan_vec = {"NaN", "nan"}; + + for (const auto& v : nan_vec) + { + auto nan_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(nan_ret.has_value(), Eq(true)); + ASSERT_THAT(std::isnan(nan_ret.value()), Eq(true)); + } +} + +TEST_F(convert_test, fromString_EdgeCase_Float_Inf) +{ + ::testing::Test::RecordProperty("TEST_ID", "82dba3ae-5802-4fbc-aa91-15f4a2953573"); + + std::vector inf_vec = { + "INF", "Inf", "inf", "INFINITY", "Infinity", "-INF", "-Inf", "-inf", "-INFINITY", "-Infinity"}; + + for (const auto& v : inf_vec) + { + auto inf_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(inf_ret.has_value(), Eq(true)); + ASSERT_THAT(std::isinf(inf_ret.value()), Eq(true)); + } +} + +TEST_F(convert_test, fromString_EdgeCase_Double_Inf) +{ + ::testing::Test::RecordProperty("TEST_ID", "e4ccd01d-b1d1-433e-ba04-548dcc479bb1"); + + std::vector inf_vec = { + "INF", "Inf", "inf", "INFINITY", "Infinity", "-INF", "-Inf", "-inf", "-INFINITY", "-Infinity"}; + + for (const auto& v : inf_vec) + { + auto inf_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(inf_ret.has_value(), Eq(true)); + ASSERT_THAT(std::isinf(inf_ret.value()), Eq(true)); + } +} + +TEST_F(convert_test, fromString_EdgeCase_LongDouble_Inf) +{ + ::testing::Test::RecordProperty("TEST_ID", "6b8a3284-5f20-4cd6-9958-a2abb348ebe2"); + + std::vector inf_vec = { + "INF", "Inf", "inf", "INFINITY", "Infinity", "-INF", "-Inf", "-inf", "-INFINITY", "-Infinity"}; + + for (const auto& v : inf_vec) + { + auto inf_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(inf_ret.has_value(), Eq(true)); + ASSERT_THAT(std::isinf(inf_ret.value()), Eq(true)); + } +} + +/// SPECIAL FLOATING POINT TYPE EDGE CASES END + TEST_F(convert_test, fromString_cxxString) { ::testing::Test::RecordProperty("TEST_ID", "dbf015bb-5f51-47e1-9d0e-0525f65e7803"); From a6d13a8e4f14c9efd3acb9a1f2450dddacc3e1d8 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Fri, 5 Jan 2024 14:50:13 +0800 Subject: [PATCH 04/10] iox-#2055 Try to fix the difference behavior in MSVC In MSVC (Windows), the POSIX function such as 'strtof' can take a sub normal floating point value as valid input, which is different from other platforms such as Linux. Therefore, we should treat this case as a special one. Additionally, "NAN" is added into 'nan_vector'. Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index e0e63b4103..c724b716a6 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -570,10 +570,17 @@ TEST_F(convert_test, fromString_EdgeCase_Float) EXPECT_THAT(float_min.value(), FloatEq(std::numeric_limits::min())); // strtof will trigger ERANGE if the input is a subnormal float, resulting in a nullopt return value. + // note that for MSVC, sub normal float is a valid input! auto normal_float_min_eps = std::nextafter(std::numeric_limits::min(), 0.0F); source = fp_to_string(std::numeric_limits::min() - normal_float_min_eps); auto float_min_dec_eps = iox::convert::from_string(source.c_str()); +#ifdef _MSC_VER + ASSERT_THAT(float_min_dec_eps.has_value(), Eq(true)); + ASSERT_THAT(std::fpclassify(float_min_dec_eps.value()), Eq(FP_SUBNORMAL)); + EXPECT_THAT(float_min_dec_eps.value(), FloatNear(0.0F, std::numeric_limits::min())); +#else ASSERT_THAT(float_min_dec_eps.has_value(), Eq(false)); +#endif source = fp_to_string(std::numeric_limits::lowest()); auto float_lowest = iox::convert::from_string(source.c_str()); @@ -598,7 +605,13 @@ TEST_F(convert_test, fromString_EdgeCase_Double) auto normal_double_min_eps = std::nextafter(std::numeric_limits::min(), 0.0); source = fp_to_string(std::numeric_limits::min() - normal_double_min_eps); auto double_min_dec_eps = iox::convert::from_string(source.c_str()); +#ifdef _MSC_VER + ASSERT_THAT(double_min_dec_eps.has_value(), Eq(true)); + ASSERT_THAT(std::fpclassify(double_min_dec_eps.value()), Eq(FP_SUBNORMAL)); + EXPECT_THAT(double_min_dec_eps.value(), DoubleNear(0.0L, std::numeric_limits::min())); +#else ASSERT_THAT(double_min_dec_eps.has_value(), Eq(false)); +#endif source = fp_to_string(std::numeric_limits::lowest()); auto double_lowest = iox::convert::from_string(source.c_str()); @@ -624,7 +637,14 @@ TEST_F(convert_test, fromString_EdgeCase_LongDouble) auto normal_long_double_min_eps = std::nextafter(std::numeric_limits::min(), 0.0L); source = fp_to_string(std::numeric_limits::min() - normal_long_double_min_eps); auto long_double_min_dec_eps = iox::convert::from_string(source.c_str()); +#ifdef _MSC_VER + ASSERT_THAT(long_double_min_dec_eps.has_value(), Eq(true)); + ASSERT_THAT(std::fpclassify(long_double_min_dec_eps.value()), Eq(FP_SUBNORMAL)); + // There's no LongDoubleNear + EXPECT_TRUE(std::fabsl(long_double_min_dec_eps.value() - 0.0L) <= std::numeric_limits::min()); +#else ASSERT_THAT(long_double_min_dec_eps.has_value(), Eq(false)); +#endif source = fp_to_string(std::numeric_limits::lowest()); auto long_double_lowest = iox::convert::from_string(source.c_str()); @@ -645,7 +665,7 @@ TEST_F(convert_test, fromString_EdgeCase_Float_NaN) { ::testing::Test::RecordProperty("TEST_ID", "772bcbc3-d55b-464f-873f-82754ad543f3"); - std::vector nan_vec = {"NaN", "nan"}; + std::vector nan_vec = {"NAN", "NaN", "nan"}; for (const auto& v : nan_vec) { @@ -659,7 +679,7 @@ TEST_F(convert_test, fromString_EdgeCase_Double_NaN) { ::testing::Test::RecordProperty("TEST_ID", "a27c8575-658c-465d-a1a2-4f2f6b9a723a"); - std::vector nan_vec = {"NaN", "nan"}; + std::vector nan_vec = {"NAN", "NaN", "nan"}; for (const auto& v : nan_vec) { @@ -673,7 +693,7 @@ TEST_F(convert_test, fromString_EdgeCase_LongDouble_NaN) { ::testing::Test::RecordProperty("TEST_ID", "486f4e78-6000-4401-bb66-62d26b1d0cce"); - std::vector nan_vec = {"NaN", "nan"}; + std::vector nan_vec = {"NAN", "NaN", "nan"}; for (const auto& v : nan_vec) { From 8cb0caac825c219e66d881713b9b0cd52561dd05 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 6 Jan 2024 03:34:55 +0800 Subject: [PATCH 05/10] iox-#2055 Refactor test cases Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 277 +++++++++++------- 1 file changed, 171 insertions(+), 106 deletions(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index c724b716a6..78a1eef60e 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -315,7 +315,7 @@ TEST_F(convert_test, fromString_LongInt_Fail) /// SINGED INTEGRAL EDGE CASES START /// inc: increment, dec: decrement -TEST_F(convert_test, fromString_EdgeCase_SignedChar) +TEST_F(convert_test, fromString_SignedChar_EdgeCase_InRange_Success) { ::testing::Test::RecordProperty("TEST_ID", "3a70481e-ae86-4b96-92c4-44169700e93a"); @@ -324,60 +324,75 @@ TEST_F(convert_test, fromString_EdgeCase_SignedChar) ASSERT_THAT(signed_char_min.has_value(), Eq(true)); EXPECT_THAT(signed_char_min.value(), Eq(static_cast(-128))); - source = "-129"; - auto signed_char_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(signed_char_min_dec_1.has_value(), Eq(false)); - source = "127"; auto signed_char_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(signed_char_max.has_value(), Eq(true)); EXPECT_THAT(signed_char_max.value(), Eq(static_cast(127))); +} + +TEST_F(convert_test, fromString_SignedChar_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "23a3aa3d-954f-43f3-96e9-6b885bbb0c86"); + + std::string source = "-129"; + auto signed_char_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(signed_char_min_dec_1.has_value(), Eq(false)); source = "128"; auto signed_char_max_inc_1 = iox::convert::from_string(source.c_str()); ASSERT_THAT(signed_char_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_EdgeCase_SignedShort) +TEST_F(convert_test, fromString_SignedShort_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "98e33efd-ba39-4b88-8307-358be30e4e73"); + ::testing::Test::RecordProperty("TEST_ID", "68f802ce-feb5-46d9-a956-dd3ca1a3ce53"); std::string source = "-32768"; auto short_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(short_min.has_value(), Eq(true)); EXPECT_THAT(short_min.value(), Eq(static_cast(-32768))); - source = "-32769"; - auto short_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(short_min_dec_1.has_value(), Eq(false)); - source = "32767"; auto short_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(short_max.has_value(), Eq(true)); EXPECT_THAT(short_max.value(), Eq(static_cast(32767))); +} + +TEST_F(convert_test, fromString_SignedShort_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "98e33efd-ba39-4b88-8307-358be30e4e73"); + + std::string source = "-32769"; + auto short_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(short_min_dec_1.has_value(), Eq(false)); source = "32768"; auto short_max_inc_1 = iox::convert::from_string(source.c_str()); ASSERT_THAT(short_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_EdgeCase_SignedInt) +TEST_F(convert_test, fromString_SignedInt_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "abf0fda5-044e-4f1b-bb1e-31b701578a3d"); + ::testing::Test::RecordProperty("TEST_ID", "7333d0f9-dd48-4a88-85c9-2167d65633db"); std::string source = "-2147483648"; auto int_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(int_min.has_value(), Eq(true)); EXPECT_THAT(int_min.value(), Eq(-2147483648)); - source = "-2147483649"; - auto int_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(int_min_dec_1.has_value(), Eq(false)); - source = "2147483647"; auto int_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(int_max.has_value(), Eq(true)); EXPECT_THAT(int_max.value(), Eq(2147483647)); +} + +TEST_F(convert_test, fromString_SignedInt_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "abf0fda5-044e-4f1b-bb1e-31b701578a3d"); + + std::string source = "-2147483649"; + auto int_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(int_min_dec_1.has_value(), Eq(false)); source = "2147483648"; auto int_max_inc_1 = iox::convert::from_string(source.c_str()); @@ -385,34 +400,39 @@ TEST_F(convert_test, fromString_EdgeCase_SignedInt) } // platform dependent (32/64 bit system only) -TEST_F(convert_test, fromString_EdgeCase_SignedLong) +TEST_F(convert_test, fromString_SignedLong_EdgeCase_InRange_Success) { ::testing::Test::RecordProperty("TEST_ID", "5dc4c773-6a51-42b6-ad94-7ec885263856"); - constexpr bool IS_32_BIT{sizeof(long) != sizeof(long long)}; - std::string source = std::to_string(std::numeric_limits::min()); auto long_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(long_min.has_value(), Eq(true)); EXPECT_THAT(long_min.value(), Eq(std::numeric_limits::min())); - source = IS_32_BIT ? "-2147483649" : "-9223372036854775809"; - auto long_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(long_min_dec_1.has_value(), Eq(false)); - source = std::to_string(std::numeric_limits::max()); auto long_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(long_max.has_value(), Eq(true)); EXPECT_THAT(long_max.value(), Eq(std::numeric_limits::max())); +} + +TEST_F(convert_test, fromString_SignedLong_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "fb349f9c-0800-4f0b-8940-9012f9946b77"); + + constexpr bool IS_32_BIT{sizeof(long) != sizeof(long long)}; + + std::string source = IS_32_BIT ? "-2147483649" : "-9223372036854775809"; + auto long_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_min_dec_1.has_value(), Eq(false)); source = IS_32_BIT ? "2147483648" : "9223372036854775808"; auto long_max_inc_1 = iox::convert::from_string(source.c_str()); ASSERT_THAT(long_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_EdgeCase_SignedLongLong) +TEST_F(convert_test, fromString_SignedLongLong_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "7c015ac0-06a7-407d-aa93-d39c50734951"); + ::testing::Test::RecordProperty("TEST_ID", "1f1b0456-3b50-49ce-a69d-fe1d71a39230"); std::string source = "-9223372036854775808"; auto long_long_min = iox::convert::from_string(source.c_str()); @@ -420,14 +440,19 @@ TEST_F(convert_test, fromString_EdgeCase_SignedLongLong) // we don't use -9223372036854775808LL here for the compiler will parse it in way we don't want EXPECT_THAT(long_long_min.value(), Eq(std::numeric_limits::min())); - source = "-9223372036854775809"; - auto long_long_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(long_long_min_dec_1.has_value(), Eq(false)); - source = "9223372036854775807"; auto long_long_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(long_long_max.has_value(), Eq(true)); EXPECT_THAT(long_long_max.value(), Eq(9223372036854775807LL)); +} + +TEST_F(convert_test, fromString_SignedLongLong_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "7c015ac0-06a7-407d-aa93-d39c50734951"); + + std::string source = "-9223372036854775809"; + auto long_long_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_long_min_dec_1.has_value(), Eq(false)); source = "9223372036854775808"; auto long_long_max_inc_1 = iox::convert::from_string(source.c_str()); @@ -438,53 +463,63 @@ TEST_F(convert_test, fromString_EdgeCase_SignedLongLong) /// UNSINGED INTEGRAL EDGE CASES START -TEST_F(convert_test, fromString_EdgeCase_UnSignedChar) +TEST_F(convert_test, fromString_UnSignedChar_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "c11d74a1-be55-41fc-952f-519546eb04fe"); + ::testing::Test::RecordProperty("TEST_ID", "4d2debf7-a6fb-40e1-b672-cb3520be98dd"); std::string source = "0"; auto unchar_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(unchar_min.has_value(), Eq(true)); EXPECT_THAT(unchar_min.value(), Eq(0)); - source = "-1"; - auto unchar_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(unchar_min_dec_1.has_value(), Eq(false)); - source = "255"; auto unchar_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unchar_max.has_value(), Eq(true)); EXPECT_THAT(unchar_max.value(), Eq(static_cast(255))); +} + +TEST_F(convert_test, fromString_UnSignedChar_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "c11d74a1-be55-41fc-952f-519546eb04fe"); + + std::string source = "-1"; + auto unchar_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unchar_min_dec_1.has_value(), Eq(false)); source = "256"; auto unchar_max_inc_1 = iox::convert::from_string(source.c_str()); ASSERT_THAT(unchar_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_EdgeCase_UnSignedShort) +TEST_F(convert_test, fromString_UnSignedShort_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "f9196939-ae5d-4c27-85bf-b3b084343261"); + ::testing::Test::RecordProperty("TEST_ID", "cf168594-e673-4dba-90b7-9b11a3edb967"); std::string source = "0"; auto unshort_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(unshort_min.has_value(), Eq(true)); EXPECT_THAT(unshort_min.value(), Eq(0)); - source = "-1"; - auto unshort_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(unshort_min_dec_1.has_value(), Eq(false)); - source = "65535"; auto unshort_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unshort_max.has_value(), Eq(true)); EXPECT_THAT(unshort_max.value(), Eq(static_cast(65535))); +} + +TEST_F(convert_test, fromString_UnSignedShort_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "f9196939-ae5d-4c27-85bf-b3b084343261"); + + std::string source = "-1"; + auto unshort_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unshort_min_dec_1.has_value(), Eq(false)); source = "65536"; auto unshort_max_inc_1 = iox::convert::from_string(source.c_str()); ASSERT_THAT(unshort_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_EdgeCase_UnSignedInt) +TEST_F(convert_test, fromString_UnSignedInt_EdgeCase_InRange_Success) { ::testing::Test::RecordProperty("TEST_ID", "c2a832ef-3e86-4303-a98c-63c7b11ea789"); @@ -493,14 +528,19 @@ TEST_F(convert_test, fromString_EdgeCase_UnSignedInt) ASSERT_THAT(unint_min.has_value(), Eq(true)); EXPECT_THAT(unint_min.value(), Eq(0)); - source = "-1"; - auto unint_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(unint_min_dec_1.has_value(), Eq(false)); - source = "4294967295"; auto unint_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unint_max.has_value(), Eq(true)); EXPECT_THAT(unint_max.value(), Eq(4294967295U)); +} + +TEST_F(convert_test, fromString_UnSignedInt_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "64216d84-c008-47fc-ab9b-0e8d77aeb6c2"); + + std::string source = "-1"; + auto unint_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unint_min_dec_1.has_value(), Eq(false)); source = "4294967296"; auto unint_max_inc_1 = iox::convert::from_string(source.c_str()); @@ -508,48 +548,58 @@ TEST_F(convert_test, fromString_EdgeCase_UnSignedInt) } // platform dependent (32/64 bit system only) -TEST_F(convert_test, fromString_EdgeCase_UnSignedLong) +TEST_F(convert_test, fromString_UnSignedLong_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "6e74e284-7f13-4d77-8d3f-009df216828f"); - - constexpr bool IS_32_BIT{sizeof(long) != sizeof(long long)}; + ::testing::Test::RecordProperty("TEST_ID", "1aaac1ae-3d59-4443-8d88-bfe4e50569a8"); std::string source = "0"; auto unlong_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(unlong_min.has_value(), Eq(true)); EXPECT_THAT(unlong_min.value(), Eq(0)); - source = "-1"; - auto unlong_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(unlong_min_dec_1.has_value(), Eq(false)); - source = std::to_string(std::numeric_limits::max()); auto unlong_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unlong_max.has_value(), Eq(true)); EXPECT_THAT(unlong_max.value(), Eq(std::numeric_limits::max())); +} + +TEST_F(convert_test, fromString_UnSignedLong_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "6e74e284-7f13-4d77-8d3f-009df216828f"); + + constexpr bool IS_32_BIT{sizeof(long) != sizeof(long long)}; + + std::string source = "-1"; + auto unlong_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_min_dec_1.has_value(), Eq(false)); source = IS_32_BIT ? "4294967296" : "18446744073709551616"; auto unlong_max_inc_1 = iox::convert::from_string(source.c_str()); ASSERT_THAT(unlong_max_inc_1.has_value(), Eq(false)); } -TEST_F(convert_test, fromString_EdgeCase_UnSignedLongLong) +TEST_F(convert_test, fromString_UnSignedLongLong_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "96456d6f-2493-4db2-b5fa-f96f92ec64dd"); + ::testing::Test::RecordProperty("TEST_ID", "b3c70efd-f875-45bd-b86c-126dc44d3238"); std::string source = "0"; auto unlong_long_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(unlong_long_min.has_value(), Eq(true)); EXPECT_THAT(unlong_long_min.value(), Eq(0)); - source = "-1"; - auto unlong_long_min_dec_1 = iox::convert::from_string(source.c_str()); - ASSERT_THAT(unlong_long_min_dec_1.has_value(), Eq(false)); - source = "18446744073709551615"; auto unlong_long_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unlong_long_max.has_value(), Eq(true)); EXPECT_THAT(unlong_long_max.value(), Eq(18446744073709551615ULL)); +} + +TEST_F(convert_test, fromString_UnSignedLongLong_EdgeCase_OutOfRange_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "96456d6f-2493-4db2-b5fa-f96f92ec64dd"); + + std::string source = "-1"; + auto unlong_long_min_dec_1 = iox::convert::from_string(source.c_str()); + ASSERT_THAT(unlong_long_min_dec_1.has_value(), Eq(false)); source = "18446744073709551616"; auto unlong_long_max_inc_1 = iox::convert::from_string(source.c_str()); @@ -560,19 +610,34 @@ TEST_F(convert_test, fromString_EdgeCase_UnSignedLongLong) /// NORMAL FLOATING POINT TYPE EDGE CASES START -TEST_F(convert_test, fromString_EdgeCase_Float) +TEST_F(convert_test, fromString_Float_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "68d4f096-a93c-406b-b081-fe50e4b1a2c9"); + ::testing::Test::RecordProperty("TEST_ID", "cf849d5d-d0ed-4447-89b8-d6b9f47287c7"); std::string source = fp_to_string(std::numeric_limits::min()); auto float_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(float_min.has_value(), Eq(true)); EXPECT_THAT(float_min.value(), FloatEq(std::numeric_limits::min())); + source = fp_to_string(std::numeric_limits::lowest()); + auto float_lowest = iox::convert::from_string(source.c_str()); + ASSERT_THAT(float_lowest.has_value(), Eq(true)); + EXPECT_THAT(float_lowest.value(), FloatEq(std::numeric_limits::lowest())); + + source = fp_to_string(std::numeric_limits::max()); + auto float_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(float_max.has_value(), Eq(true)); + EXPECT_THAT(float_max.value(), FloatEq(std::numeric_limits::max())); +} + +TEST_F(convert_test, fromString_Float_EdgeCase_SubNormalFloat_ShouldFailExceptMsvc) +{ + ::testing::Test::RecordProperty("TEST_ID", "68d4f096-a93c-406b-b081-fe50e4b1a2c9"); + // strtof will trigger ERANGE if the input is a subnormal float, resulting in a nullopt return value. // note that for MSVC, sub normal float is a valid input! auto normal_float_min_eps = std::nextafter(std::numeric_limits::min(), 0.0F); - source = fp_to_string(std::numeric_limits::min() - normal_float_min_eps); + std::string source = fp_to_string(std::numeric_limits::min() - normal_float_min_eps); auto float_min_dec_eps = iox::convert::from_string(source.c_str()); #ifdef _MSC_VER ASSERT_THAT(float_min_dec_eps.has_value(), Eq(true)); @@ -581,38 +646,17 @@ TEST_F(convert_test, fromString_EdgeCase_Float) #else ASSERT_THAT(float_min_dec_eps.has_value(), Eq(false)); #endif - - source = fp_to_string(std::numeric_limits::lowest()); - auto float_lowest = iox::convert::from_string(source.c_str()); - ASSERT_THAT(float_lowest.has_value(), Eq(true)); - EXPECT_THAT(float_lowest.value(), FloatEq(std::numeric_limits::lowest())); - - source = fp_to_string(std::numeric_limits::max()); - auto float_max = iox::convert::from_string(source.c_str()); - ASSERT_THAT(float_max.has_value(), Eq(true)); - EXPECT_THAT(float_max.value(), FloatEq(std::numeric_limits::max())); } -TEST_F(convert_test, fromString_EdgeCase_Double) +TEST_F(convert_test, fromString_Double_EdgeCase_InRange_Success) { - ::testing::Test::RecordProperty("TEST_ID", "af7ca2e6-ba7e-41f7-a321-5f68617d3566"); + ::testing::Test::RecordProperty("TEST_ID", "d5e5e5ad-92ed-4229-8128-4ee82059fbf7"); std::string source = fp_to_string(std::numeric_limits::min()); auto double_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(double_min.has_value(), Eq(true)); EXPECT_THAT(double_min.value(), DoubleEq(std::numeric_limits::min())); - auto normal_double_min_eps = std::nextafter(std::numeric_limits::min(), 0.0); - source = fp_to_string(std::numeric_limits::min() - normal_double_min_eps); - auto double_min_dec_eps = iox::convert::from_string(source.c_str()); -#ifdef _MSC_VER - ASSERT_THAT(double_min_dec_eps.has_value(), Eq(true)); - ASSERT_THAT(std::fpclassify(double_min_dec_eps.value()), Eq(FP_SUBNORMAL)); - EXPECT_THAT(double_min_dec_eps.value(), DoubleNear(0.0L, std::numeric_limits::min())); -#else - ASSERT_THAT(double_min_dec_eps.has_value(), Eq(false)); -#endif - source = fp_to_string(std::numeric_limits::lowest()); auto double_lowest = iox::convert::from_string(source.c_str()); ASSERT_THAT(double_lowest.has_value(), Eq(true)); @@ -624,9 +668,25 @@ TEST_F(convert_test, fromString_EdgeCase_Double) EXPECT_THAT(double_max.value(), DoubleEq(std::numeric_limits::max())); } -TEST_F(convert_test, fromString_EdgeCase_LongDouble) +TEST_F(convert_test, fromString_Double_EdgeCase_SubNormalDouble_ShouldFailExceptMsvc) { - ::testing::Test::RecordProperty("TEST_ID", "fb96e526-8fb6-4af9-87f0-dfd4193237a5"); + ::testing::Test::RecordProperty("TEST_ID", "af7ca2e6-ba7e-41f7-a321-5f68617d3566"); + + auto normal_double_min_eps = std::nextafter(std::numeric_limits::min(), 0.0); + std::string source = fp_to_string(std::numeric_limits::min() - normal_double_min_eps); + auto double_min_dec_eps = iox::convert::from_string(source.c_str()); +#ifdef _MSC_VER + ASSERT_THAT(double_min_dec_eps.has_value(), Eq(true)); + ASSERT_THAT(std::fpclassify(double_min_dec_eps.value()), Eq(FP_SUBNORMAL)); + EXPECT_THAT(double_min_dec_eps.value(), DoubleNear(0.0, std::numeric_limits::min())); +#else + ASSERT_THAT(double_min_dec_eps.has_value(), Eq(false)); +#endif +} + +TEST_F(convert_test, fromString_LongDouble_EdgeCase_InRange_Success) +{ + ::testing::Test::RecordProperty("TEST_ID", "cab1c90b-1de0-4654-bbea-4bb4e55e4fc3"); std::string source = fp_to_string(std::numeric_limits::min()); auto long_double_min = iox::convert::from_string(source.c_str()); @@ -634,8 +694,23 @@ TEST_F(convert_test, fromString_EdgeCase_LongDouble) // There's no LongDoubleEq EXPECT_THAT(long_double_min.value(), Eq(std::numeric_limits::min())); + source = fp_to_string(std::numeric_limits::lowest()); + auto long_double_lowest = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_double_lowest.has_value(), Eq(true)); + EXPECT_THAT(long_double_lowest.value(), Eq(std::numeric_limits::lowest())); + + source = fp_to_string(std::numeric_limits::max()); + auto long_double_max = iox::convert::from_string(source.c_str()); + ASSERT_THAT(long_double_max.has_value(), Eq(true)); + EXPECT_THAT(long_double_max.value(), Eq(std::numeric_limits::max())); +} + +TEST_F(convert_test, fromString_LongDouble_EdgeCase_SubNormalLongDouble_ShouldFailExceptMsvc) +{ + ::testing::Test::RecordProperty("TEST_ID", "fb96e526-8fb6-4af9-87f0-dfd4193237a5"); + auto normal_long_double_min_eps = std::nextafter(std::numeric_limits::min(), 0.0L); - source = fp_to_string(std::numeric_limits::min() - normal_long_double_min_eps); + std::string source = fp_to_string(std::numeric_limits::min() - normal_long_double_min_eps); auto long_double_min_dec_eps = iox::convert::from_string(source.c_str()); #ifdef _MSC_VER ASSERT_THAT(long_double_min_dec_eps.has_value(), Eq(true)); @@ -645,23 +720,13 @@ TEST_F(convert_test, fromString_EdgeCase_LongDouble) #else ASSERT_THAT(long_double_min_dec_eps.has_value(), Eq(false)); #endif - - source = fp_to_string(std::numeric_limits::lowest()); - auto long_double_lowest = iox::convert::from_string(source.c_str()); - ASSERT_THAT(long_double_lowest.has_value(), Eq(true)); - EXPECT_THAT(long_double_lowest.value(), Eq(std::numeric_limits::lowest())); - - source = fp_to_string(std::numeric_limits::max()); - auto long_double_max = iox::convert::from_string(source.c_str()); - ASSERT_THAT(long_double_max.has_value(), Eq(true)); - EXPECT_THAT(long_double_max.value(), Eq(std::numeric_limits::max())); } /// NORMAL FLOATING POINT TYPE EDGE CASES END /// SPECIAL FLOATING POINT TYPE EDGE CASES START -TEST_F(convert_test, fromString_EdgeCase_Float_NaN) +TEST_F(convert_test, fromString_Float_EdgeCase_Nan_Success) { ::testing::Test::RecordProperty("TEST_ID", "772bcbc3-d55b-464f-873f-82754ad543f3"); @@ -675,7 +740,7 @@ TEST_F(convert_test, fromString_EdgeCase_Float_NaN) } } -TEST_F(convert_test, fromString_EdgeCase_Double_NaN) +TEST_F(convert_test, fromString_Double_EdgeCase_Nan_Success) { ::testing::Test::RecordProperty("TEST_ID", "a27c8575-658c-465d-a1a2-4f2f6b9a723a"); @@ -689,7 +754,7 @@ TEST_F(convert_test, fromString_EdgeCase_Double_NaN) } } -TEST_F(convert_test, fromString_EdgeCase_LongDouble_NaN) +TEST_F(convert_test, fromString_LongDouble_EdgeCase_Nan_Success) { ::testing::Test::RecordProperty("TEST_ID", "486f4e78-6000-4401-bb66-62d26b1d0cce"); @@ -703,7 +768,7 @@ TEST_F(convert_test, fromString_EdgeCase_LongDouble_NaN) } } -TEST_F(convert_test, fromString_EdgeCase_Float_Inf) +TEST_F(convert_test, fromString_Float_EdgeCase_Inf_Success) { ::testing::Test::RecordProperty("TEST_ID", "82dba3ae-5802-4fbc-aa91-15f4a2953573"); @@ -718,7 +783,7 @@ TEST_F(convert_test, fromString_EdgeCase_Float_Inf) } } -TEST_F(convert_test, fromString_EdgeCase_Double_Inf) +TEST_F(convert_test, fromString_Double_EdgeCase_Inf_Success) { ::testing::Test::RecordProperty("TEST_ID", "e4ccd01d-b1d1-433e-ba04-548dcc479bb1"); @@ -733,7 +798,7 @@ TEST_F(convert_test, fromString_EdgeCase_Double_Inf) } } -TEST_F(convert_test, fromString_EdgeCase_LongDouble_Inf) +TEST_F(convert_test, fromString_LongDouble_EdgeCase_Inf_Success) { ::testing::Test::RecordProperty("TEST_ID", "6b8a3284-5f20-4cd6-9958-a2abb348ebe2"); From eb3dc38f2a43f498d2f71d2c0bb8912e5e230fe2 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sun, 7 Jan 2024 03:21:24 +0800 Subject: [PATCH 06/10] iox-#2055 Make test expecting result more readable Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index 78a1eef60e..e7b7b15c6d 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -322,12 +322,12 @@ TEST_F(convert_test, fromString_SignedChar_EdgeCase_InRange_Success) std::string source = "-128"; auto signed_char_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(signed_char_min.has_value(), Eq(true)); - EXPECT_THAT(signed_char_min.value(), Eq(static_cast(-128))); + EXPECT_THAT(signed_char_min.value(), Eq(std::numeric_limits::min())); source = "127"; auto signed_char_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(signed_char_max.has_value(), Eq(true)); - EXPECT_THAT(signed_char_max.value(), Eq(static_cast(127))); + EXPECT_THAT(signed_char_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_SignedChar_EdgeCase_OutOfRange_Fail) @@ -350,12 +350,12 @@ TEST_F(convert_test, fromString_SignedShort_EdgeCase_InRange_Success) std::string source = "-32768"; auto short_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(short_min.has_value(), Eq(true)); - EXPECT_THAT(short_min.value(), Eq(static_cast(-32768))); + EXPECT_THAT(short_min.value(), Eq(std::numeric_limits::min())); source = "32767"; auto short_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(short_max.has_value(), Eq(true)); - EXPECT_THAT(short_max.value(), Eq(static_cast(32767))); + EXPECT_THAT(short_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_SignedShort_EdgeCase_OutOfRange_Fail) @@ -378,12 +378,12 @@ TEST_F(convert_test, fromString_SignedInt_EdgeCase_InRange_Success) std::string source = "-2147483648"; auto int_min = iox::convert::from_string(source.c_str()); ASSERT_THAT(int_min.has_value(), Eq(true)); - EXPECT_THAT(int_min.value(), Eq(-2147483648)); + EXPECT_THAT(int_min.value(), Eq(std::numeric_limits::min())); source = "2147483647"; auto int_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(int_max.has_value(), Eq(true)); - EXPECT_THAT(int_max.value(), Eq(2147483647)); + EXPECT_THAT(int_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_SignedInt_EdgeCase_OutOfRange_Fail) @@ -443,7 +443,7 @@ TEST_F(convert_test, fromString_SignedLongLong_EdgeCase_InRange_Success) source = "9223372036854775807"; auto long_long_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(long_long_max.has_value(), Eq(true)); - EXPECT_THAT(long_long_max.value(), Eq(9223372036854775807LL)); + EXPECT_THAT(long_long_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_SignedLongLong_EdgeCase_OutOfRange_Fail) @@ -475,7 +475,7 @@ TEST_F(convert_test, fromString_UnSignedChar_EdgeCase_InRange_Success) source = "255"; auto unchar_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unchar_max.has_value(), Eq(true)); - EXPECT_THAT(unchar_max.value(), Eq(static_cast(255))); + EXPECT_THAT(unchar_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_UnSignedChar_EdgeCase_OutOfRange_Fail) @@ -503,7 +503,7 @@ TEST_F(convert_test, fromString_UnSignedShort_EdgeCase_InRange_Success) source = "65535"; auto unshort_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unshort_max.has_value(), Eq(true)); - EXPECT_THAT(unshort_max.value(), Eq(static_cast(65535))); + EXPECT_THAT(unshort_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_UnSignedShort_EdgeCase_OutOfRange_Fail) @@ -531,7 +531,7 @@ TEST_F(convert_test, fromString_UnSignedInt_EdgeCase_InRange_Success) source = "4294967295"; auto unint_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unint_max.has_value(), Eq(true)); - EXPECT_THAT(unint_max.value(), Eq(4294967295U)); + EXPECT_THAT(unint_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_UnSignedInt_EdgeCase_OutOfRange_Fail) @@ -590,7 +590,7 @@ TEST_F(convert_test, fromString_UnSignedLongLong_EdgeCase_InRange_Success) source = "18446744073709551615"; auto unlong_long_max = iox::convert::from_string(source.c_str()); ASSERT_THAT(unlong_long_max.has_value(), Eq(true)); - EXPECT_THAT(unlong_long_max.value(), Eq(18446744073709551615ULL)); + EXPECT_THAT(unlong_long_max.value(), Eq(std::numeric_limits::max())); } TEST_F(convert_test, fromString_UnSignedLongLong_EdgeCase_OutOfRange_Fail) From 26e351d38268469289a63a1df9c4f34c8f9061fa Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sun, 7 Jan 2024 03:25:12 +0800 Subject: [PATCH 07/10] iox-#2055 Rename to 'fromString_ioxString' Signed-off-by: Dennis Liu --- iceoryx_hoofs/test/moduletests/test_utility_convert.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index e7b7b15c6d..1b8bcdfb9f 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -815,7 +815,7 @@ TEST_F(convert_test, fromString_LongDouble_EdgeCase_Inf_Success) /// SPECIAL FLOATING POINT TYPE EDGE CASES END -TEST_F(convert_test, fromString_cxxString) +TEST_F(convert_test, fromString_ioxString) { ::testing::Test::RecordProperty("TEST_ID", "dbf015bb-5f51-47e1-9d0e-0525f65e7803"); std::string source = "hello"; From 2e76a176eb50fde1a6f6b5decd005c01b0660b24 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sun, 7 Jan 2024 15:55:57 +0800 Subject: [PATCH 08/10] iox-#2055 Add tests of decimal notation Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index 1b8bcdfb9f..c20a039d2c 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -23,6 +23,7 @@ #include #include +#include namespace { using namespace ::testing; @@ -813,6 +814,81 @@ TEST_F(convert_test, fromString_LongDouble_EdgeCase_Inf_Success) } } +TEST_F(convert_test, fromString_Float_EdgeCase_ZeroDecimalNotation_Success) +{ + ::testing::Test::RecordProperty("TEST_ID", "4ac285f9-e107-4d74-8aca-5d87032794db"); + + std::vector decimal_notation_vec = {"0", "-0", ".0", "-.0", "0.0", "-0.0", "0.", "-0."}; + + for (const auto& v : decimal_notation_vec) + { + auto decimal_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(decimal_ret.has_value(), Eq(true)); + ASSERT_THAT(decimal_ret.value(), Eq(0.0F)); + } +} + +TEST_F(convert_test, fromString_Double_EdgeCase_ZeroDecimalNotation_Success) +{ + ::testing::Test::RecordProperty("TEST_ID", "98938eaa-c472-4338-a153-5c2de9eb4940"); + + std::vector decimal_notation_vec = {"0", "-0", ".0", "-.0", "0.0", "-0.0", "0.", "-0."}; + + for (const auto& v : decimal_notation_vec) + { + auto decimal_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(decimal_ret.has_value(), Eq(true)); + ASSERT_THAT(decimal_ret.value(), Eq(0.0)); + } +} + +TEST_F(convert_test, fromString_LongDouble_EdgeCase_ZeroDecimalNotation_Success) +{ + ::testing::Test::RecordProperty("TEST_ID", "49fc0812-47c0-4815-8a15-a94a81493ea0"); + + std::vector decimal_notation_vec = {"0", "-0", ".0", "-.0", "0.0", "-0.0", "0.", "-0."}; + + for (const auto& v : decimal_notation_vec) + { + auto decimal_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(decimal_ret.has_value(), Eq(true)); + ASSERT_THAT(decimal_ret.value(), Eq(0.0L)); + } +} + +TEST_F(convert_test, fromString_Float_EdgeCase_OtherDecimalNotation_Success) +{ + ::testing::Test::RecordProperty("TEST_ID", "278ff5af-28bd-4c11-839e-160e148c5a64"); + + std::string source = ".1"; + + auto decimal_ret = iox::convert::from_string(source.c_str()); + ASSERT_THAT(decimal_ret.has_value(), Eq(true)); + ASSERT_THAT(decimal_ret.value(), Eq(0.1F)); +} + +TEST_F(convert_test, fromString_Double_EdgeCase_OtherDecimalNotation_Success) +{ + ::testing::Test::RecordProperty("TEST_ID", "a8539f9a-1c7a-4d81-9a88-ef8a6630f065"); + + std::string source = ".1"; + + auto decimal_ret = iox::convert::from_string(source.c_str()); + ASSERT_THAT(decimal_ret.has_value(), Eq(true)); + ASSERT_THAT(decimal_ret.value(), Eq(0.1)); +} + +TEST_F(convert_test, fromString_LongDouble_EdgeCase_OtherDecimalNotation_Success) +{ + ::testing::Test::RecordProperty("TEST_ID", "d71ec687-aaab-45d5-aee5-2ec9a51602d0"); + + std::string source = ".1"; + + auto decimal_ret = iox::convert::from_string(source.c_str()); + ASSERT_THAT(decimal_ret.has_value(), Eq(true)); + ASSERT_THAT(decimal_ret.value(), Eq(0.1L)); +} + /// SPECIAL FLOATING POINT TYPE EDGE CASES END TEST_F(convert_test, fromString_ioxString) From cb6b50acf8cfb1c5a9ebaae5d28d1b03f1d36951 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sun, 7 Jan 2024 16:04:11 +0800 Subject: [PATCH 09/10] iox-#2055 Add tests of invalid trailing char Signed-off-by: Dennis Liu --- .../test/moduletests/test_utility_convert.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index c20a039d2c..4f0f9444a1 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -313,6 +313,35 @@ TEST_F(convert_test, fromString_LongInt_Fail) ASSERT_THAT(result.has_value(), Eq(false)); } +TEST_F(convert_test, fromString_Integer_InvalidTrailingChar_Fail) +{ + ::testing::Test::RecordProperty("TEST_ID", "6a70f10f-227b-4b0a-8149-e5ca3c793b5d"); + + using IntegerType = std::tuple; + std::vector invalid_input = {"42a", "74 ", "-52-"}; + + // a lambda to iterate all invalid_input cases converting to type decltype(dummy) + auto expect_failure = [&invalid_input](auto dummy) { + using T = decltype(dummy); + for (const auto& v : invalid_input) + { + auto invalid_ret = iox::convert::from_string(v.c_str()); + ASSERT_THAT(invalid_ret.has_value(), Eq(false)); + } + }; + + std::apply([&](auto... args) { (..., expect_failure(args)); }, IntegerType{}); +} + /// SINGED INTEGRAL EDGE CASES START /// inc: increment, dec: decrement From a9ff0cb62d0b8c7cdf871560a66df7a1b3d19daf Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sun, 7 Jan 2024 18:54:07 +0800 Subject: [PATCH 10/10] iox-#2055 Fix lambda complaint from MSVC Signed-off-by: Dennis Liu --- iceoryx_hoofs/test/moduletests/test_utility_convert.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp index 4f0f9444a1..0d17481ca4 100644 --- a/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp +++ b/iceoryx_hoofs/test/moduletests/test_utility_convert.cpp @@ -339,7 +339,7 @@ TEST_F(convert_test, fromString_Integer_InvalidTrailingChar_Fail) } }; - std::apply([&](auto... args) { (..., expect_failure(args)); }, IntegerType{}); + std::apply([&expect_failure](auto... args) { (..., expect_failure(args)); }, IntegerType{}); } /// SINGED INTEGRAL EDGE CASES START