Skip to content

Commit

Permalink
adding some unit tests for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Becheler committed Nov 11, 2021
1 parent 9f4be50 commit e16f886
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
#include <vector>
#include "assert.h"
#include <iostream>

#include <fstream>

namespace quetzal
{
namespace demography
Expand Down Expand Up @@ -109,7 +110,6 @@ namespace quetzal
return v;
}

private:
/**
* \brief Checks if population size is defined at deme x at time t
* \return True if variable is defined, else false.
Expand All @@ -121,6 +121,8 @@ namespace quetzal
&& (m_populations.at(t).find(x) != m_populations.at(t).end());
}

private:

std::string get_archive_name(time_type t) const
{
const std::string prefix = "N-";
Expand Down
96 changes: 54 additions & 42 deletions test/unit_test/demography_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,60 @@ BOOST_AUTO_TEST_CASE( population_size_default )
std::cout << "}\n" << std::endl;
}

// BOOST_AUTO_TEST_CASE( population_size_optimized )
// {
// using coord_type = unsigned int;
// std::vector<coord_type> demes = {0,1,2,3,4,5,6};
// unsigned int duration = 10;
// double N_0 = 1000;
// coord_type x_0 = 0;
// // Constructor
// quetzal::demography::PopulationSizeVectorImplementation N(demes, duration);
// assert( N(0,0) == 1000 );
// // Assignment
// N(x_0, 0) = N_0;
// assert(N(0,0) == N_0);
// }
BOOST_AUTO_TEST_CASE( population_size_on_disk )
{
// Data types declaration
using coord_type = std::string;
using time_type = unsigned int;
using size_type = unsigned int;
// Initializing some data
time_type t0 = 0;
coord_type x0 = "A";
size_type N0 = 12;

quetzal::demography::PopulationSizeOnDiskImplementation<coord_type, time_type, size_type> N;
BOOST_TEST( !N.is_defined(x0, t0) );
// Set first population size
N.set(x0, t0, N0);
BOOST_TEST( N.is_defined(x0, t0) );
BOOST_CHECK_EQUAL( N.get(x0,t0) , N0 );
BOOST_CHECK_EQUAL( N.definition_space(t0).size() , 1 );
// Set second population time
N.set("B", t0, 2*N0);
BOOST_CHECK_EQUAL( N.definition_space(t0).size() , 2 );
// Retrieve definition space (demes with population size > 0)
auto X = N.definition_space(t0);
}

BOOST_AUTO_TEST_CASE( population_size_on_disk_moving_windows )
{
// Data types declaration
using coord_type = int;
using time_type = unsigned int;
using size_type = unsigned int;
// Initializing some data
time_type t0 = 0;
coord_type x0 = 0;
size_type N0 = 1;

quetzal::demography::PopulationSizeOnDiskImplementation<coord_type, time_type, size_type> N;
BOOST_TEST( !N.is_defined(x0, t0) );
// Set first population size
N.set(x0, t0, N0);
BOOST_TEST( N.is_defined(x0, t0) );
BOOST_CHECK_EQUAL( N.get(x0,t0) , N0 );
BOOST_CHECK_EQUAL( N.definition_space(t0).size() , 1 );
// Set other population sizes
for(unsigned int i=1; i <= 10; ++i)
{
N.set(i, i, 2*N.get(i-1,i-1));
}
BOOST_CHECK_EQUAL( N.definition_space(10).size() , 1 );
BOOST_CHECK_EQUAL( N.get(10,10) , 1024 );
BOOST_CHECK_EQUAL( N.get(1,1) , 2 );
BOOST_CHECK_EQUAL( N.get(0,0) , 1 );
BOOST_CHECK_EQUAL( N.get(5,5) , 32 );
}

BOOST_AUTO_TEST_CASE( flow )
{
Expand Down Expand Up @@ -149,32 +189,4 @@ BOOST_AUTO_TEST_CASE( individual_based_history_default_storage )

}

// const bool pre_allocated_implemented = false;
//
// BOOST_AUTO_TEST_CASE( mass_based_history_default_storage, * utf::enable_if<pre_allocated_implemented>() )
// {
// using time_type = unsigned int;
// using coord_type = std::string;
// using generator_type = std::mt19937;
// // Number of non-overlapping generations for the demographic simulation
// unsigned int nb_generations = 5;
// std::vector<coord_type> demes {"A","B"};
// unsigned int N0 = 10;
// coord_type x0 = "A";
// // Declare a mass-based history, where gene copies are assumed to be divisible quantities
// using quetzal::demography::dispersal_policy::mass_based;
// // Declare memory should be pre-allocated: best for long histories
// using quetzal::memory::pre_allocated;
// quetzal::demography::History<coord_type, mass_based, pre_allocated> history(x0, N0, nb_generations, demes);
// // Growth function
// auto N = history.get_functor_N();
// auto growth = [N](auto&, coord_type x, time_type t){ return 2*N(x,t) ; };
// // Random number generation
// generator_type gen;
// transition_matrix M;
// history.simulate_forward(growth, M, gen);
// std::cout << "\nKnowing an indiviual was in deme A at time 4, simulate its location at time 3?\n";
// std::cout << "Location at t=3: " << history.backward_kernel("A", 4, gen) << std::endl;
// }

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e16f886

Please sign in to comment.