diff --git a/Worksheet2/test2.cpp b/Worksheet2/test2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79447be1136ed975c754e7ae408abffb361a72d8 --- /dev/null +++ b/Worksheet2/test2.cpp @@ -0,0 +1,40 @@ +#include <iostream> +#include <cstddef> +#include "bumpalld.hpp" // Include the BumpAllocator class definition + +// Simple Test Framework +#define TEST_MESSAGE(condition, message) \ + if (!(condition)) { \ + std::cout << "Test failed: " << message << std::endl; \ + return 1; \ + } \ + else { \ + std::cout << "Test passed: " << #condition << std::endl; \ + } + +int BumpTest() { + std::cout << "Running BumpTest..." << std::endl; + + BumpAllocator bumper(20 * sizeof(int)); // Create an allocator with space for 20 integers + + int *x = static_cast<int*>(bumper.allocate(10 * sizeof(int))); // Allocate space for 10 integers + TEST_MESSAGE(x != nullptr, "Failed to allocate 10 integers"); + + int *y = static_cast<int*>(bumper.allocate(10 * sizeof(int))); // Allocate space for another 10 integers + TEST_MESSAGE(y != nullptr, "Failed to allocate another 10 integers"); + + int *z = static_cast<int*>(bumper.allocate(10 * sizeof(int))); // Try to allocate space for 10 more integers + TEST_MESSAGE(z == nullptr, "Should have failed to allocate 10 more integers"); + + return 0; // Return 0 to indicate success +} + +int main() { + int result = BumpTest(); + if (result != 0) { + std::cerr << "Some tests failed." << std::endl; + return 1; + } + std::cout << "All tests passed!" << std::endl; + return 0; +}