diff --git a/.gitignore b/.gitignore index 12b6bd37..1b122cad 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ kernel/bin/moon.elf initrd/sys/moon.sym initrd/bin/** apps/bin/** +tests/**/bin/** base/usr/include/** base/usr/lib/** **/*.a \ No newline at end of file diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 78a7a28f..13030466 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -35,6 +35,9 @@ #include "thread/PIT.h" #include "thread/Scheduler.h" +#define STRINGIZE(x) #x +#define STRINGIZE_VALUE_OF(x) STRINGIZE(x) + extern "C" void _start() { Init::check_magic(); @@ -73,7 +76,11 @@ extern "C" void _start() } }); +#ifdef RUN_TEST_AS_INIT + Scheduler::load_user_task(STRINGIZE_VALUE_OF(RUN_TEST_AS_INIT)); +#else Scheduler::load_user_task("/bin/init"); +#endif kinfoln("Prepared scheduler tasks"); diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 00000000..38f21741 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,9 @@ +build: + make -C libc build + +install: + make -C libc install + +test: + make -C libc test + \ No newline at end of file diff --git a/tests/Test.h b/tests/Test.h new file mode 100644 index 00000000..0430c7e5 --- /dev/null +++ b/tests/Test.h @@ -0,0 +1,36 @@ +#pragma once +#include + +int printf(const char*, ...); + +#define DEFINE_TEST(name) bool test_##name() +#define START_TEST(name) printf("testing whether %s works... ", #name) +#define START_TEST_CASE(name) printf("testing %s...\n", #name) + +#define TEST_SUCCESS() \ + do { \ + printf("yes!\n"); \ + return true; \ + } while (0) + +#define TEST_FAIL() \ + do { \ + printf("no\n"); \ + return false; \ + } while (0) + +#define EXPECT(expr) \ + do { \ + if (!(expr)) { TEST_FAIL(); } \ + } while (0) + +#define EXPECT_EQ(a, b) EXPECT((a) == (b)) + +#define EXPECT_NOT_EQ(a, b) EXPECT((a) != (b)) + +#define EXPECT_NOT_CRASHED() TEST_SUCCESS() + +#define RUN_TEST(name) \ + do { \ + if (!test_##name()) return 1; \ + } while (0) \ No newline at end of file diff --git a/tests/libc/Makefile b/tests/libc/Makefile new file mode 100644 index 00000000..3f6e9ce5 --- /dev/null +++ b/tests/libc/Makefile @@ -0,0 +1,15 @@ +TESTDIR := $(LUNA_ROOT)/tests/libc +DESTDIR := $(LUNA_ROOT)/initrd/bin + +build: + @mkdir -p $(TESTDIR)/bin + $(LUNA_ROOT)/tools/sync-libc.sh + $(CC) $(TESTDIR)/string.c $(TESTDIR)/Test.c -I$(LUNA_ROOT)/tests -o $(TESTDIR)/bin/test-libc + +install: + $(LUNA_ROOT)/tools/clean.sh + @mkdir -p $(DESTDIR) + cp $(TESTDIR)/bin/test-libc $(DESTDIR)/test-libc + +test: + CFLAGS="-DRUN_TEST_AS_INIT=/bin/test-libc" $(LUNA_ROOT)/tools/run.sh \ No newline at end of file diff --git a/tests/libc/Test.c b/tests/libc/Test.c new file mode 100644 index 00000000..b97475b0 --- /dev/null +++ b/tests/libc/Test.c @@ -0,0 +1,9 @@ +#include "Test.h" + +DEFINE_TEST(strlen); + +int main() +{ + START_TEST_CASE(string.h); + RUN_TEST(strlen); +} \ No newline at end of file diff --git a/tests/libc/string.c b/tests/libc/string.c new file mode 100644 index 00000000..a603dcc6 --- /dev/null +++ b/tests/libc/string.c @@ -0,0 +1,21 @@ +#include "Test.h" +#include + +DEFINE_TEST(strlen) +{ + START_TEST(strlen); + + const char* str = "Hello, World!"; + + size_t len = strlen(str); + + EXPECT_EQ(len, 13); + + char null[] = {'\0'}; + + len = strlen(null); + + EXPECT_EQ(len, 0); + + TEST_SUCCESS(); +} \ No newline at end of file diff --git a/tools/rebuild-and-run.sh b/tools/rebuild-and-run.sh index 678c3f60..1ac23fa1 100755 --- a/tools/rebuild-and-run.sh +++ b/tools/rebuild-and-run.sh @@ -3,6 +3,8 @@ set -e source $(dirname $0)/env.sh +cd $LUNA_ROOT + tools/rebuild-iso.sh qemu-system-x86_64 -cdrom Luna.iso -smp 1 -m 256M -serial stdio -enable-kvm $@ \ No newline at end of file diff --git a/tools/run.sh b/tools/run.sh index e1904c95..f2f3e326 100755 --- a/tools/run.sh +++ b/tools/run.sh @@ -3,6 +3,8 @@ set -e source $(dirname $0)/env.sh +cd $LUNA_ROOT + tools/build-iso.sh qemu-system-x86_64 -cdrom Luna.iso -smp 1 -m 256M -serial stdio -enable-kvm $@ \ No newline at end of file diff --git a/tools/test.sh b/tools/test.sh new file mode 100755 index 00000000..d73fd091 --- /dev/null +++ b/tools/test.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -e +source $(dirname $0)/env.sh + +cd $LUNA_ROOT + +make -C tests build +make -C tests install +make -C tests test \ No newline at end of file