Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for calculate_index function #495

Merged
merged 18 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/HealthGPS.Core/column_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <exception>
#include <limits>
#include <memory>
#include <stdexcept>
#include <vector>

#include "column.h"
Expand Down
2 changes: 2 additions & 0 deletions src/HealthGPS.Core/math_util.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "math_util.h"

#include <algorithm>
#include <cmath>

namespace hgps::core {
Expand Down
85 changes: 85 additions & 0 deletions src/HealthGPS.Tests/AnalysisModule.Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "pch.h"

#include "HealthGPS/analysis_module.h"

#include "simulation.h"

hgps::Person create_test_person(int age = 20,
hgps::core::Gender gender = hgps::core::Gender::male) {
auto person = hgps::Person{};
person.age = age;
person.gender = gender;
return person;
TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved
}

namespace hgps {
class TestAnalysisModule : public ::testing::Test {
protected:
hgps::core::DataTable data;

hgps::input::DataManager manager = hgps::input::DataManager(test_datastore_path);
hgps::CachedRepository repository = hgps::CachedRepository(manager);

hgps::ModelInput inputs = create_test_configuration(data);

std::unique_ptr<hgps::AnalysisModule> analysis_module =
build_analysis_module(repository, inputs);

hgps::Person test_person_1 = create_test_person(16, hgps::core::Gender::male);
hgps::Person test_person_2 = create_test_person(19, hgps::core::Gender::male);
hgps::DefaultEventBus bus = DefaultEventBus{};
hgps::SyncChannel channel;
std::unique_ptr<hgps::MTRandom32> rnd = std::make_unique<MTRandom32>(123456789);
TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved
std::unique_ptr<hgps::BaselineScenario> scenario = std::make_unique<BaselineScenario>(channel);
hgps::SimulationDefinition definition =
SimulationDefinition(inputs, std::move(scenario), std::move(rnd));

hgps::RuntimeContext context = RuntimeContext(bus, definition);

TestAnalysisModule() {
create_test_datatable(data);

auto config = create_test_configuration(data);

context.set_current_time(2024);

context.reset_population(12);

// Let's set some ages for the population.
// We will create a pair of male and female persons with sequential ages
for (size_t i = 0, j = 15; i < context.population().size(); i = i + 2, j++) {
context.population()[i].age = static_cast<unsigned>(j);
context.population()[i + 1].age = static_cast<unsigned>(j);
}
TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved

// Let's set half the population gender to male, and the other half to female
for (size_t i = 0; i < context.population().size(); i = i + 2) {
context.population()[i].gender = core::Gender::male;
TinyMarsh marked this conversation as resolved.
Show resolved Hide resolved
context.population()[i + 1].gender = core::Gender::female;
}

// For each person, we need to set the risk factors which we can get from channels_
for (size_t i = 0; i < context.population().size(); i++) {
for (const auto &factor : context.mapping().entries()) {
context.population()[i].risk_factors[factor.key()] = 1.0 + i;
}
}

auto ses_module = build_ses_noise_module(repository, config);
ses_module->initialise_population(context);

analysis_module->initialise_population(context);
}
};

TEST_F(TestAnalysisModule, CalculateIndex) {
// Test that the index is calculated correctly

size_t index_1 = analysis_module->calculate_index(test_person_1);
size_t index_2 = analysis_module->calculate_index(test_person_2);

ASSERT_EQ(index_1, 7 * 87);
ASSERT_EQ(index_2, 10 * 87);
}

} // namespace hgps
5 changes: 3 additions & 2 deletions src/HealthGPS.Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ target_sources(
"TestMain.cpp"
"WeightModel.Test.cpp"
"CountryModule.h"
"RiskFactorData.h"
"Interval.Test.cpp"
"LoadNutrientTable.cpp")
"LoadNutrientTable.cpp"
"AnalysisModule.Test.cpp"
"simulation.cpp")

target_link_libraries(
HealthGPS.Tests
Expand Down
273 changes: 0 additions & 273 deletions src/HealthGPS.Tests/RiskFactorData.h

This file was deleted.

Loading