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 625bcf2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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 625bcf2

Please sign in to comment.