Skip to content

Commit

Permalink
Unit test for reading a network from file
Browse files Browse the repository at this point in the history
  • Loading branch information
MSallermann committed Mar 13, 2024
1 parent fd1f00f commit 5143d58
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
13 changes: 7 additions & 6 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ exe = executable('seldon', sources_seldon + 'src/main.cpp',


tests = [
['Test Tarjan', 'test/test_tarjan.cpp'],
['Test DeGroot', 'test/test_deGroot.cpp'],
['Test Activity Driven', 'test/test_activity.cpp'],
['Test Network', 'test/test_network.cpp'],
['Test Network Generation', 'test/test_network_generation.cpp'],
['Test Sampling', 'test/test_sampling.cpp'],
['Test_Tarjan', 'test/test_tarjan.cpp'],
['Test_DeGroot', 'test/test_deGroot.cpp'],
['Test_Activity Driven', 'test/test_activity.cpp'],
['Test_Network', 'test/test_network.cpp'],
['Test_Network Generation', 'test/test_network_generation.cpp'],
['Test_Sampling', 'test/test_sampling.cpp'],
['Test_IO', 'test/test_io.cpp'],
]

Catch2 = dependency('Catch2', method : 'cmake', modules : ['Catch2::Catch2WithMain', 'Catch2::Catch2'])
Expand Down
4 changes: 4 additions & 0 deletions test/res/network.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# idx_agent,n_neighbors_in,indices_neighbors_in[...],weights_in[...]
0, 2, 2, 1, 0.1, -0.2
1, 0
2, 1, 1, 1.2
35 changes: 35 additions & 0 deletions test/test_io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "catch2/matchers/catch_matchers.hpp"
#include "network.hpp"
#include "network_generation.hpp"

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>

#include <config_parser.hpp>
#include <filesystem>
#include <simulation.hpp>
namespace fs = std::filesystem;

TEST_CASE( "Test reading in the network from a file", "[io_network]" )
{
using namespace Seldon;
using namespace Catch::Matchers;

auto proj_root_path = fs::current_path();
auto network_file = proj_root_path / fs::path( "test/res/network.txt" );

auto network = Seldon::generate_from_file( network_file );

REQUIRE( network->n_agents() == 3 );

std::vector<std::vector<int>> neighbours_expected = { { 2, 1 }, {}, { 1 } };
std::vector<std::vector<Network::WeightT>> weights_expected = { { 0.1, -0.2 }, {}, { 1.2 } };

for( int i = 0; i < network->n_agents(); i++ )
{
fmt::print( "{}", i );
REQUIRE_THAT( neighbours_expected[i], Catch::Matchers::UnorderedRangeEquals( network->get_neighbours( i ) ) );
REQUIRE_THAT( weights_expected[i], Catch::Matchers::UnorderedRangeEquals( network->get_weights( i ) ) );
}
}

0 comments on commit 5143d58

Please sign in to comment.