From 1ca147f9e08202dfbc78692e204adac280f8238b Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sat, 14 Feb 2015 19:15:43 +0100 Subject: Rearrange testing architecture : just type 'make run_tests' --- src/tests/Makefile | 2 -- src/tests/ktests/breakpoint/Makefile | 1 + src/tests/ktests/breakpoint/test.c | 13 +++++++++++ src/tests/ktests/cmdline/Makefile | 1 + src/tests/ktests/cmdline/test.c | 24 ++++++++++++++++++++ src/tests/ktests/hashtbl1/Makefile | 1 + src/tests/ktests/hashtbl1/test.c | 41 +++++++++++++++++++++++++++++++++ src/tests/ktests/hashtbl2/Makefile | 1 + src/tests/ktests/hashtbl2/test.c | 42 ++++++++++++++++++++++++++++++++++ src/tests/ktests/kmalloc/Makefile | 1 + src/tests/ktests/kmalloc/test.c | 32 ++++++++++++++++++++++++++ src/tests/ktests/region1/Makefile | 1 + src/tests/ktests/region1/test.c | 36 +++++++++++++++++++++++++++++ src/tests/ktests/region2/Makefile | 1 + src/tests/ktests/region2/test.c | 34 ++++++++++++++++++++++++++++ src/tests/ktests/rules.make | 17 ++++++++++++++ src/tests/ktests/run_qemu_test.sh | 12 ++++++++++ src/tests/run_tests.sh | 13 +++++++++++ src/tests/slab_test.c | 44 ------------------------------------ src/tests/slab_test/Makefile | 5 ++++ src/tests/slab_test/slab_test.c | 44 ++++++++++++++++++++++++++++++++++++ 21 files changed, 320 insertions(+), 46 deletions(-) delete mode 100644 src/tests/Makefile create mode 120000 src/tests/ktests/breakpoint/Makefile create mode 100644 src/tests/ktests/breakpoint/test.c create mode 120000 src/tests/ktests/cmdline/Makefile create mode 100644 src/tests/ktests/cmdline/test.c create mode 120000 src/tests/ktests/hashtbl1/Makefile create mode 100644 src/tests/ktests/hashtbl1/test.c create mode 120000 src/tests/ktests/hashtbl2/Makefile create mode 100644 src/tests/ktests/hashtbl2/test.c create mode 120000 src/tests/ktests/kmalloc/Makefile create mode 100644 src/tests/ktests/kmalloc/test.c create mode 120000 src/tests/ktests/region1/Makefile create mode 100644 src/tests/ktests/region1/test.c create mode 120000 src/tests/ktests/region2/Makefile create mode 100644 src/tests/ktests/region2/test.c create mode 100644 src/tests/ktests/rules.make create mode 100755 src/tests/ktests/run_qemu_test.sh create mode 100755 src/tests/run_tests.sh delete mode 100644 src/tests/slab_test.c create mode 100644 src/tests/slab_test/Makefile create mode 100644 src/tests/slab_test/slab_test.c (limited to 'src/tests') diff --git a/src/tests/Makefile b/src/tests/Makefile deleted file mode 100644 index 4a21edf..0000000 --- a/src/tests/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -slab_test.bin: slab_test.c ../include/slab_alloc.h ../lib/slab_alloc.c - gcc -m32 -o $@ -std=c99 -I ../include slab_test.c ../lib/slab_alloc.c diff --git a/src/tests/ktests/breakpoint/Makefile b/src/tests/ktests/breakpoint/Makefile new file mode 120000 index 0000000..4630a7c --- /dev/null +++ b/src/tests/ktests/breakpoint/Makefile @@ -0,0 +1 @@ +../rules.make \ No newline at end of file diff --git a/src/tests/ktests/breakpoint/test.c b/src/tests/ktests/breakpoint/test.c new file mode 100644 index 0000000..cd577e0 --- /dev/null +++ b/src/tests/ktests/breakpoint/test.c @@ -0,0 +1,13 @@ + +void test_breakpoint() { + BEGIN_TEST("breakpoint-test"); + + asm volatile("int $0x3"); // test breakpoint + + TEST_OK; +} + +#undef TEST_PLACEHOLDER_AFTER_IDT +#define TEST_PLACEHOLDER_AFTER_IDT { test_breakpoint(); } + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/ktests/cmdline/Makefile b/src/tests/ktests/cmdline/Makefile new file mode 120000 index 0000000..4630a7c --- /dev/null +++ b/src/tests/ktests/cmdline/Makefile @@ -0,0 +1 @@ +../rules.make \ No newline at end of file diff --git a/src/tests/ktests/cmdline/test.c b/src/tests/ktests/cmdline/test.c new file mode 100644 index 0000000..a6d0a4c --- /dev/null +++ b/src/tests/ktests/cmdline/test.c @@ -0,0 +1,24 @@ + +void test_cmdline(multiboot_info_t *mbd, fs_t *devfs) { + BEGIN_TEST("test-cmdline"); + + fs_handle_t *f = fs_open(devfs, "/cmdline", FM_READ); + ASSERT(f != 0); + + char buf[256]; + size_t l = file_read(f, 0, 255, buf); + ASSERT(l > 0); + buf[l] = 0; + + unref_file(f); + dbg_printf("Command line as in /cmdline file: '%s'.\n", buf); + + ASSERT(strcmp(buf, (char*)mbd->cmdline) == 0); + + TEST_OK; +} + +#undef TEST_PLACEHOLDER_AFTER_DEVFS +#define TEST_PLACEHOLDER_AFTER_DEVFS { test_cmdline(mbd, devfs); } + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/ktests/hashtbl1/Makefile b/src/tests/ktests/hashtbl1/Makefile new file mode 120000 index 0000000..4630a7c --- /dev/null +++ b/src/tests/ktests/hashtbl1/Makefile @@ -0,0 +1 @@ +../rules.make \ No newline at end of file diff --git a/src/tests/ktests/hashtbl1/test.c b/src/tests/ktests/hashtbl1/test.c new file mode 100644 index 0000000..993718c --- /dev/null +++ b/src/tests/ktests/hashtbl1/test.c @@ -0,0 +1,41 @@ +#include + +void test_hashtbl_1() { + BEGIN_TEST("test-hashtbl-1"); + + // hashtable test + hashtbl_t *ht = create_hashtbl(str_key_eq_fun, str_hash_fun, 0); + ASSERT(ht != 0); + + ASSERT(hashtbl_add(ht, "test1", "STRTEST1")); + ASSERT(hashtbl_add(ht, "test2", "STRTEST2")); + ASSERT(hashtbl_find(ht, "test1") != 0 && + strcmp(hashtbl_find(ht, "test1"), "STRTEST1") == 0); + ASSERT(hashtbl_find(ht, "test2") != 0 && + strcmp(hashtbl_find(ht, "test2"), "STRTEST2") == 0); + ASSERT(hashtbl_find(ht, "test") == 0); + + ASSERT(hashtbl_add(ht, "test", "Forever alone")); + ASSERT(hashtbl_find(ht, "test1") != 0 && + strcmp(hashtbl_find(ht, "test1"), "STRTEST1") == 0); + ASSERT(hashtbl_find(ht, "test2") != 0 && + strcmp(hashtbl_find(ht, "test2"), "STRTEST2") == 0); + ASSERT(hashtbl_find(ht, "test") != 0 && + strcmp(hashtbl_find(ht, "test"), "Forever alone") == 0); + + hashtbl_remove(ht, "test1"); + ASSERT(hashtbl_find(ht, "test1") == 0); + ASSERT(hashtbl_find(ht, "test2") != 0 && + strcmp(hashtbl_find(ht, "test2"), "STRTEST2") == 0); + ASSERT(hashtbl_find(ht, "test") != 0 && + strcmp(hashtbl_find(ht, "test"), "Forever alone") == 0); + + delete_hashtbl(ht); + + TEST_OK; +} + +#undef TEST_PLACEHOLDER_AFTER_TASKING +#define TEST_PLACEHOLDER_AFTER_TASKING { test_hashtbl_1(); } + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/ktests/hashtbl2/Makefile b/src/tests/ktests/hashtbl2/Makefile new file mode 120000 index 0000000..4630a7c --- /dev/null +++ b/src/tests/ktests/hashtbl2/Makefile @@ -0,0 +1 @@ +../rules.make \ No newline at end of file diff --git a/src/tests/ktests/hashtbl2/test.c b/src/tests/ktests/hashtbl2/test.c new file mode 100644 index 0000000..2441e79 --- /dev/null +++ b/src/tests/ktests/hashtbl2/test.c @@ -0,0 +1,42 @@ +#include + +void test_hashtbl_2() { + BEGIN_TEST("test-hashtbl-2"); + + hashtbl_t *ht = create_hashtbl(id_key_eq_fun, id_hash_fun, 0); + ASSERT(ht != 0); + + ASSERT(hashtbl_add(ht, (void*)12, "TESTSTR12")); + ASSERT(hashtbl_add(ht, (void*)777, "TESTSTR777")); + + ASSERT(hashtbl_find(ht, (void*)12) != 0 && + strcmp(hashtbl_find(ht, (void*)12), "TESTSTR12") == 0); + ASSERT(hashtbl_find(ht, (void*)777) != 0 && + strcmp(hashtbl_find(ht, (void*)777), "TESTSTR777") == 0); + ASSERT(hashtbl_find(ht, (void*)144) == 0); + + ASSERT(hashtbl_add(ht, (void*)144, "Forever alone")); + + ASSERT(hashtbl_find(ht, (void*)12) != 0 && + strcmp(hashtbl_find(ht, (void*)12), "TESTSTR12") == 0); + ASSERT(hashtbl_find(ht, (void*)144) != 0 && + strcmp(hashtbl_find(ht, (void*)144), "Forever alone") == 0); + ASSERT(hashtbl_find(ht, (void*)777) != 0 && + strcmp(hashtbl_find(ht, (void*)777), "TESTSTR777") == 0); + + hashtbl_remove(ht, (void*)12); + ASSERT(hashtbl_find(ht, (void*)12) == 0); + ASSERT(hashtbl_find(ht, (void*)144) != 0 && + strcmp(hashtbl_find(ht, (void*)144), "Forever alone") == 0); + ASSERT(hashtbl_find(ht, (void*)777) != 0 && + strcmp(hashtbl_find(ht, (void*)777), "TESTSTR777") == 0); + + delete_hashtbl(ht); + + TEST_OK; +} + +#undef TEST_PLACEHOLDER_AFTER_TASKING +#define TEST_PLACEHOLDER_AFTER_TASKING { test_hashtbl_2(); } + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/ktests/kmalloc/Makefile b/src/tests/ktests/kmalloc/Makefile new file mode 120000 index 0000000..4630a7c --- /dev/null +++ b/src/tests/ktests/kmalloc/Makefile @@ -0,0 +1 @@ +../rules.make \ No newline at end of file diff --git a/src/tests/ktests/kmalloc/test.c b/src/tests/ktests/kmalloc/test.c new file mode 100644 index 0000000..2b5dc6d --- /dev/null +++ b/src/tests/ktests/kmalloc/test.c @@ -0,0 +1,32 @@ + +void kmalloc_test(void* kernel_data_end) { + BEGIN_TEST("kmalloc-test"); + + dbg_print_region_info(); + const int m = 200; + uint16_t** ptr = malloc(m * sizeof(uint32_t)); + for (int i = 0; i < m; i++) { + size_t s = 1 << ((i * 7) % 11 + 2); + ptr[i] = (uint16_t*)malloc(s); + ASSERT((void*)ptr[i] >= kernel_data_end && (size_t)ptr[i] < 0xFFC00000); + *ptr[i] = ((i * 211) % 1024); + } + dbg_printf("Fully allocated.\n"); + dbg_print_region_info(); + for (int i = 0; i < m; i++) { + for (int j = i; j < m; j++) { + ASSERT(*ptr[j] == (j * 211) % 1024); + } + free(ptr[i]); + } + free(ptr); + dbg_printf("Kmalloc test OK.\n"); + dbg_print_region_info(); + + TEST_OK; +} + +#undef TEST_PLACEHOLDER_AFTER_KMALLOC +#define TEST_PLACEHOLDER_AFTER_KMALLOC { kmalloc_test(kernel_data_end); } + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/ktests/region1/Makefile b/src/tests/ktests/region1/Makefile new file mode 120000 index 0000000..4630a7c --- /dev/null +++ b/src/tests/ktests/region1/Makefile @@ -0,0 +1 @@ +../rules.make \ No newline at end of file diff --git a/src/tests/ktests/region1/test.c b/src/tests/ktests/region1/test.c new file mode 100644 index 0000000..1a42638 --- /dev/null +++ b/src/tests/ktests/region1/test.c @@ -0,0 +1,36 @@ + +void test_region_1() { + BEGIN_TEST("region-test-1"); + + void* p = region_alloc(0x1000, "Test region", 0); + dbg_printf("Allocated one-page region: 0x%p\n", p); + dbg_print_region_info(); + void* q = region_alloc(0x1000, "Test region", 0); + dbg_printf("Allocated one-page region: 0x%p\n", q); + dbg_print_region_info(); + void* r = region_alloc(0x2000, "Test region", 0); + dbg_printf("Allocated two-page region: 0x%p\n", r); + dbg_print_region_info(); + void* s = region_alloc(0x10000, "Test region", 0); + dbg_printf("Allocated 16-page region: 0x%p\n", s); + dbg_print_region_info(); + region_free(p); + dbg_printf("Freed region 0x%p\n", p); + dbg_print_region_info(); + region_free(q); + dbg_printf("Freed region 0x%p\n", q); + dbg_print_region_info(); + region_free(r); + dbg_printf("Freed region 0x%p\n", r); + dbg_print_region_info(); + region_free(s); + dbg_printf("Freed region 0x%p\n", s); + dbg_print_region_info(); + + TEST_OK; +} + +#undef TEST_PLACEHOLDER_AFTER_REGION +#define TEST_PLACEHOLDER_AFTER_REGION { test_region_1(); } + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/ktests/region2/Makefile b/src/tests/ktests/region2/Makefile new file mode 120000 index 0000000..4630a7c --- /dev/null +++ b/src/tests/ktests/region2/Makefile @@ -0,0 +1 @@ +../rules.make \ No newline at end of file diff --git a/src/tests/ktests/region2/test.c b/src/tests/ktests/region2/test.c new file mode 100644 index 0000000..24908b0 --- /dev/null +++ b/src/tests/ktests/region2/test.c @@ -0,0 +1,34 @@ + +void test_region_2() { + BEGIN_TEST("region-test-2"); + + // allocate a big region and try to write into it + const size_t n = 200; + void* p0 = region_alloc(n * PAGE_SIZE, "Test big region", default_allocator_pf_handler); + for (size_t i = 0; i < n; i++) { + uint32_t *x = (uint32_t*)(p0 + i * PAGE_SIZE); + x[0] = 12; + x[1] = (i * 20422) % 122; + } + // unmap memory + for (size_t i = 0; i < n; i++) { + void* p = p0 + i * PAGE_SIZE; + uint32_t *x = (uint32_t*)p; + ASSERT(x[1] == (i * 20422) % 122); + + uint32_t f = pd_get_frame(p); + ASSERT(f != 0); + pd_unmap_page(p); + ASSERT(pd_get_frame(p) == 0); + + frame_free(f, 1); + } + region_free(p0); + + TEST_OK; +} + +#undef TEST_PLACEHOLDER_AFTER_REGION +#define TEST_PLACEHOLDER_AFTER_REGION { test_region_2(); } + +/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/ktests/rules.make b/src/tests/ktests/rules.make new file mode 100644 index 0000000..f31ab89 --- /dev/null +++ b/src/tests/ktests/rules.make @@ -0,0 +1,17 @@ +LIB = ../../../kernel/kernel.lib + +OBJ = kmain.o + +CFLAGS = -I . -I ../../../common/include -I ../../../kernel/include -DIS_A_TEST +LDFLAGS = -T ../../../kernel/linker.ld -Xlinker -Map=test_kernel.map + +OUT = test_kernel.bin + +include ../../../rules.make + +kmain.o: ../../../kernel/core/kmain.c test.c + $(CC) -c $< -o $@ $(CFLAGS) + +run_test: all + ../run_qemu_test.sh + diff --git a/src/tests/ktests/run_qemu_test.sh b/src/tests/ktests/run_qemu_test.sh new file mode 100755 index 0000000..93c7320 --- /dev/null +++ b/src/tests/ktests/run_qemu_test.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +(qemu-system-i386 -kernel test_kernel.bin -serial stdio -m 16 -display none & echo $! >&3) 3>pid \ + | tee >(grep -m 1 "TEST-" >result; kill -INT `cat pid`) \ + +RESULT=`cat result` + +rm result +rm pid + +if [ $RESULT != '(TEST-OK)' ]; then exit 1; fi + diff --git a/src/tests/run_tests.sh b/src/tests/run_tests.sh new file mode 100755 index 0000000..bd23801 --- /dev/null +++ b/src/tests/run_tests.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +cd `dirname $0` + +for FILE in */Makefile */*/Makefile; do + TEST=`dirname $FILE` + echo -n "Running test $TEST ... " + if make -C $TEST run_test > $TEST/test.log 2>&1; then + echo OK + else + echo FAIL + fi +done diff --git a/src/tests/slab_test.c b/src/tests/slab_test.c deleted file mode 100644 index 747c785..0000000 --- a/src/tests/slab_test.c +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include - -slab_type_t slab_sizes[] = { - { "8B obj", 8, 1 }, - { "16B obj", 16, 2 }, - { "32B obj", 32, 2 }, - { "64B obj", 64, 2 }, - { "128B obj", 128, 2 }, - { "256B obj", 256, 4 }, - { "512B obj", 512, 4 }, - { "1KB obj", 1024, 8 }, - { "2KB obj", 2048, 8 }, - { "4KB obj", 4096, 16 }, - { 0, 0, 0 } -}; - - -int main(int argc, char *argv[]) { - mem_allocator_t *a = - create_slab_allocator(slab_sizes, malloc, free); - - const int m = 10000; - uint32_t* ptr[m]; - for (int i = 0; i < m; i++) { - size_t s = 1 << ((i * 7) % 12 + 2); - ptr[i] = (uint32_t*)slab_alloc(a, s); - ASSERT(ptr[i] != 0); - *ptr[i] = ((i * 2117) % (1<<30)); - printf("Alloc %i : 0x%p\n", s, ptr[i]); - } - for (int i = 0; i < m; i++) { - for (int j = i; j < m; j++) { - ASSERT(*ptr[j] == (j * 2117) % (1<<30)); - } - slab_free(a, ptr[i]); - } - destroy_slab_allocator(a); - - return 0; -} - -/* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/tests/slab_test/Makefile b/src/tests/slab_test/Makefile new file mode 100644 index 0000000..4cda341 --- /dev/null +++ b/src/tests/slab_test/Makefile @@ -0,0 +1,5 @@ +slab_test.bin: slab_test.c ../../common/include/slab_alloc.h ../../common/libkogata/slab_alloc.c + gcc -m32 -o $@ -std=c99 -I ../../common/include slab_test.c ../../common/libkogata/slab_alloc.c + +run_test: slab_test.bin + ./slab_test.bin diff --git a/src/tests/slab_test/slab_test.c b/src/tests/slab_test/slab_test.c new file mode 100644 index 0000000..747c785 --- /dev/null +++ b/src/tests/slab_test/slab_test.c @@ -0,0 +1,44 @@ +#include +#include +#include + +slab_type_t slab_sizes[] = { + { "8B obj", 8, 1 }, + { "16B obj", 16, 2 }, + { "32B obj", 32, 2 }, + { "64B obj", 64, 2 }, + { "128B obj", 128, 2 }, + { "256B obj", 256, 4 }, + { "512B obj", 512, 4 }, + { "1KB obj", 1024, 8 }, + { "2KB obj", 2048, 8 }, + { "4KB obj", 4096, 16 }, + { 0, 0, 0 } +}; + + +int main(int argc, char *argv[]) { + mem_allocator_t *a = + create_slab_allocator(slab_sizes, malloc, free); + + const int m = 10000; + uint32_t* ptr[m]; + for (int i = 0; i < m; i++) { + size_t s = 1 << ((i * 7) % 12 + 2); + ptr[i] = (uint32_t*)slab_alloc(a, s); + ASSERT(ptr[i] != 0); + *ptr[i] = ((i * 2117) % (1<<30)); + printf("Alloc %i : 0x%p\n", s, ptr[i]); + } + for (int i = 0; i < m; i++) { + for (int j = i; j < m; j++) { + ASSERT(*ptr[j] == (j * 2117) % (1<<30)); + } + slab_free(a, ptr[i]); + } + destroy_slab_allocator(a); + + return 0; +} + +/* vim: set ts=4 sw=4 tw=0 noet :*/ -- cgit v1.2.3