This repository has been archived on 2020-12-30. You can view files and clone it, but cannot push or open issues or pull requests.
NetworkPacketComposer/test/core/test_tree.cpp

114 lines
3.7 KiB
C++
Raw Permalink Normal View History

2018-01-11 23:20:46 +01:00
#include <iostream>
#include <string>
#include "gtest/gtest.h"
#include "../src/core/Tree.h"
#include "../src/core/TreeNode.h"
class TreeTest: public ::testing::Test {
protected:
virtual void SetUp(){
for(int i = 0; i < 5; ++i) {
t2.getRoot()->addChildObject(std::make_shared<std::string>("a"+std::to_string(i)));
}
for(int i = 0; i < 5; ++i) {
t3.getRoot()->addChildObject(std::make_shared<std::string>("a"+std::to_string(i)));
}
for(int i = 0; i < 5; ++i) {
t3.getRoot()->getChildren()->at(2)->addChildObject(std::make_shared<std::string>("b" + std::to_string(i)));
}
for(int i = 0; i < 3; ++i) {
t3.getRoot()->getChildren()->at(4)->addChildObject(std::make_shared<std::string>("c" + std::to_string(i)));
2018-01-11 23:20:46 +01:00
}
}
virtual void TearDown(){}
Tree<std::string> t;
Tree<std::string> t2;
Tree<std::string> t3;
};
TEST_F(TreeTest, IsEmptyInitially) {
EXPECT_EQ(0, t.getRoot()->getDistanceFromRoot());
EXPECT_EQ(0, t.getRoot()->getChildren()->size());
2018-01-11 23:20:46 +01:00
}
TEST_F(TreeTest, AddingOneLayer) {
EXPECT_EQ(5, t2.getRoot()->getChildren()->size());
auto last = t2.getRoot()->getChildren()->back();
2018-01-11 23:20:46 +01:00
EXPECT_EQ(1, last->getDistanceFromRoot());
}
TEST_F(TreeTest, AddingTwoLayers) {
EXPECT_EQ(2, t3.getRoot()->getChildren()->at(2)->getChildren()->at(2)->getDistanceFromRoot());
}
TEST_F(TreeTest, RemovalSingleNode) {
auto node = t3.getRoot()->getChildren()->at(2)->getChildren()->at(2);
node->remove();
EXPECT_EQ(4, t3.getRoot()->getChildren()->at(2)->getChildren()->size());
}
TEST_F(TreeTest, RecursiveRemoval) {
auto node = t3.getRoot()->getChildren()->at(2);
node->remove();
EXPECT_EQ(4, t3.getRoot()->getChildren()->size());
}
2018-01-11 23:20:46 +01:00
TEST_F(TreeTest, SingleMovement) {
auto node = t3.getRoot()->getChildren()->at(2)->getChildren()->at(3);
auto to = t3.getRoot()->getChildren()->at(3);
node->move(to);
EXPECT_EQ(1, to->getChildren()->size());
}
TEST_F(TreeTest, RecursiveMovement) {
auto node = t3.getRoot()->getChildren()->at(2);
auto to = t3.getRoot()->getChildren()->at(3);
node->move(to);
EXPECT_EQ(1, to->getChildren()->size());
EXPECT_EQ(5, to->getChildren()->at(0)->getChildren()->size());
}
2018-01-11 23:20:46 +01:00
TEST_F(TreeTest, Copy) {
auto node = t3.getRoot()->getChildren()->at(2);
auto newnode = node->copy();
EXPECT_EQ(0, newnode->getChildren()->size());
}
TEST_F(TreeTest, RecursiveCopy) {
auto node = t3.getRoot()->getChildren()->at(2);
auto newnode = node->rcopy();
EXPECT_EQ(5, newnode->getChildren()->size());
}
2018-01-11 23:20:46 +01:00
TEST_F(TreeTest, TraverseTreeDepth) {
EXPECT_EQ(5, t3.getRoot()->getChildren()->size());
EXPECT_EQ(5, t3.getRoot()->getChildren()->at(2)->getChildren()->size());
std::string expectedResult = " a0 a1 a2 b0 b1 b2 b3 b4 a3 a4 c0 c1 c2";
std::string result = "";
TreeWalkStrategy walk = TreeWalkStrategy::depthFirst;
auto node = t3.getFirst();
while (node != nullptr) {
result = result + " " + node->getData()->c_str();
node = node->next(walk);
}
std::cout << result <<std::endl;
EXPECT_EQ(expectedResult, result);
}
TEST_F(TreeTest, TraverseTreeBreadth) {
EXPECT_EQ(5, t3.getRoot()->getChildren()->size());
EXPECT_EQ(5, t3.getRoot()->getChildren()->at(2)->getChildren()->size());
std::string expectedResult = " a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 c0 c1 c2";
std::string result = "";
TreeWalkStrategy walk = TreeWalkStrategy::breadthFirst;
auto node = t3.getFirst();
while (node != nullptr) {
result = result + " " + node->getData()->c_str();
node = node->next(walk);
}
std::cout << result <<std::endl;
EXPECT_EQ(expectedResult, result);
2018-01-11 23:20:46 +01:00
}