Skip to content

Commit

Permalink
Add Tests for the Spanning Tree Algorithm (#84)
Browse files Browse the repository at this point in the history
This PR adds the tests for the spanning tree algorithm to the framework.
  • Loading branch information
franziska-wegner authored Jan 4, 2024
1 parent 2b7d961 commit 65d978a
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/Algorithms/SpanningTree/TestSpanningTree.cpp
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

View workflow job for this annotation

GitHub Actions / cpp-linter (ubuntu-latest, Debug, gcc, g++)

Run clang-format on tests/Algorithms/SpanningTree/TestSpanningTree.cpp

File tests/Algorithms/SpanningTree/TestSpanningTree.cpp does not conform to Custom style guidelines. (lines 10, 14, 22, 30, 31, 32)
* TestSpanningTree.cpp
*
* Created on: May 8, 2019
* Author: Franziska Wegner, 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
114 changes: 114 additions & 0 deletions tests/Algorithms/SpanningTree/TestSpanningTree.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* TestSpanningTree.hpp
*
* Created on: May 8, 2019
* Author: Franziska Wegner, 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"

Check failure on line 11 in tests/Algorithms/SpanningTree/TestSpanningTree.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter (ubuntu-latest, Debug, gcc, g++)

tests/Algorithms/SpanningTree/TestSpanningTree.hpp:11:10 [clang-diagnostic-error]

'gtest/gtest.h' file not found
#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

0 comments on commit 65d978a

Please sign in to comment.