Skip to content

Commit

Permalink
DFS paths: Test for search
Browse files Browse the repository at this point in the history
Added a test for NetworkOperation class, for the DFS path between a
source and vertex
  • Loading branch information
amritagos committed May 31, 2024
1 parent dc405cc commit f9d3525
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ graphlib_dep = declare_dependency(include_directories : inc)

tests = [
['Test_Directed_Network', 'test/test_directed_network.cpp'],
['Test_Network_Operations', 'test/test_network_operations.cpp']
]

test_inc = []
Expand All @@ -17,9 +18,11 @@ test_inc += 'test/util'

Catch2 = dependency('Catch2', method : 'cmake', modules : ['Catch2::Catch2WithMain', 'Catch2::Catch2'])

fmt_dep = dependency('fmt')

foreach t : tests
exe = executable(t.get(0), t.get(1),
dependencies : [Catch2],
dependencies : [Catch2, fmt_dep],
include_directories : test_inc
)
test(t.get(0), exe, workdir : meson.project_source_root())
Expand Down
2 changes: 0 additions & 2 deletions test/test_directed_network.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "directed_network.hpp"
#include "util/network_generation.hpp"
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>
#include <cstddef>
#include <random>
#include <set>
#include <vector>

Expand Down
44 changes: 44 additions & 0 deletions test/test_network_operations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "catch2/matchers/catch_matchers.hpp"
#include "network_operations.hpp"
#include "undirected_network.hpp"
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>
#include <cstddef>
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <memory>
#include <set>
#include <vector>

TEST_CASE("Testing network operations") {
using namespace Graph;
using UndirectedNetwork = UndirectedNetwork<double>;
using WeightT = double;

// Generate some network
const auto n_agents = 6;
auto network = UndirectedNetwork(n_agents);
const WeightT weight_edge = 1.0;
const auto total_n_edges = 5;
// Add edges to the network
network.push_back_neighbour_and_weight(0, 2, weight_edge);
network.push_back_neighbour_and_weight(2, 1, weight_edge);
network.push_back_neighbour_and_weight(2, 3, weight_edge);
network.push_back_neighbour_and_weight(3, 4, weight_edge);
network.push_back_neighbour_and_weight(3, 5, weight_edge);

// Check that the network has been created with the prescribed number of edges
REQUIRE(network.n_edges() == total_n_edges);
// There is only one edge connected to 0
REQUIRE(network.n_edges(0) == 1);

// Create a graph traversal object
auto graph_traversal = NetworkOperations<WeightT>(network);

// Find a path from 0 to 5
const auto path_ref = std::vector<size_t>{0, 2, 3, 5}; // Desired path
auto path = graph_traversal.path_from_dfs(0, 5);
INFO(fmt::format("path from DFS = {}\n", path.value()));

REQUIRE_THAT(path.value(), Catch::Matchers::UnorderedRangeEquals(path_ref));
}

0 comments on commit f9d3525

Please sign in to comment.