test(Format): test shortest representation format specifiers

This commit is contained in:
nyashbox 2024-12-02 02:10:41 +02:00
parent 42da9b0f1e
commit b92c6b22b1
No known key found for this signature in database
GPG Key ID: B40E74A291D1DFA4
2 changed files with 76 additions and 0 deletions

View File

@ -325,6 +325,80 @@ void FormatTest::testAnyInt()
assertTrue (s == "42");
}
void FormatTest::testShortestRepr()
{
std::string str;
//
// uppercase tests
//
{
str = format("%G", 123.456);
assertEquals("123.456", str);
str = format("%G", 12345.6789);
assertEquals("12345.7", str);
str = format("%G", 0.000123);
assertEquals("0.000123", str);
str = format("%G", 1234567890.123);
assertEquals("1.23457E+09", str);
str = format("%G", 0.0);
assertEquals("0", str);
str = format("%G", -9876.54321);
assertEquals("-9876.54", str);
str = format("%.2G", 123.456);
assertEquals("1.2E+02", str);
str = format("%G", 1.0);
assertEquals("1", str);
str = format("%G", 0.00000000123);
assertEquals("1.23E-09", str);
str = format("%G", 0.00000000000123);
assertEquals("1.23E-12", str);
}
//
// lowercase tests
//
{
str = format("%g", 123.456);
assertEquals("123.456", str);
str = format("%g", 12345.6789);
assertEquals("12345.7", str);
str = format("%g", 0.000123);
assertEquals("0.000123", str);
str = format("%g", 1234567890.123);
assertEquals("1.23457e+09", str);
str = format("%g", 0.0);
assertEquals("0", str);
str = format("%g", -9876.54321);
assertEquals("-9876.54", str);
str = format("%.2g", 123.456);
assertEquals("1.2e+02", str);
str = format("%g", 1.0);
assertEquals("1", str);
str = format("%g", 0.00000000123);
assertEquals("1.23e-09", str);
str = format("%g", 0.00000000000123);
assertEquals("1.23e-12", str);
}
}
void FormatTest::testFloatFix()
{
@ -527,6 +601,7 @@ CppUnit::Test* FormatTest::suite()
CppUnit_addTest(pSuite, FormatTest, testMultiple);
CppUnit_addTest(pSuite, FormatTest, testIndex);
CppUnit_addTest(pSuite, FormatTest, testAny);
CppUnit_addTest(pSuite, FormatTest, testShortestRepr);
return pSuite;
}

View File

@ -29,6 +29,7 @@ public:
void testFloatSci();
void testString();
void testStringView();
void testShortestRepr();
void testMultiple();
void testIndex();
void testAny();