Maybe include the file I want to commit

This commit is contained in:
Maxime Bloch 2020-01-28 22:32:32 +01:00
parent a042169845
commit d547432270
No known key found for this signature in database
GPG key ID: CE32A7D95B7D6418
2 changed files with 153 additions and 0 deletions

91
kernel/tests/tests.c Normal file
View file

@ -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 <stddef.h>
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

62
kernel/util/printer.c Normal file
View file

@ -0,0 +1,62 @@
#ifndef TABS_PRINTER_C
#define TABS_PRINTER_C
#include <stdarg.h>
#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