tests: Start testing libluna
All checks were successful
continuous-integration/drone/push Build is passing

Hooray!!
This commit is contained in:
apio 2023-04-23 21:52:28 +02:00
parent cf8a8c145a
commit a99a0e5a54
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 153 additions and 0 deletions

1
.gitignore vendored
View File

@ -4,4 +4,5 @@ build/
initrd/boot/moon initrd/boot/moon
env-local.sh env-local.sh
initrd/bin/** initrd/bin/**
initrd/tests/**
base/ base/

View File

@ -47,3 +47,4 @@ add_subdirectory(libos)
add_subdirectory(libc) add_subdirectory(libc)
add_subdirectory(kernel) add_subdirectory(kernel)
add_subdirectory(apps) add_subdirectory(apps)
add_subdirectory(tests)

19
tests/CMakeLists.txt Normal file
View File

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

View File

@ -0,0 +1,90 @@
#include <luna/Vector.h>
#include <test.h>
TestResult test_vector_empty_capacity()
{
Vector<char> v;
validate(v.capacity() == 0);
test_success;
}
TestResult test_vector_reserve_should_raise_capacity()
{
Vector<char> v;
validate(v.capacity() == 0);
TRY(v.try_reserve(8));
validate(v.capacity() == 8);
test_success;
}
TestResult test_vector_append_should_raise_capacity()
{
Vector<char> 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<char> 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<char> 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<char> 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<void> 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 {};
}

14
tests/test.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <test.h>
int main(int, char** argv)
{
printf("%s\n", argv[0]);
auto rc = test_main();
if (rc.has_error())
{
perror("error");
return 1;
}
return 0;
}

28
tests/test.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <luna/DebugLog.h>
#include <luna/Result.h>
Result<void> 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<bool> TestResult;