ADD: switched to Catch2 for testing

This commit is contained in:
Dominik Meyer 2024-01-28 22:45:55 +01:00
parent 9721ab5288
commit 1cbb6c108e
Signed by: byterazor
GPG Key ID: EABDA0FD5981BC97
5 changed files with 77 additions and 172 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libs/Catch2"]
path = libs/Catch2
url = https://github.com/catchorg/Catch2.git

View File

@ -50,6 +50,13 @@ option(TEST_TREE "Turn running of tree specific tests on/off" ON)
IF(${TEST_TREE}) IF(${TEST_TREE})
enable_testing() enable_testing()
message(STATUS "Tree tests enabled") message(STATUS "Tree tests enabled")
IF(NOT TARGET Catch2)
add_subdirectory(libs/Catch2 EXCLUDE_FROM_ALL)
include(libs/Catch2/extras/Catch.cmake)
ENDIF()
include(CTest) include(CTest)
ELSE() ELSE()
message(STATUS "Tree tests disabled") message(STATUS "Tree tests disabled")
@ -57,24 +64,9 @@ ENDIF()
IF (${TEST_TREE}) IF (${TEST_TREE})
add_my_test(TEST test_base add_executable(test_base tests/test_base.cpp)
SOURCES tests/test_base.cpp target_link_libraries(test_base Catch2WithMain tree)
LIBS tree catch_discover_tests(test_base)
)
add_my_test(TEST test_add_child
SOURCES tests/test_add_child.cpp
LIBS tree
)
get_property(_mytests GLOBAL PROPERTY _mytests)
FOREACH( _test ${_mytests})
IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
target_compile_options(${_test} PUBLIC -Wall -g -O0)
ELSE()
target_compile_options(${_test} PUBLIC -O4)
ENDIF()
ENDFOREACH()
ENDIF() ENDIF()

1
libs/Catch2 Submodule

@ -0,0 +1 @@
Subproject commit 1078e7e95b3a06d4dadc75188de48bc4afffb955

View File

@ -1,114 +0,0 @@
/*
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;
}

View File

@ -1,48 +1,71 @@
#include <iostream> #include "Tree/tree.hpp"
#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers.hpp"
#include <memory> #include <memory>
#include <Tree/tree.hpp> #define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>
SCENARIO("Testing Base Tree Methods")
int main()
{ {
bool error=false;
std::shared_ptr<Tree::TreeRoot> t=std::make_shared<Tree::TreeRoot>();
// first test if the parent node is a nullptr for root node GIVEN("Nothing")
if ( t->getParent() != nullptr ) {
{
std::cerr << "Failed Test: Root node parent is not null" << std::endl;
error=true;
}
/*
* create a temp node and add to root
*/
std::shared_ptr<Tree::TempNode> temp0=std::make_shared<Tree::TempNode>();
t->addChild(temp0);
// check if the parent node of temp0 has been set correctly
if (temp0->getParent() != t)
{
std::cerr << "Failed Test: temp0 node parent is null" << std::endl;
error=true;
}
// check if the number of direct children of t is 1
if (t->getNrDirectChildren() != 1)
{
std::cerr << "Failed Test: nr of direct children is not 1" << std::endl;
error=true;
}
if (error) WHEN("Root Node is created")
{ {
return -1; std::shared_ptr<Tree::TreeRoot> root = nullptr;
}
std::cerr << "Test OK" << std::endl; REQUIRE_NOTHROW(root = std::make_shared<Tree::TreeRoot>());
return 0;
THEN("attributed are as expected")
{
REQUIRE_FALSE(root == nullptr);
REQUIRE(root->getRoot() == root);
REQUIRE(root->getParent() == nullptr);
REQUIRE(root->getNrChildren() == 0);
REQUIRE(root->getNrDirectChildren() == 0);
REQUIRE(root->getChildrenBegin() == root->getChildrenEnd());
REQUIRE(root->type() == "Tree::TreeRoot");
REQUIRE(root->base() == "Tree::Base");
}
}
}
AND_GIVEN("A valid Root Node")
{
auto root = std::make_shared<Tree::TreeRoot>();
WHEN("adding a temporary node")
{
auto temp = std::make_shared<Tree::TempNode>();
root->addChild(temp);
THEN("the tree is correct")
{
REQUIRE_FALSE(root == nullptr);
REQUIRE(root->getRoot() == root);
REQUIRE(root->getParent() == nullptr);
REQUIRE(root->getNrChildren() == 1);
REQUIRE(root->getNrDirectChildren() == 1);
REQUIRE(root->type() == "Tree::TreeRoot");
REQUIRE(root->base() == "Tree::Base");
std::shared_ptr<Tree::TempNode> child;
REQUIRE_NOTHROW(child = std::static_pointer_cast<Tree::TempNode>(*(root->getChildrenBegin())));
REQUIRE(child->getRoot() == root);
REQUIRE(child->getParent() == root);
REQUIRE(child->getNrChildren() == 0);
REQUIRE(child->getNrDirectChildren() == 0);
REQUIRE(child->type() == "Tree::TempNode");
REQUIRE(child->base() == "Tree::Base");
}
}
}
} }