- Fixed some missing and erroneous testing logic for containers

- Lots of bug-fixing for containers
 - Performance optimization for containers
This commit is contained in:
2025-09-17 17:13:52 -04:00
parent 80925965d4
commit a35f2a699d
15 changed files with 886 additions and 262 deletions

View File

@@ -0,0 +1,109 @@
// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#ifndef FENNEC_TEST_CONTAINERS_BINTREE_H
#define FENNEC_TEST_CONTAINERS_BINTREE_H
#include "../../test.h"
#include <fennec/containers/bintree.h>
namespace fennec
{
namespace test
{
inline void fennec_test_containers_bintree() {
using tree_t = bintree<size_t>;
tree_t test;
constexpr size_t npos = tree_t::npos;
constexpr size_t pre_order [] = { 1, 2, 4, 5, 3, 6 };
constexpr size_t in_order [] = { 4, 2, 5, 1, 3, 6 };
constexpr size_t post_order[] = { 4, 5, 2, 6, 3, 1 };
const size_t n = 50;
for (size_t i = 0; i < n; ++i) {
const size_t parent = rand() % max(test.size(), size_t(1));
const bool side = rand() % 2;
size_t l = side ? test.insert_right(parent, i) : test.insert_left(parent, i);
assertf(test.parent(l) == parent, "Tree Construct Test Failed.");
}
fennec_test_spacer(1);
test.clear();
fennec_test_run(test.empty(), true);
fennec_test_spacer(1);
size_t n1 = test.insert_left(npos, 1);
size_t n2 = test.insert_left(n1, 2);
size_t n3 = test.insert_right(n1, 3);
size_t n4 = test.insert_left(n2, 4);
size_t n5 = test.insert_right(n2, 5);
size_t n6 = test.insert_right(n3, 6);
fennec_test_run(n1 != npos, true);
fennec_test_run(n2 != npos, true);
fennec_test_run(n3 != npos, true);
fennec_test_run(n4 != npos, true);
fennec_test_run(n5 != npos, true);
fennec_test_run(n6 != npos, true);
fennec_test_spacer(1);
size_t i;
i = 0;
test.traverse<tree_t::pre_order>([&](size_t x, size_t) -> uint8_t {
fennec_test_run(x, pre_order[i++]);
return traversal_control_continue;
}, test.root());
fennec_test_spacer(1);
i = 0;
test.traverse<tree_t::in_order>([&](size_t x, size_t) -> uint8_t {
fennec_test_run(x, in_order[i++]);
return traversal_control_continue;
}, test.root());
fennec_test_spacer(1);
i = 0;
test.traverse<tree_t::post_order>([&](size_t x, size_t) -> uint8_t {
fennec_test_run(x, post_order[i++]);
return traversal_control_continue;
}, test.root());
fennec_test_spacer(1);
test.clear();
fennec_test_run(test.empty(), true);
}
}
}
#endif // FENNEC_TEST_CONTAINERS_RDTREE_H

View File

@@ -0,0 +1,56 @@
// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#ifndef FENNEC_TEST_CONTAINERS_PRIORITY_QUEUE_H
#define FENNEC_TEST_CONTAINERS_PRIORITY_QUEUE_H
#include "../../test.h"
#include <cstdlib>
#include <fennec/containers/priority_queue.h>
#include <fennec/containers/sequence.h>
namespace fennec::test
{
inline void fennec_test_containers_priority_queue() {
using type_t = decltype(rand());
sequence<type_t> ref;
priority_queue<type_t> test;
size_t n = 50;
for (size_t i = 0; i < n; ++i) {
type_t v = rand();
test.push(v);
ref.insert(v);
}
fennec_test_run(test.size(), n);
for (type_t x : ref) {
assert(x == test.front(), "Failed Priority Queue Test!");
test.pop();
}
std::cout << "passed" << std::endl;
}
}
#endif // FENNEC_TEST_CONTAINERS_PRIORITY_QUEUE_H

View File

@@ -55,11 +55,6 @@ inline void fennec_test_containers_rdtree() {
fennec_test_spacer(1);
test.traverse<tree_t::pre_order>([](size_t i, size_t n) -> uint8_t {
assertf(i + 1 == n, "Tree Traverse Test Failed");
return traversal_control_continue;
});
test.erase(0);
fennec_test_run(test.empty(), true);

View File

@@ -39,17 +39,29 @@ namespace test
{
inline void fennec_test_containers_sequence() {
dynarray<size_t> ref;
sequence<size_t> test;
using type_t = decltype(rand());
dynarray<type_t> ref;
sequence<type_t> test;
const size_t n = 50;
for (size_t i = 0; i < n; ++i) {
size_t v = rand();
type_t v = rand();
ref.push_back(v);
test.insert(v);
}
for (size_t v : ref) {
fennec_test_run(test.size(), n);
type_t p = -1;
size_t c = 0;
for (type_t x : test) {
assertf(x > p, "Failed Sequence Test!");
p = x;
++c;
}
fennec_test_run(c, n);
for (type_t v : ref) {
assertf(test.contains(v), "Failed Sequence Test!");
test.erase(v);
}