From a99a0e5a54697c4d9b69a83f804c31bde57315dd Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 23 Apr 2023 21:52:28 +0200 Subject: [PATCH] tests: Start testing libluna Hooray!! --- .gitignore | 1 + CMakeLists.txt | 1 + tests/CMakeLists.txt | 19 ++++++++ tests/libluna/TestVector.cpp | 90 ++++++++++++++++++++++++++++++++++++ tests/test.cpp | 14 ++++++ tests/test.h | 28 +++++++++++ 6 files changed, 153 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/libluna/TestVector.cpp create mode 100644 tests/test.cpp create mode 100644 tests/test.h diff --git a/.gitignore b/.gitignore index bd68726a..318c54b5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ build/ initrd/boot/moon env-local.sh initrd/bin/** +initrd/tests/** base/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 82371dab..6e70a34e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,3 +47,4 @@ add_subdirectory(libos) add_subdirectory(libc) add_subdirectory(kernel) add_subdirectory(apps) +add_subdirectory(tests) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..4412f7c0 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,19 @@ +add_library(test test.cpp test.h) +target_compile_options(test PRIVATE ${COMMON_FLAGS}) +target_include_directories(test PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +target_include_directories(test PUBLIC ${LUNA_BASE}/usr/include) + +function(luna_test SOURCE_FILE APP_NAME SETUID) + add_executable(${APP_NAME} ${SOURCE_FILE}) + target_compile_options(${APP_NAME} PRIVATE -Os ${COMMON_FLAGS} -Wno-write-strings) + add_dependencies(${APP_NAME} libc) + target_include_directories(${APP_NAME} PRIVATE ${LUNA_BASE}/usr/include) + target_link_libraries(${APP_NAME} PRIVATE test os) + if(${SETUID}) + install(TARGETS ${APP_NAME} DESTINATION ${LUNA_ROOT}/initrd/tests PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE SETUID) + else() + install(TARGETS ${APP_NAME} DESTINATION ${LUNA_ROOT}/initrd/tests) + endif() +endfunction() + +luna_test(libluna/TestVector.cpp TestVector OFF) diff --git a/tests/libluna/TestVector.cpp b/tests/libluna/TestVector.cpp new file mode 100644 index 00000000..a3af09e1 --- /dev/null +++ b/tests/libluna/TestVector.cpp @@ -0,0 +1,90 @@ +#include +#include + +TestResult test_vector_empty_capacity() +{ + Vector v; + validate(v.capacity() == 0); + + test_success; +} + +TestResult test_vector_reserve_should_raise_capacity() +{ + Vector v; + validate(v.capacity() == 0); + + TRY(v.try_reserve(8)); + validate(v.capacity() == 8); + + test_success; +} + +TestResult test_vector_append_should_raise_capacity() +{ + Vector v; + TRY(v.try_append('c')); + + validate(v.capacity() > 0); + validate(v.size() == 1); + + test_success; +} + +TestResult test_vector_pop_should_remove_last_element() +{ + Vector v; + TRY(v.try_append('c')); + + validate(v.capacity() > 0); + validate(v.size() == 1); + + auto value = v.try_pop(); + validate(value.has_value()); + validate(value.value() == 'c'); + validate(v.size() == 0); + + test_success; +} + +TestResult test_empty_vector_pop_should_not_remove_anything() +{ + Vector v; + validate(v.size() == 0); + + auto value = v.try_pop(); + validate(!value.has_value()); + validate(v.size() == 0); + + test_success; +} + +TestResult test_vector_clear_should_free_memory() +{ + Vector v; + TRY(v.try_append('a')); + TRY(v.try_append('b')); + + validate(v.size() == 2); + + v.clear(); + + validate(v.size() == 0); + validate(v.capacity() == 0); + + test_success; +} + +Result test_main() +{ + test_prelude; + + run_test(test_vector_empty_capacity); + run_test(test_vector_reserve_should_raise_capacity); + run_test(test_vector_append_should_raise_capacity); + run_test(test_vector_pop_should_remove_last_element); + run_test(test_empty_vector_pop_should_not_remove_anything); + run_test(test_vector_clear_should_free_memory); + + return {}; +} diff --git a/tests/test.cpp b/tests/test.cpp new file mode 100644 index 00000000..4e7ec7d5 --- /dev/null +++ b/tests/test.cpp @@ -0,0 +1,14 @@ +#include +#include + +int main(int, char** argv) +{ + printf("%s\n", argv[0]); + auto rc = test_main(); + if (rc.has_error()) + { + perror("error"); + return 1; + } + return 0; +} diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 00000000..824f0072 --- /dev/null +++ b/tests/test.h @@ -0,0 +1,28 @@ +#pragma once +#include +#include + +Result test_main(); + +#define test_prelude bool rc + +#define test_success return true + +#define run_test(name) \ + rc = TRY(name()); \ + if (!rc) \ + { \ + dbgln("test failed: %s", #name); \ + check(false); \ + } \ + else \ + dbgln("test passed: %s", #name); + +#define validate(cond) \ + if (!(cond)) \ + { \ + dbgln("unexpected result: %s", #cond); \ + return false; \ + } + +typedef Result TestResult;