// =====================================================================================================================
//  fennec, a free and open source game engine
//  Copyright © 2025 - 2026  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.is_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 = 0;

	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.is_empty(), true);

}

}

}

#endif // FENNEC_TEST_CONTAINERS_RDTREE_H