diff options
author | Tom Hughes <tomhughes@chromium.org> | 2022-08-19 14:42:56 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-08-22 19:44:41 +0000 |
commit | ae82a4a901a7fc128ff60ef1417f3b0585f7c07c (patch) | |
tree | a6c4ae54359a69b5e9e136163fa1642a4fd920dc /test | |
parent | a8b737d88617758499ae5abd3637e07cc52f33d3 (diff) | |
download | chrome-ec-ae82a4a901a7fc128ff60ef1417f3b0585f7c07c.tar.gz |
test/printf: Fix printf test when using toolchain C standard lib
Account for bugs in the EC builtin C standard library printf
implementation with conditionals until the implementation is fixed.
BRANCH=none
BUG=b:234181908, b:239233116
TEST=make USE_BUILTIN_STDLIB=0 run-printf
TEST=make run-printf
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Change-Id: I1b9a50a41342dfb81e9e7057fad69056ec7acfc8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3843214
Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
Diffstat (limited to 'test')
-rw-r--r-- | test/printf.c | 59 |
1 files changed, 51 insertions, 8 deletions
diff --git a/test/printf.c b/test/printf.c index 080beed0da..2a571be54f 100644 --- a/test/printf.c +++ b/test/printf.c @@ -21,10 +21,12 @@ */ #include "builtin/stdio.h" #define VSNPRINTF crec_vsnprintf +#define SNPRINTF crec_snprintf static const bool use_builtin_stdlib = true; #else #include <stdio.h> #define VSNPRINTF vsnprintf +#define SNPRINTF snprintf static const bool use_builtin_stdlib = false; #endif @@ -147,14 +149,55 @@ test_static int test_vsnprintf_int(void) T(expect_success(" +123", "%+5d", 123)); T(expect_success("00123", "%05d", 123)); T(expect_success("00123", "%005d", 123)); - /* Fixed point. */ - T(expect_success("0.00123", "%.5d", 123)); - T(expect_success("12.3", "%2.1d", 123)); - /* Precision or width larger than buffer should fail. */ - T(expect(EC_ERROR_OVERFLOW, " 1", false, 4, "%5d", 123)); - T(expect(EC_ERROR_OVERFLOW, " ", false, 4, "%10d", 123)); - T(expect(EC_ERROR_OVERFLOW, "123", false, 4, "%-10d", 123)); - T(expect(EC_ERROR_OVERFLOW, "0.0", false, 4, "%.10d", 123)); + + if (use_builtin_stdlib) { + /* + * TODO(b/239233116): These are incorrect and should be fixed. + */ + /* Fixed point. */ + T(expect_success("0.00123", "%.5d", 123)); + T(expect_success("12.3", "%2.1d", 123)); + /* Precision or width larger than buffer should fail. */ + T(expect(EC_ERROR_OVERFLOW, " 1", false, 4, "%5d", 123)); + T(expect(EC_ERROR_OVERFLOW, " ", false, 4, "%10d", 123)); + T(expect(EC_ERROR_OVERFLOW, "123", false, 4, "%-10d", 123)); + T(expect(EC_ERROR_OVERFLOW, "0.0", false, 4, "%.10d", 123)); + } else { + int ret; + + T(expect_success("00123", "%.5d", 123)); + T(expect_success("123", "%2.1d", 123)); + + /* + * From the man page: The functions snprintf() and vsnprintf() + * do not write more than size bytes (including the + * terminating null byte ('\0')). If the output was truncated + * due to this limit, then the return value is the number of + * characters (excluding the terminating null byte) which + * would have been written to the final string if enough + * space had been available. Thus, a return value of size or + * more means that the output was truncated. + */ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-truncation" + ret = SNPRINTF(output, 4, "%5d", 123); + TEST_ASSERT_ARRAY_EQ(output, " 1", 4); + TEST_EQ(ret, 5, "%d"); + + ret = SNPRINTF(output, 4, "%10d", 123); + TEST_ASSERT_ARRAY_EQ(output, " ", 4); + TEST_EQ(ret, 10, "%d"); + + ret = SNPRINTF(output, 4, "%-10d", 123); + TEST_ASSERT_ARRAY_EQ(output, "123", 4); + TEST_EQ(ret, 10, "%d"); + + ret = SNPRINTF(output, 4, "%.10d", 123); + TEST_ASSERT_ARRAY_EQ(output, "000", 4); + TEST_EQ(ret, 10, "%d"); +#pragma GCC diagnostic pop + } + if (use_builtin_stdlib) { /* * TODO(b/239233116): These are incorrect and should be fixed. |