Files
XCEngine/tests/memory/test_linear_allocator.cpp

78 lines
1.7 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
#include <XCEngine/Memory/LinearAllocator.h>
#include <cstring>
using namespace XCEngine::Memory;
namespace {
TEST(LinearAllocator, Allocate) {
LinearAllocator allocator(1024);
void* ptr = allocator.Allocate(64);
ASSERT_NE(ptr, nullptr);
EXPECT_EQ(allocator.GetUsedSize(), 64u);
}
TEST(LinearAllocator, Allocate_Aligned) {
LinearAllocator allocator(1024);
void* ptr = allocator.Allocate(64, 16);
ASSERT_NE(ptr, nullptr);
uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
EXPECT_EQ(addr % 16, 0u);
}
TEST(LinearAllocator, Clear) {
LinearAllocator allocator(1024);
allocator.Allocate(64);
EXPECT_EQ(allocator.GetUsedSize(), 64u);
allocator.Clear();
EXPECT_EQ(allocator.GetUsedSize(), 0u);
}
TEST(LinearAllocator, Marker) {
LinearAllocator allocator(1024);
allocator.Allocate(64);
void* marker = allocator.GetMarker();
allocator.Allocate(32);
EXPECT_EQ(allocator.GetUsedSize(), 96u);
allocator.SetMarker(marker);
EXPECT_EQ(allocator.GetUsedSize(), 64u);
}
TEST(LinearAllocator, AllocateMultiple) {
LinearAllocator allocator(1024);
allocator.Allocate(100);
allocator.Allocate(200);
allocator.Allocate(300);
EXPECT_GE(allocator.GetUsedSize(), 600u);
}
TEST(LinearAllocator, OutOfMemory) {
LinearAllocator allocator(200);
void* ptr1 = allocator.Allocate(50, 8);
void* ptr2 = allocator.Allocate(50, 8);
ASSERT_NE(ptr1, nullptr);
ASSERT_NE(ptr2, nullptr);
void* ptr3 = allocator.Allocate(100, 8);
EXPECT_EQ(ptr3, nullptr);
}
TEST(LinearAllocator, GetCapacity) {
LinearAllocator allocator(512);
EXPECT_EQ(allocator.GetCapacity(), 512u);
}
} // namespace