#include "../src/tree/binarytree.hpp" #include "testutil.hpp" #include #include void FillWithUniqueRandomNumbers(BinaryTree::Tree & tree, std::unordered_set & control, unsigned const count = 10000u) { for(unsigned i = 0u; i < count; ++i) { unsigned const value = Util::GetRandomNumber(); control.insert(value); tree.InsertNoDuplicates(value); } } bool TestInsert() { unsigned const testSize = 4096; std::vector control; BinaryTree::Tree tree; for(unsigned i = 0u; i < testSize; ++i) { unsigned const value = Util::GetRandomNumber(); control.push_back(value); tree.Insert(value); } for(auto const & num : control) { if(!tree.Contains(num)) { //std::printf("\tValue %u inserted but cannot be found!\n", num); return false; } } return true; } bool TestInsertNoDuplicates() { std::unordered_set control; BinaryTree::Tree tree; FillWithUniqueRandomNumbers(tree, control); for(auto const & num : control) { if(!tree.Contains(num)) { //std::printf("\tValue %u inserted but cannot be found!\n", num); return false; } } return true; } bool TestDeletion() { std::unordered_set control; BinaryTree::Tree tree; FillWithUniqueRandomNumbers(tree, control); std::vector deletedValues; unsigned const toDeleteCount = control.size() / 4; for(unsigned i = 0u; i < toDeleteCount; ++i) { auto const valueTodelete = Util::GetRandomNumber(); auto controlElement = control.find(valueTodelete); if(controlElement != control.end()) { control.erase(controlElement); tree.Delete(valueTodelete); deletedValues.push_back(valueTodelete); } } for(auto const & num : control) { if(!tree.Contains(num)) { std::puts("[INFO] Tree does not contain a value that it should."); //std::printf("Value %u inserted but cannot be found!\n", num); return false; } } for(auto const & num : deletedValues) { if(tree.Contains(num)) { std::puts("[INFO] Tree contains deleted value that it should not."); return false; } } return true; } bool TestDeletionCase( std::vector const toInsert, std::vector const toDelete, std::vector const result) { BinaryTree::Tree tree; for(auto n : toInsert) { tree.Insert(n); } for(auto n : toDelete) { tree.Delete(n); } bool ok = true; for(auto n : result) { if(!tree.Contains(n)) { std::printf("\tError, tree does not contain value %u.\n", n); ok = false; } } for(auto n : toDelete) { if(tree.Contains(n)) { std::printf("\tError, tree contains deleted value %u.\n", n); ok = false; } } return ok; } bool TestDeletionCases() { std::puts("Testing deleting root with single child right."); TestDeletionCase( std::vector { 10, 12, 11, 13}, std::vector { 10 }, std::vector { 12, 11, 13 }); std::puts("Testing deleting root with single child left."); TestDeletionCase( std::vector { 15, 12, 11, 13}, std::vector { 15 }, std::vector { 12, 11, 13 }); std::puts("Testing deleting root with 2 children but no child right left."); TestDeletionCase( std::vector { 15, 10, 20, 22, 25, 18, 5}, std::vector { 20 }, std::vector { 15, 10, 22, 25, 18, 5 }); std::puts("Testing deleting root with 2 children."); TestDeletionCase( std::vector { 50, 40, 60, 75, 55, 45, 42, 58 }, std::vector { 50 }, std::vector { 40, 60, 75, 55, 45, 42, 58 }); return true; } int main() { Test::Execute(TestInsert, "Insertion and find test"); Test::Execute(TestInsertNoDuplicates, "Insertion without duplicates test"); Test::Execute(TestDeletion, "Insertion and deletion test"); Test::Execute(TestDeletionCases, "Deletion cases test"); return 0; }