This commit is contained in:
Ilusha 2024-03-15 23:42:10 +03:00
parent 00d8fa0886
commit 65cb26a627
34 changed files with 288 additions and 264 deletions

View file

@ -8,8 +8,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC ./public/)
target_link_libraries(${PROJECT_NAME} PUBLIC Modules)
### -------------------------- Tests -------------------------- ###
enable_testing()
file(GLOB TEST_SOURCES "./tests/*.cpp")
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Allocators UnitTest++)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)
add_executable(Tests${PROJECT_NAME} ${TEST_SOURCES})
target_link_libraries(Tests${PROJECT_NAME} ${PROJECT_NAME} UnitTest++)
add_test(NAME Tests${PROJECT_NAME} COMMAND Tests${PROJECT_NAME})

View file

@ -5,9 +5,6 @@
namespace tp {
static ModuleManifest* sModuleDependencies[] = { &gModuleBase, nullptr };
ModuleManifest gModuleContainers = ModuleManifest("Containers", nullptr, nullptr, sModuleDependencies);
void* DefaultAllocator::allocate(ualni size) { return malloc(size); }
void DefaultAllocator::deallocate(void* p) { free(p); }

View file

@ -13,7 +13,12 @@ namespace tp {
class Buffer2D {
public:
typedef ualni Index;
typedef Pair<Index, Index> Index2D;
struct Index2D {
Index x = 0;
Index y = 0;
};
typedef SelectValueOrReference<tType> tTypeArg;
private:

View file

@ -4,7 +4,6 @@
#include "Module.hpp"
namespace tp {
extern ModuleManifest gModuleContainers;
class DefaultAllocator {
public:

View file

@ -164,7 +164,6 @@ namespace tp {
public:
Map() {
MODULE_SANITY_CHECK(gModuleContainers)
mNSlots = next2pow(uhalni(tTableInitialSize - 1));
mTable = newTable(mNSlots);
}
@ -243,7 +242,13 @@ namespace tp {
if (this == &in) {
return *this;
}
removeAll();
for (ualni i = 0; i < mNSlots; i++) {
if (mTable[i] && !isDeletedNode(mTable[i])) {
deleteNode(mTable[i]);
}
}
mNEntries = 0;
mNSlots = in.mNSlots;
deleteTable(mTable);
mTable = newTable(mNSlots);

View file

@ -62,7 +62,7 @@ namespace tp {
};
public:
AvlTree() { MODULE_SANITY_CHECK(gModuleContainers) }
AvlTree() {}
~AvlTree() { removeAll(); }
[[nodiscard]] ualni size() const { return mSize; }

View file

@ -1,15 +1,13 @@
#include "Buffer2D.hpp"
#include "Tests.hpp"
#include "HeapAllocator.hpp"
using namespace tp;
const ualni size = 1000;
SUITE(Buffer2D) {
TEST(Simple) {
Buffer2D<int, tp::HeapAlloc> buff;
Buffer2D<int, TestAllocator> buff;
buff.reserve({ 4, 4 });
buff.set({ 2, 2 }, 5);
CHECK(buff.get({ 2, 2 }) == 5);

View file

@ -7,7 +7,7 @@ const ualni size = 1000;
SUITE(Buffer) {
TEST(Simple1) {
Buffer<TestClass, tp::HeapAlloc> buff;
Buffer<TestClass, TestAllocator> buff;
CHECK(buff.size() == 0);
for (auto i : Range(size * 10)) {
buff.append(TestClass(i));
@ -19,7 +19,7 @@ SUITE(Buffer) {
}
TEST(Simple2) {
Buffer<TestClass, tp::HeapAlloc> buff(size);
Buffer<TestClass, TestAllocator> buff(size);
CHECK(buff.size() == size);
for (auto i : Range(size * 10))
buff.append(TestClass(i));

View file

@ -224,7 +224,7 @@ SUITE(IntervalTree) {
ualni numFound = 0;
ualni lastDataFound = 0;
[[nodiscard]] bool isSingleData(ualni aData) const { return numFound == 1 & lastDataFound == aData; }
[[nodiscard]] bool isSingleData(ualni aData) const { return numFound == 1 && lastDataFound == aData; }
[[nodiscard]] bool notFound() const { return numFound == 0; }
[[nodiscard]] bool found(ualni num) const { return numFound == num; }
};

View file

@ -7,7 +7,7 @@ using namespace tp;
SUITE(DoubleLinkedList) {
TEST(SimpleReference) {
tp::List<TestClass, tp::HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
list.pushBack(TestClass(5));
list.pushFront(TestClass(0));
@ -24,7 +24,9 @@ SUITE(DoubleLinkedList) {
}
TEST(SimplePointer) {
tp::List<TestClass*, tp::HeapAlloc> list = { new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4) };
tp::List<TestClass*, TestAllocator> list = {
new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4)
};
list.pushBack(new TestClass(5));
list.pushFront(new TestClass(0));
@ -45,8 +47,8 @@ SUITE(DoubleLinkedList) {
}
TEST(Copy) {
tp::List<TestClass, tp::HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, tp::HeapAlloc> list2 = list;
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, TestAllocator> list2 = list;
CHECK(list == list2);
@ -55,7 +57,7 @@ SUITE(DoubleLinkedList) {
}
TEST(Serialization) {
tp::List<TestClass, tp::HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
ArchiverExample<1024, false> write;
ArchiverExample<1024, true> read;

View file

@ -6,7 +6,7 @@ using namespace tp;
SUITE(HashTable) {
TEST(SimpleReference) {
tp::Map<tp::ualni, TestClass, tp::HeapAlloc> map;
tp::Map<tp::ualni, TestClass, TestAllocator> map;
for (auto i : Range(1000, 100000)) {
map.put(i, TestClass(i));
@ -40,7 +40,7 @@ SUITE(HashTable) {
}
TEST(SimplePointer) {
tp::Map<tp::ualni, TestClass*, tp::HeapAlloc> map;
tp::Map<tp::ualni, TestClass*, TestAllocator> map;
for (auto i : Range(1000)) {
map.put(i, new TestClass(i));
@ -78,13 +78,13 @@ SUITE(HashTable) {
}
TEST(Copy) {
tp::Map<tp::ualni, TestClass, tp::HeapAlloc> map;
tp::Map<tp::ualni, TestClass, TestAllocator> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));
}
tp::Map<tp::ualni, TestClass, tp::HeapAlloc> map2 = map;
tp::Map<tp::ualni, TestClass, TestAllocator> map2 = map;
CHECK(map == map2);
@ -93,7 +93,7 @@ SUITE(HashTable) {
}
TEST(SaveLoad) {
tp::Map<tp::ualni, TestClass, tp::HeapAlloc> map;
tp::Map<tp::ualni, TestClass, TestAllocator> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));

View file

@ -2,18 +2,23 @@
#include "Tests.hpp"
#include "UnitTest++/UnitTest++.h"
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, &tp::gModuleAllocators, nullptr };
tp::ModuleManifest testModule("ContainersTest", nullptr, nullptr, deps);
if (!testModule.initialize()) {
return 1;
}
bool res = UnitTest::RunAllTests();
testModule.deinitialize();
return res;
void* TestAllocator::allocate(tp::ualni size) {
count++;
return malloc(size);
}
void TestAllocator::deallocate(void* p) {
if (p) {
free(p);
count--;
}
}
TestAllocator::~TestAllocator() {
ASSERT(!count);
}
int main() {
return UnitTest::RunAllTests();
}

View file

@ -2,9 +2,20 @@
#include "UnitTest++/UnitTest++.h"
#include "Allocators.hpp"
#include "Utils.hpp"
// counting number of allocations and deallocations
struct TestAllocator {
TestAllocator() { count = 0; };
tp::ualni count = 0;
void* allocate(tp::ualni size);
void deallocate(void*);
~TestAllocator();
};
class TestClass {
tp::ualni val2 = 0;
tp::ualni val1;

View file

@ -5,7 +5,7 @@ using namespace tp;
SUITE(AvlTree) {
TEST(Simple) {
AvlTree<AvlNumericKey<alni>, TestClass, tp::HeapAlloc> tree;
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
CHECK(tree.size() == 0);
CHECK(tree.head() == nullptr);
@ -22,7 +22,7 @@ SUITE(AvlTree) {
}
TEST(Persistance) {
AvlTree<AvlNumericKey<alni>, TestClass, tp::HeapAlloc> tree;
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
const auto size = 1000;