This commit is contained in:
IlushaShurupov 2023-07-16 18:28:24 +03:00 committed by Ilya Shurupov
parent f91b45643d
commit 53aa0e34a8
24 changed files with 116 additions and 110 deletions

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());