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特化 - 修复构建错误和测试用例问题
This commit is contained in:
31
tests/memory/CMakeLists.txt
Normal file
31
tests/memory/CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
# ============================================================
|
||||
# Memory Library Tests
|
||||
# ============================================================
|
||||
|
||||
set(MEMORY_TEST_SOURCES
|
||||
test_memory_manager.cpp
|
||||
test_linear_allocator.cpp
|
||||
test_pool_allocator.cpp
|
||||
)
|
||||
|
||||
add_executable(xcengine_memory_tests ${MEMORY_TEST_SOURCES})
|
||||
|
||||
if(MSVC)
|
||||
set_target_properties(xcengine_memory_tests PROPERTIES
|
||||
LINK_FLAGS "/NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:libcmt.lib"
|
||||
)
|
||||
endif()
|
||||
|
||||
target_link_libraries(xcengine_memory_tests
|
||||
PRIVATE
|
||||
XCEngine
|
||||
GTest::gtest
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
target_include_directories(xcengine_memory_tests PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/engine/include
|
||||
${CMAKE_SOURCE_DIR}/tests/fixtures
|
||||
)
|
||||
|
||||
add_test(NAME MemoryTests COMMAND xcengine_memory_tests)
|
||||
77
tests/memory/test_linear_allocator.cpp
Normal file
77
tests/memory/test_linear_allocator.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#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
|
||||
44
tests/memory/test_memory_manager.cpp
Normal file
44
tests/memory/test_memory_manager.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <XCEngine/Memory/MemoryManager.h>
|
||||
|
||||
using namespace XCEngine::Memory;
|
||||
|
||||
namespace {
|
||||
|
||||
class MemoryTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
MemoryManager::Get().Initialize();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
MemoryManager::Get().Shutdown();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(MemoryTest, GetSystemAllocator_ReturnsValidPointer) {
|
||||
IAllocator* allocator = MemoryManager::Get().GetSystemAllocator();
|
||||
ASSERT_NE(allocator, nullptr);
|
||||
}
|
||||
|
||||
TEST_F(MemoryTest, CreateLinearAllocator) {
|
||||
auto allocator = MemoryManager::Get().CreateLinearAllocator(1024);
|
||||
ASSERT_NE(allocator, nullptr);
|
||||
EXPECT_EQ(allocator->GetCapacity(), 1024u);
|
||||
}
|
||||
|
||||
TEST_F(MemoryTest, CreatePoolAllocator) {
|
||||
auto allocator = MemoryManager::Get().CreatePoolAllocator(64, 100);
|
||||
ASSERT_NE(allocator, nullptr);
|
||||
EXPECT_EQ(allocator->GetBlockSize(), 64u);
|
||||
EXPECT_EQ(allocator->GetTotalBlockCount(), 100u);
|
||||
EXPECT_EQ(allocator->GetFreeBlockCount(), 100u);
|
||||
}
|
||||
|
||||
TEST_F(MemoryTest, CreateProxyAllocator) {
|
||||
auto allocator = MemoryManager::Get().CreateProxyAllocator("Test");
|
||||
ASSERT_NE(allocator, nullptr);
|
||||
EXPECT_STREQ(allocator->GetName(), "Test");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
83
tests/memory/test_pool_allocator.cpp
Normal file
83
tests/memory/test_pool_allocator.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <XCEngine/Memory/PoolAllocator.h>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user