From 9f3e2a7226090071513f9e79db0168d4a1a44a49 Mon Sep 17 00:00:00 2001 From: Eyal Rozenberg Date: Thu, 15 Jul 2021 00:05:58 +0300 Subject: [PATCH] Exposing bugs #75, #99, #109, #110 via the test suite. --- printf.c | 6 +++--- test/test_suite.cpp | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/printf.c b/printf.c index 17856c4e..da81d526 100644 --- a/printf.c +++ b/printf.c @@ -226,14 +226,14 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen // internal itoa format -static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags) +static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int precision, unsigned int width, unsigned int flags) { // pad leading zeros if (!(flags & FLAGS_LEFT)) { if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { width--; } - while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) { + while ((len < precision) && (len < PRINTF_NTOA_BUFFER_SIZE)) { buf[len++] = '0'; } while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) { @@ -243,7 +243,7 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma // handle hash if (flags & (FLAGS_HASH | FLAGS_POINTER)) { - if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) { + if (!(flags & FLAGS_PRECISION) && len && ((len == precision) || (len == width))) { len--; if (len && (base == 16U)) { len--; diff --git a/test/test_suite.cpp b/test/test_suite.cpp index ca4d02bc..c04ff67b 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -485,10 +485,37 @@ TEST_CASE("- flag - non-standard format", "[]" ) { TEST_CASE("# flag", "[]" ) { char buffer[100]; - test::sprintf(buffer, "%#.0x", 0); - REQUIRE(!strcmp(buffer, "")); - test::sprintf(buffer, "%#.1x", 0); + test::sprintf(buffer, "%#o", 0); + REQUIRE(!strcmp(buffer, "0")); + test::sprintf(buffer, "%#0o", 0); + REQUIRE(!strcmp(buffer, "0")); + test::sprintf(buffer, "%#.0o", 0); REQUIRE(!strcmp(buffer, "0")); + test::sprintf(buffer, "%#.1o", 0); + REQUIRE(!strcmp(buffer, "0")); + test::sprintf(buffer, "%#4o", 0); + REQUIRE(!strcmp(buffer, " 0")); + test::sprintf(buffer, "%#.4o", 0); + REQUIRE(!strcmp(buffer, "0000")); + + test::sprintf(buffer, "%#o", 1); + REQUIRE(!strcmp(buffer, "01")); + test::sprintf(buffer, "%#0o", 1); + REQUIRE(!strcmp(buffer, "01")); + test::sprintf(buffer, "%#.0o", 1); + REQUIRE(!strcmp(buffer, "01")); + test::sprintf(buffer, "%#.1o", 1); + REQUIRE(!strcmp(buffer, "01")); + test::sprintf(buffer, "%#4o", 1); + REQUIRE(!strcmp(buffer, " 01")); + test::sprintf(buffer, "%#.4o", 1); + REQUIRE(!strcmp(buffer, "0001")); + + test::sprintf(buffer, "%#04x", 1001); + REQUIRE(!strcmp(buffer, "0x1001")); + test::sprintf(buffer, "%#04o", 01001); + REQUIRE(!strcmp(buffer, "01001")); + test::sprintf(buffer, "%#.0llx", (long long)0); REQUIRE(!strcmp(buffer, "")); test::sprintf(buffer, "%#.8x", 0x614e); @@ -1341,6 +1368,9 @@ TEST_CASE("float", "[]" ) { REQUIRE(!strcmp(buffer, "a0.5 end")); #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL + test::sprintf(buffer, "%.4g", 1.0); + REQUIRE(!strcmp(buffer, "1")); + test::sprintf(buffer, "%G", 12345.678); REQUIRE(!strcmp(buffer, "12345.7"));