Skip to content

Commit

Permalink
Refactor BumpAllocator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mempler committed Dec 16, 2023
1 parent 3b58e86 commit c65b475
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/runtime/tests/Allocators/BumpAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ TEST_CASE ("AllocatePages", "[BumpAllocator]") {

RtlBumpAllocatorInitialize (&allocator);

//
// Setup code
//
allocator.bumpers[0].heap_start = (UINTN) new UINT8[PAGE_SIZE_4K];
allocator.bumpers[0].heap_end = allocator.bumpers[0].heap_start + PAGE_SIZE_4K;
allocator.bumpers[0].next = allocator.bumpers[0].heap_start;
Expand All @@ -29,11 +31,6 @@ TEST_CASE ("AllocatePages", "[BumpAllocator]") {
assert(status == STATUS_NOT_INITIALIZED);
}

SECTION("Make sure we cannot free while using NULL as Self") {
STATUS status = allocator.FreePages(NULL, NULL, 1);
assert(status == STATUS_NOT_INITIALIZED);
}

SECTION ("Allocates 4K page") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE (&allocator, &address, 1, PAGE_SIZE_4K);
Expand Down Expand Up @@ -118,7 +115,38 @@ TEST_CASE ("AllocatePages", "[BumpAllocator]") {
REQUIRE (address == NULL);
}

// Just in case
//
// Cleanup code
//
delete[] (UINT8*)allocator.bumpers[0].heap_start;
delete[] (UINT8*)allocator.bumpers[1].heap_start;
}

// Just in case
TEST_CASE ("FreePages", "[BumpAllocator]") {
BUMP_ALLOCATOR allocator { };

SECTION("Make sure allocator cannot be NULL") {
STATUS status = RtlBumpAllocatorInitialize (NULL);
REQUIRE (status == STATUS_INVALID_PARAMETER);
}

RtlBumpAllocatorInitialize (&allocator);

// Setup code
allocator.bumpers[0].heap_start = (UINTN) new UINT8[PAGE_SIZE_4K];
allocator.bumpers[0].heap_end = allocator.bumpers[0].heap_start + PAGE_SIZE_4K;
allocator.bumpers[0].next = allocator.bumpers[0].heap_start;

allocator.bumpers[1].heap_start = (UINTN) new UINT8[PAGE_SIZE_4K * 2];
allocator.bumpers[1].heap_end = allocator.bumpers[1].heap_start + PAGE_SIZE_4K * 2;
allocator.bumpers[1].next = allocator.bumpers[1].heap_start;

SECTION("Make sure we cannot free while using NULL as Self") {
STATUS status = allocator.FreePages(NULL, NULL, 1);
assert(status == STATUS_NOT_INITIALIZED);
}

SECTION("Free should always succeed") {
VOID* address = NULL;
STATUS status = PA_ALLOCATE(&allocator, &address, 1, PAGE_SIZE_4K);
Expand All @@ -133,4 +161,4 @@ TEST_CASE ("AllocatePages", "[BumpAllocator]") {
STATUS status = PA_FREE(&allocator, NULL, 1);
REQUIRE(status == STATUS_INVALID_PARAMETER);
}
}
}

0 comments on commit c65b475

Please sign in to comment.