libtree/tests/test_add_child.cpp

115 lines
3.4 KiB
C++

/*
Copyright (C) 2019 Dominik Meyer
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/>.
*/
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <Tree/tree.hpp>
#include <cstdint>
int main()
{
bool error=false;
std::vector<std::shared_ptr<Tree::TempNode>> nodes;
// create a root node of the tree
std::shared_ptr<Tree::TreeRoot> t=std::make_shared<Tree::TreeRoot>();
// create one child
std::shared_ptr<Tree::TempNode> temp0=std::make_shared<Tree::TempNode>();
// add child to tree
t->addChild(temp0);
if (t->getNrDirectChildren() != 1 || t->getNrChildren()!=1)
{
error=true;
std::cerr << std::string(__FILE__) + ":" + std::to_string(__LINE__) + " NR of children do not match added children" << std::endl;
}
// create another child
std::shared_ptr<Tree::TempNode> temp1=std::make_shared<Tree::TempNode>();
// add child to tree
temp0->addChild(temp1);
if (t->getNrDirectChildren() != 1 || t->getNrChildren()!=2 || temp0->getNrDirectChildren()!=1 || temp0->getNrChildren() != 1)
{
error=true;
std::cerr << std::string(__FILE__) + ":" + std::to_string(__LINE__) + " NR of children do not match added children" << std::endl;
}
// remove a node from the list of children of root node
t->removeChild(temp0);
if (t->getNrDirectChildren() != 0 || t->getNrChildren()!= 0 || temp0->getNrDirectChildren()!=1 || temp0->getNrChildren() != 1)
{
error=true;
std::cerr << std::string(__FILE__) + ":" + std::to_string(__LINE__) + " NR of children do not match added children" << std::endl;
}
// readd the removed node
t->addChild(temp0);
if (t->getNrDirectChildren() != 1 || t->getNrChildren()!=2 || temp0->getNrDirectChildren()!=1 || temp0->getNrChildren() != 1)
{
error=true;
std::cerr << std::string(__FILE__) + ":" + std::to_string(__LINE__) + " NR of children do not match added children" << std::endl;
}
// delete Child
temp0->getParent()->deleteChild(temp0);
if (t->getNrDirectChildren() != 0 || t->getNrChildren()!=0 )
{
error=true;
std::cerr << std::string(__FILE__) + ":" + std::to_string(__LINE__) + " NR of children do not match added children" << std::endl;
}
// add a bunch of children
for(int i=0; i<20; i++)
{
std::shared_ptr<Tree::TempNode> temp0=std::make_shared<Tree::TempNode>();
nodes.push_back(temp0);
}
t->addChildren(nodes.begin(), nodes.end());
if (t->getNrDirectChildren() != 20 || t->getNrChildren()!=20 )
{
error=true;
std::cerr << std::string(__FILE__) + ":" + std::to_string(__LINE__) + " NR of children do not match added children" << std::endl;
}
std::cout << "--------------" << std::endl;
t->printTree();
std::cout << "--------------" << std::endl;
if (error)
{
std::cerr << "Test failed" << std::endl;
return -1;
}
std::cerr << "Test OK" << std::endl;
return 0;
}