This commit is contained in:
IlushaShurupov 2023-07-16 18:28:24 +03:00
parent fabceb52d3
commit bc892da992
24 changed files with 116 additions and 110 deletions

View file

@ -16,7 +16,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Modules)
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} Allocators)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/lib)

View file

@ -80,7 +80,7 @@ namespace tp {
}
explicit Buffer(ualni size) : mSize(size), mLoad(0) {
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * tMinSize);
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * size);
}
Buffer(const Buffer& in) : mSize(in.mSize), mLoad(in.mLoad) {

View file

@ -7,7 +7,10 @@ namespace tp {
template< typename tType, ualni tSizeX, ualni tSizeY >
using ConstSizeBuffer2D = ConstSizeBuffer<ConstSizeBuffer<tType, tSizeX>, tSizeY>;
template <typename tType>
template <
typename tType,
class tAllocator = DefaultAllocator
>
class Buffer2D {
public:
typedef ualni Index;
@ -15,15 +18,28 @@ namespace tp {
typedef SelCopyArg<tType> tTypeArg;
private:
tAllocator mAlloc;
tType* mBuff = nullptr;
Index2D mSize = { 0, 0 };
void deleteBuffer() {
if (!mBuff) return;
for (ualni i = 0; i < mSize.x * mSize.y; i++) mBuff[i].~tType();
mAlloc.deallocate(mBuff);
}
void allocateBuffer(Index2D size) {
deleteBuffer();
mBuff = (tType*) mAlloc.allocate(sizeof(tType) * size.x * size.y);
for (ualni i = 0; i < mSize.x * mSize.y; i++) new (mBuff + i) tType();
}
public:
Buffer2D() = default;
~Buffer2D() {
delete mBuff;
deleteBuffer();
}
explicit Buffer2D(Index2D aSize) {
@ -55,8 +71,7 @@ namespace tp {
void reserve(const Index2D& newSize) {
if (mSize.x != newSize.x || mSize.y != newSize.y) {
delete mBuff;
mBuff = new tType[newSize.x * newSize.y];
allocateBuffer(newSize);
mSize = newSize;
}
}

View file

@ -387,6 +387,13 @@ namespace tp {
}
}
~Map() { removeAll(); }
~Map() {
for (ualni i = 0; i < mNSlots; i++) {
if (mTable[i] && !isDeletedNode(mTable[i])) {
deleteNode(mTable[i]);
}
}
deleteTable(mTable);
}
};
}

View file

@ -1,17 +1,14 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include "Buffer2D.hpp"
#include <iostream>
using namespace tp;
const ualni size = 1000;
TEST_DEF_STATIC(Simple1) {
Buffer2D<int> buff;
Buffer2D<int, HeapAlloc> buff;
buff.reserve({ 4, 4 });
buff.set( { 2, 2 }, 5);
TEST(buff.get( { 2, 2 } ) == 5);

View file

@ -1,18 +1,14 @@
#include "Tests.hpp"
#include "Buffer.hpp"
#include "Testing.hpp"
#include <iostream>
using namespace tp;
const ualni size = 1000;
TEST_DEF_STATIC(Simple1) {
Buffer<TestClass> buff;
Buffer<TestClass, HeapAlloc> buff;
TEST(buff.size() == 0);
for (auto i : Range(size * 10)) {
buff.append(TestClass(i));
@ -23,7 +19,7 @@ TEST_DEF_STATIC(Simple1) {
}
TEST_DEF_STATIC(Simple2) {
Buffer<TestClass> buff(size);
Buffer<TestClass, HeapAlloc> buff(size);
TEST(buff.size() == 0);
for (auto i : Range(size * 10)) buff.append(TestClass(i));
TEST(buff.size() == size * 10);

View file

@ -2,12 +2,10 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include <iostream>
using namespace tp;
TEST_DEF_STATIC(SimpleReference) {
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
list.pushBack(TestClass(5));
list.pushFront(TestClass(0));
@ -21,11 +19,10 @@ TEST_DEF_STATIC(SimpleReference) {
TEST(i == 5);
list.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
}
TEST_DEF_STATIC(SimplePointer) {
tp::List<TestClass*, TestAllocator> list = { new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4) };
tp::List<TestClass*, HeapAlloc> list = { new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4) };
list.pushBack(new TestClass(5));
list.pushFront(new TestClass(0));
@ -38,26 +35,25 @@ TEST_DEF_STATIC(SimplePointer) {
TEST(i == 5);
list.removeAll();
for (auto iter : list) {
delete iter.data();
}
TEST(list.getAllocator().getAllocationsCount() == 0);
list.removeAll();
}
TEST_DEF_STATIC(Copy) {
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, TestAllocator> list2 = list;
tp::List<TestClass, HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, HeapAlloc> list2 = list;
TEST_EQUAL(list, list2);
list.removeAll();
list2.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
TEST(list2.getAllocator().getAllocationsCount() == 0);
}
TEST_DEF_STATIC(SaveLoad) {
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
TestFile file;
@ -77,8 +73,6 @@ TEST_DEF_STATIC(SaveLoad) {
TEST(i == 4);
list.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
}
TEST_DEF(List) {

View file

@ -1,15 +1,12 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include "Map.hpp"
#include <iostream>
using namespace tp;
TEST_DEF_STATIC(SimpleReference) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map;
for (auto i : Range(1000, 100000)) {
map.put(i, TestClass(i));
@ -40,12 +37,10 @@ TEST_DEF_STATIC(SimpleReference) {
}
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(SimplePointer) {
tp::Map<tp::ualni, TestClass*, TestAllocator> map;
tp::Map<tp::ualni, TestClass*, HeapAlloc> map;
for (auto i : Range(1000)) {
map.put(i, new TestClass(i));
@ -57,11 +52,14 @@ TEST_DEF_STATIC(SimplePointer) {
}
for (auto i : Range(1000)) {
auto del = map.get(i);
map.put(i, new TestClass(i));
delete del;
}
for (auto i : Range(900, 1000)) {
TEST(map.presents(i));
delete map.get(i);
map.remove(i);
TEST(!map.presents(i));
}
@ -77,30 +75,25 @@ TEST_DEF_STATIC(SimplePointer) {
}
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(Copy) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));
}
tp::Map<tp::ualni, TestClass, TestAllocator> map2 = map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map2 = map;
TEST_EQUAL(map, map2);
map.removeAll();
map2.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map2.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(SaveLoad) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, HeapAlloc> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));
@ -112,13 +105,13 @@ TEST_DEF_STATIC(SaveLoad) {
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map.size() == 0);
file.setAddress(0);
map.read(file);
TEST(map.getAllocator().getAllocationsCount() == 11);
TEST(map.size() == 10);
for (auto i : Range(10)) {
TEST(map.presents(i));
@ -126,8 +119,6 @@ TEST_DEF_STATIC(SaveLoad) {
}
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF(Map) {

View file

@ -1,32 +1,15 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include <cstdlib>
static bool init(const tp::ModuleManifest* self) {
tp::gTesting.setRootName(self->getName());
return true;
}
void* TestAllocator::allocate(tp::ualni size) {
nAllocations++;
return malloc(size);
}
void TestAllocator::deallocate(void* p) {
nAllocations--;
free(p);
}
tp::ualni TestAllocator::getAllocationsCount() const {
return nAllocations;
}
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleContainers, &tp::gModuleUtils, nullptr };
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, &tp::gModuleAllocators, nullptr };
tp::ModuleManifest testModule("ContainersTest", init, nullptr, deps);
if (!testModule.initialize()) {
@ -36,7 +19,7 @@ int main() {
testList();
testMap();
testAvl();
testBuffer();
testBuffer();
testBuffer2d();
testModule.deinitialize();

View file

@ -1,6 +1,7 @@
#pragma once
#include "Utils.hpp"
#include "Allocators.hpp"
class TestClass {
tp::ualni val2 = 0;
@ -27,15 +28,6 @@ public:
void setVal(tp::ualni val) { val1 = val; }
};
class TestAllocator {
tp::ualni nAllocations = 0;
public:
TestAllocator() = default;
void* allocate(tp::ualni size);
void deallocate(void* p);
[[nodiscard]] tp::ualni getAllocationsCount() const;
};
class TestFile {
tp::ualni mem[1024] = { 0 };
tp::ualni address = 0;

View file

@ -1,16 +1,12 @@
#include "Tests.hpp"
#include "Tree.hpp"
#include "Testing.hpp"
#include <iostream>
using namespace tp;
TEST_DEF_STATIC(Simple) {
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
AvlTree<AvlNumericKey<alni>, TestClass, HeapAlloc> tree;
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
@ -27,7 +23,7 @@ TEST_DEF_STATIC(Simple) {
}
TEST_DEF_STATIC(Persistance) {
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
AvlTree<AvlNumericKey<alni>, TestClass, HeapAlloc> tree;
const auto size = 1000;
@ -46,7 +42,7 @@ TEST_DEF_STATIC(Persistance) {
// random load
ualni loadSize = 0;
while (loadSize < size / 2) {
ualni idx = rand() % (size - 1);
auto idx = ualni(randomFloat() * (size - 1));
DEBUG_ASSERT(idx < size)
if (!buff[idx].presents) {
tree.insert((alni) buff[idx].data.getVal(), buff[idx].data);
@ -86,7 +82,7 @@ TEST_DEF_STATIC(Persistance) {
// random unload
ualni unloadSize = 0;
while (unloadSize < size / 2) {
ualni idx = rand() % (size - 1);
auto idx = ualni(randomFloat() * (size - 1));
if (buff[idx].presents) {
tree.remove((alni) buff[idx].data.getVal());