Use UnitTest-Cpp library for testing

This commit is contained in:
Ilusha 2024-03-15 16:03:33 +03:00 committed by Ilya Shurupov
parent 79d018ffb4
commit b8bcf58a29
53 changed files with 1046 additions and 1383 deletions

View file

@ -11,5 +11,5 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Utils)
enable_testing()
file(GLOB TEST_SOURCES "./tests/*.cpp")
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils)
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} UnitTest++)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)

View file

@ -13,7 +13,10 @@ using namespace tp;
// ----------------------- Release Implementation ---------------------------- //
void* HeapAlloc::allocate(ualni aBlockSize) { return malloc(aBlockSize); }
void HeapAlloc::deallocate(void* aPtr) { free(aPtr); }
void HeapAlloc::deallocate(void* aPtr) {
if (!aPtr) return;
free(aPtr);
}
HeapAlloc::~HeapAlloc() {}
#else
@ -45,6 +48,8 @@ void* HeapAlloc::allocate(ualni aBlockSize) {
}
void HeapAlloc::deallocate(void* aPtr) {
if (!aPtr) return;
auto head = ((MemHeadLocal*) (aPtr)) - 1;
mNumAllocations--;
@ -60,7 +65,7 @@ void HeapAlloc::deallocate(void* aPtr) {
HeapAlloc::~HeapAlloc() {
if (mNumAllocations) {
DEBUG_BREAK("Destruction of not freed Allocator")
DEBUG_ASSERT(0 && "Destruction of not freed Allocator")
#ifdef MEM_STACK_TRACE
// TODO : log leaks and free them up

View file

@ -16,7 +16,10 @@ using namespace tp;
// ----------------------- Release Implementation ---------------------------- //
void* HeapAllocGlobal::allocate(ualni aBlockSize) { return malloc(aBlockSize); }
void HeapAllocGlobal::deallocate(void* aPtr) { free(aPtr); }
void HeapAllocGlobal::deallocate(void* aPtr) {
if (!aPtr) return;
free(aPtr);
}
HeapAllocGlobal::~HeapAllocGlobal() = default;
bool HeapAllocGlobal::checkLeaks() { return false; }
void HeapAllocGlobal::startIgnore() {}

View file

@ -132,6 +132,7 @@ namespace tp {
}
void deallocate(void* aPtr) {
if (!aPtr) return;
auto chunk = mChunks.find(aPtr);
(*chunk)->deallocate(aPtr);
if ((*chunk)->isEmpty()) {

View file

@ -1,12 +1,9 @@
#include "Testing.hpp"
#include "UnitTest++/UnitTest++.h"
#include "Allocators.hpp"
#include "Utils.hpp"
#include "ChunkAllocator.hpp"
#include "HeapAllocator.hpp"
#include "HeapAllocatorGlobal.hpp"
#include "PoolAllocator.hpp"
#include <cmath>
using namespace tp;
@ -80,7 +77,7 @@ private:
if (mIsLoaded[idx]) return;
verifyIntegrity();
mLoaded[idx] = new (mAlloc.allocate(sizeof(TestStruct))) TestStruct(mData[idx]);
TEST(mLoaded[idx]);
ASSERT(mLoaded[idx]);
mIsLoaded[idx] = true;
mLoadedNum++;
verifyIntegrity();
@ -181,7 +178,7 @@ private:
uint1 val = *address;
*address = 5;
TEST(!mAlloc.checkWrap());
ASSERT(!mAlloc.checkWrap());
*address = val;
}
@ -208,38 +205,45 @@ void testAlloc() {
TestBenches<size, Alloc> heapTests{};
heapTests.runTests();
} catch (...) {
TEST(false);
ASSERT(false);
}
}
TEST_DEF_STATIC(GlobalHeap) { testAlloc<HeapAllocGlobal>(); }
SUITE(Allocators) {
TEST(GlobalHeap) { testAlloc<HeapAllocGlobal>(); }
TEST_DEF_STATIC(Heap) { testAlloc<tp::HeapAlloc>(); }
TEST(Heap) { testAlloc<tp::HeapAlloc>(); }
TEST_DEF_STATIC(Chunk) {
testAlloc<ChunkAlloc<TestStruct, size>>();
testAlloc<ChunkAlloc<TestStruct, size * 2>>();
TEST(Chunk) {
testAlloc<ChunkAlloc<TestStruct, size>>();
testAlloc<ChunkAlloc<TestStruct, size * 2>>();
}
TEST(Pool) {
testAlloc<PoolAlloc<TestStruct, 1>>();
testAlloc<PoolAlloc<TestStruct, size / 100>>();
testAlloc<PoolAlloc<TestStruct, size>>();
}
TEST(Simple) {
auto a = new TestStruct(-1);
delete a;
}
}
TEST_DEF_STATIC(Pool) {
testAlloc<PoolAlloc<TestStruct, 1>>();
testAlloc<PoolAlloc<TestStruct, size / 100>>();
testAlloc<PoolAlloc<TestStruct, size>>();
}
TEST_DEF_STATIC(Simple) {
auto a = new TestStruct(-1);
delete a;
}
int main() {
TEST_DEF(All) {
testSimple();
tp::ModuleManifest* deps[] = { &tp::gModuleAllocators, &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("AllocatorsTest", nullptr, nullptr, deps);
testGlobalHeap();
testHeap();
testChunk();
testPool();
if (!testModule.initialize()) {
return 1;
}
// TEST(HeapAllocGlobal::checkLeaks());
// TEST(false);
bool res = UnitTest::RunAllTests();
testModule.deinitialize();
return res;
}

View file

@ -1,20 +0,0 @@
#include "Allocators.hpp"
#include "Tests.hpp"
using namespace tp;
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleAllocators, &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("AllocatorsTest", nullptr, nullptr, deps);
if (!testModule.initialize()) {
return 1;
}
testAll();
testModule.deinitialize();
}

View file

@ -1,6 +0,0 @@
#pragma once
#include "Testing.hpp"
#include "Utils.hpp"
void testAll();