Add hulk.test module to test kernel functionality

This commit is contained in:
Josh Holtrop 2023-11-22 20:47:21 -05:00
parent cd9a3a7284
commit 35e5aa2ee5
2 changed files with 135 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import hulk.serial;
import hulk.usb; import hulk.usb;
import hulk.pit; import hulk.pit;
import hulk.time; import hulk.time;
import hulk.test;
extern extern(C) __gshared ubyte _hulk_bss_size; extern extern(C) __gshared ubyte _hulk_bss_size;
@ -97,6 +98,9 @@ void hulk_start()
} }
Klog.writefln("\a5HULK Initialization Complete!"); Klog.writefln("\a5HULK Initialization Complete!");
/* Run kernel tests. */
Test.run();
/* Idle loop. */ /* Idle loop. */
Time.msleep(1); Time.msleep(1);
for (;;) for (;;)

131
src/hulk/test.d Normal file
View File

@ -0,0 +1,131 @@
/**
* Kernel tests.
*/
module hulk.test;
import hulk.klog;
import hulk.list;
struct Test
{
/**
* Run tests.
*/
public static void run()
{
Klog.writefln("\a3Running kernel tests");
test_list();
Klog.writefln("\a3Kernel tests complete");
}
private static void test_list()
{
Klog.writefln("Testing list...");
List!ulong list;
assert_eq(null, list.head);
assert_eq(null, list.tail);
ulong v = 33;
list.add(42);
assert_neq(null, list.head);
assert_neq(null, list.tail);
list.add(v);
list.add(0xFFFF);
v = 55;
size_t count;
assert_eq(3, list.count);
foreach (entry; list)
{
switch (count)
{
case 0:
assert_eq(42, entry);
break;
case 1:
assert_eq(33, entry);
break;
case 2:
assert_eq(0xFFFF, entry);
break;
case 3:
assert_eq(0, 1);
break;
default:
break;
}
count++;
}
foreach (entry; list)
{
if (entry == 33)
{
list.remove(entry);
}
}
assert_eq(2, list.count);
count = 0;
foreach (entry; list)
{
switch (count)
{
case 0:
assert_eq(42, entry);
break;
case 1:
assert_eq(0xFFFF, entry);
break;
case 2:
assert_eq(0, 1);
break;
default:
break;
}
count++;
}
foreach (entry; list)
{
if (entry == 42)
{
list.remove(entry);
}
}
assert_eq(1, list.count);
count = 0;
foreach (entry; list)
{
switch (count)
{
case 0:
assert_eq(0xFFFF, entry);
break;
case 1:
assert_eq(0, 1);
break;
default:
break;
}
count++;
}
assert_neq(null, list.head);
assert_neq(null, list.tail);
list.remove(list.head);
assert_eq(0, list.count);
assert_eq(null, list.head);
assert_eq(null, list.tail);
}
private static void assert_eq(T)(T first, T second)
{
if (first != second)
{
Klog.fatal_error("Assertion failed! %x != %x", first, second);
}
}
private static void assert_neq(T)(T first, T second)
{
if (first == second)
{
Klog.fatal_error("Assertion failed! %x == %x", first, second);
}
}
}