Formatting code

This commit is contained in:
Ilusha 2023-06-30 14:02:55 +00:00
parent 6dc64ce76b
commit 13a8f07e32
37 changed files with 2242 additions and 2281 deletions

View file

@ -10,122 +10,122 @@
using namespace tp;
TEST_DEF_STATIC(Simple) {
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
tree.insert(6, TestClass(6));
TEST(tree.isValid());
TEST(tree.size() == 1);
TEST(tree.head()->data == TestClass(6));
tree.insert(6, TestClass(6));
TEST(tree.isValid());
TEST(tree.size() == 1);
TEST(tree.head()->data == TestClass(6));
tree.remove(6);
TEST(tree.isValid());
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
tree.remove(6);
TEST(tree.isValid());
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
}
TEST_DEF_STATIC(Persistance) {
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
const auto size = 1000;
const auto size = 1000;
struct Item {
Item() : data(0) {}
bool presents = false;
TestClass data;
};
struct Item {
Item() : data(0) {}
bool presents = false;
TestClass data;
};
Item buff[size];
Item buff[size];
for (auto i : Range(size)) {
buff[i].data.setVal(i);
}
for (auto i : Range(size)) {
buff[i].data.setVal(i);
}
// random load
ualni loadSize = 0;
while (loadSize < size / 2) {
ualni idx = rand() % (size - 1);
DEBUG_ASSERT(idx < size)
if (!buff[idx].presents) {
tree.insert((alni) buff[idx].data.getVal(), buff[idx].data);
loadSize++;
buff[idx].presents = true;
// random load
ualni loadSize = 0;
while (loadSize < size / 2) {
ualni idx = rand() % (size - 1);
DEBUG_ASSERT(idx < size)
if (!buff[idx].presents) {
tree.insert((alni) buff[idx].data.getVal(), buff[idx].data);
loadSize++;
buff[idx].presents = true;
TEST(tree.isValid());
TEST(tree.size() == loadSize);
}
}
TEST(tree.isValid());
TEST(tree.size() == loadSize);
}
}
for (auto& item : buff) {
if (item.presents) continue;
tree.insert((alni) item.data.getVal(), item.data);
loadSize++;
item.presents = true;
for (auto& item : buff) {
if (item.presents) continue;
tree.insert((alni) item.data.getVal(), item.data);
loadSize++;
item.presents = true;
TEST(tree.isValid());
TEST(tree.size() == loadSize);
}
TEST(tree.isValid());
TEST(tree.size() == loadSize);
}
TEST(tree.size() == size);
TEST(tree.maxNode(tree.head())->data.getVal() == size - 1);
TEST(tree.minNode(tree.head())->data.getVal() == 0);
TEST(tree.size() == size);
TEST(tree.maxNode(tree.head())->data.getVal() == size - 1);
TEST(tree.minNode(tree.head())->data.getVal() == 0);
// find
for (auto item : buff) {
auto node = tree.find((alni) item.data.getVal());
TEST(node);
if (!node) continue;
TEST(node->data.getVal() == item.data.getVal());
}
// find
for (auto item : buff) {
auto node = tree.find((alni) item.data.getVal());
TEST(node);
if (!node) continue;
TEST(node->data.getVal() == item.data.getVal());
}
TEST(!tree.find(size + 1));
TEST(!tree.find(-1));
TEST(!tree.find(size + 1));
TEST(!tree.find(-1));
// random unload
ualni unloadSize = 0;
while (unloadSize < size / 2) {
ualni idx = rand() % (size - 1);
if (buff[idx].presents) {
// random unload
ualni unloadSize = 0;
while (unloadSize < size / 2) {
ualni idx = rand() % (size - 1);
if (buff[idx].presents) {
tree.remove((alni) buff[idx].data.getVal());
tree.remove((alni) buff[idx].data.getVal());
unloadSize++;
buff[idx].presents = false;
unloadSize++;
buff[idx].presents = false;
// find
for (auto item : buff) {
if (!item.presents) continue;
auto node = tree.find((alni) item.data.getVal());
TEST(node);
if (!node) continue;
TEST(node->data.getVal() == item.data.getVal());
}
// find
for (auto item : buff) {
if (!item.presents) continue;
auto node = tree.find((alni) item.data.getVal());
TEST(node);
if (!node) continue;
TEST(node->data.getVal() == item.data.getVal());
}
TEST(tree.isValid());
TEST(tree.size() == size - unloadSize);
}
}
TEST(tree.isValid());
TEST(tree.size() == size - unloadSize);
}
}
for (auto& item : buff) {
if (item.presents) {
tree.remove((alni) item.data.getVal());
unloadSize++;
item.presents = false;
for (auto& item : buff) {
if (item.presents) {
tree.remove((alni) item.data.getVal());
unloadSize++;
item.presents = false;
TEST(tree.isValid());
TEST(tree.size() == size - unloadSize);
}
}
TEST(tree.isValid());
TEST(tree.size() == size - unloadSize);
}
}
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
TEST(tree.maxNode(tree.head()) == nullptr);
TEST(tree.minNode(tree.head()) == nullptr);
TEST(tree.size() == 0);
TEST(tree.head() == nullptr);
TEST(tree.maxNode(tree.head()) == nullptr);
TEST(tree.minNode(tree.head()) == nullptr);
}
TEST_DEF(Avl) {
testSimple();
testPersistance();
testSimple();
testPersistance();
}

View file

@ -7,82 +7,82 @@
using namespace tp;
TEST_DEF_STATIC(SimpleReference) {
tp::List<TestClass, TestAllocator> 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));
list.pushBack(TestClass(5));
list.pushFront(TestClass(0));
ualni i = -1;
for (auto iter : list) {
i++;
TEST_EQUAL(iter->getVal(), i);
}
ualni i = -1;
for (auto iter : list) {
i++;
TEST_EQUAL(iter->getVal(), i);
}
TEST(i == 5);
TEST(i == 5);
list.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
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*, TestAllocator> list = { new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4) };
list.pushBack(new TestClass(5));
list.pushFront(new TestClass(0));
list.pushBack(new TestClass(5));
list.pushFront(new TestClass(0));
ualni i = -1;
for (auto iter : list) {
i++;
TEST_EQUAL(iter->getVal(), i);
}
ualni i = -1;
for (auto iter : list) {
i++;
TEST_EQUAL(iter->getVal(), i);
}
TEST(i == 5);
TEST(i == 5);
list.removeAll();
list.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
TEST(list.getAllocator().getAllocationsCount() == 0);
}
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, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
tp::List<TestClass, TestAllocator> list2 = list;
TEST_EQUAL(list, list2);
TEST_EQUAL(list, list2);
list.removeAll();
list2.removeAll();
list.removeAll();
list2.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
TEST(list2.getAllocator().getAllocationsCount() == 0);
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, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
TestFile file;
TestFile file;
list.write(file);
list.write(file);
list.removeAll();
list.removeAll();
file.setAddress(0);
file.setAddress(0);
list.read(file);
list.read(file);
ualni i = 0;
for (auto iter : list) {
i++;
TEST_EQUAL(iter->getVal(), i);
}
TEST(i == 4);
ualni i = 0;
for (auto iter : list) {
i++;
TEST_EQUAL(iter->getVal(), i);
}
TEST(i == 4);
list.removeAll();
list.removeAll();
TEST(list.getAllocator().getAllocationsCount() == 0);
TEST(list.getAllocator().getAllocationsCount() == 0);
}
TEST_DEF(List) {
testSimplePointer();
testSimpleReference();
testSaveLoad();
testSimplePointer();
testSimpleReference();
testSaveLoad();
}

View file

@ -9,129 +9,129 @@
using namespace tp;
TEST_DEF_STATIC(SimpleReference) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, TestAllocator> map;
for (auto i : Range(1000, 100000)) {
map.put(i, TestClass(i));
}
for (auto i : Range(1000, 100000)) {
map.put(i, TestClass(i));
}
for (auto i : Range(1000, 100000)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i).getVal(), i);
}
for (auto i : Range(1000, 100000)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i).getVal(), i);
}
for (auto i : Range(1000, 100000)) {
map.put(i, TestClass(i));
}
for (auto i : Range(1000, 100000)) {
map.put(i, TestClass(i));
}
for (auto i : Range(1000, 2000)) {
TEST(map.presents(i));
map.remove(i);
TEST(!map.presents(i));
}
for (auto i : Range(1000, 2000)) {
TEST(map.presents(i));
map.remove(i);
TEST(!map.presents(i));
}
for (auto i : Range(2000, 100000)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i).getVal(), i);
}
for (auto i : Range(2000, 100000)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i).getVal(), i);
}
for (auto i : map) {
i->val.setVal(3);
}
for (auto i : map) {
i->val.setVal(3);
}
map.removeAll();
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(SimplePointer) {
tp::Map<tp::ualni, TestClass*, TestAllocator> map;
tp::Map<tp::ualni, TestClass*, TestAllocator> map;
for (auto i : Range(1000)) {
map.put(i, new TestClass(i));
}
for (auto i : Range(1000)) {
map.put(i, new TestClass(i));
}
for (auto i : Range(1000)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i)->getVal(), i);
}
for (auto i : Range(1000)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i)->getVal(), i);
}
for (auto i : Range(1000)) {
map.put(i, new TestClass(i));
}
for (auto i : Range(1000)) {
map.put(i, new TestClass(i));
}
for (auto i : Range(900, 1000)) {
TEST(map.presents(i));
map.remove(i);
TEST(!map.presents(i));
}
for (auto i : Range(900, 1000)) {
TEST(map.presents(i));
map.remove(i);
TEST(!map.presents(i));
}
for (auto i : Range(900)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i)->getVal(), i);
}
for (auto i : Range(900)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i)->getVal(), i);
}
for (auto i : map) {
i->val->setVal(3);
delete i->val;
}
for (auto i : map) {
i->val->setVal(3);
delete i->val;
}
map.removeAll();
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF_STATIC(Copy) {
tp::Map<tp::ualni, TestClass, TestAllocator> map;
tp::Map<tp::ualni, TestClass, TestAllocator> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));
}
for (auto i : Range(10)) {
map.put(i, TestClass(i));
}
tp::Map<tp::ualni, TestClass, TestAllocator> map2 = map;
tp::Map<tp::ualni, TestClass, TestAllocator> map2 = map;
TEST_EQUAL(map, map2);
TEST_EQUAL(map, map2);
map.removeAll();
map2.removeAll();
map.removeAll();
map2.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map2.getAllocator().getAllocationsCount() == 1);
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, TestAllocator> map;
for (auto i : Range(10)) {
map.put(i, TestClass(i));
}
for (auto i : Range(10)) {
map.put(i, TestClass(i));
}
TestFile file;
TestFile file;
map.write(file);
map.write(file);
map.removeAll();
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map.getAllocator().getAllocationsCount() == 1);
file.setAddress(0);
file.setAddress(0);
map.read(file);
map.read(file);
TEST(map.getAllocator().getAllocationsCount() == 11);
TEST(map.getAllocator().getAllocationsCount() == 11);
for (auto i : Range(10)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i).getVal(), i);
}
for (auto i : Range(10)) {
TEST(map.presents(i));
TEST_EQUAL(map.get(i).getVal(), i);
}
map.removeAll();
map.removeAll();
TEST(map.getAllocator().getAllocationsCount() == 1);
TEST(map.getAllocator().getAllocationsCount() == 1);
}
TEST_DEF(Map) {
testSimplePointer();
testSimpleReference();
testSaveLoad();
testSimplePointer();
testSimpleReference();
testSaveLoad();
}

View file

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

View file

@ -3,69 +3,69 @@
#include "Utils.hpp"
class TestClass {
tp::ualni val2 = 0;
tp::ualni val1;
tp::ualni val2 = 0;
tp::ualni val1;
public:
explicit TestClass(tp::ualni val) : val1(val) {}
explicit TestClass(tp::ualni val) : val1(val) {}
template<class Saver>
void write(Saver& file) const {
file.write(val1);
}
template<class Saver>
void write(Saver& file) const {
file.write(val1);
}
template<class Loader>
void read(Loader& file) {
file.read(val1);
}
template<class Loader>
void read(Loader& file) {
file.read(val1);
}
[[nodiscard]] bool operator==(const TestClass& in) const {
return in.val1 == val1;
}
[[nodiscard]] bool operator==(const TestClass& in) const {
return in.val1 == val1;
}
[[nodiscard]] tp::ualni getVal() const { return val1; }
void setVal(tp::ualni val) { val1 = val; }
[[nodiscard]] tp::ualni getVal() const { return val1; }
void setVal(tp::ualni val) { val1 = val; }
};
class TestAllocator {
tp::ualni nAllocations = 0;
tp::ualni nAllocations = 0;
public:
TestAllocator() = default;
void* allocate(tp::ualni size);
void deallocate(void* p);
[[nodiscard]] tp::ualni getAllocationsCount() const;
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;
tp::ualni mem[1024] = { 0 };
tp::ualni address = 0;
public:
TestFile() = default;
TestFile() = default;
template<typename Type>
void write(const Type& val) {
val.write(*this);
}
template<typename Type>
void write(const Type& val) {
val.write(*this);
}
template<>
void write<tp::ualni>(const tp::ualni& val) {
mem[address] = val;
address++;
}
template<>
void write<tp::ualni>(const tp::ualni& val) {
mem[address] = val;
address++;
}
void setAddress(tp::ualni addr) { address = addr; }
void setAddress(tp::ualni addr) { address = addr; }
template<typename Type>
void read(Type& val) {
val.read(*this);
}
template<typename Type>
void read(Type& val) {
val.read(*this);
}
template<>
void read<tp::ualni>(tp::ualni& val) {
val = mem[address];
address++;
}
template<>
void read<tp::ualni>(tp::ualni& val) {
val = mem[address];
address++;
}
};
void testList();