Files
XCEngine/tests/memory/test_linear_allocator.cpp
ssdfasd 34c75e7129 feat: 实现Containers、Memory、Threading核心模块及单元测试
- Containers: String, Array, HashMap 容器实现及测试
- Memory: Allocator, LinearAllocator, PoolAllocator, ProxyAllocator, MemoryManager 实现及测试
- Threading: Mutex, SpinLock, ReadWriteLock, Thread, Task, TaskSystem 实现及测试
- 修复Windows平台兼容性: _aligned_malloc, std::hash特化
- 修复构建错误和测试用例问题
2026-03-13 20:37:08 +08:00

78 lines
1.7 KiB
C++

#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