diff --git a/Worksheets/Worksheet2/Task 2/unitTest.cpp b/Worksheets/Worksheet2/Task 2/unitTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9938d7206df46d61796653a9c8e6643915e0350 --- /dev/null +++ b/Worksheets/Worksheet2/Task 2/unitTest.cpp @@ -0,0 +1,86 @@ +#include "allocator.hpp" +#include "simpletest/simpletest.h" +#include <iostream> + +using namespace std; + +char const* groups[] = { + "Bump", +}; + +// Test 1 - Basic Allocation and Deallocation +DEFINE_TEST_G(BumpBasicTest, Bump) { + std::cout << "Test 1 - basic allocation and deallocation: " << std::endl; + bAllocator allocator(64); // 64 byte + int *x = allocator.alloc<int>(4); // 16 byte + TEST_MESSAGE(x != nullptr, "Allocation Failed for 4 ints"); + + allocator.dealloc(); + allocator.dealloc(); +} + +// Test 2 - Full Allocation +DEFINE_TEST_G(BumpFullAllocationTest, Bump) { + std::cout << "Test 2 - full allocation: " << std::endl; + bAllocator allocator(16); // 16 byte + + int *a = allocator.alloc<int>(3); // 12 byte + TEST_MESSAGE(a != nullptr, "Failed to allocate 3 ints."); + + char *b = allocator.alloc<char>(4); // 4 byte + TEST_MESSAGE(b != nullptr, "Failed to allocate 4 int"); +} + +// Test 3 - Mixed type allocation +DEFINE_TEST_G(BumpMixedTypesTest, Bump) { + std::cout << "Test 3 - mixed type allocation: " << std::endl; + bAllocator allocator(32); //32 byte + + int *x = allocator.alloc<int>(2); // 8 byte + char *y = allocator.alloc<char>(8); // 8 byte + double *z = allocator.alloc<double>(1); // 8 byte + + TEST_MESSAGE(x != nullptr, "Failed to allocate 2 ints."); + TEST_MESSAGE(y != nullptr, "Failed to allocate 10 chars."); + TEST_MESSAGE(z != nullptr, "Failed to allocate double."); +} + +// Test 4 - Reset when Deallocation is complete +DEFINE_TEST_G(BumpResetOnDeallocationTest, Bump) { + std::cout << "Test 4 - reset when deallocated: " << std::endl; + bAllocator allocator(32); // 32 byte + int *x = allocator.alloc<int>(3); // 12 byte + char *y = allocator.alloc<char>(4); // 4 byte + + TEST_MESSAGE(x != nullptr, "Failed to allocate 4 ints."); + TEST_MESSAGE(y != nullptr, "Failed to alllocate 6 chars."); + + allocator.dealloc(); + allocator.dealloc(); // frees up heap + + int *z = allocator.alloc<int>(6); // 24 byte + TEST_MESSAGE(z != nullptr, "Allocator failed to reset."); +} + +// Test 5 - Over capacity of allocation +DEFINE_TEST_G(BumpOverCapacityTest, Bump) { + std::cout << "Test 5 - Over capacity of allocation: " << std::endl; + bAllocator allocator(8); // 8 byte + + int *x = allocator.alloc<int>(1); // 4 byte + int *y = allocator.alloc<int>(1); // 4 byte + int *z = allocator.alloc<int>(1); // 4 byte - should fail + + TEST_MESSAGE(x != nullptr, "First allocation failure."); + TEST_MESSAGE(y != nullptr, "Second allocation failure."); + TEST_MESSAGE(z == nullptr, "Third allocation failure."); + +} + +int main() { + bool pass = true; + for (auto group : groups) { + pass &= TestFixture::ExecuteTestGroup(group, TestFixture::Verbose); + } + return pass ? 0 : 1; +} \ No newline at end of file