Fix #13611: Zero under decimal is not displayed (#13618)

* fix for bug #13611; handled too few sig figs fixed point case

* added unit test cases to cover modified fixed point code

* removed blank line to satistfy clang-format

* sorted includes in formatting tests for clang-format

* removed redundant static_cast

 - already a char literal and assigning to char[] so code
 is functionally equivalent
This commit is contained in:
csunday95 2020-12-22 01:45:45 -08:00 committed by GitHub
parent cc6b56f1f5
commit fa5437f920
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 273 additions and 0 deletions

View file

@ -341,6 +341,11 @@ namespace OpenRCT2
buffer[i++] = static_cast<char>('0' + (num % 10));
num /= 10;
}
// handle case where value has fewer sig figs than required decimal places
while (num == 0 && i < TDecimalPlace && i < sizeof(buffer))
{
buffer[i++] = '0';
}
auto decSep = GetDecimalSeparator();
AppendSeparator(buffer, i, decSep);

View file

@ -16,6 +16,8 @@
#include <openrct2/core/String.hpp>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/StringIds.h>
#include <sstream>
#include <string>
using namespace OpenRCT2;
@ -336,3 +338,269 @@ TEST_F(FormattingTests, using_legacy_buffer_args)
ASSERT_EQ(len, 23U);
ASSERT_STREQ("Queuing for Boat Hire 2", buffer);
}
TEST_F(FormattingTests, format_number_basic)
{
std::stringstream ss;
// test basic integral conversion
FormatArgument<int32_t>(ss, FormatToken::UInt16, 123);
ASSERT_STREQ("123", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_basic_int32)
{
std::stringstream ss;
// test that case fallthrough works
FormatArgument<int32_t>(ss, FormatToken::Int32, 123);
ASSERT_STREQ("123", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_negative)
{
std::stringstream ss;
// test negative conversion
FormatArgument<int32_t>(ss, FormatToken::Int32, -123);
ASSERT_STREQ("-123", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma16_basic)
{
std::stringstream ss;
// test separator formatter
// test base case separator formatter
FormatArgument<int32_t>(ss, FormatToken::Comma16, 123);
ASSERT_STREQ("123", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma16_negative)
{
std::stringstream ss;
// test separator formatter
// test base case separator formatter
FormatArgument<int32_t>(ss, FormatToken::Comma16, -123);
ASSERT_STREQ("-123", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma16_large)
{
std::stringstream ss;
// test larger value for separator formatter
FormatArgument<int32_t>(ss, FormatToken::Comma16, 123456789);
ASSERT_STREQ("123,456,789", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma16_large_negative)
{
std::stringstream ss;
// test larger value for separator formatter with negative
FormatArgument<int32_t>(ss, FormatToken::Comma16, -123456789);
ASSERT_STREQ("-123,456,789", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma16_uneven)
{
std::stringstream ss;
// test non-multiple of 3
FormatArgument<int32_t>(ss, FormatToken::Comma16, 12345678);
ASSERT_STREQ("12,345,678", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma16_uneven_negative)
{
std::stringstream ss;
// test non-multiple of 3 with negative
FormatArgument<int32_t>(ss, FormatToken::Comma16, -12345678);
ASSERT_STREQ("-12,345,678", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma16_zero)
{
std::stringstream ss;
// test zero
FormatArgument<int32_t>(ss, FormatToken::Comma16, 0);
ASSERT_STREQ("0", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_zero)
{
std::stringstream ss;
// zero case
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, 0);
ASSERT_STREQ("0.0", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_leading_zero)
{
std::stringstream ss;
// test leading zero
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, 5);
ASSERT_STREQ("0.5", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_leading_zero_negative)
{
std::stringstream ss;
// test leading zero with negative value
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, -5);
ASSERT_STREQ("-0.5", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_small_value)
{
std::stringstream ss;
// test small value
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, 75);
ASSERT_STREQ("7.5", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_small_value_negative)
{
std::stringstream ss;
// test small value with negative
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, -75);
ASSERT_STREQ("-7.5", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_trailing_zeros)
{
std::stringstream ss;
// test value with trailing zero, no commas
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, 1000);
ASSERT_STREQ("100.0", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_trailing_zeros_negative)
{
std::stringstream ss;
// test value with trailing zero, no commas
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, -1000);
ASSERT_STREQ("-100.0", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_large_trailing_zeros)
{
std::stringstream ss;
// test value with commas and trailing zeros
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, 10000000);
ASSERT_STREQ("1,000,000.0", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_large_trailing_zeros_negative)
{
std::stringstream ss;
// test value with commas and trailing zeros
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, -10000000);
ASSERT_STREQ("-1,000,000.0", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_large_value)
{
std::stringstream ss;
// test large value
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, 123456789);
ASSERT_STREQ("12,345,678.9", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma1dp16_large_value_negative)
{
std::stringstream ss;
// test large value
FormatArgument<int32_t>(ss, FormatToken::Comma1dp16, -123456789);
ASSERT_STREQ("-12,345,678.9", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_zero)
{
std::stringstream ss;
// zero case
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, 0);
ASSERT_STREQ("0.00", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_less_sig_figs)
{
std::stringstream ss;
// test leading zero
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, 5);
ASSERT_STREQ("0.05", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_less_sig_figs_negative)
{
std::stringstream ss;
// test leading zero
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, -5);
ASSERT_STREQ("-0.05", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_leading_zero)
{
std::stringstream ss;
// test small value
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, 75);
ASSERT_STREQ("0.75", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_leading_zero_negative)
{
std::stringstream ss;
// test small value
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, -75);
ASSERT_STREQ("-0.75", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_trailing_zeros)
{
std::stringstream ss;
// test value with trailing zero, no commas
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, 1000);
ASSERT_STREQ("10.00", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_trailing_zeros_negative)
{
std::stringstream ss;
// test value with trailing zero, no commas
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, -1000);
ASSERT_STREQ("-10.00", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_large_trailing_zeros)
{
std::stringstream ss;
// test value with commas and trailing zeros
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, 10000000);
ASSERT_STREQ("100,000.00", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_large_trailing_zeros_negative)
{
std::stringstream ss;
// test value with commas and trailing zeros
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, -10000000);
ASSERT_STREQ("-100,000.00", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_large_value)
{
std::stringstream ss;
// test large value
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, 123456789);
ASSERT_STREQ("1,234,567.89", ss.str().c_str());
}
TEST_F(FormattingTests, format_number_comma2dp32_large_value_negative)
{
std::stringstream ss;
// test large value
FormatArgument<int32_t>(ss, FormatToken::Comma2dp32, -123456789);
ASSERT_STREQ("-1,234,567.89", ss.str().c_str());
// extra note:
// for some reason the FormatArgument function contains constexpr
// checks for std::is_floating_point<T>, even though a template
// specialization for which that would ever be the case is never
// declared. As such the following line won't be able to find
// the necessary symbol to link with.
// FormatArgument<double>(ss, FormatToken::Comma1dp16, 12.372);
}