aboutsummaryrefslogtreecommitdiff
path: root/kernel/test.c
blob: 6a69cb671407f073ae142eedbbc8dc6a82cfb3ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// MIT License, Copyright (c) 2020 Marvin Borner

#include <boot.h>
#include <conv.h>
#include <cpu.h>
#include <math.h>
#include <mem.h>
#include <serial.h>
#include <str.h>
#include <test.h>

void pass_or_fail(const char *file_name, int line_num, const char *func, const char *first,
		  const char *second, int success)
{
	printf("\x1B[%s\x1B[0m %s:%d: %s: %s == %s\n", success ? "32m[PASS]" : "31m[FAIL]",
	       file_name, line_num, func, first, second);
}

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);
}

void test_math()
{
	equals(pow(2, 3), 8);
	equals(pow(0, 3), 0);
	equals(pow(0, 0), 1);
}

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(buf1), 0x42);
	equals_str(itoa(42), "42");
	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();
}