#include #include using namespace XCEngine::Memory; namespace { TEST(PoolAllocator, Allocate) { PoolAllocator allocator(64, 10); void* ptr = allocator.Allocate(32); ASSERT_NE(ptr, nullptr); EXPECT_EQ(allocator.GetFreeBlockCount(), 9u); } TEST(PoolAllocator, AllocateTooLarge) { PoolAllocator allocator(64, 10); void* ptr = allocator.Allocate(128); EXPECT_EQ(ptr, nullptr); EXPECT_EQ(allocator.GetFreeBlockCount(), 10u); } TEST(PoolAllocator, Free) { PoolAllocator allocator(64, 10); void* ptr = allocator.Allocate(32); ASSERT_NE(ptr, nullptr); EXPECT_EQ(allocator.GetFreeBlockCount(), 9u); allocator.Free(ptr); EXPECT_EQ(allocator.GetFreeBlockCount(), 10u); } TEST(PoolAllocator, AllocateAllBlocks) { PoolAllocator allocator(64, 5); void* blocks[5]; for (int i = 0; i < 5; ++i) { blocks[i] = allocator.Allocate(32); ASSERT_NE(blocks[i], nullptr); } EXPECT_EQ(allocator.GetFreeBlockCount(), 0u); void* extra = allocator.Allocate(32); EXPECT_EQ(extra, nullptr); } TEST(PoolAllocator, ReuseFreedBlocks) { PoolAllocator allocator(64, 3); void* ptr1 = allocator.Allocate(32); void* ptr2 = allocator.Allocate(32); void* ptr3 = allocator.Allocate(32); allocator.Free(ptr2); EXPECT_EQ(allocator.GetFreeBlockCount(), 1u); void* ptr4 = allocator.Allocate(32); EXPECT_EQ(ptr4, ptr2); } TEST(PoolAllocator, GetBlockSize) { PoolAllocator allocator(64, 10); EXPECT_EQ(allocator.GetBlockSize(), 64u); } TEST(PoolAllocator, GetTotalBlockCount) { PoolAllocator allocator(64, 10); EXPECT_EQ(allocator.GetTotalBlockCount(), 10u); } TEST(PoolAllocator, Contains) { PoolAllocator allocator(64, 10); void* ptr = allocator.Allocate(32); ASSERT_NE(ptr, nullptr); EXPECT_TRUE(allocator.Contains(ptr)); } } // namespace