diff options
author | dzwdz | 2022-08-03 00:23:50 +0200 |
---|---|---|
committer | dzwdz | 2022-08-03 00:23:50 +0200 |
commit | a2a1e4bbfcda8c1314b592c8c940e96e95cb68d4 (patch) | |
tree | 200f63d71eb07b9f9c53ab4f14b58b7b15b2f4bf /src/user/app/tests | |
parent | b4447c1c7bc8639774df6bd74428977908bff61e (diff) |
shared: clean up printf, %u support (amongst other things)
Diffstat (limited to 'src/user/app/tests')
-rw-r--r-- | src/user/app/tests/printf.c | 32 | ||||
-rw-r--r-- | src/user/app/tests/tests.c | 11 | ||||
-rw-r--r-- | src/user/app/tests/tests.h | 4 |
3 files changed, 36 insertions, 11 deletions
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 <stdio.h> +#include <string.h> + +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")); +} diff --git a/src/user/app/tests/tests.c b/src/user/app/tests/tests.c index 4390747..f8abc49 100644 --- a/src/user/app/tests/tests.c +++ b/src/user/app/tests/tests.c @@ -237,15 +237,6 @@ static void test_execbuf(void) { test_fail(); } -static void test_snprintf(void) { - char buf[16]; - memset(buf, '?', 16); - 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)); -} - static void test_misc(void) { assert(_syscall(~0, 0, 0, 0, 0, 0) < 0); /* try making an invalid syscall */ } @@ -263,7 +254,7 @@ int main(void) { run_forked(test_semaphore); run_forked(test_efault); run_forked(test_execbuf); - run_forked(test_snprintf); + run_forked(test_printf); run_forked(test_misc); return 1; } diff --git a/src/user/app/tests/tests.h b/src/user/app/tests/tests.h index 39294eb..5f36fe0 100644 --- a/src/user/app/tests/tests.h +++ b/src/user/app/tests/tests.h @@ -1,18 +1,20 @@ #pragma once #include <camellia/syscalls.h> #include <stdio.h> +#include <unistd.h> void stress_all(void); void test_all(void); void test_pipe(void); +void test_printf(void); void test_semaphore(void); #ifdef TEST_MACROS #define argify(str) str, sizeof(str) - 1 #define test_fail() do { \ - printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \ + printf("\033[31m" "TEST FAILED: %s():%u\n" "\033[0m", __func__, __LINE__); \ exit(0); \ } while (0) #define assert(cond) if (!(cond)) test_fail(); |