From fa7631214b19b12a0cbdcfd0ff347542c64f6746 Mon Sep 17 00:00:00 2001 From: j2-tulloch <james2.tulloch@live.uwe.ac.uk> Date: Wed, 20 Dec 2023 21:00:44 +0000 Subject: [PATCH] Upload test1 --- Worksheet2/test1.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Worksheet2/test1.cpp diff --git a/Worksheet2/test1.cpp b/Worksheet2/test1.cpp new file mode 100644 index 0000000..24ce862 --- /dev/null +++ b/Worksheet2/test1.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <cstddef> +#include "bumpall.hpp" // Assuming BumpAllocator is defined in BumpAllocator.h + +int main() { + // Test case 1: Create an allocator and allocate some memory + std::cout << "Test case 1: Create an allocator and allocate some memory" << std::endl; + BumpAllocator allocator(100); // Create an allocator with 100 bytes + + void* ptr1 = allocator.allocate(50); // Allocate 50 bytes + if (ptr1 != nullptr) { + std::cout << "Allocation of 50 bytes succeeded." << std::endl; + } + + // Test case 2: Attempt to allocate more than remaining space + std::cout << "\nTest case 2: Attempt to allocate more than remaining space" << std::endl; + void* ptr2 = allocator.allocate(60); // Attempt to allocate 60 bytes, should fail + if (ptr2 == nullptr) { + std::cout << "Allocation of 60 bytes failed as expected." << std::endl; + } + + // Test case 3: Allocate remaining space + std::cout << "\nTest case 3: Allocate remaining space" << std::endl; + void* ptr3 = allocator.allocate(50); // Allocate the remaining 50 bytes + if (ptr3 != nullptr) { + std::cout << "Allocation of remaining 50 bytes succeeded." << std::endl; + } + + // Test case 4: Reset the allocator and allocate again + std::cout << "\nTest case 4: Reset the allocator and allocate again" << std::endl; + allocator.reset(); // Reset the allocator + void* ptr4 = allocator.allocate(100); // Allocate 100 bytes again + if (ptr4 != nullptr) { + std::cout << "Allocation of 100 bytes after reset succeeded." << std::endl; + } + + return 0; +} -- GitLab