Tests: Add test framework

This commit is contained in:
apio 2022-10-16 15:31:58 +02:00
parent f8154ce230
commit d62eb6c791
10 changed files with 112 additions and 0 deletions

1
.gitignore vendored
View File

@ -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

View File

@ -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");

9
tests/Makefile Normal file
View File

@ -0,0 +1,9 @@
build:
make -C libc build
install:
make -C libc install
test:
make -C libc test

36
tests/Test.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include <stdbool.h>
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)

15
tests/libc/Makefile Normal file
View File

@ -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

9
tests/libc/Test.c Normal file
View File

@ -0,0 +1,9 @@
#include "Test.h"
DEFINE_TEST(strlen);
int main()
{
START_TEST_CASE(string.h);
RUN_TEST(strlen);
}

21
tests/libc/string.c Normal file
View File

@ -0,0 +1,21 @@
#include "Test.h"
#include <string.h>
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();
}

View File

@ -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 $@

View File

@ -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 $@

10
tools/test.sh Executable file
View File

@ -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