-
Notifications
You must be signed in to change notification settings - Fork 18
Add test infrastructure #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
frogrammer9
merged 5 commits into
Operacja-System:main
from
dawidpawliczek4:add-test-infra
May 4, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ca2f1e
Introduce unit tests infrastructure
dawidpawliczek4 2654a64
Enhance unit test configuration to output results on failure
dawidpawliczek4 063ec8e
Enhance unit test run configuration with verbose output
dawidpawliczek4 5fce38f
use aligned_alloc
dawidpawliczek4 278ef24
Remove hal_irq_stub from test_pmallocator executable
dawidpawliczek4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ dkms.conf | |
|
|
||
| # Build | ||
| build/ | ||
| build-tests/ | ||
| .cache/ | ||
|
|
||
| # Disk images | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| include(FetchContent) | ||
|
|
||
| FetchContent_Declare( | ||
| unity | ||
| GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git | ||
| GIT_TAG v2.6.1 | ||
| GIT_SHALLOW TRUE | ||
| ) | ||
| FetchContent_MakeAvailable(unity) | ||
|
|
||
| add_subdirectory(kernel/memory_management/physical_memory) |
18 changes: 18 additions & 0 deletions
18
tests/kernel/memory_management/physical_memory/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| add_executable(test_pmallocator | ||
| test_allocator.c | ||
| ${PROJECT_SOURCE_DIR}/src/kernel/memory_management/physical_memory/allocator.c | ||
| ) | ||
|
|
||
| target_compile_features(test_pmallocator PUBLIC c_std_23) | ||
| target_compile_options(test_pmallocator PRIVATE -Wall -Wextra) | ||
|
|
||
| target_include_directories(test_pmallocator PRIVATE | ||
| ${PROJECT_SOURCE_DIR}/include | ||
| ${PROJECT_SOURCE_DIR}/external/include | ||
| ${PROJECT_SOURCE_DIR}/src/kernel | ||
| ${PROJECT_SOURCE_DIR}/src/kernel/memory_management/physical_memory | ||
| ) | ||
|
|
||
| target_link_libraries(test_pmallocator PRIVATE unity) | ||
|
|
||
| add_test(NAME pmallocator_tests COMMAND test_pmallocator) |
199 changes: 199 additions & 0 deletions
199
tests/kernel/memory_management/physical_memory/test_allocator.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| #include <unity.h> | ||
| #include <stdlib.h> | ||
| #include "allocator.h" | ||
| #include "memory_management/include/physical_memory/manager.h" | ||
| #include "stdbigos/memory_types.h" | ||
|
|
||
| #define TEST_PAGES 64 | ||
| #define TEST_SIZE (TEST_PAGES * PAGE_SIZE) | ||
|
|
||
| #define RESERVED1_OFFSET 0 | ||
| #define RESERVED1_SIZE PAGE_SIZE | ||
|
|
||
| #define RESERVED2_OFFSET (32 * PAGE_SIZE) | ||
| #define RESERVED2_SIZE (2 * PAGE_SIZE) | ||
|
|
||
| #define TOTAL_RESERVED_PAGES 3 | ||
|
|
||
| typedef struct { | ||
| memory_area_t* areas; | ||
| size_t count; | ||
| size_t index; | ||
| } reserved_ctx_t; | ||
|
|
||
| static bool enumerator_stub(void* user, memory_area_t* out) { | ||
| reserved_ctx_t* ctx = user; | ||
| if (ctx->index >= ctx->count) { | ||
| ctx->index = 0; | ||
| return false; | ||
| } | ||
| *out = ctx->areas[ctx->index++]; | ||
| return true; | ||
| } | ||
|
|
||
| static void* g_area_buf; | ||
| static memory_region_t g_header_region; | ||
| static memory_area_t g_area; | ||
| static size_t g_header_pages; | ||
|
|
||
| void setUp(void) { | ||
| g_area_buf = aligned_alloc(PAGE_SIZE, TEST_SIZE); | ||
|
|
||
| g_area = (memory_area_t){ | ||
| .addr = (uintptr_t)g_area_buf, | ||
| .size = TEST_SIZE, | ||
| }; | ||
|
|
||
| memory_area_t reserved[] = { | ||
| {.addr = g_area.addr + RESERVED1_OFFSET, .size = RESERVED1_SIZE}, | ||
| {.addr = g_area.addr + RESERVED2_OFFSET, .size = RESERVED2_SIZE}, | ||
| }; | ||
| reserved_ctx_t ctx = {.areas = reserved, .count = 2, .index = 0}; | ||
|
|
||
| memory_area_t header_area; | ||
| error_t err = pmallocator_get_header(g_area, enumerator_stub, &ctx, &header_area); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err); | ||
| TEST_ASSERT_EQUAL(g_area.addr + PAGE_SIZE, header_area.addr); | ||
| TEST_ASSERT_TRUE(header_area.size > 0); | ||
| TEST_ASSERT_EQUAL(0, header_area.size % PAGE_SIZE); | ||
| g_header_pages = header_area.size / PAGE_SIZE; | ||
|
|
||
| g_header_region = (memory_region_t){ | ||
| .addr = (void*)header_area.addr, | ||
| .size = header_area.size, | ||
| }; | ||
|
|
||
| ctx.index = 0; | ||
| error_t err2 = pmallocator_init_region(g_area, g_header_region, enumerator_stub, &ctx); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err2); | ||
| } | ||
|
|
||
| void tearDown(void) { | ||
| free(g_area_buf); | ||
| g_area_buf = NULL; | ||
| } | ||
|
|
||
|
|
||
| static void test_allocate_single_page(void) { | ||
| memory_area_t result; | ||
| error_t err = pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &result); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err); | ||
| TEST_ASSERT_EQUAL(PAGE_SIZE, result.size); | ||
| TEST_ASSERT_EQUAL(0, result.addr % PAGE_SIZE); | ||
| TEST_ASSERT_TRUE(result.addr >= g_area.addr); | ||
| TEST_ASSERT_TRUE(result.addr + result.size <= g_area.addr + TEST_SIZE); | ||
| } | ||
|
|
||
| static void test_allocate_until_full(void) { | ||
| size_t expected = TEST_PAGES - TOTAL_RESERVED_PAGES - g_header_pages; | ||
| size_t count = 0; | ||
| memory_area_t result; | ||
| size_t prev_addr = -1; | ||
|
|
||
| while (pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &result) == ERR_NONE) { | ||
| TEST_ASSERT_FALSE( | ||
| do_memory_areas_overlap(result,(memory_area_t){ .addr = g_area.addr + RESERVED1_OFFSET, .size = RESERVED1_SIZE }) | ||
| ); | ||
|
|
||
| TEST_ASSERT_FALSE( | ||
| do_memory_areas_overlap(result,(memory_area_t){ .addr = g_area.addr + RESERVED2_OFFSET, .size = RESERVED2_SIZE }) | ||
| ); | ||
|
|
||
| TEST_ASSERT_FALSE( | ||
| prev_addr == result.addr | ||
| ); | ||
|
|
||
| prev_addr = result.addr; | ||
| count++; | ||
| } | ||
| TEST_ASSERT_EQUAL(expected, count); | ||
| } | ||
|
|
||
|
|
||
| static void test_free_and_reallocate(void) { | ||
| memory_area_t first; | ||
| memory_area_t second; | ||
| error_t err = pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &first); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err); | ||
| error_t err2 = pmallocator_free(first, g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err2); | ||
| error_t err3 = pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &second); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err3); | ||
| error_t err4 = pmallocator_free(second, g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err4); | ||
| TEST_ASSERT_EQUAL(first.addr, second.addr); | ||
| } | ||
|
|
||
| static void test_free_double_free(void) { | ||
| memory_area_t result; | ||
| (void)pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &result); | ||
| error_t err = pmallocator_free(result, g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err); | ||
| error_t err2 = pmallocator_free(result, g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_NOT_VALID, err2); | ||
| } | ||
|
|
||
| static void test_free_out_of_bounds(void) { | ||
| memory_area_t bad = { .addr = g_area.addr - PAGE_SIZE, .size = PAGE_SIZE }; | ||
| error_t err = pmallocator_free(bad, g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_OUT_OF_BOUNDS, err); | ||
| } | ||
|
|
||
| static void test_free_not_allocated(void) { | ||
| memory_area_t not_allocated = { .addr = g_area.addr + 2 * PAGE_SIZE, .size = PAGE_SIZE }; | ||
| error_t err = pmallocator_free(not_allocated, g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_NOT_VALID, err); | ||
| } | ||
|
|
||
|
|
||
| static void test_full_interleaved(void) { | ||
| memory_area_t a; | ||
| memory_area_t b; | ||
| memory_area_t c; | ||
| memory_area_t d; | ||
| (void)pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &a); | ||
| (void)pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &b); | ||
| (void)pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &c); | ||
| error_t err = pmallocator_free(b, g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err); | ||
| (void)pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &d); | ||
| TEST_ASSERT_EQUAL(b.addr, d.addr); | ||
| } | ||
|
|
||
| static void test_full_cycle(void) { | ||
| size_t expected = TEST_PAGES - TOTAL_RESERVED_PAGES - g_header_pages; | ||
| memory_area_t results[TEST_PAGES]; | ||
| size_t count = 0; | ||
|
|
||
| while (pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &results[count]) == ERR_NONE) { | ||
| count++; | ||
| } | ||
|
|
||
| for (size_t i = 0; i < count; i++) { | ||
| error_t err = pmallocator_free(results[i], g_header_region); | ||
| TEST_ASSERT_EQUAL(ERR_NONE, err); | ||
| } | ||
|
|
||
| size_t count2 = 0; | ||
| memory_area_t tmp; | ||
| while (pmallocator_allocate(FRAME_ORDER_4KiB, g_header_region, &tmp) == ERR_NONE) { | ||
| count2++; | ||
| } | ||
|
|
||
| TEST_ASSERT_EQUAL(expected, count); | ||
| TEST_ASSERT_EQUAL(count, count2); | ||
| } | ||
|
|
||
|
|
||
| int main(void) { | ||
| UNITY_BEGIN(); | ||
| RUN_TEST(test_allocate_single_page); | ||
| RUN_TEST(test_allocate_until_full); | ||
| RUN_TEST(test_free_and_reallocate); | ||
| RUN_TEST(test_free_double_free); | ||
| RUN_TEST(test_free_out_of_bounds); | ||
| RUN_TEST(test_free_not_allocated); | ||
| RUN_TEST(test_full_interleaved); | ||
| RUN_TEST(test_full_cycle); | ||
| return UNITY_END(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.