From be9c643032474f9dab60026e54fa604abf8840ae Mon Sep 17 00:00:00 2001 From: ckormanyos Date: Fri, 4 Jul 2025 19:58:46 +0200 Subject: [PATCH] Try for even more cover lines --- .../boost/multiprecision/cpp_dec_float.hpp | 29 ++++++++++------- test/test_arithmetic.hpp | 16 +++++----- test/test_cpp_bin_float_io.cpp | 6 ++++ test/test_various_edges_more.cpp | 32 +++++++++++++++++++ 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/include/boost/multiprecision/cpp_dec_float.hpp b/include/boost/multiprecision/cpp_dec_float.hpp index dade6983..7014e12b 100644 --- a/include/boost/multiprecision/cpp_dec_float.hpp +++ b/include/boost/multiprecision/cpp_dec_float.hpp @@ -2017,12 +2017,14 @@ std::string cpp_dec_float::str(std::intmax_t const std::uint32_t ix = number_of_digits == 0 ? 0 : static_cast(static_cast(my_str[static_cast(number_of_digits - 1)]) - static_cast('0')); if ((ix & 1u) == 0) { - // We have an even digit followed by a 5, so we might not actually need to round up - // if all the remaining digits are zero: + // We have an even digit followed by a 5, so we might not actually + // need to round up if all the remaining digits are zero: if (my_str.find_first_not_of('0', static_cast(number_of_digits + 1)) == std::string::npos) { bool all_zeros = true; - // No none-zero trailing digits in the string, now check whatever parts we didn't convert to the string: + + // There are no non-zero trailing digits in the string, + // now check whatever parts we didn't convert to the string: for (std::size_t i = number_of_elements; i < data.size(); i++) { if (data[i]) @@ -2139,11 +2141,12 @@ bool cpp_dec_float::rd_string(const char* con // Get a possible +/- sign and remove it. neg = false; - if (my_str.size()) + if (!my_str.empty()) { if (my_str[0] == '-') { neg = true; + my_str.erase(0, 1); } else if (my_str[0] == '+') @@ -2151,23 +2154,25 @@ bool cpp_dec_float::rd_string(const char* con my_str.erase(0, 1); } } + // // Special cases for infinities and NaN's: // if ((my_str == "inf") || (my_str == "INF") || (my_str == "infinity") || (my_str == "INFINITY")) { - if (neg) - { - *this = this->inf(); - this->negate(); - } - else - *this = this->inf(); + const bool tmp_neg { neg }; + + *this = this->inf(); + + neg = tmp_neg; + return true; } + if ((my_str.size() >= 3) && ((my_str.substr(0, 3) == "nan") || (my_str.substr(0, 3) == "NAN") || (my_str.substr(0, 3) == "NaN"))) { *this = this->nan(); + return true; } @@ -2485,7 +2490,7 @@ cpp_dec_float::cpp_dec_float(const double man d -= static_cast(n); d *= static_cast(cpp_dec_float_elem_mask); } -} +} // LCOV_EXCL_LINE template template diff --git a/test/test_arithmetic.hpp b/test/test_arithmetic.hpp index ffde719c..7d7cd1b7 100644 --- a/test/test_arithmetic.hpp +++ b/test/test_arithmetic.hpp @@ -3045,45 +3045,45 @@ void test_basic_conditionals(Real a, Real b) { if (a) { - BOOST_ERROR("Unexpected non-zero result"); + BOOST_ERROR("Unexpected non-zero result"); // LCOV_EXCL_LINE } if (!a) { } else { - BOOST_ERROR("Unexpected zero result"); + BOOST_ERROR("Unexpected zero result"); // LCOV_EXCL_LINE } b = 2; if (!b) { - BOOST_ERROR("Unexpected zero result"); + BOOST_ERROR("Unexpected zero result"); // LCOV_EXCL_LINE } if (b) { } else { - BOOST_ERROR("Unexpected non-zero result"); + BOOST_ERROR("Unexpected non-zero result"); // LCOV_EXCL_LINE } if (a && b) { - BOOST_ERROR("Unexpected zero result"); + BOOST_ERROR("Unexpected zero result"); // LCOV_EXCL_LINE } if (!(a || b)) { - BOOST_ERROR("Unexpected zero result"); + BOOST_ERROR("Unexpected zero result"); // LCOV_EXCL_LINE } if (a + b) { } else { - BOOST_ERROR("Unexpected zero result"); + BOOST_ERROR("Unexpected zero result"); // LCOV_EXCL_LINE } if (b - 2) { - BOOST_ERROR("Unexpected non-zero result"); + BOOST_ERROR("Unexpected non-zero result"); // LCOV_EXCL_LINE } } diff --git a/test/test_cpp_bin_float_io.cpp b/test/test_cpp_bin_float_io.cpp index ee22bd15..378eb29e 100644 --- a/test/test_cpp_bin_float_io.cpp +++ b/test/test_cpp_bin_float_io.cpp @@ -44,6 +44,7 @@ struct stopwatch typename Clock::time_point m_start; }; +// LCOV_EXCL_START void print_flags(std::ios_base::fmtflags f) { std::cout << "Formatting flags were: "; @@ -57,6 +58,7 @@ void print_flags(std::ios_base::fmtflags f) std::cout << "showpos "; std::cout << std::endl; } +// LCOV_EXCL_STOP template void test() @@ -88,6 +90,7 @@ void test() const char* expect = string_data[j][col]; if (ss.str() != expect) { + // LCOV_EXCL_START std::cout << std::setprecision(20) << "Testing value " << val << std::endl; print_flags(f[i]); std::cout << "Precision is: " << prec << std::endl; @@ -95,6 +98,7 @@ void test() std::cout << "Expected: " << expect << std::endl; ++boost::detail::test_errors(); mp_t(val).str(prec, f[i]); // for debugging + // LCOV_EXCL_STOP } } } @@ -120,6 +124,7 @@ void test() const char* expect = zeros[col]; if (ss.str() != expect) { + // LCOV_EXCL_START std::cout << std::setprecision(20) << "Testing value " << val << std::endl; print_flags(f[i]); std::cout << "Precision is: " << prec << std::endl; @@ -127,6 +132,7 @@ void test() std::cout << "Expected: " << expect << std::endl; ++boost::detail::test_errors(); mp_t(val).str(prec, f[i]); // for debugging + // LCOV_EXCL_STOP } } } diff --git a/test/test_various_edges_more.cpp b/test/test_various_edges_more.cpp index 98ca7666..c1950f1a 100644 --- a/test/test_various_edges_more.cpp +++ b/test/test_various_edges_more.cpp @@ -358,6 +358,38 @@ namespace local result_is_ok = (result_funky_strings_is_ok && result_is_ok); } + { + const std::initializer_list + infty_strings_list + { + std::string("inf"), std::string("INF"), std::string("infinity"), std::string("INFINITY"), + std::string("+inf"), std::string("+INF"), std::string("+infinity"), std::string("+INFINITY"), + std::string("-inf"), std::string("-INF"), std::string("-infinity"), std::string("-INFINITY") + }; + + const std::vector infty_strings(infty_strings_list); + + std::size_t infty_count { }; + + for(const auto& str : infty_strings) + { + const float_type flt_from_inf_string(str); + + const bool + result_infty_strings_is_ok + { + (boost::multiprecision::isinf)(flt_from_inf_string) + && ((infty_count < std::size_t { UINT8_C(8) }) ? (!signbit(flt_from_inf_string)) : signbit(flt_from_inf_string)) + }; + + ++infty_count; + + BOOST_TEST(result_infty_strings_is_ok); + + result_is_ok = (result_infty_strings_is_ok && result_is_ok); + } + } + { for(auto index = static_cast(UINT8_C(0)); index < static_cast(UINT8_C(16)); ++index) {