Skip to content

Commit a6d13a8

Browse files
committed
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 <[email protected]>
1 parent 30e60cc commit a6d13a8

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

iceoryx_hoofs/test/moduletests/test_utility_convert.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,17 @@ TEST_F(convert_test, fromString_EdgeCase_Float)
570570
EXPECT_THAT(float_min.value(), FloatEq(std::numeric_limits<float>::min()));
571571

572572
// strtof will trigger ERANGE if the input is a subnormal float, resulting in a nullopt return value.
573+
// note that for MSVC, sub normal float is a valid input!
573574
auto normal_float_min_eps = std::nextafter(std::numeric_limits<float>::min(), 0.0F);
574575
source = fp_to_string(std::numeric_limits<float>::min() - normal_float_min_eps);
575576
auto float_min_dec_eps = iox::convert::from_string<float>(source.c_str());
577+
#ifdef _MSC_VER
578+
ASSERT_THAT(float_min_dec_eps.has_value(), Eq(true));
579+
ASSERT_THAT(std::fpclassify(float_min_dec_eps.value()), Eq(FP_SUBNORMAL));
580+
EXPECT_THAT(float_min_dec_eps.value(), FloatNear(0.0F, std::numeric_limits<float>::min()));
581+
#else
576582
ASSERT_THAT(float_min_dec_eps.has_value(), Eq(false));
583+
#endif
577584

578585
source = fp_to_string(std::numeric_limits<float>::lowest());
579586
auto float_lowest = iox::convert::from_string<float>(source.c_str());
@@ -598,7 +605,13 @@ TEST_F(convert_test, fromString_EdgeCase_Double)
598605
auto normal_double_min_eps = std::nextafter(std::numeric_limits<double>::min(), 0.0);
599606
source = fp_to_string(std::numeric_limits<double>::min() - normal_double_min_eps);
600607
auto double_min_dec_eps = iox::convert::from_string<double>(source.c_str());
608+
#ifdef _MSC_VER
609+
ASSERT_THAT(double_min_dec_eps.has_value(), Eq(true));
610+
ASSERT_THAT(std::fpclassify(double_min_dec_eps.value()), Eq(FP_SUBNORMAL));
611+
EXPECT_THAT(double_min_dec_eps.value(), DoubleNear(0.0L, std::numeric_limits<double>::min()));
612+
#else
601613
ASSERT_THAT(double_min_dec_eps.has_value(), Eq(false));
614+
#endif
602615

603616
source = fp_to_string(std::numeric_limits<double>::lowest());
604617
auto double_lowest = iox::convert::from_string<double>(source.c_str());
@@ -624,7 +637,14 @@ TEST_F(convert_test, fromString_EdgeCase_LongDouble)
624637
auto normal_long_double_min_eps = std::nextafter(std::numeric_limits<long double>::min(), 0.0L);
625638
source = fp_to_string(std::numeric_limits<long double>::min() - normal_long_double_min_eps);
626639
auto long_double_min_dec_eps = iox::convert::from_string<long double>(source.c_str());
640+
#ifdef _MSC_VER
641+
ASSERT_THAT(long_double_min_dec_eps.has_value(), Eq(true));
642+
ASSERT_THAT(std::fpclassify(long_double_min_dec_eps.value()), Eq(FP_SUBNORMAL));
643+
// There's no LongDoubleNear
644+
EXPECT_TRUE(std::fabsl(long_double_min_dec_eps.value() - 0.0L) <= std::numeric_limits<long double>::min());
645+
#else
627646
ASSERT_THAT(long_double_min_dec_eps.has_value(), Eq(false));
647+
#endif
628648

629649
source = fp_to_string(std::numeric_limits<long double>::lowest());
630650
auto long_double_lowest = iox::convert::from_string<long double>(source.c_str());
@@ -645,7 +665,7 @@ TEST_F(convert_test, fromString_EdgeCase_Float_NaN)
645665
{
646666
::testing::Test::RecordProperty("TEST_ID", "772bcbc3-d55b-464f-873f-82754ad543f3");
647667

648-
std::vector<std::string> nan_vec = {"NaN", "nan"};
668+
std::vector<std::string> nan_vec = {"NAN", "NaN", "nan"};
649669

650670
for (const auto& v : nan_vec)
651671
{
@@ -659,7 +679,7 @@ TEST_F(convert_test, fromString_EdgeCase_Double_NaN)
659679
{
660680
::testing::Test::RecordProperty("TEST_ID", "a27c8575-658c-465d-a1a2-4f2f6b9a723a");
661681

662-
std::vector<std::string> nan_vec = {"NaN", "nan"};
682+
std::vector<std::string> nan_vec = {"NAN", "NaN", "nan"};
663683

664684
for (const auto& v : nan_vec)
665685
{
@@ -673,7 +693,7 @@ TEST_F(convert_test, fromString_EdgeCase_LongDouble_NaN)
673693
{
674694
::testing::Test::RecordProperty("TEST_ID", "486f4e78-6000-4401-bb66-62d26b1d0cce");
675695

676-
std::vector<std::string> nan_vec = {"NaN", "nan"};
696+
std::vector<std::string> nan_vec = {"NAN", "NaN", "nan"};
677697

678698
for (const auto& v : nan_vec)
679699
{

0 commit comments

Comments
 (0)