-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the spanning tree algorithm
- Loading branch information
1 parent
2b7d961
commit 6749f4f
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Check notice on line 1 in tests/Algorithms/SpanningTree/TestSpanningTree.cpp GitHub Actions / cpp-linter (ubuntu-latest, Debug, gcc, g++)Run clang-format on tests/Algorithms/SpanningTree/TestSpanningTree.cpp
|
||
* TestSpanningTree.cpp | ||
* | ||
* Created on: May 8, 2019 | ||
* Author: Matthias Wolf | ||
*/ | ||
|
||
#include "TestSpanningTree.hpp" | ||
|
||
namespace egoa::test { | ||
|
||
#pragma mark EMPTY_GRAPH | ||
|
||
TYPED_TEST(TestSpanningTreeEmpty, Run) { | ||
Subgraph<TGraph> expectedResult(&this->graph_, {}, {}); | ||
|
||
this->algo_.Run(); | ||
auto result = this->algo_.Result(); | ||
EXPECT_EQ(expectedResult, result); | ||
} | ||
|
||
TYPED_TEST(TestSpanningTreeTriangle, Run) { | ||
Subgraph<TGraph> expectedResult(&this->graph_, {0, 1, 2}, {0, 1}); | ||
|
||
this->algo_.Run(); | ||
auto result = this->algo_.Result(); | ||
EXPECT_EQ(expectedResult, result); | ||
} | ||
|
||
TYPED_TEST(TestSpanningTreeFiveVertexGraph, Run) { | ||
Subgraph<TGraph> expectedResult(&this->graph_, | ||
{0, 1, 2, 3, 4}, | ||
{0, 3, 4, 5}); | ||
|
||
this->algo_.Run(); | ||
auto result = this->algo_.Result(); | ||
EXPECT_EQ(expectedResult, result); | ||
} | ||
|
||
} // namespace egoa::test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* TestSpanningTree.hpp | ||
* | ||
* Created on: May 8, 2019 | ||
* Author: Matthias Wolf | ||
*/ | ||
|
||
#ifndef EGOA___TESTS___ALGORITHMS___SPANNING_TREE__TEST_SPANNING_TREE_HPP | ||
#define EGOA___TESTS___ALGORITHMS___SPANNING_TREE__TEST_SPANNING_TREE_HPP | ||
|
||
#include "gtest/gtest.h" | ||
#include "gmock/gmock.h" | ||
|
||
#include <vector> | ||
|
||
#include "Auxiliary/Comparators.hpp" | ||
#include "Auxiliary/Types.hpp" | ||
|
||
#include "Algorithms/SpanningTree/Kruskal.hpp" | ||
#include "Algorithms/SpanningTree/Prim.hpp" | ||
|
||
#include "DataStructures/Graphs/StaticGraph.hpp" | ||
|
||
#include "Helper/TestHelper.hpp" | ||
|
||
namespace egoa::test { | ||
|
||
using TGraph = egoa::StaticGraph<MinimalProperties, MinimalProperties>; | ||
using TWeight = Types::integer; | ||
using TWeightVector = std::vector<TWeight>; | ||
|
||
template<typename GraphType> | ||
class PrivateIdBasedComparator { | ||
public: | ||
PrivateIdBasedComparator(GraphType const & graph) : graph_(graph) {} | ||
|
||
bool operator()(egoa::Types::edgeId lhs, egoa::Types::edgeId rhs) { | ||
return graph_.EdgeAt(lhs).Properties().PrivateId() | ||
< graph_.EdgeAt(rhs).Properties().PrivateId(); | ||
} | ||
|
||
private: | ||
GraphType const & graph_; | ||
}; | ||
|
||
/** | ||
* @brief A fixture for testing MST algorithms on empty graphs. | ||
* | ||
* @tparam AlgorithmType The type of the algorithm under test. | ||
*/ | ||
template<typename AlgorithmType> | ||
class TestSpanningTreeEmpty : public ::testing::Test { | ||
protected: | ||
using TAlgorithm = AlgorithmType; | ||
|
||
TGraph graph_; | ||
TWeightVector weights_; | ||
TAlgorithm algo_{graph_, PrivateIdBasedComparator(graph_)}; | ||
}; | ||
|
||
template<typename AlgorithmType> | ||
class TestSpanningTreeTriangle : public ::testing::Test { | ||
protected: | ||
using TAlgorithm = AlgorithmType; | ||
|
||
TestSpanningTreeTriangle() { | ||
graph_.AddVertex(MinimalProperties(0)); | ||
graph_.AddVertex(MinimalProperties(1)); | ||
graph_.AddVertex(MinimalProperties(2)); | ||
graph_.AddEdge(0, 1, MinimalProperties(12)); | ||
graph_.AddEdge(1, 2, MinimalProperties(21)); | ||
graph_.AddEdge(0, 2, MinimalProperties(23)); | ||
} | ||
|
||
TGraph graph_; | ||
TAlgorithm algo_{graph_, PrivateIdBasedComparator(graph_)}; | ||
}; | ||
|
||
|
||
template<typename AlgorithmType> | ||
class TestSpanningTreeFiveVertexGraph : public ::testing::Test { | ||
protected: | ||
using TAlgorithm = AlgorithmType; | ||
|
||
TestSpanningTreeFiveVertexGraph() { | ||
graph_.AddVertex(MinimalProperties(0)); | ||
graph_.AddVertex(MinimalProperties(1)); | ||
graph_.AddVertex(MinimalProperties(2)); | ||
graph_.AddVertex(MinimalProperties(3)); | ||
graph_.AddVertex(MinimalProperties(4)); | ||
graph_.AddEdge(0, 1, MinimalProperties(12)); | ||
graph_.AddEdge(1, 2, MinimalProperties(15)); | ||
graph_.AddEdge(0, 2, MinimalProperties(9)); | ||
graph_.AddEdge(3, 0, MinimalProperties(8)); | ||
graph_.AddEdge(4, 3, MinimalProperties(2)); | ||
graph_.AddEdge(4, 2, MinimalProperties(8)); | ||
} | ||
|
||
TGraph graph_; | ||
TAlgorithm algo_{graph_, PrivateIdBasedComparator(graph_)}; | ||
}; | ||
|
||
using AlgorithmTypes = ::testing::Types< | ||
egoa::Kruskal<TGraph>, | ||
egoa::Prim<TGraph> | ||
>; | ||
|
||
TYPED_TEST_SUITE(TestSpanningTreeEmpty, AlgorithmTypes); | ||
TYPED_TEST_SUITE(TestSpanningTreeTriangle, AlgorithmTypes); | ||
TYPED_TEST_SUITE(TestSpanningTreeFiveVertexGraph, AlgorithmTypes); | ||
|
||
} // namespace egoa::test | ||
|
||
#endif //EGOA___TESTS___ALGORITHMS___SPANNING_TREE__TEST_SPANNING_TREE_HPP |