Handle int64 edge cases in Format

This commit is contained in:
Ted John 2020-11-21 16:46:00 +00:00
parent dfcd8a4bbc
commit 9173ec2111
2 changed files with 34 additions and 5 deletions

View file

@ -276,12 +276,27 @@ namespace OpenRCT2
char buffer[32];
size_t i = 0;
size_t num;
if (value < 0)
uint64_t num;
if constexpr (std::is_signed<T>::value)
{
// TODO handle edge case: std::numeric_limits<int64_t>::min();
num = -value;
ss << '-';
if (value < 0)
{
ss << '-';
if (value == std::numeric_limits<int64_t>::min())
{
// Edge case: int64_t can not store this number so manually assign num to (int64_t::max + 1)
num = static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + 1;
}
else
{
// Cast negative number to int64_t and then reverse sign
num = -static_cast<int64_t>(value);
}
}
else
{
num = value;
}
}
else
{
@ -571,6 +586,8 @@ namespace OpenRCT2
template void FormatArgument(std::stringstream&, FormatToken, uint16_t);
template void FormatArgument(std::stringstream&, FormatToken, int16_t);
template void FormatArgument(std::stringstream&, FormatToken, int32_t);
template void FormatArgument(std::stringstream&, FormatToken, int64_t);
template void FormatArgument(std::stringstream&, FormatToken, uint64_t);
template void FormatArgument(std::stringstream&, FormatToken, const char*);
bool IsRealNameStringId(rct_string_id id)

View file

@ -106,6 +106,18 @@ TEST_F(FormattingTests, comma_0)
ASSERT_EQ("Guests: 0", actual);
}
TEST_F(FormattingTests, comma_large_negative)
{
auto actual = FormatString("{COMMA16}", std::numeric_limits<int64_t>::min());
ASSERT_EQ("-9,223,372,036,854,775,808", actual);
}
TEST_F(FormattingTests, comma_large)
{
auto actual = FormatString("{COMMA16}", std::numeric_limits<uint64_t>::max());
ASSERT_EQ("18,446,744,073,709,551,615", actual);
}
TEST_F(FormattingTests, currency)
{
gConfigGeneral.currency_format = CurrencyType::Pounds;