diff --git a/alib2graph_data/test-src/graph/GraphTest.cpp b/alib2graph_data/test-src/graph/GraphTest.cpp index 4b5c163ff6e9d74bd09a4cf93edf2715a3064bd0..42b018188c4259f029cb493b592c36d04b967405 100644 --- a/alib2graph_data/test-src/graph/GraphTest.cpp +++ b/alib2graph_data/test-src/graph/GraphTest.cpp @@ -7,146 +7,129 @@ // Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved. // Git repository: https://gitlab.fit.cvut.cz/algorithms-library-toolkit/automata-library -#include "GraphTest.hpp" +#include <catch2/catch.hpp> #include <graph/GraphClasses.hpp> using namespace graph; -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(GraphTest, "graph"); -CPPUNIT_TEST_SUITE_REGISTRATION(GraphTest); - -// --------------------------------------------------------------------------------------------------------------------- - -void GraphTest::setUp() { - TestFixture::setUp(); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GraphTest::tearDown() { - TestFixture::tearDown(); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GraphTest::testUndirectedGraph() { - graph::UndirectedGraph<int, ext::pair<int, int> > graph; - - graph.addNode(1); - graph.addNode(2); - graph.addNode(3); - graph.addNode(4); - graph.addNode(5); - graph.addNode(6); - - graph.addEdge(ext::make_pair(1, 2)); - graph.addEdge(2, 3); - graph.addEdge(3, 4); - graph.addEdge(5, 1); - - CPPUNIT_ASSERT(graph.nodeCount() == 6); - CPPUNIT_ASSERT(graph.edgeCount() == 4); - - ext::set<int> ref1 = {2, 5}; - CPPUNIT_ASSERT(graph.successors(1) == ref1); - - ext::vector<ext::pair<int, int>> ref2 = {ext::make_pair(1, 2), ext::make_pair(5, 1)}; - ext::vector<ext::pair<int, int>> res2 = graph.successorEdges(1); - std::sort(ref2.begin(), ref2.end()); - std::sort(res2.begin(), res2.end()); - CPPUNIT_ASSERT(res2.size() == ref2.size()); - CPPUNIT_ASSERT(res2 == ref2); - - ext::set<int> ref3 = {1, 3}; - CPPUNIT_ASSERT(graph.predecessors(2) == ref3); - - ext::vector<ext::pair<int, int>> ref4 = {ext::make_pair(1, 2), ext::make_pair(2, 3)}; - ext::vector<ext::pair<int, int>> res4 = graph.predecessorEdges(2); - std::sort(ref4.begin(), ref4.end()); - std::sort(res4.begin(), res4.end()); - CPPUNIT_ASSERT(res4.size() == ref4.size()); - CPPUNIT_ASSERT(res4 == ref4); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GraphTest::testDirectedGraph() { - graph::DirectedGraph<int, ext::pair<int, int> > graph; - - graph.addNode(1); - graph.addNode(2); - graph.addNode(3); - graph.addNode(4); - graph.addNode(5); - graph.addNode(6); - - graph.addEdge(ext::make_pair(1, 2)); - graph.addEdge(2, 3); - graph.addEdge(3, 4); - graph.addEdge(5, 1); - - CPPUNIT_ASSERT(graph.nodeCount() == 6); - CPPUNIT_ASSERT(graph.edgeCount() == 4); - - ext::set<int> ref1 = {2}; - CPPUNIT_ASSERT(graph.successors(1) == ref1); - - ext::vector<ext::pair<int, int>> ref2 = {ext::make_pair(1, 2)}; - ext::vector<ext::pair<int, int>> res2 = graph.successorEdges(1); - std::sort(ref2.begin(), ref2.end()); - std::sort(res2.begin(), res2.end()); - CPPUNIT_ASSERT(res2.size() == ref2.size()); - CPPUNIT_ASSERT(res2 == ref2); - - ext::set<int> ref3 = {1}; - CPPUNIT_ASSERT(graph.predecessors(2) == ref3); - - ext::vector<ext::pair<int, int>> ref4 = {ext::make_pair(1, 2)}; - ext::vector<ext::pair<int, int>> res4 = graph.predecessorEdges(2); - std::sort(ref4.begin(), ref4.end()); - std::sort(res4.begin(), res4.end()); - CPPUNIT_ASSERT(res4.size() == ref4.size()); - CPPUNIT_ASSERT(res4 == ref4); +TEST_CASE ( "Graph", "[unit][graph][graph]" ) { + SECTION ( "Undirected Graphs" ) { + graph::UndirectedGraph<int, ext::pair<int, int> > graph; + + graph.addNode(1); + graph.addNode(2); + graph.addNode(3); + graph.addNode(4); + graph.addNode(5); + graph.addNode(6); + + graph.addEdge(ext::make_pair(1, 2)); + graph.addEdge(2, 3); + graph.addEdge(3, 4); + graph.addEdge(5, 1); + + CHECK(graph.nodeCount() == 6); + CHECK(graph.edgeCount() == 4); + + ext::set<int> ref1 = {2, 5}; + CHECK(graph.successors(1) == ref1); + + ext::vector<ext::pair<int, int>> ref2 = {ext::make_pair(1, 2), ext::make_pair(5, 1)}; + ext::vector<ext::pair<int, int>> res2 = graph.successorEdges(1); + std::sort(ref2.begin(), ref2.end()); + std::sort(res2.begin(), res2.end()); + CHECK(res2.size() == ref2.size()); + CHECK(res2 == ref2); + + ext::set<int> ref3 = {1, 3}; + CHECK(graph.predecessors(2) == ref3); + + ext::vector<ext::pair<int, int>> ref4 = {ext::make_pair(1, 2), ext::make_pair(2, 3)}; + ext::vector<ext::pair<int, int>> res4 = graph.predecessorEdges(2); + std::sort(ref4.begin(), ref4.end()); + std::sort(res4.begin(), res4.end()); + CHECK(res4.size() == ref4.size()); + CHECK(res4 == ref4); + } + + // --------------------------------------------------------------------------------------------------------------------- + + SECTION ( "Directed Graph" ) { + graph::DirectedGraph<int, ext::pair<int, int> > graph; + + graph.addNode(1); + graph.addNode(2); + graph.addNode(3); + graph.addNode(4); + graph.addNode(5); + graph.addNode(6); + + graph.addEdge(ext::make_pair(1, 2)); + graph.addEdge(2, 3); + graph.addEdge(3, 4); + graph.addEdge(5, 1); + + CHECK(graph.nodeCount() == 6); + CHECK(graph.edgeCount() == 4); + + ext::set<int> ref1 = {2}; + CHECK(graph.successors(1) == ref1); + + ext::vector<ext::pair<int, int>> ref2 = {ext::make_pair(1, 2)}; + ext::vector<ext::pair<int, int>> res2 = graph.successorEdges(1); + std::sort(ref2.begin(), ref2.end()); + std::sort(res2.begin(), res2.end()); + CHECK(res2.size() == ref2.size()); + CHECK(res2 == ref2); + + ext::set<int> ref3 = {1}; + CHECK(graph.predecessors(2) == ref3); + + ext::vector<ext::pair<int, int>> ref4 = {ext::make_pair(1, 2)}; + ext::vector<ext::pair<int, int>> res4 = graph.predecessorEdges(2); + std::sort(ref4.begin(), ref4.end()); + std::sort(res4.begin(), res4.end()); + CHECK(res4.size() == ref4.size()); + CHECK(res4 == ref4); + } + + // --------------------------------------------------------------------------------------------------------------------- + + SECTION ( "Mixed Graph" ) { + graph::MixedGraph<int, ext::pair<int, int>> graph; + graph.addNode(1); + graph.addNode(2); + graph.addNode(3); + graph.addNode(4); + graph.addNode(5); + graph.addNode(6); + + graph.addEdge(ext::make_pair(1, 2)); + graph.addEdge(2, 3); + graph.addEdge(3, 4); + graph.addArc(5, 1); + + CHECK(graph.nodeCount() == 6); + CHECK(graph.edgeCount() == 4); + + ext::set<int> ref1 = {1}; + CHECK(graph.successors(5) == ref1); + + ext::vector<ext::pair<int, int>> ref2 = {ext::make_pair(1, 2)}; + ext::vector<ext::pair<int, int>> res2 = graph.successorEdges(1); + std::sort(ref2.begin(), ref2.end()); + std::sort(res2.begin(), res2.end()); + CHECK(res2.size() == ref2.size()); + CHECK(res2 == ref2); + + ext::set<int> ref3 = {1, 3}; + CHECK(graph.predecessors(2) == ref3); + + ext::vector<ext::pair<int, int>> ref4 = {ext::make_pair(1, 2), ext::make_pair(2, 3)}; + ext::vector<ext::pair<int, int>> res4 = graph.predecessorEdges(2); + std::sort(ref4.begin(), ref4.end()); + std::sort(res4.begin(), res4.end()); + CHECK(res4.size() == ref4.size()); + CHECK(res4 == ref4); + } } - -// --------------------------------------------------------------------------------------------------------------------- - -void GraphTest::testMixedGraph() { - graph::MixedGraph<int, ext::pair<int, int>> graph; - graph.addNode(1); - graph.addNode(2); - graph.addNode(3); - graph.addNode(4); - graph.addNode(5); - graph.addNode(6); - - graph.addEdge(ext::make_pair(1, 2)); - graph.addEdge(2, 3); - graph.addEdge(3, 4); - graph.addArc(5, 1); - - CPPUNIT_ASSERT(graph.nodeCount() == 6); - CPPUNIT_ASSERT(graph.edgeCount() == 4); - - ext::set<int> ref1 = {1}; - CPPUNIT_ASSERT(graph.successors(5) == ref1); - - ext::vector<ext::pair<int, int>> ref2 = {ext::make_pair(1, 2)}; - ext::vector<ext::pair<int, int>> res2 = graph.successorEdges(1); - std::sort(ref2.begin(), ref2.end()); - std::sort(res2.begin(), res2.end()); - CPPUNIT_ASSERT(res2.size() == ref2.size()); - CPPUNIT_ASSERT(res2 == ref2); - - ext::set<int> ref3 = {1, 3}; - CPPUNIT_ASSERT(graph.predecessors(2) == ref3); - - ext::vector<ext::pair<int, int>> ref4 = {ext::make_pair(1, 2), ext::make_pair(2, 3)}; - ext::vector<ext::pair<int, int>> res4 = graph.predecessorEdges(2); - std::sort(ref4.begin(), ref4.end()); - std::sort(res4.begin(), res4.end()); - CPPUNIT_ASSERT(res4.size() == ref4.size()); - CPPUNIT_ASSERT(res4 == ref4); -} - -// --------------------------------------------------------------------------------------------------------------------- diff --git a/alib2graph_data/test-src/graph/GraphTest.hpp b/alib2graph_data/test-src/graph/GraphTest.hpp deleted file mode 100644 index fc1cfe8cce1d4c65335bda812792e73a38df2c39..0000000000000000000000000000000000000000 --- a/alib2graph_data/test-src/graph/GraphTest.hpp +++ /dev/null @@ -1,32 +0,0 @@ -// GraphTest.hpp -// -// Created on: 21. 01. 2018 -// Author: Jan Uhlik -// Modified by: -// -// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved. -// Git repository: https://gitlab.fit.cvut.cz/algorithms-library-toolkit/automata-library - -#ifndef ALIB2_GRAPHTEST_HPP -#define ALIB2_GRAPHTEST_HPP - -#include <cppunit/TestFixture.h> -#include <cppunit/extensions/HelperMacros.h> - -class GraphTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(GraphTest); - CPPUNIT_TEST(testUndirectedGraph); - CPPUNIT_TEST(testDirectedGraph); - CPPUNIT_TEST(testMixedGraph); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() override; - void tearDown() override; - - void testUndirectedGraph(); - void testDirectedGraph(); - void testMixedGraph(); -}; - -#endif //ALIB2_GRAPHTEST_HPP diff --git a/alib2graph_data/test-src/grid/GridTest.cpp b/alib2graph_data/test-src/grid/GridTest.cpp index d4cce67861e3e46e93e0588c0273db5f984f8a41..9e7739a4171d6839d4655338a84169edcdd73fe6 100644 --- a/alib2graph_data/test-src/grid/GridTest.cpp +++ b/alib2graph_data/test-src/grid/GridTest.cpp @@ -7,240 +7,226 @@ // Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved. // Git repository: https://gitlab.fit.cvut.cz/algorithms-library-toolkit/automata-library -#include "GridTest.hpp" +#include <catch2/catch.hpp> #include <grid/GridClasses.hpp> using namespace grid; -CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(GridTest, "grid"); -CPPUNIT_TEST_SUITE_REGISTRATION(GridTest); -// --------------------------------------------------------------------------------------------------------------------- - -void GridTest::setUp() { - TestFixture::setUp(); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GridTest::tearDown() { - TestFixture::tearDown(); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GridTest::testGrid4() { - grid::SquareGrid4<> grid4(10, 10); - using node_type = decltype(grid4)::node_type; - using edge_type = decltype(grid4)::edge_type; - - ext::set<node_type> ref1 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5)}; - CPPUNIT_ASSERT(ref1 == grid4.successors(node_type(5, 5))); - ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4)), - edge_type(node_type(5, 5), node_type(5, 6)), - edge_type(node_type(5, 5), node_type(4, 5)), - edge_type(node_type(5, 5), node_type(6, 5))}; - ext::vector<edge_type> res1e = grid4.successorEdges(node_type(5, 5)); - std::sort(ref1e.begin(), ref1e.end()); - std::sort(res1e.begin(), res1e.end()); - CPPUNIT_ASSERT(ref1e == res1e); - - grid4.addObstacle(node_type(5, 5)); - - ext::set<node_type> ref2 = {}; - CPPUNIT_ASSERT(ref2 == grid4.successors(node_type(5, 5))); - ext::vector<edge_type> ref2e = {}; - ext::vector<edge_type> res2e = grid4.successorEdges(node_type(5, 5)); - std::sort(ref2e.begin(), ref2e.end()); - std::sort(res2e.begin(), res2e.end()); - CPPUNIT_ASSERT(ref2e == res2e); - - ext::set<node_type> ref3 = {node_type(4, 6), node_type(4, 4), node_type(3, 5)}; - CPPUNIT_ASSERT(ref3 == grid4.successors(node_type(4, 5))); - ext::vector<edge_type> ref3e = {edge_type(node_type(4, 5), node_type(4, 6)), - edge_type(node_type(4, 5), node_type(4, 4)), - edge_type(node_type(4, 5), node_type(3, 5))}; - ext::vector<edge_type> res3e = grid4.successorEdges(node_type(4, 5)); - std::sort(ref3e.begin(), ref3e.end()); - std::sort(res3e.begin(), res3e.end()); - CPPUNIT_ASSERT(ref3e == res3e); - - ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0)}; - CPPUNIT_ASSERT(ref4 == grid4.successors(node_type(0, 0))); - ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1)), - edge_type(node_type(0, 0), node_type(1, 0))}; - ext::vector<edge_type> res4e = grid4.successorEdges(node_type(0, 0)); - std::sort(ref4e.begin(), ref4e.end()); - std::sort(res4e.begin(), res4e.end()); - CPPUNIT_ASSERT(ref4e == res4e); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GridTest::testGrid8() { - grid::SquareGrid8<> grid8(10, 10); - using node_type = decltype(grid8)::node_type; - using edge_type = decltype(grid8)::edge_type; - - ext::set<node_type> ref1 = - {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), - node_type(6, 4), node_type(4, 4)}; - CPPUNIT_ASSERT(ref1 == grid8.successors(node_type(5, 5))); - ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4)), - edge_type(node_type(5, 5), node_type(5, 6)), - edge_type(node_type(5, 5), node_type(4, 5)), - edge_type(node_type(5, 5), node_type(4, 6)), - edge_type(node_type(5, 5), node_type(6, 6)), - edge_type(node_type(5, 5), node_type(6, 4)), - edge_type(node_type(5, 5), node_type(4, 4)), - edge_type(node_type(5, 5), node_type(6, 5))}; - ext::vector<edge_type> res1e = grid8.successorEdges(node_type(5, 5)); - std::sort(ref1e.begin(), ref1e.end()); - std::sort(res1e.begin(), res1e.end()); - CPPUNIT_ASSERT(ref1e == res1e); - - grid8.addObstacle(node_type(4, 4)); - - ext::set<node_type> ref2 = {}; - CPPUNIT_ASSERT(ref2 == grid8.successors(node_type(4, 4))); - ext::vector<edge_type> ref2e = {}; - ext::vector<edge_type> res2e = grid8.successorEdges(node_type(4, 4)); - std::sort(ref2e.begin(), ref2e.end()); - std::sort(res2e.begin(), res2e.end()); - CPPUNIT_ASSERT(ref2e == res2e); - - ext::set<node_type> - ref3 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), - node_type(6, 4)}; - CPPUNIT_ASSERT(ref3 == grid8.successors(node_type(5, 5))); - ext::vector<edge_type> ref3e = {edge_type(node_type(5, 5), node_type(5, 4)), - edge_type(node_type(5, 5), node_type(5, 6)), - edge_type(node_type(5, 5), node_type(4, 5)), - edge_type(node_type(5, 5), node_type(4, 6)), - edge_type(node_type(5, 5), node_type(6, 6)), - edge_type(node_type(5, 5), node_type(6, 4)), - edge_type(node_type(5, 5), node_type(6, 5))}; - ext::vector<edge_type> res3e = grid8.successorEdges(node_type(5, 5)); - std::sort(ref3e.begin(), ref3e.end()); - std::sort(res3e.begin(), res3e.end()); - CPPUNIT_ASSERT(ref3e == res3e); - - ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0), node_type(1, 1)}; - CPPUNIT_ASSERT(ref4 == grid8.successors(node_type(0, 0))); - ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1)), - edge_type(node_type(0, 0), node_type(1, 1)), - edge_type(node_type(0, 0), node_type(1, 0))}; - ext::vector<edge_type> res4e = grid8.successorEdges(node_type(0, 0)); - std::sort(ref4e.begin(), ref4e.end()); - std::sort(res4e.begin(), res4e.end()); - CPPUNIT_ASSERT(ref4e == res4e); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GridTest::testGrid4Weighted() { - grid::WeightedSquareGrid4<> grid4(10, 10); - using node_type = decltype(grid4)::node_type; - using edge_type = decltype(grid4)::edge_type; - - ext::set<node_type> ref1 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5)}; - CPPUNIT_ASSERT(ref1 == grid4.successors(node_type(5, 5))); - ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4), 1), - edge_type(node_type(5, 5), node_type(5, 6), 1), - edge_type(node_type(5, 5), node_type(4, 5), 1), - edge_type(node_type(5, 5), node_type(6, 5), 1)}; - ext::vector<edge_type> res1e = grid4.successorEdges(node_type(5, 5)); - std::sort(ref1e.begin(), ref1e.end()); - std::sort(res1e.begin(), res1e.end()); - CPPUNIT_ASSERT(ref1e == res1e); - - grid4.addObstacle(node_type(5, 5)); - - ext::set<node_type> ref2 = {}; - CPPUNIT_ASSERT(ref2 == grid4.successors(node_type(5, 5))); - ext::vector<edge_type> ref2e = {}; - ext::vector<edge_type> res2e = grid4.successorEdges(node_type(5, 5)); - std::sort(ref2e.begin(), ref2e.end()); - std::sort(res2e.begin(), res2e.end()); - CPPUNIT_ASSERT(ref2e == res2e); - - ext::set<node_type> ref3 = {node_type(4, 6), node_type(4, 4), node_type(3, 5)}; - CPPUNIT_ASSERT(ref3 == grid4.successors(node_type(4, 5))); - ext::vector<edge_type> ref3e = {edge_type(node_type(4, 5), node_type(4, 6), 1), - edge_type(node_type(4, 5), node_type(4, 4), 1), - edge_type(node_type(4, 5), node_type(3, 5), 1)}; - ext::vector<edge_type> res3e = grid4.successorEdges(node_type(4, 5)); - std::sort(ref3e.begin(), ref3e.end()); - std::sort(res3e.begin(), res3e.end()); - CPPUNIT_ASSERT(ref3e == res3e); - - ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0)}; - CPPUNIT_ASSERT(ref4 == grid4.successors(node_type(0, 0))); - ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1), 1), - edge_type(node_type(0, 0), node_type(1, 0), 1)}; - ext::vector<edge_type> res4e = grid4.successorEdges(node_type(0, 0)); - std::sort(ref4e.begin(), ref4e.end()); - std::sort(res4e.begin(), res4e.end()); - CPPUNIT_ASSERT(ref4e == res4e); -} - -// --------------------------------------------------------------------------------------------------------------------- - -void GridTest::testGrid8Weighted() { - grid::WeightedSquareGrid8<> grid8(10, 10); - using node_type = decltype(grid8)::node_type; - using edge_type = decltype(grid8)::edge_type; - - ext::set<node_type> ref1 = - {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), - node_type(6, 4), node_type(4, 4)}; - CPPUNIT_ASSERT(ref1 == grid8.successors(node_type(5, 5))); - ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4), 1), - edge_type(node_type(5, 5), node_type(5, 6), 1), - edge_type(node_type(5, 5), node_type(4, 5), 1), - edge_type(node_type(5, 5), node_type(4, 6), M_SQRT2), - edge_type(node_type(5, 5), node_type(6, 6), M_SQRT2), - edge_type(node_type(5, 5), node_type(6, 4), M_SQRT2), - edge_type(node_type(5, 5), node_type(4, 4), M_SQRT2), - edge_type(node_type(5, 5), node_type(6, 5), 1)}; - ext::vector<edge_type> res1e = grid8.successorEdges(node_type(5, 5)); - std::sort(ref1e.begin(), ref1e.end()); - std::sort(res1e.begin(), res1e.end()); - CPPUNIT_ASSERT(ref1e == res1e); - - grid8.addObstacle(node_type(4, 4)); - - ext::set<node_type> ref2 = {}; - CPPUNIT_ASSERT(ref2 == grid8.successors(node_type(4, 4))); - ext::vector<edge_type> ref2e = {}; - ext::vector<edge_type> res2e = grid8.successorEdges(node_type(4, 4)); - std::sort(ref2e.begin(), ref2e.end()); - std::sort(res2e.begin(), res2e.end()); - CPPUNIT_ASSERT(ref2e == res2e); - - ext::set<node_type> - ref3 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), - node_type(6, 4)}; - CPPUNIT_ASSERT(ref3 == grid8.successors(node_type(5, 5))); - ext::vector<edge_type> ref3e = {edge_type(node_type(5, 5), node_type(5, 4), 1), - edge_type(node_type(5, 5), node_type(5, 6), 1), - edge_type(node_type(5, 5), node_type(4, 5), 1), - edge_type(node_type(5, 5), node_type(4, 6), M_SQRT2), - edge_type(node_type(5, 5), node_type(6, 6), M_SQRT2), - edge_type(node_type(5, 5), node_type(6, 4), M_SQRT2), - edge_type(node_type(5, 5), node_type(6, 5), 1)}; - ext::vector<edge_type> res3e = grid8.successorEdges(node_type(5, 5)); - std::sort(ref3e.begin(), ref3e.end()); - std::sort(res3e.begin(), res3e.end()); - CPPUNIT_ASSERT(ref3e == res3e); - - ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0), node_type(1, 1)}; - CPPUNIT_ASSERT(ref4 == grid8.successors(node_type(0, 0))); - ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1), 1), - edge_type(node_type(0, 0), node_type(1, 1), M_SQRT2), - edge_type(node_type(0, 0), node_type(1, 0), 1)}; - ext::vector<edge_type> res4e = grid8.successorEdges(node_type(0, 0)); - std::sort(ref4e.begin(), ref4e.end()); - std::sort(res4e.begin(), res4e.end()); - CPPUNIT_ASSERT(ref4e == res4e); +TEST_CASE ( "GridTest", "[unit][graph][grid]" ) { + SECTION ( "Grid4" ) { + grid::SquareGrid4<> grid4(10, 10); + using node_type = decltype(grid4)::node_type; + using edge_type = decltype(grid4)::edge_type; + + ext::set<node_type> ref1 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5)}; + CHECK(ref1 == grid4.successors(node_type(5, 5))); + ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4)), + edge_type(node_type(5, 5), node_type(5, 6)), + edge_type(node_type(5, 5), node_type(4, 5)), + edge_type(node_type(5, 5), node_type(6, 5))}; + ext::vector<edge_type> res1e = grid4.successorEdges(node_type(5, 5)); + std::sort(ref1e.begin(), ref1e.end()); + std::sort(res1e.begin(), res1e.end()); + CHECK(ref1e == res1e); + + grid4.addObstacle(node_type(5, 5)); + + ext::set<node_type> ref2 = {}; + CHECK(ref2 == grid4.successors(node_type(5, 5))); + ext::vector<edge_type> ref2e = {}; + ext::vector<edge_type> res2e = grid4.successorEdges(node_type(5, 5)); + std::sort(ref2e.begin(), ref2e.end()); + std::sort(res2e.begin(), res2e.end()); + CHECK(ref2e == res2e); + + ext::set<node_type> ref3 = {node_type(4, 6), node_type(4, 4), node_type(3, 5)}; + CHECK(ref3 == grid4.successors(node_type(4, 5))); + ext::vector<edge_type> ref3e = {edge_type(node_type(4, 5), node_type(4, 6)), + edge_type(node_type(4, 5), node_type(4, 4)), + edge_type(node_type(4, 5), node_type(3, 5))}; + ext::vector<edge_type> res3e = grid4.successorEdges(node_type(4, 5)); + std::sort(ref3e.begin(), ref3e.end()); + std::sort(res3e.begin(), res3e.end()); + CHECK(ref3e == res3e); + + ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0)}; + CHECK(ref4 == grid4.successors(node_type(0, 0))); + ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1)), + edge_type(node_type(0, 0), node_type(1, 0))}; + ext::vector<edge_type> res4e = grid4.successorEdges(node_type(0, 0)); + std::sort(ref4e.begin(), ref4e.end()); + std::sort(res4e.begin(), res4e.end()); + CHECK(ref4e == res4e); + } + + // --------------------------------------------------------------------------------------------------------------------- + + SECTION ( "Grid8" ) { + grid::SquareGrid8<> grid8(10, 10); + using node_type = decltype(grid8)::node_type; + using edge_type = decltype(grid8)::edge_type; + + ext::set<node_type> ref1 = + {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), + node_type(6, 4), node_type(4, 4)}; + CHECK(ref1 == grid8.successors(node_type(5, 5))); + ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4)), + edge_type(node_type(5, 5), node_type(5, 6)), + edge_type(node_type(5, 5), node_type(4, 5)), + edge_type(node_type(5, 5), node_type(4, 6)), + edge_type(node_type(5, 5), node_type(6, 6)), + edge_type(node_type(5, 5), node_type(6, 4)), + edge_type(node_type(5, 5), node_type(4, 4)), + edge_type(node_type(5, 5), node_type(6, 5))}; + ext::vector<edge_type> res1e = grid8.successorEdges(node_type(5, 5)); + std::sort(ref1e.begin(), ref1e.end()); + std::sort(res1e.begin(), res1e.end()); + CHECK(ref1e == res1e); + + grid8.addObstacle(node_type(4, 4)); + + ext::set<node_type> ref2 = {}; + CHECK(ref2 == grid8.successors(node_type(4, 4))); + ext::vector<edge_type> ref2e = {}; + ext::vector<edge_type> res2e = grid8.successorEdges(node_type(4, 4)); + std::sort(ref2e.begin(), ref2e.end()); + std::sort(res2e.begin(), res2e.end()); + CHECK(ref2e == res2e); + + ext::set<node_type> + ref3 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), + node_type(6, 4)}; + CHECK(ref3 == grid8.successors(node_type(5, 5))); + ext::vector<edge_type> ref3e = {edge_type(node_type(5, 5), node_type(5, 4)), + edge_type(node_type(5, 5), node_type(5, 6)), + edge_type(node_type(5, 5), node_type(4, 5)), + edge_type(node_type(5, 5), node_type(4, 6)), + edge_type(node_type(5, 5), node_type(6, 6)), + edge_type(node_type(5, 5), node_type(6, 4)), + edge_type(node_type(5, 5), node_type(6, 5))}; + ext::vector<edge_type> res3e = grid8.successorEdges(node_type(5, 5)); + std::sort(ref3e.begin(), ref3e.end()); + std::sort(res3e.begin(), res3e.end()); + CHECK(ref3e == res3e); + + ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0), node_type(1, 1)}; + CHECK(ref4 == grid8.successors(node_type(0, 0))); + ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1)), + edge_type(node_type(0, 0), node_type(1, 1)), + edge_type(node_type(0, 0), node_type(1, 0))}; + ext::vector<edge_type> res4e = grid8.successorEdges(node_type(0, 0)); + std::sort(ref4e.begin(), ref4e.end()); + std::sort(res4e.begin(), res4e.end()); + CHECK(ref4e == res4e); + } + + // --------------------------------------------------------------------------------------------------------------------- + + SECTION ( "Grid4 Weighted" ) { + grid::WeightedSquareGrid4<> grid4(10, 10); + using node_type = decltype(grid4)::node_type; + using edge_type = decltype(grid4)::edge_type; + + ext::set<node_type> ref1 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5)}; + CHECK(ref1 == grid4.successors(node_type(5, 5))); + ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4), 1), + edge_type(node_type(5, 5), node_type(5, 6), 1), + edge_type(node_type(5, 5), node_type(4, 5), 1), + edge_type(node_type(5, 5), node_type(6, 5), 1)}; + ext::vector<edge_type> res1e = grid4.successorEdges(node_type(5, 5)); + std::sort(ref1e.begin(), ref1e.end()); + std::sort(res1e.begin(), res1e.end()); + CHECK(ref1e == res1e); + + grid4.addObstacle(node_type(5, 5)); + + ext::set<node_type> ref2 = {}; + CHECK(ref2 == grid4.successors(node_type(5, 5))); + ext::vector<edge_type> ref2e = {}; + ext::vector<edge_type> res2e = grid4.successorEdges(node_type(5, 5)); + std::sort(ref2e.begin(), ref2e.end()); + std::sort(res2e.begin(), res2e.end()); + CHECK(ref2e == res2e); + + ext::set<node_type> ref3 = {node_type(4, 6), node_type(4, 4), node_type(3, 5)}; + CHECK(ref3 == grid4.successors(node_type(4, 5))); + ext::vector<edge_type> ref3e = {edge_type(node_type(4, 5), node_type(4, 6), 1), + edge_type(node_type(4, 5), node_type(4, 4), 1), + edge_type(node_type(4, 5), node_type(3, 5), 1)}; + ext::vector<edge_type> res3e = grid4.successorEdges(node_type(4, 5)); + std::sort(ref3e.begin(), ref3e.end()); + std::sort(res3e.begin(), res3e.end()); + CHECK(ref3e == res3e); + + ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0)}; + CHECK(ref4 == grid4.successors(node_type(0, 0))); + ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1), 1), + edge_type(node_type(0, 0), node_type(1, 0), 1)}; + ext::vector<edge_type> res4e = grid4.successorEdges(node_type(0, 0)); + std::sort(ref4e.begin(), ref4e.end()); + std::sort(res4e.begin(), res4e.end()); + CHECK(ref4e == res4e); + } + + // --------------------------------------------------------------------------------------------------------------------- + + SECTION ( "Grid8 Weighted" ) { + grid::WeightedSquareGrid8<> grid8(10, 10); + using node_type = decltype(grid8)::node_type; + using edge_type = decltype(grid8)::edge_type; + + ext::set<node_type> ref1 = + {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), + node_type(6, 4), node_type(4, 4)}; + CHECK(ref1 == grid8.successors(node_type(5, 5))); + ext::vector<edge_type> ref1e = {edge_type(node_type(5, 5), node_type(5, 4), 1), + edge_type(node_type(5, 5), node_type(5, 6), 1), + edge_type(node_type(5, 5), node_type(4, 5), 1), + edge_type(node_type(5, 5), node_type(4, 6), M_SQRT2), + edge_type(node_type(5, 5), node_type(6, 6), M_SQRT2), + edge_type(node_type(5, 5), node_type(6, 4), M_SQRT2), + edge_type(node_type(5, 5), node_type(4, 4), M_SQRT2), + edge_type(node_type(5, 5), node_type(6, 5), 1)}; + ext::vector<edge_type> res1e = grid8.successorEdges(node_type(5, 5)); + std::sort(ref1e.begin(), ref1e.end()); + std::sort(res1e.begin(), res1e.end()); + CHECK(ref1e == res1e); + + grid8.addObstacle(node_type(4, 4)); + + ext::set<node_type> ref2 = {}; + CHECK(ref2 == grid8.successors(node_type(4, 4))); + ext::vector<edge_type> ref2e = {}; + ext::vector<edge_type> res2e = grid8.successorEdges(node_type(4, 4)); + std::sort(ref2e.begin(), ref2e.end()); + std::sort(res2e.begin(), res2e.end()); + CHECK(ref2e == res2e); + + ext::set<node_type> + ref3 = {node_type(5, 4), node_type(5, 6), node_type(4, 5), node_type(6, 5), node_type(4, 6), node_type(6, 6), + node_type(6, 4)}; + CHECK(ref3 == grid8.successors(node_type(5, 5))); + ext::vector<edge_type> ref3e = {edge_type(node_type(5, 5), node_type(5, 4), 1), + edge_type(node_type(5, 5), node_type(5, 6), 1), + edge_type(node_type(5, 5), node_type(4, 5), 1), + edge_type(node_type(5, 5), node_type(4, 6), M_SQRT2), + edge_type(node_type(5, 5), node_type(6, 6), M_SQRT2), + edge_type(node_type(5, 5), node_type(6, 4), M_SQRT2), + edge_type(node_type(5, 5), node_type(6, 5), 1)}; + ext::vector<edge_type> res3e = grid8.successorEdges(node_type(5, 5)); + std::sort(ref3e.begin(), ref3e.end()); + std::sort(res3e.begin(), res3e.end()); + CHECK(ref3e == res3e); + + ext::set<node_type> ref4 = {node_type(0, 1), node_type(1, 0), node_type(1, 1)}; + CHECK(ref4 == grid8.successors(node_type(0, 0))); + ext::vector<edge_type> ref4e = {edge_type(node_type(0, 0), node_type(0, 1), 1), + edge_type(node_type(0, 0), node_type(1, 1), M_SQRT2), + edge_type(node_type(0, 0), node_type(1, 0), 1)}; + ext::vector<edge_type> res4e = grid8.successorEdges(node_type(0, 0)); + std::sort(ref4e.begin(), ref4e.end()); + std::sort(res4e.begin(), res4e.end()); + CHECK(ref4e == res4e); + } } diff --git a/alib2graph_data/test-src/grid/GridTest.hpp b/alib2graph_data/test-src/grid/GridTest.hpp deleted file mode 100644 index d7392587f25a97cab38e283d28c1b4c7ed753986..0000000000000000000000000000000000000000 --- a/alib2graph_data/test-src/grid/GridTest.hpp +++ /dev/null @@ -1,35 +0,0 @@ -// GridTest.hpp -// -// Created on: 29. 03. 2018 -// Author: Jan Uhlik -// Modified by: -// -// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved. -// Git repository: https://gitlab.fit.cvut.cz/algorithms-library-toolkit/automata-library - -#ifndef ALIB2_GRIDTEST_HPP -#define ALIB2_GRIDTEST_HPP - -#include <cppunit/TestFixture.h> -#include <cppunit/extensions/HelperMacros.h> - -class GridTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(GridTest); - CPPUNIT_TEST(testGrid4); - CPPUNIT_TEST(testGrid8); - CPPUNIT_TEST(testGrid4Weighted); - CPPUNIT_TEST(testGrid8Weighted); - CPPUNIT_TEST_SUITE_END(); - - public: - void setUp() override; - void tearDown() override; - - void testGrid4(); - void testGrid8(); - void testGrid4Weighted(); - void testGrid8Weighted(); - -}; - -#endif //ALIB2_GRIDTEST_HPP diff --git a/alib2graph_data/test-src/main.cpp b/alib2graph_data/test-src/main.cpp index f98d8b477ee8676118c924d19411bf35bd858170..4ed06df1f7bea8cc18ee161389b9c3e2741b08a0 100644 --- a/alib2graph_data/test-src/main.cpp +++ b/alib2graph_data/test-src/main.cpp @@ -1,159 +1,2 @@ -#include <version.hpp> - -#include <tclap/CmdLine.h> - -#include <cppunit/CompilerOutputter.h> -#include <cppunit/extensions/TestFactoryRegistry.h> -#include <cppunit/ui/text/TestRunner.h> -#include <cppunit/TestResultCollector.h> -#include <cppunit/TestResult.h> -#include <cppunit/XmlOutputter.h> - -#include <cppunit/TestFailure.h> -#include <cppunit/SourceLine.h> -#include <cppunit/Exception.h> - -#include <exception/CommonException.h> - -CPPUNIT_NS_BEGIN - -class CPPUNIT_API TestProgressListener : public TestListener { - public: - TestProgressListener(); - - virtual ~TestProgressListener(); - - void startTest(Test *test); - - void addFailure(const TestFailure &failure); - - void endTest(Test *test); - - int getResult() const; - - void printResults() const; - - private: - TestProgressListener(const TestProgressListener ©); - - void operator=(const TestProgressListener ©); - - private: - int m_Failures; - int m_Tests; - int m_Assertions; - bool m_lastTestFailed; -}; - -TestProgressListener::TestProgressListener() : m_Failures(0), m_Tests(0), m_Assertions(0), m_lastTestFailed(false) { -} - -TestProgressListener::~TestProgressListener() { -} - -void TestProgressListener::startTest(Test *test) { - stdCOut() << test->getName() << ":" << "\n"; - stdCOut().flush(); - - m_lastTestFailed = false; - m_Tests++; -} - -void TestProgressListener::addFailure(const TestFailure &failure) { - stdCOut() << (failure.isError() ? "error" : "assertion") << " : " << failure.failedTestName() << " : " - << failure.sourceLine().lineNumber() << "\n"; - stdCOut() << "Exception " << failure.thrownException()->message().details(); - - m_lastTestFailed = true; - if (failure.isError()) m_Failures++; else m_Assertions++; -} - -void TestProgressListener::endTest(Test *test) { - stdCOut() << "Result (" << test->getName() << ")"; - stdCOut().flush(); - - if (!m_lastTestFailed) - stdCOut() << " : OK"; - else - stdCOut() << " : Fail"; - stdCOut() << "\n\n"; -} - -int TestProgressListener::getResult() const { - return m_Failures + m_Assertions; -} - -void TestProgressListener::printResults() const { - stdCOut() << "Overal result: Tests: " << m_Tests << " Assertions: " << m_Assertions << " Failures: " << m_Failures - << "\n"; -} - -CPPUNIT_NS_END - -int main(int argc, char *argv[]) { - try { - TCLAP::CmdLine cmd("Main test binary.", ' ', ALIB_VERSION_INFO); - - TCLAP::MultiArg<std::string> testPathSegments("p", "path", "test path", false, "string"); - cmd.add(testPathSegments); - - cmd.parse(argc, argv); - - CppUnit::TestResult controller; - - CppUnit::TestResultCollector result; - controller.addListener(&result); - - CppUnit::TestProgressListener progressListener; - controller.addListener(&progressListener); - - CppUnit::Test *suite = NULL; - std::string testPath = ""; - if (testPathSegments.getValue().size() == 0) { - // Get the top level suite from the registry - suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); - } else if (testPathSegments.getValue().size() == 1) { - suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest(); - } else { - suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest(); - bool first = true; - for (const std::string &path : testPathSegments.getValue()) { - if (first) { - first = false; - continue; - } - testPath += path + "/"; - } - testPath.pop_back(); - } - - // Adds the test to the list of test to run - CppUnit::TextUi::TestRunner runner; - runner.addTest(suite); - - // Change the default outputter to a compiler error format outputter - runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr)); - // Run the tests. - runner.run(controller, testPath); - - progressListener.printResults(); - - std::ofstream xmlFileResults("CppUnitTestResults.xml"); - CppUnit::XmlOutputter xmlOut(&result, xmlFileResults); - xmlOut.write(); - - return progressListener.getResult(); - } catch (const exception::CommonException &exception) { - std::cerr << exception.getCause() << std::endl; - return 1; - } catch (const TCLAP::ArgException &exception) { - std::cerr << exception.error() << std::endl; - return 2; - } catch (const std::exception &exception) { - std::cerr << "Exception caught: " << exception.what() << std::endl; - return 3; - } catch (...) { - std::cerr << "Unknown exception caught." << std::endl; - return 127; - } -} +#define CATCH_CONFIG_MAIN +#include <catch2/catch.hpp>