From 749a150e37fbfdaf33a8d6738e95306e6d95e8b5 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 4 Aug 2022 23:23:50 +0200 Subject: move the kernel util tests to userland --- src/user/app/tests/libc/string.c | 28 ++++++++++++++++++++++ src/user/app/tests/printf.c | 35 --------------------------- src/user/app/tests/shared/printf.c | 35 +++++++++++++++++++++++++++ src/user/app/tests/shared/ringbuf.c | 47 +++++++++++++++++++++++++++++++++++++ src/user/app/tests/tests.c | 3 ++- src/user/app/tests/tests.h | 3 ++- 6 files changed, 114 insertions(+), 37 deletions(-) delete mode 100644 src/user/app/tests/printf.c create mode 100644 src/user/app/tests/shared/printf.c create mode 100644 src/user/app/tests/shared/ringbuf.c (limited to 'src/user/app') diff --git a/src/user/app/tests/libc/string.c b/src/user/app/tests/libc/string.c index 8f889dc..82aff98 100644 --- a/src/user/app/tests/libc/string.c +++ b/src/user/app/tests/libc/string.c @@ -1,6 +1,32 @@ #include "../tests.h" #include +static void test_memcmp(void) { + test(0 == memcmp("some", "thing", 0)); + test(0 != memcmp("some", "thing", 1)); + test(0 != memcmp("some", "thing", 4)); + + test(0 == memcmp("test", "tennis", 0)); + test(0 == memcmp("test", "tennis", 1)); + test(0 == memcmp("test", "tennis", 2)); + test(0 != memcmp("test", "tennis", 3)); + test(0 != memcmp("test", "tennis", 4)); + test(0 != memcmp("test", "tennis", 5)); + + test(0 > memcmp("foo", "moo", 4)); + test(0 < memcmp("moo", "foo", 4)); + test(0 > memcmp("555", "654", 3)); + test(0 < memcmp("654", "555", 3)); +} + +static void test_strcmp(void) { + test(0 == strcmp("string", "string")); + test(0 > strcmp("str", "string")); + test(0 < strcmp("string", "str")); + + test(0 != strcmp("stress", "string")); +} + static void test_strtol(void) { char *end; test(1234 == strtol("1234", NULL, 10)); @@ -21,5 +47,7 @@ static void test_strtol(void) { } void r_libc_string(void) { + run_test(test_memcmp); + run_test(test_strcmp); run_test(test_strtol); } diff --git a/src/user/app/tests/printf.c b/src/user/app/tests/printf.c deleted file mode 100644 index 4dc45bb..0000000 --- a/src/user/app/tests/printf.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "tests.h" -#include -#include - -static void test_printf(void) { - char buf[64]; - memset(buf, '?', 64); - - /* test proper overflow handling in snprintf */ - test(13 == snprintf(buf, 15, "That's 0x%x", 0x1337)); - test(!memcmp(buf, "That's 0x1337\0??", 16)); - test(17 == snprintf(buf, 15, "%05x %05x %05x", 0, 0, 0)); - test(!memcmp(buf, "00000 00000 00\0?", 16)); - - /* all the other stuff */ - snprintf(buf, sizeof buf, "%010x", 0x1BABE); - test(!strcmp(buf, "000001babe")); - snprintf(buf, sizeof buf, "%10x", 0x1BABE); - test(!strcmp(buf, " 1babe")); - snprintf(buf, sizeof buf, "%10s", "hello"); - test(!strcmp(buf, " hello")); - - 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, "%u %x", 0, 0); - test(!strcmp(buf, "0 0")); -} - -void r_printf(void) { - run_test(test_printf); -} diff --git a/src/user/app/tests/shared/printf.c b/src/user/app/tests/shared/printf.c new file mode 100644 index 0000000..04175db --- /dev/null +++ b/src/user/app/tests/shared/printf.c @@ -0,0 +1,35 @@ +#include "../tests.h" +#include +#include + +static void test_printf(void) { + char buf[64]; + memset(buf, '?', 64); + + /* test proper overflow handling in snprintf */ + test(13 == snprintf(buf, 15, "That's 0x%x", 0x1337)); + test(!memcmp(buf, "That's 0x1337\0??", 16)); + test(17 == snprintf(buf, 15, "%05x %05x %05x", 0, 0, 0)); + test(!memcmp(buf, "00000 00000 00\0?", 16)); + + /* all the other stuff */ + snprintf(buf, sizeof buf, "%010x", 0x1BABE); + test(!strcmp(buf, "000001babe")); + snprintf(buf, sizeof buf, "%10x", 0x1BABE); + test(!strcmp(buf, " 1babe")); + snprintf(buf, sizeof buf, "%10s", "hello"); + test(!strcmp(buf, " hello")); + + 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, "%u %x", 0, 0); + test(!strcmp(buf, "0 0")); +} + +void r_s_printf(void) { + run_test(test_printf); +} diff --git a/src/user/app/tests/shared/ringbuf.c b/src/user/app/tests/shared/ringbuf.c new file mode 100644 index 0000000..7330bbe --- /dev/null +++ b/src/user/app/tests/shared/ringbuf.c @@ -0,0 +1,47 @@ +#include "../tests.h" +#include +#include + +static void test_ringbuf(void) { + char backbuf[16], cmpbuf[16]; + size_t num_read = 0, num_written = 0; + uint8_t c; + + ring_t r = {backbuf, 16, 0, 0}; + + /* aliasing */ + for (size_t i = 0; i < 16; i++) { + test(ring_size(&r) == 0); + ring_put(&r, "11 bytes...", 11); + test(ring_size(&r) == 11); + + memset(cmpbuf, 0, sizeof cmpbuf); + test(ring_get(&r, cmpbuf, 16) == 11); + test(memcmp(cmpbuf, "11 bytes...", 11) == 0); + } + + test(ring_size(&r) == 0); + for (size_t i = 0; i < 7; i++) + ring_put1b(&r, num_written++); + test(ring_size(&r) == 7); + for (size_t i = 0; i < 3; i++) { + ring_get(&r, &c, 1); + test(num_read++ == c); + } + test(ring_size(&r) == 4); + + for (size_t j = 0; j < 40; j++) { + for (size_t i = 0; i < 7; i++) + ring_put1b(&r, num_written++ & 0xff); + test(ring_size(&r) == 11); + for (size_t i = 0; i < 7; i++) { + ring_get(&r, &c, 1); + test((num_read++ & 0xff) == c); + } + test(ring_size(&r) == 4); + } +} + +void r_s_ringbuf(void) { + run_test(test_ringbuf); +} diff --git a/src/user/app/tests/tests.c b/src/user/app/tests/tests.c index a6e7d52..9cdf81c 100644 --- a/src/user/app/tests/tests.c +++ b/src/user/app/tests/tests.c @@ -18,6 +18,7 @@ int main(void) { r_k_miscsyscall(); r_libc_esemaphore(); r_libc_string(); - r_printf(); + r_s_printf(); + r_s_ringbuf(); return 0; } diff --git a/src/user/app/tests/tests.h b/src/user/app/tests/tests.h index 974014a..fadbc60 100644 --- a/src/user/app/tests/tests.h +++ b/src/user/app/tests/tests.h @@ -12,7 +12,8 @@ void r_k_misc(void); void r_k_miscsyscall(void); void r_libc_esemaphore(void); void r_libc_string(void); -void r_printf(void); +void r_s_printf(void); +void r_s_ringbuf(void); #define argify(str) str, sizeof(str) - 1 #define test_fail() do { \ -- cgit v1.2.3