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

@ -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;