From 00adfffc296476f3fe247ce3f341c9f5addd190d Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 30 Aug 2020 21:49:52 +0200 Subject: Even mooore tests or sth --- kernel/inc/test.h | 28 ++++---------------- kernel/main.c | 5 ++-- kernel/test.c | 79 +++++++++++++++++++++++++++++++++++++++++++------------ libc/inc/mem.h | 2 -- 4 files changed, 70 insertions(+), 44 deletions(-) diff --git a/kernel/inc/test.h b/kernel/inc/test.h index 6f2cdbb..8bd0d41 100644 --- a/kernel/inc/test.h +++ b/kernel/inc/test.h @@ -10,32 +10,14 @@ #define a_mag 0x55 #define b_mag 0x42 -#define check(exp) \ - if ((exp)) { \ - printf("\x1B[32m[PASS]\x1B[0m %s:%d: %s: Test '%s'\n", __FILE__, __LINE__, \ - __func__, #exp); \ - } else { \ - printf("\x1B[31m[FAIL]\x1B[0m %s:%d: %s: Test '%s'\n", __FILE__, __LINE__, \ - __func__, #exp); \ - } +void pass_or_fail(const char *file_name, int line_num, const char *func, const char *first, + const char *second, int success); +#define check(exp) pass_or_fail(__FILE__, __LINE__, __func__, #exp, "1", exp); #define equals(first, second) \ - if ((first) == (second)) { \ - printf("\x1B[32m[PASS]\x1B[0m %s:%d: %s: Test equality '%s'(%d) == '%s'(%d)\n", \ - __FILE__, __LINE__, __func__, #first, (first), #second, (second)); \ - } else { \ - printf("\x1B[31m[FAIL]\x1B[0m %s:%d: %s: Test equality '%s'(%d) == '%s'(%d)\n", \ - __FILE__, __LINE__, __func__, #first, (first), #second, (second)); \ - } - + pass_or_fail(__FILE__, __LINE__, __func__, #first, #second, (first) == (second)); #define equals_str(first, second) \ - if (strcmp((first), (second)) == 0) { \ - printf("\x1B[32m[PASS]\x1B[0m %s:%d: %s: Test equality %s(%s) '%s'(%s)\n", \ - __FILE__, __LINE__, __func__, #first, (first), #second, (second)); \ - } else { \ - printf("\x1B[31m[FAIL]\x1B[0m %s:%d: %s: Test equality %s(%s) '%s'(%s)\n", \ - __FILE__, __LINE__, __func__, #first, (first), #second, (second)); \ - } + pass_or_fail(__FILE__, __LINE__, __func__, #first, #second, strcmp((first), (second)) == 0); void test_all(struct vid_info *vid_info); diff --git a/kernel/main.c b/kernel/main.c index 93e8806..759343e 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -9,14 +9,14 @@ #include #include #include +#include #include void kernel_main(struct vid_info *vid_info) { #ifdef test -#include test_all(vid_info); -#endif +#else heap_init(0x00f00000); @@ -41,4 +41,5 @@ void kernel_main(struct vid_info *vid_info) proc_init(); idle(); +#endif } diff --git a/kernel/test.c b/kernel/test.c index f1a49a6..6a69cb6 100644 --- a/kernel/test.c +++ b/kernel/test.c @@ -9,38 +9,83 @@ #include #include -void test_all(struct vid_info *vid_info) +void pass_or_fail(const char *file_name, int line_num, const char *func, const char *first, + const char *second, int success) { - // Serial connection - serial_install(); - serial_print("\nConnected testing.\n"); - - // Boot passed - check(vid_info && vid_info->mode && vid_info->vbe); + printf("\x1B[%s\x1B[0m %s:%d: %s: %s == %s\n", success ? "32m[PASS]" : "31m[FAIL]", + file_name, line_num, func, first, second); +} - // Test malloc +void test_malloc() +{ heap_init(0x00f00000); u32 *a = malloc(a_mag); u32 *b = malloc(b_mag); equals(a[-1], a_mag); equals(a[a_mag], b_mag); equals(b[-1], b_mag); +} - // Test math +void test_math() +{ equals(pow(2, 3), 8); equals(pow(0, 3), 0); equals(pow(0, 0), 1); +} - // Test conv - char buf[10] = { 0 }; - strcpy(buf, "42"); - equals(atoi(buf), 42); +void test_conv() +{ + char buf0[3] = { 0 }; + char buf1[3] = { 0 }; + char buf2[1] = { 0 }; + char buf3[7] = { 0 }; + char buf4[5] = { 0 }; + char buf5[2] = { 0 }; + strcpy(buf0, "42"); + strcpy(buf1, "42"); + equals(atoi(buf0), 42); equals_str(htoa(0x42), "42"); - equals(htoi(buf), 0x42); + equals(htoi(buf1), 0x42); equals_str(itoa(42), "42"); - equals_str(conv_base(42, buf, 0, 0), ""); - equals_str(conv_base(42, buf, 2, 0), "101010"); - /* equals_str(conv_base(424242, buf, 36, 0), "93ci"); // TODO: THIS */ + equals_str(conv_base(42, buf2, 0, 0), ""); + equals_str(conv_base(42, buf3, 2, 0), "101010"); + equals_str(conv_base(424242, buf4, 36, 0), "93ci"); + equals_str(conv_base(0xffffffff, buf5, 10, 1), "-1"); +} + +void test_mem() +{ + char *str0 = ""; + char *str1 = ""; + char *str2 = "12345"; + char *str3 = "12345"; + char *str4 = "12354"; + equals(memcmp(str4, str2, strlen(str2)), 1); + equals(memcmp(str2, str4, strlen(str2)), -1); + equals(memcmp(str2, str3, strlen(str2)), 0); + equals(memcmp(str0, str1, strlen(str0)), 0); + equals(memcmp(NULL, NULL, 0), 0); + + char buf[6]; + equals_str(memcpy(buf, "hallo", 5), "hallo"); + + char buf2[6]; + equals_str(memset(buf2, 'x', 5), "xxxxx"); +} + +void test_all(struct vid_info *vid_info) +{ + // Serial connection + serial_install(); + serial_print("\nConnected testing.\n"); + + // Boot passed + check(vid_info && vid_info->mode && vid_info->vbe); + + test_malloc(); + test_math(); + test_conv(); + test_mem(); idle(); } diff --git a/libc/inc/mem.h b/libc/inc/mem.h index 9aeeee9..8977a48 100644 --- a/libc/inc/mem.h +++ b/libc/inc/mem.h @@ -12,8 +12,6 @@ void heap_init(u32 start); void *malloc(u32 size); void free(void *ptr); -/* #define malloc(n) (void *)((HEAP += n) - n) // TODO: Implement real/better malloc/free */ -/* #define free(ptr) */ #elif defined(userspace) #include #define malloc(n) (void *)sys1(SYS_MALLOC, n) -- cgit v1.2.3