From d5474322701f94d5c6e40907aae76b31cfc49915 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Tue, 28 Jan 2020 22:32:32 +0100 Subject: [PATCH] Maybe include the file I want to commit --- kernel/tests/tests.c | 91 +++++++++++++++++++++++++++++++++++++++++++ kernel/util/printer.c | 62 +++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 kernel/tests/tests.c create mode 100644 kernel/util/printer.c diff --git a/kernel/tests/tests.c b/kernel/tests/tests.c new file mode 100644 index 0000000..1ccef94 --- /dev/null +++ b/kernel/tests/tests.c @@ -0,0 +1,91 @@ +#ifndef TESTS_C +#define TESTS_C + +#define TESTS_ENABLED +#ifdef TESTS_ENABLED + + +#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_run_test(test) do { char *message = test(); tests_run++; \ + if (message) return message; } while (0) +extern int tests_run; + +#include "../memory.c" +#include + + +int tests_run = 0; + + +static char *test_memory() { + + ll_node *ll_head = __new_node(); + ll_head->data = malloc(sizeof(size_t) * 5000); + ll_head->used = true; + ll_head->size = 1; + void *ptr0 = __alloc(ll_head, 5); + void *ptr1 = __alloc(ll_head, 5); + void *ptr4 = __alloc(ll_head, 5); + print(ll_head); + mu_assert("Error: chain should be complete at 1", ll_head->next != NULL); + mu_assert("Error: chain should be complete at 2", ll_head->next->next != NULL); + mu_assert("Error: chain should be complete at 3", ll_head->next->next->next != NULL); + mu_assert("Error: chain should be complete at 4", ll_head->next->next->next->next == NULL); + + ll_node *current = ll_head; + while (current->next != NULL) { + current = current->next; + + mu_assert("Error: block should have size 5", current->size == 5); + mu_assert("Error: block should be in use", current->used == true); + } + + __free(ll_head, ptr1); + mu_assert("", ll_head->next->used == true); + mu_assert("", ll_head->next->next->used == false); + mu_assert("", ll_head->next->next->next->used == true); + + printf("Alloc 3 bytes\n"); + void *ptr2 = __alloc(ll_head, 3); + print(ll_head); + + printf("Alloc 3 bytes\n"); + __alloc(ll_head, 3); + print(ll_head); + + printf("Alloc 2 bytes\n"); + void *ptr3 = __alloc(ll_head, 2); + print(ll_head); + + __free(ll_head, ptr2); + print(ll_head); + __free(ll_head, ptr4); + print(ll_head); + __free(ll_head, ptr3); + print(ll_head); + + __free(ll_head, ptr0); + print(ll_head); + return 0; +} + +static char *all_tests() { + mu_run_test(test_memory); + return 0; +} +// +//int main(int argc, char **argv) { +// char *result = all_tests(); +// if (result != 0) { +// printf("%s\n", result); +// } else { +// printf("ALL TESTS PASSED\n"); +// } +// printf("Tests run: %d\n", tests_run); +// +// return result != 0; +//} + +#endif // TESTS_ENABLED + +#endif // TESTS_C \ No newline at end of file diff --git a/kernel/util/printer.c b/kernel/util/printer.c new file mode 100644 index 0000000..91cf8b3 --- /dev/null +++ b/kernel/util/printer.c @@ -0,0 +1,62 @@ +#ifndef TABS_PRINTER_C +#define TABS_PRINTER_C + +#include + +#include "../terminal.c" + +/** + * fmt is a string with specifiers where variables can be inserted. + * + * ex: ("Hello %s", "world") -> "Hello world" + * + * Specifiers are + * - %c: character + * - %d: digit (integer) + * - %p: pointer (void*) + * @param fmt Formatter string + * @param ... Variable amount of arguments to be inserted + */ +void print(const char *fmt, ...) { + const char *p; + va_list argp; + int i; + char *s; + char fmtbuf[256]; + + va_start(argp, fmt); + + for (p = fmt; *p != '\0'; p++) + if (*p != '%') { + terminal_putchar(*p); + } else { + switch (*++p) { + case 'c': + i = va_arg(argp, int); + + terminal_putchar((char) i); + break; + case 'd': + i = va_arg(argp, int); + s = itoa(i, fmtbuf, 10); + terminal_writestring(s); + break; + case 's': + s = va_arg(argp, char *); + terminal_writestring(s); + break; + case 'x': + i = va_arg(argp, int); + s = itoa(i, fmtbuf, 16); + terminal_writestring(s); + break; + case '%': + terminal_putchar('%'); + break; + } + } + + va_end(argp); +} + +#endif //TABS_PRINTER_C \ No newline at end of file