Skip to content
Snippets Groups Projects
Commit 923fd2e0 authored by j2-pelczar's avatar j2-pelczar :cowboy:
Browse files

Upload New File

parent 9e485bb8
No related branches found
No related tags found
No related merge requests found
#include <iostream>
#include <cassert>
#include "bump_allocator.hpp"
void testBumpAllocator() {
std::cout << "Running BumpAllocator tests...\n";
std::cout << "---------------------------------\n";
// Test 1: Create an allocator and allocate some memory
BumpAllocator allocator(1024);
std::cout << "[Test 1] Allocator created with 1024 bytes.\n";
// Test 2: Allocating memory
int* p1 = allocator.alloc<int>(10);
assert(p1 != nullptr && "Test 2 Failed: Allocation for integers failed!");
std::cout << "[Test 2] Allocated memory for 10 integers.\n";
// Test 3: Allocate more memory
double* p2 = allocator.alloc<double>(5);
assert(p2 != nullptr && "Test 3 Failed: Allocation for doubles failed!");
std::cout << "[Test 3] Allocated memory for 5 doubles.\n";
// Test 4: Check the status of the allocator
std::cout << "[Test 4] Checking allocator status:\n";
allocator.printStatus();
// Test 5: Deallocate memory partially
allocator.dealloc();
std::cout << "[Test 5] Deallocated memory (partial reset).\n";
allocator.printStatus();
// Test 6: Allocate again after deallocation
int* p3 = allocator.alloc<int>(20);
assert(p3 != nullptr && "Test 6 Failed: Allocation after deallocation failed!");
std::cout << "[Test 6] Allocated memory for 20 integers after deallocation.\n";
// Test 7: Exhaust the allocator
int* p4 = allocator.alloc<int>(1000);
assert(p4 == nullptr && "Test 7 Failed: Allocator should not allow exceeding capacity!");
std::cout << "[Test 7] Attempted to allocate beyond capacity (expected failure).\n";
// Test 8: Completely reset the allocator
allocator.dealloc();
std::cout << "[Test 8] Completely reset the allocator.\n";
allocator.printStatus();
assert(allocator.alloc<int>(128) != nullptr && "Test 8 Failed: Allocator should work after reset!");
std::cout << "[Test 8] Allocated memory after full reset.\n";
std::cout << "---------------------------------\n";
std::cout << "All tests passed successfully!\n";
}
int main() {
testBumpAllocator();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment