Added tests for NAN and INF

Fixed result for -INF
This commit is contained in:
John Wellbelove 2026-06-06 19:21:01 +01:00
parent a983b7177b
commit c34197ab2b
2 changed files with 39 additions and 3 deletions

View File

@ -218,7 +218,7 @@ namespace etl
/// Helper function for floating point nan and inf.
//***************************************************************************
template <typename TIString>
void add_nan_inf(const bool not_a_number, const bool infinity, TIString& str, const etl::basic_format_spec<TIString>& format)
void add_nan_inf(const bool not_a_number, const bool infinity, const bool is_negative, TIString& str, const etl::basic_format_spec<TIString>& format)
{
typedef typename TIString::value_type type;
@ -240,6 +240,11 @@ namespace etl
}
else if (infinity)
{
if (is_negative)
{
str.push_back(type('-'));
}
if (format.is_upper_case())
{
str.insert(str.end(), ETL_OR_STD11::begin(inf_upper), ETL_OR_STD11::end(inf_upper));
@ -433,7 +438,7 @@ namespace etl
if (isnan(value) || isinf(value))
{
etl::private_to_string::add_nan_inf(isnan(value), isinf(value), str, format);
etl::private_to_string::add_nan_inf(isnan(value), isinf(value), etl::is_negative(value), str, format);
}
else
{

View File

@ -28,7 +28,7 @@ SOFTWARE.
#include "unit_test_framework.h"
#include <format>
#include <strstream>
#include <iomanip>
#include <ostream>
@ -714,5 +714,36 @@ namespace
CHECK(etl::string<64>(STR(" 0")) == s6);
#endif
}
//*************************************************************************
TEST(test_issue_1436_case_support_for_nan_inf_in_to_string)
{
if (std::numeric_limits<double>::is_iec559)
{
etl::string<64> s1;
etl::to_string(std::numeric_limits<double>::quiet_NaN(), s1, Format().precision(5).width(15).right().scientific(true).upper_case(false));
CHECK(etl::string<64>(STR(" nan")) == s1);
etl::string<64> s2;
etl::to_string(std::numeric_limits<double>::infinity(), s2, Format().precision(5).width(15).right().scientific(true).upper_case(false));
CHECK(etl::string<64>(STR(" inf")) == s2);
etl::string<64> s3;
etl::to_string(-std::numeric_limits<double>::infinity(), s3, Format().precision(5).width(15).right().scientific(true).upper_case(false));
CHECK(etl::string<64>(STR(" -inf")) == s3);
etl::string<64> s4;
etl::to_string(std::numeric_limits<double>::quiet_NaN(), s4, Format().precision(5).width(15).right().scientific(true).upper_case(true));
CHECK(etl::string<64>(STR(" NAN")) == s4);
etl::string<64> s5;
etl::to_string(std::numeric_limits<double>::infinity(), s5, Format().precision(5).width(15).right().scientific(true).upper_case(true));
CHECK(etl::string<64>(STR(" INF")) == s5);
etl::string<64> s6;
etl::to_string(-std::numeric_limits<double>::infinity(), s6, Format().precision(5).width(15).right().scientific(true).upper_case(true));
CHECK(etl::string<64>(STR(" -INF")) == s6);
}
}
}
} // namespace