Skip to content

Commit

Permalink
Add test for formatting large (u)int64 numbers
Browse files Browse the repository at this point in the history
As reported in #235 formatting the first number which doesn't fit into
int64_t anymore fails to add the thousands separators.
I.e.:
`9223372036854775807` -> `9,223,372,036,854,775,807`
`9223372036854775808` -> `9223372036854775808`

Add a test reproducing that that for all backends.
  • Loading branch information
Flamefire committed Jan 12, 2025
1 parent b219350 commit f31ba3a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
38 changes: 37 additions & 1 deletion test/formatting_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,40 @@ void test_parse_multi_number()
BOOST_LOCALE_CALL(wchar_t);
#undef BOOST_LOCALE_CALL
#undef BOOST_LOCALE_CALL_I
}
}

template<typename CharType>
void test_format_large_number_by_char(const std::locale& locale)
{
std::basic_ostringstream<CharType> output;
output.imbue(locale);
output << boost::locale::as::number;

constexpr int64_t high_signed64 = 9223372036854775807;
static_assert(high_signed64 == std::numeric_limits<int64_t>::max(), "Value must match");

empty_stream(output) << high_signed64;
TEST_EQ(output.str(), ascii_to<CharType>("9,223,372,036,854,775,807"));
empty_stream(output) << static_cast<uint64_t>(high_signed64);
TEST_EQ(output.str(), ascii_to<CharType>("9,223,372,036,854,775,807"));
empty_stream(output) << (static_cast<uint64_t>(high_signed64) + 1u);
TEST_EQ(output.str(), ascii_to<CharType>("9,223,372,036,854,775,808"));
empty_stream(output) << (static_cast<uint64_t>(high_signed64) + 579u);
TEST_EQ(output.str(), ascii_to<CharType>("9,223,372,036,854,776,386"));
}

void test_format_large_number()
{
const auto locale = boost::locale::generator{}("en_US.UTF-8");

std::cout << "Testing char" << std::endl;
test_format_large_number_by_char<char>(locale);

std::cout << "Testing wchar_t" << std::endl;
test_format_large_number_by_char<wchar_t>(locale);

#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
std::cout << "Testing char16_t" << std::endl;
test_format_large_number_by_char<char16_t>(locale);
#endif
}
1 change: 1 addition & 0 deletions test/test_formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ void test_main(int argc, char** argv)
test_format_class<char32_t>();
#endif

test_format_large_number();
test_parse_multi_number();
}

Expand Down
1 change: 1 addition & 0 deletions test/test_posix_formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void test_main(int /*argc*/, char** /*argv*/)
TEST(v == "12345,45" || v == "12 345,45" || v == "12.345,45");
}
}
test_format_large_number();
test_parse_multi_number();
}

Expand Down
4 changes: 3 additions & 1 deletion test/test_std_formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ void test_main(int /*argc*/, char** /*argv*/)
}
// Std backend silently falls back to the C locale when the locale is not supported
// which breaks the test assumptions
if(has_std_locale("en_US.UTF-8"))
if(has_std_locale("en_US.UTF-8")) {
test_format_large_number();
test_parse_multi_number();
}
}

// boostinspect:noascii
1 change: 1 addition & 0 deletions test/test_winapi_formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void test_main(int /*argc*/, char** /*argv*/)
test_by_char<wchar_t>(l, name, name_lcid.second);
}
}
test_format_large_number();
test_parse_multi_number();
std::cout << "- Testing strftime" << std::endl;
test_date_time(gen("en_US.UTF-8"));
Expand Down

0 comments on commit f31ba3a

Please sign in to comment.