From 9cf3079b95ad7e995880ec5553372191c73efb0e Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 15 Sep 2022 22:36:43 +0200 Subject: shared/printf: properly implement number precision --- src/user/app/tests/shared/printf.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/user') diff --git a/src/user/app/tests/shared/printf.c b/src/user/app/tests/shared/printf.c index 4e5ffb8..37b9679 100644 --- a/src/user/app/tests/shared/printf.c +++ b/src/user/app/tests/shared/printf.c @@ -23,16 +23,26 @@ static void test_printf(void) { snprintf(buf, sizeof buf, "%s%%%s", "ab", "cd"); test(!strcmp(buf, "ab%cd")); - snprintf(buf, sizeof buf, "%05u %05u", 1234, 56789); - test(!strcmp(buf, "01234 56789")); - - snprintf(buf, sizeof buf, "%5d %5d", 123, 4567); - test(!strcmp(buf, " 123 4567")); - snprintf(buf, sizeof buf, "%5d %5d", -123, -4567); - test(!strcmp(buf, " -123 -4567")); - - snprintf(buf, sizeof buf, "%u %d %x", 0, 0, 0); - test(!strcmp(buf, "0 0 0")); + snprintf(buf, sizeof buf, "%05u,%05u", 1234, 56789); + test(!strcmp(buf, "01234,56789")); + + snprintf(buf, sizeof buf, "%5d,%5d", 123, 4567); + test(!strcmp(buf, " 123, 4567")); + snprintf(buf, sizeof buf, "%5d,%5d", -123, -4567); + test(!strcmp(buf, " -123,-4567")); + + snprintf(buf, sizeof buf, "%u,%d,%x", 0, 0, 0); + test(!strcmp(buf, "0,0,0")); + + /* precision */ + snprintf(buf, sizeof buf, "%5.2u,%5.2d,%5.2x", 0, 0, 0); + test(!strcmp(buf, " 00, 00, 00")); + snprintf(buf, sizeof buf, "%5.2u,%5.2d,%5.2x", 10, -10, 0x10); + test(!strcmp(buf, " 10, -10, 10")); + snprintf(buf, sizeof buf, "%5.3d", -1); + test(!strcmp(buf, " -001")); + snprintf(buf, sizeof buf, "%.5d", 123); + test(!strcmp(buf, "00123")); } void r_s_printf(void) { -- cgit v1.2.3