#include #include /* Implement Pool here */ // Global variable used for testing bool has_been_destroyed { false }; struct X { ~X() { has_been_destroyed = true; } }; int main() { // Set block_size to 8 and count to 4 Pool p { 8, 4 }; // try to allocate objects auto ptr1 { p.allocate() }; auto ptr2 { p.allocate() }; auto ptr3 { p.allocate() }; // assign to these objets *ptr1 = 3; *ptr2 = 4.5; *ptr3 = '6'; // check that they all got the correct values assert( *ptr1 == 3 ); assert( *ptr2 == 4.5 ); assert( *ptr3 == '6' ); // Check that we are not using the same block for any of the objects assert( reinterpret_cast(ptr1) != reinterpret_cast(ptr2) ); assert( reinterpret_cast(ptr2) != reinterpret_cast(ptr3) ); // deallocate one object p.deallocate(ptr1); // allocate a new object, *after* a deallocation auto ptr4 = p.allocate(); *ptr4 = 6.5; assert( *ptr4 == 6.5 ); // make sure that the allocation re-uses that last removed address assert( reinterpret_cast(ptr4) == reinterpret_cast(ptr1) ); // remove another object p.deallocate(ptr2); // allocate a new object, *after* a deallocation auto ptr5 = p.allocate(); // make sure that the allocation re-uses that last removed address assert( reinterpret_cast(ptr5) == reinterpret_cast(ptr2) ); // Deallocating ptr5 should result in the X destructor being called p.deallocate(ptr5); // Ensure that the X destructor was called assert( has_been_destroyed ); }