Skip to content

Commit

Permalink
Fix/add allocator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterymath committed Jan 26, 2024
1 parent a87117d commit 42908db
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
26 changes: 26 additions & 0 deletions SingleSource/UnitTests/aligned_alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#undef NDEBUG
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>

int main() {
void* align_8 = aligned_alloc(8, 1);
assert(align_8);
assert((uintptr_t)align_8 % 8 == 0);

void* align_16 = aligned_alloc(16, 1);
assert(align_16);
assert((uintptr_t)align_16 % 16 == 0);

free(align_8);
void* align_2 = aligned_alloc(2, 1);
assert(align_2);
assert((uintptr_t)align_8 % 2 == 0);

void* align_1 = aligned_alloc(1, 42);
assert(align_1);

// Alignment must be a power of two
void* align_3 = aligned_alloc(3, 1);
assert(!align_3);
}
1 change: 1 addition & 0 deletions SingleSource/UnitTests/aligned_alloc.reference_output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit 0
2 changes: 1 addition & 1 deletion SingleSource/UnitTests/new_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int main() {

const auto new_limit = ::heap_limit() + 8192;
::set_heap_limit(new_limit);
for (; i < 1000; i += 1) {
for (; i < ALLOC_COUNT; i += 1) {
vector_of_ptrs[i] = new std::size_t{i};
}

Expand Down
10 changes: 5 additions & 5 deletions SingleSource/UnitTests/realloc_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// The amount of bytes used in overhead is an implementation detail.
// This amount might need to be adjusted if the implementation changes.
static constexpr size_t ALLOC_OVERHEAD = 32;
static constexpr size_t ALLOC_OVERHEAD = 16;

// test the allocation pattern typical of a vector.
static void testVectorAlloc() {
Expand Down Expand Up @@ -81,25 +81,25 @@ static void testAdjacentRealloc() {
}

if (__heap_bytes_used() > 13 + 15 + 42 + ALLOC_OVERHEAD) {
puts("UNEXPECTED MEM USAGE AFTER REALLOC");
printf("UNEXPECTED MEM USAGE AFTER REALLOC: %u", __heap_bytes_used());
abort();
}

free(allocLeft);
if (__heap_bytes_used() > 15 + 42 + ALLOC_OVERHEAD) {
puts("UNEXPECTED MEM USAGE AFTER FIRST FREE)");
printf("UNEXPECTED MEM USAGE AFTER FIRST FREE: %u", __heap_bytes_used());
abort();
}

free(moveMiddleAlloc);
if (__heap_bytes_used() > 15 + ALLOC_OVERHEAD) {
puts("UNEXPECTED MEM USAGE AFTER SECOND FREE");
printf("UNEXPECTED MEM USAGE AFTER SECOND FREE: %u", __heap_bytes_used());
abort();
}

free(allocRight);
if (__heap_bytes_used() > ALLOC_OVERHEAD) {
puts("MEM LEAKED IN TESTADJACENTREALLOC");
printf("MEM LEAKED IN TESTADJACENTREALLOC: %u", __heap_bytes_used());
abort();
}
}
Expand Down

0 comments on commit 42908db

Please sign in to comment.