From a2a1e4bbfcda8c1314b592c8c940e96e95cb68d4 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 3 Aug 2022 00:23:50 +0200 Subject: shared: clean up printf, %u support (amongst other things) --- src/user/app/tests/printf.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/user/app/tests/printf.c (limited to 'src/user/app/tests/printf.c') diff --git a/src/user/app/tests/printf.c b/src/user/app/tests/printf.c new file mode 100644 index 0000000..6c6c609 --- /dev/null +++ b/src/user/app/tests/printf.c @@ -0,0 +1,32 @@ +#define TEST_MACROS +#include "tests.h" +#include +#include + +void test_printf(void) { + char buf[64]; + memset(buf, '?', 64); + + /* test proper overflow handling in snprintf */ + assert(13 == snprintf(buf, 15, "That's 0x%x", 0x1337)); + assert(!memcmp(buf, "That's 0x1337\0??", 16)); + assert(17 == snprintf(buf, 15, "%05x %05x %05x", 0, 0, 0)); + assert(!memcmp(buf, "00000 00000 00\0?", 16)); + + /* all the other stuff */ + snprintf(buf, sizeof buf, "%010x", 0x1BABE); + assert(!strcmp(buf, "000001babe")); + snprintf(buf, sizeof buf, "%10x", 0x1BABE); + assert(!strcmp(buf, " 1babe")); + snprintf(buf, sizeof buf, "%10s", "hello"); + assert(!strcmp(buf, " hello")); + + snprintf(buf, sizeof buf, "%s%%%s", "ab", "cd"); + assert(!strcmp(buf, "ab%cd")); + + snprintf(buf, sizeof buf, "%05u %05u", 1234, 56789); + assert(!strcmp(buf, "01234 56789")); + + snprintf(buf, sizeof buf, "%u %x", 0, 0); + assert(!strcmp(buf, "0 0")); +} -- cgit v1.2.3