Skip to content

Commit

Permalink
Tests for proper operation of deferred providers.
Browse files Browse the repository at this point in the history
Add tests to Bmi_Multi_Formulation_Test to make sure input values from
deferred providers are handled and pass properly.
  • Loading branch information
robertbartel authored and mattw-nws committed Mar 17, 2022
1 parent f8829a2 commit e78af11
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test/realizations/catchments/Bmi_Multi_Formulation_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
#include "gtest/gtest.h"
#include "Bmi_Multi_Formulation.hpp"
#include "Bmi_Module_Formulation.hpp"
#include "Bmi_Fortran_Formulation.hpp"
#include "Bmi_Py_Formulation.hpp"
#include "CsvPerFeatureForcingProvider.hpp"
#include "ConfigurationException.hpp"
#include "FileChecker.h"

#ifdef ACTIVATE_PYTHON
Expand Down Expand Up @@ -52,6 +55,13 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test {
return formulation.modules[nested_index]->get_bmi_main_output_var();
}

template <class N>
static double get_friend_nested_var_value(const Bmi_Multi_Formulation& formulation, const int mod_index,
const std::string& var_name) {
std::shared_ptr<N> nested = std::static_pointer_cast<N>(formulation.modules[mod_index]);
return nested->get_var_value_as_double(var_name);
}

/*
static std::vector<nested_module_ptr> get_friend_nested_formulations(Bmi_Multi_Formulation& formulation) {
return formulation.get_bmi_model();
Expand Down Expand Up @@ -613,6 +623,59 @@ TEST_F(Bmi_Multi_Formulation_Test, GetResponse_1_b) {
ASSERT_EQ(expected, response);
}

/**
* Simple test of get response in example 3, which uses a deferred provider.
*/
TEST_F(Bmi_Multi_Formulation_Test, GetResponse_3_a) {
int ex_index = 3;

Bmi_Multi_Formulation formulation(catchment_ids[ex_index], std::make_unique<CsvPerFeatureForcingProvider>(*forcing_params_examples[ex_index]), utils::StreamHandler());
formulation.create_formulation(config_prop_ptree[ex_index]);

double response = formulation.get_response(0, 3600);
ASSERT_EQ(response, 00);
}

/**
* Test of get response in example 3, which uses a deferred provider, after several iterations.
*/
TEST_F(Bmi_Multi_Formulation_Test, GetResponse_3_b) {
int ex_index = 3;

Bmi_Multi_Formulation formulation(catchment_ids[ex_index], std::make_unique<CsvPerFeatureForcingProvider>(*forcing_params_examples[ex_index]), utils::StreamHandler());
formulation.create_formulation(config_prop_ptree[ex_index]);

double response;
for (int i = 0; i < 39; i++) {
response = formulation.get_response(i, 3600);
}
double expected = 4.866464273262429e-08;
ASSERT_EQ(expected, response);
}

/**
* Test of get response in example 3, which uses a deferred provider, checking the values for several iterations.
*/
TEST_F(Bmi_Multi_Formulation_Test, GetResponse_3_c) {
int ex_index = 3;

Bmi_Multi_Formulation formulation(catchment_ids[ex_index], std::make_unique<CsvPerFeatureForcingProvider>(*forcing_params_examples[ex_index]), utils::StreamHandler());
formulation.create_formulation(config_prop_ptree[ex_index]);

double response, mod_0_input_1, mod_1_output_2;
for (int i = 0; i < 39; i++) {
// Note that we need to get this before the "current" get_response ...
mod_1_output_2 = get_friend_nested_var_value<Bmi_Py_Formulation>(formulation, 1, "OUTPUT_VAR_2");
response = formulation.get_response(i, 3600);
// But we need to get this after the "current" get_response ...
mod_0_input_1 = get_friend_nested_var_value<Bmi_Fortran_Formulation>(formulation, 0, "INPUT_VAR_1");
// ... and also, this won't work for the 0th index
if (i != 0) {
EXPECT_EQ(mod_0_input_1, mod_1_output_2);
}
}
}

/**
* Simple test of output for example 0.
*/
Expand Down

0 comments on commit e78af11

Please sign in to comment.