From ce6de42aad0219e7a6b10eba925fd9dfa668f2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Thu, 24 Oct 2024 21:09:18 -0700 Subject: [PATCH] Added vsnprintf test against standard compliant implementation. --- tests/vsnprintf_test.cpp | 203 +++++++++++++++++++++++---------------- 1 file changed, 121 insertions(+), 82 deletions(-) diff --git a/tests/vsnprintf_test.cpp b/tests/vsnprintf_test.cpp index f93d041..9f80e38 100644 --- a/tests/vsnprintf_test.cpp +++ b/tests/vsnprintf_test.cpp @@ -47,38 +47,77 @@ TEST_CASE("Truncated output buffer.", "[string][printf]") REQUIRE(0 == bx::strCmp(buffer7, "SevenE") ); } -static bool test(const char* _expected, const char* _format, ...) +template +static bool test(const char* _expected, const char* _format, va_list _argList) { int32_t max = (int32_t)bx::strLen(_expected) + 1; - char* temp = (char*)alloca(max); + char* bxTemp = (char*)alloca(max); va_list argList; - va_start(argList, _format); - int32_t len = bx::vsnprintf(temp, max, _format, argList); - va_end(argList); + va_copy(argList, _argList); + const int32_t bxLen = bx::vsnprintf(bxTemp, max, _format, argList); bool result = true - && len == max-1 - && 0 == bx::strCmp(_expected, temp) + && bxLen == max-1 + && 0 == bx::strCmp(_expected, bxTemp) ; + char* crtTemp = NULL; + int32_t crtLen = 0; + + if (!result + || StdCompliantT) + { + BX_ASSERT(bx::strFind(_format, "%S").isEmpty() + , "String format test is using '%%S' bx::StringView specific format specifier which is not standard compliant. " + "Use `testNotStdCompliant` string testing method." + ); + + crtTemp = (char*)alloca(max); + + va_copy(argList, _argList); + crtLen = ::vsnprintf(crtTemp, max, _format, argList); + + result &= true + && crtLen == bxLen + && 0 == bx::strCmp(bx::StringView(bxTemp, bxLen), bx::StringView(crtTemp, crtLen) ) + ; + } + if (!result) { printf("---\n"); printf("printf format '%s'\n", _format); - printf(" result (%4d) '%s'\n", len, temp); + printf(" result (%4d) '%s'\n", bxLen, bxTemp); printf(" expected (%4d) '%s'\n", max-1, _expected); - - va_start(argList, _format); - len = ::vsnprintf(temp, max, _format, argList); - va_end(argList); - - printf("CRT vsnprintf (%d) '%s'\n", len, temp); + printf("CRT vsnprintf (%4d) '%s'\n", crtLen, crtTemp); } return result; } +// Test against CRT's vsnprintf implementation. +static bool test(const char* _expected, const char* _format, ...) +{ + va_list argList; + va_start(argList, _format); + const bool result = test(_expected, _format, argList); + va_end(argList); + + return result; +} + +// Skip test against CRT's vsnprintf implementation. +static bool testNotStdCompliant(const char* _expected, const char* _format, ...) +{ + va_list argList; + va_start(argList, _format); + const bool result = test(_expected, _format, argList); + va_end(argList); + + return result; +} + TEST_CASE("Format %f", "[string][printf]") { REQUIRE(test("1.337", "%0.3f", 1.337) ); @@ -103,63 +142,63 @@ TEST_CASE("Format %f", "[string][printf]") REQUIRE(test("0.0039", "%.4f", 0.00390625) ); REQUIRE(test("0.003906", "%f", 0.00390625) ); - REQUIRE(test("-1.234567e-9", "%f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.234567e-9", "%f", -1.234567e-9) ); - REQUIRE(test("-1e-9", "%.0f", -1.234567e-9) ); - REQUIRE(test("-1.2e-9", "%.1f", -1.234567e-9) ); - REQUIRE(test("-1.23e-9", "%.2f", -1.234567e-9) ); - REQUIRE(test("-1.234e-9", "%.3f", -1.234567e-9) ); - REQUIRE(test("-1.2345e-9", "%.4f", -1.234567e-9) ); - REQUIRE(test("-1.23456e-9", "%.5f", -1.234567e-9) ); - REQUIRE(test("-1.234567e-9", "%.6f", -1.234567e-9) ); - REQUIRE(test("-1.2345670e-9", "%.7f", -1.234567e-9) ); - REQUIRE(test("-1.23456700e-9", "%.8f", -1.234567e-9) ); - REQUIRE(test("-1.234567000e-9", "%.9f", -1.234567e-9) ); - REQUIRE(test("-1.2345670000e-9", "%.10f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1e-9", "%.0f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.2e-9", "%.1f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.23e-9", "%.2f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.234e-9", "%.3f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.2345e-9", "%.4f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.23456e-9", "%.5f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.234567e-9", "%.6f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.2345670e-9", "%.7f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.23456700e-9", "%.8f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.234567000e-9", "%.9f", -1.234567e-9) ); + REQUIRE(testNotStdCompliant("-1.2345670000e-9", "%.10f", -1.234567e-9) ); - REQUIRE(test("3.141592", "%f", 3.1415926535897932) ); - REQUIRE(test("3.141592", "%F", 3.1415926535897932) ); - REQUIRE(test("3", "%.0f", 3.1415926535897932) ); - REQUIRE(test("3.1", "%.1f", 3.1415926535897932) ); - REQUIRE(test("3.14", "%.2f", 3.1415926535897932) ); - REQUIRE(test("3.141", "%.3f", 3.1415926535897932) ); - REQUIRE(test("3.1415", "%.4f", 3.1415926535897932) ); - REQUIRE(test("3.14159", "%.5f", 3.1415926535897932) ); - REQUIRE(test("3.141592", "%.6f", 3.1415926535897932) ); - REQUIRE(test("3.1415926", "%.7f", 3.1415926535897932) ); - REQUIRE(test("3.14159265", "%.8f", 3.1415926535897932) ); - REQUIRE(test("3.141592653", "%.9f", 3.1415926535897932) ); - REQUIRE(test("3.1415926535", "%.10f", 3.1415926535897932) ); - REQUIRE(test("3.14159265358", "%.11f", 3.1415926535897932) ); - REQUIRE(test("3.141592653589", "%.12f", 3.1415926535897932) ); - REQUIRE(test("3.1415926535897", "%.13f", 3.1415926535897932) ); - REQUIRE(test("3.14159265358979", "%.14f", 3.1415926535897932) ); - REQUIRE(test("3.141592653589793", "%.15f", 3.1415926535897932) ); - REQUIRE(test("3.1415926535897930", "%.16f", 3.1415926535897932) ); - REQUIRE(test("3.1415926535897930", "%.16F", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.141592", "%f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.141592", "%F", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3", "%.0f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.1", "%.1f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.14", "%.2f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.141", "%.3f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.1415", "%.4f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.14159", "%.5f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.141592", "%.6f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.1415926", "%.7f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.14159265", "%.8f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.141592653", "%.9f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.1415926535", "%.10f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.14159265358", "%.11f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.141592653589", "%.12f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.1415926535897", "%.13f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.14159265358979", "%.14f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.141592653589793", "%.15f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.1415926535897930", "%.16f", 3.1415926535897932) ); + REQUIRE(testNotStdCompliant("3.1415926535897930", "%.16F", 3.1415926535897932) ); - REQUIRE(test("-3.141592e-9", "%f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.141592E-9", "%F", -3.1415926535897932e-9) ); - REQUIRE(test("-3e-9", "%.0f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.1e-9", "%.1f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.14e-9", "%.2f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.141e-9", "%.3f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.1415e-9", "%.4f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.14159e-9", "%.5f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.141592e-9", "%.6f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.1415926e-9", "%.7f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.14159265e-9", "%.8f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.141592653e-9", "%.9f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.1415926535e-9", "%.10f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.14159265358e-9", "%.11f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.141592653589e-9", "%.12f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.1415926535897e-9", "%.13f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.14159265358979e-9", "%.14f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.141592653589793e-9", "%.15f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.1415926535897930e-9", "%.16f", -3.1415926535897932e-9) ); - REQUIRE(test("-3.1415926535897930E-9", "%.16F", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.141592e-9", "%f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.141592E-9", "%F", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3e-9", "%.0f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.1e-9", "%.1f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.14e-9", "%.2f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.141e-9", "%.3f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.1415e-9", "%.4f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.14159e-9", "%.5f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.141592e-9", "%.6f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.1415926e-9", "%.7f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.14159265e-9", "%.8f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.141592653e-9", "%.9f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.1415926535e-9", "%.10f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.14159265358e-9", "%.11f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.141592653589e-9", "%.12f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.1415926535897e-9", "%.13f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.14159265358979e-9", "%.14f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.141592653589793e-9", "%.15f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.1415926535897930e-9", "%.16f", -3.1415926535897932e-9) ); + REQUIRE(testNotStdCompliant("-3.1415926535897930E-9", "%.16F", -3.1415926535897932e-9) ); - REQUIRE(test("1e-12", "%f", 1e-12)); + REQUIRE(testNotStdCompliant("1e-12", "%f", 1e-12)); REQUIRE(test("0.00390625", "%.8f", 0.00390625) ); REQUIRE(test("-0.00390625", "%.8f", -0.00390625) ); @@ -201,14 +240,14 @@ TEST_CASE("Format %d, %i, %o, %u, %x", "[string][printf]") REQUIRE(test("000000000000edcb5433", "%020x", -0x1234abcd) ); REQUIRE(test("000000000000EDCB5433", "%020X", -0x1234abcd) ); - REQUIRE(test("0xf", "0x%01x", -1) ); - REQUIRE(test("0xff", "0x%02x", -1) ); - REQUIRE(test("0xfff", "0x%03x", -1) ); - REQUIRE(test("0xffff", "0x%04x", -1) ); - REQUIRE(test("0xfffff", "0x%05x", -1) ); - REQUIRE(test("0xffffff", "0x%06x", -1) ); - REQUIRE(test("0xfffffff", "0x%07x", -1) ); - REQUIRE(test("0xffffffff", "0x%08x", -1) ); + REQUIRE(testNotStdCompliant("0xf", "0x%01x", -1) ); + REQUIRE(testNotStdCompliant("0xff", "0x%02x", -1) ); + REQUIRE(testNotStdCompliant("0xfff", "0x%03x", -1) ); + REQUIRE(testNotStdCompliant("0xffff", "0x%04x", -1) ); + REQUIRE(testNotStdCompliant("0xfffff", "0x%05x", -1) ); + REQUIRE(testNotStdCompliant("0xffffff", "0x%06x", -1) ); + REQUIRE(testNotStdCompliant("0xfffffff", "0x%07x", -1) ); + REQUIRE(testNotStdCompliant("0xffffffff", "0x%08x", -1) ); REQUIRE(test(" -1", "% 4i", -1) ); REQUIRE(test(" -1", "% 4i", -1) ); @@ -216,7 +255,7 @@ TEST_CASE("Format %d, %i, %o, %u, %x", "[string][printf]") REQUIRE(test(" 1", "% 4i", 1) ); REQUIRE(test(" 1", "% 4o", 1) ); REQUIRE(test(" +1", "%+4i", 1) ); - REQUIRE(test(" +1", "%+4o", 1) ); + REQUIRE(testNotStdCompliant(" +1", "%+4o", 1) ); REQUIRE(test(" +0", "%+4i", 0) ); REQUIRE(test(" -1", "%+4i", -1) ); REQUIRE(test("0001", "%04i", 1) ); @@ -308,11 +347,11 @@ TEST_CASE("Format %c, %s, %S", "[string][printf]") REQUIRE(test("hello ", "%-20s", "hello") ); REQUIRE(test("hello, world!", "%s, %s!", "hello", "world") ); - REQUIRE(test("h", "%1s", "hello") ); - REQUIRE(test("he", "%2s", "hello") ); - REQUIRE(test("hel", "%3s", "hello") ); - REQUIRE(test("hell", "%4s", "hello") ); - REQUIRE(test("hello", "%5s", "hello") ); + REQUIRE(testNotStdCompliant("h", "%1s", "hello") ); + REQUIRE(testNotStdCompliant("he", "%2s", "hello") ); + REQUIRE(testNotStdCompliant("hel", "%3s", "hello") ); + REQUIRE(testNotStdCompliant("hell", "%4s", "hello") ); + REQUIRE(testNotStdCompliant("hello", "%5s", "hello") ); bx::StringView str("0hello1world2"); bx::StringView hello(str, 1, 5); @@ -322,7 +361,7 @@ TEST_CASE("Format %c, %s, %S", "[string][printf]") , world.getLength(), world.getPtr() ) ); - REQUIRE(test("hello, world!", "%S, %S!" + REQUIRE(testNotStdCompliant("hello, world!", "%S, %S!" , &hello , &world ) );