From 1cbb6c108e85c588b3f1c6bec191dca931585ea5 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Sun, 28 Jan 2024 22:45:55 +0100 Subject: [PATCH] ADD: switched to Catch2 for testing --- .gitmodules | 3 ++ CMakeLists.txt | 28 ++++------ libs/Catch2 | 1 + tests/test_add_child.cpp | 114 --------------------------------------- tests/test_base.cpp | 103 +++++++++++++++++++++-------------- 5 files changed, 77 insertions(+), 172 deletions(-) create mode 100644 .gitmodules create mode 160000 libs/Catch2 delete mode 100644 tests/test_add_child.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6357757 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/Catch2"] + path = libs/Catch2 + url = https://github.com/catchorg/Catch2.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 4048c76..3f1ee7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,13 @@ option(TEST_TREE "Turn running of tree specific tests on/off" ON) IF(${TEST_TREE}) enable_testing() 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) ELSE() message(STATUS "Tree tests disabled") @@ -57,24 +64,9 @@ ENDIF() IF (${TEST_TREE}) -add_my_test(TEST test_base - SOURCES tests/test_base.cpp - LIBS tree - ) +add_executable(test_base tests/test_base.cpp) +target_link_libraries(test_base Catch2WithMain 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() \ No newline at end of file diff --git a/libs/Catch2 b/libs/Catch2 new file mode 160000 index 0000000..1078e7e --- /dev/null +++ b/libs/Catch2 @@ -0,0 +1 @@ +Subproject commit 1078e7e95b3a06d4dadc75188de48bc4afffb955 diff --git a/tests/test_add_child.cpp b/tests/test_add_child.cpp deleted file mode 100644 index 1b7881b..0000000 --- a/tests/test_add_child.cpp +++ /dev/null @@ -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 . -*/ - -#include -#include -#include -#include -#include -#include - -int main() -{ - bool error=false; - - - std::vector> nodes; - - // create a root node of the tree - std::shared_ptr t=std::make_shared(); - - // create one child - std::shared_ptr temp0=std::make_shared(); - - // 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 temp1=std::make_shared(); - - // 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 temp0=std::make_shared(); - 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; -} diff --git a/tests/test_base.cpp b/tests/test_base.cpp index 3c955b2..b73e8d0 100644 --- a/tests/test_base.cpp +++ b/tests/test_base.cpp @@ -1,48 +1,71 @@ -#include +#include "Tree/tree.hpp" +#include "catch2/catch_test_macros.hpp" +#include "catch2/matchers/catch_matchers.hpp" #include -#include +#define CATCH_CONFIG_MAIN +#include - -int main() +SCENARIO("Testing Base Tree Methods") { - bool error=false; - std::shared_ptr t=std::make_shared(); - // first test if the parent node is a nullptr for root node - 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 temp0=std::make_shared(); - - 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; - } + GIVEN("Nothing") + { - if (error) - { - return -1; - } + WHEN("Root Node is created") + { + std::shared_ptr root = nullptr; - std::cerr << "Test OK" << std::endl; - return 0; -} + REQUIRE_NOTHROW(root = std::make_shared()); + + 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(); + + WHEN("adding a temporary node") + { + auto temp = std::make_shared(); + + 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 child; + + REQUIRE_NOTHROW(child = std::static_pointer_cast(*(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"); + + } + } + } +} \ No newline at end of file