diff --git a/tests/gtest/test_population_test_fixture.hpp b/tests/gtest/test_population_test_fixture.hpp index aaca79aa..9cf1424a 100644 --- a/tests/gtest/test_population_test_fixture.hpp +++ b/tests/gtest/test_population_test_fixture.hpp @@ -2,65 +2,59 @@ #include "population_dynamics/population/population.hpp" -namespace -{ - - // Use test fixture to reuse the same configuration of objects for - // several different tests. To use a test fixture, derive a class - // from testing::Test. - class PopulationInitializeTestFixture : public testing::Test - { - // Make members protected and they can be accessed from - // sub-classes. - protected: - // Use SetUp function to prepare the objects for each test. - // Use override in C++11 to make sure SetUp (e.g., not Setup with - // a lowercase u) is spelled - // correctly. - void SetUp() override - { - population.id_g = id_g; - population.nyears = nyears; - population.nseasons = nseasons; - population.nages = nages; - for (int i = 0; i < nfleets; i++) - { - auto fleet = std::make_shared>(); - population.fleets.push_back(fleet); - } +namespace { + +// Use test fixture to reuse the same configuration of objects for +// several different tests. To use a test fixture, derive a class +// from testing::Test. +class PopulationInitializeTestFixture : public testing::Test { + // Make members protected and they can be accessed from + // sub-classes. + protected: + // Use SetUp function to prepare the objects for each test. + // Use override in C++11 to make sure SetUp (e.g., not Setup with + // a lowercase u) is spelled + // correctly. + void SetUp() override { + population.id_g = id_g; + population.nyears = nyears; + population.nseasons = nseasons; + population.nages = nages; + for (int i = 0; i < nfleets; i++) { + auto fleet = std::make_shared>(); + population.fleets.push_back(fleet); } - - // Virtual void TearDown() will be called after each test is - // run. It needs to be defined if there is clearup work to - // do. Otherwise, it does not need to be provided. - virtual void TearDown() {} - - fims_popdy::Population population; - - // Use default values from the Li et al., 2021 - // https://github.com/Bai-Li-NOAA/Age_Structured_Stock_Assessment_Model_Comparison/blob/master/R/save_initial_input.R - int id_g = 0; - int nyears = 30; - int nseasons = 1; - int nages = 12; - int nfleets = 2; - }; - - class PopulationEvaluateTestFixture : public testing::Test - { - protected: - void SetUp() override - { - population.id_g = id_g; - population.nyears = nyears; - population.nseasons = nseasons; - population.nages = nages; - population.nfleets = nfleets; - - // C++ code to set up true values for log_naa, log_M, - // log_Fmort, and log_q: - int seed = 1234; - std::default_random_engine generator(seed); + } + + // Virtual void TearDown() will be called after each test is + // run. It needs to be defined if there is clearup work to + // do. Otherwise, it does not need to be provided. + virtual void TearDown() {} + + fims_popdy::Population population; + + // Use default values from the Li et al., 2021 + // https://github.com/Bai-Li-NOAA/Age_Structured_Stock_Assessment_Model_Comparison/blob/master/R/save_initial_input.R + int id_g = 0; + int nyears = 30; + int nseasons = 1; + int nages = 12; + int nfleets = 2; +}; + +class PopulationEvaluateTestFixture : public testing::Test { + protected: + void SetUp() override { + population.id_g = id_g; + population.nyears = nyears; + population.nseasons = nseasons; + population.nages = nages; + population.nfleets = nfleets; + + // C++ code to set up true values for log_naa, log_M, + // log_Fmort, and log_q: + int seed = 1234; + std::default_random_engine generator(seed); // log_Fmort double log_Fmort_min = fims_math::log(0.1); @@ -80,242 +74,224 @@ namespace // and population object needs a shared pointer in population.hpp // (std::vector > > fleets;) - // Does Fmort need to be in side of the year loop like log_q? - for (int i = 0; i < nfleets; i++) - { - auto fleet = std::make_shared>(); - auto selectivity = + // Does Fmort need to be in side of the year loop like log_q? + for (int i = 0; i < nfleets; i++) { + auto fleet = std::make_shared>(); + auto selectivity = std::make_shared>(); - selectivity->inflection_point = 7; - selectivity->slope = 0.5; - - fleet->Initialize(nyears, nages); - fleet->selectivity = selectivity; - fleet->log_q = log_q_distribution(generator); - for (int year = 0; year < nyears; year++) - { - fleet->log_Fmort[year] = log_Fmort_distribution(generator); - } - if (i == 0) - { - fleet->is_survey = true; - } - fleet->Prepare(); - population.fleets.push_back(fleet); + selectivity->inflection_point = 7; + selectivity->slope = 0.5; + + fleet->Initialize(nyears, nages); + fleet->selectivity = selectivity; + fleet->log_q = log_q_distribution(generator); + for (int year = 0; year < nyears; year++) { + fleet->log_Fmort[year] = log_Fmort_distribution(generator); } - - population.Initialize(nyears, nseasons, nages); - - for (int i = 0; i < nages; i++) - { - population.ages[i] = i + 1; + if (i == 0) { + fleet->is_survey = true; } + fleet->Prepare(); + population.fleets.push_back(fleet); + } - // log_naa - double log_init_naa_min = 10.0; - double log_init_naa_max = 12.0; - std::uniform_real_distribution log_naa_distribution( - log_init_naa_min, log_init_naa_max); - for (int i = 0; i < nages; i++) - { - population.log_init_naa[i] = log_naa_distribution(generator); - } + population.Initialize(nyears, nseasons, nages); - // log_M - double log_M_min = fims_math::log(0.1); - double log_M_max = fims_math::log(0.3); - std::uniform_real_distribution log_M_distribution(log_M_min, - log_M_max); - for (int i = 0; i < nyears * nages; i++) - { - population.log_M[i] = log_M_distribution(generator); - } + for (int i = 0; i < nages; i++) { + population.ages[i] = i + 1; + } - // numbers_at_age - double numbers_at_age_min = fims_math::exp(10.0); - double numbers_at_age_max = fims_math::exp(12.0); - std::uniform_real_distribution numbers_at_age_distribution( - numbers_at_age_min, numbers_at_age_max); - for (int i = 0; i < (nyears + 1) * nages; i++) - { - population.numbers_at_age[i] = numbers_at_age_distribution(generator); - } + // log_naa + double log_init_naa_min = 10.0; + double log_init_naa_max = 12.0; + std::uniform_real_distribution log_naa_distribution( + log_init_naa_min, log_init_naa_max); + for (int i = 0; i < nages; i++) { + population.log_init_naa[i] = log_naa_distribution(generator); + } - // weight_at_age - double weight_at_age_min = 0.5; - double weight_at_age_max = 12.0; - - std::shared_ptr> growth = - std::make_shared>(); - std::uniform_real_distribution weight_at_age_distribution( - weight_at_age_min, weight_at_age_max); - for (int i = 0; i < nages; i++) - { - growth->ewaa[static_cast(population.ages[i])] = - weight_at_age_distribution(generator); - } + // log_M + double log_M_min = fims_math::log(0.1); + double log_M_max = fims_math::log(0.3); + std::uniform_real_distribution log_M_distribution(log_M_min, + log_M_max); + for (int i = 0; i < nyears * nages; i++) { + population.log_M[i] = log_M_distribution(generator); + } - population.growth = growth; + // numbers_at_age + double numbers_at_age_min = fims_math::exp(10.0); + double numbers_at_age_max = fims_math::exp(12.0); + std::uniform_real_distribution numbers_at_age_distribution( + numbers_at_age_min, numbers_at_age_max); + for (int i = 0; i < (nyears + 1) * nages; i++) { + population.numbers_at_age[i] = numbers_at_age_distribution(generator); + } - population.Prepare(); + // weight_at_age + double weight_at_age_min = 0.5; + double weight_at_age_max = 12.0; + + std::shared_ptr> growth = + std::make_shared>(); + std::uniform_real_distribution weight_at_age_distribution( + weight_at_age_min, weight_at_age_max); + for (int i = 0; i < nages; i++) { + growth->ewaa[static_cast(population.ages[i])] = + weight_at_age_distribution(generator); + } - auto maturity = std::make_shared>(); - maturity->inflection_point = 6; - maturity->slope = 0.15; - population.maturity = maturity; + population.growth = growth; - auto recruitment = std::make_shared>(); - recruitment->logit_steep = fims_math::logit(0.2, 1.0, 0.75); - recruitment->log_rzero = fims_math::log(1000000.0); - recruitment->recruit_deviations.resize(nyears); - for (int i = 0; i < recruitment->recruit_deviations.size(); i++) { - recruitment->recruit_deviations[i] = 1.0; - } - population.recruitment = recruitment; + population.Prepare(); - int year = 4; - int age = 6; - int i_age_year = year * population.nages + age; - int i_agem1_yearm1 = (year - 1) * population.nages + age - 1; + auto maturity = std::make_shared>(); + maturity->inflection_point = 6; + maturity->slope = 0.15; + population.maturity = maturity; - population.CalculateMortality(i_age_year, year, age); - population.CalculateNumbersAA(i_age_year, i_agem1_yearm1, age); + auto recruitment = std::make_shared>(); + recruitment->logit_steep = fims_math::logit(0.2, 1.0, 0.75); + recruitment->log_rzero = fims_math::log(1000000.0); + recruitment->recruit_deviations.resize(nyears); + for (int i = 0; i < recruitment->recruit_deviations.size(); i++) { + recruitment->recruit_deviations[i] = 1.0; } - - virtual void TearDown() {} - - fims_popdy::Population population; - int id_g = 0; - int nyears = 30; - int nseasons = 1; - int nages = 12; - int nfleets = 2; + population.recruitment = recruitment; int year = 4; int age = 6; - int i_age_year = year * nages + age; - int i_agem1_yearm1 = (year - 1) * nages + age - 1; - - }; - - class PopulationPrepareTestFixture : public testing::Test - { - protected: - void SetUp() override - { - population.id_g = id_g; - population.nyears = nyears; - population.nseasons = nseasons; - population.nages = nages; - population.nfleets = nfleets; - - // C++ code to set up true values for log_Fmort, and log_q: - int seed = 1234; - std::default_random_engine generator(seed); - - // log_Fmort - double log_Fmort_min = fims_math::log(0.1); - double log_Fmort_max = fims_math::log(2.3); - std::uniform_real_distribution log_Fmort_distribution( - log_Fmort_min, log_Fmort_max); - - // log_q - double log_q_min = fims_math::log(0.1); - double log_q_max = fims_math::log(1); - std::uniform_real_distribution log_q_distribution(log_q_min, - log_q_max); - - // Make a shared pointer to selectivity and fleet because - // fleet object needs a shared pointer in fleet.hpp - // (std::shared_ptr > selectivity;) - // and population object needs a shared pointer in population.hpp - // (std::vector > > fleets;) - - for (int i = 0; i < nfleets; i++) - { - auto fleet = std::make_shared>(); - auto selectivity = std::make_shared>(); - selectivity->inflection_point = 7; - selectivity->slope = 0.5; - - fleet->Initialize(nyears, nages); - fleet->selectivity = selectivity; - fleet->log_q = log_q_distribution(generator); - for (int year = 0; year < nyears; year++) - { - fleet->log_Fmort[year] = log_Fmort_distribution(generator); - } - if (i == 0) - { - fleet->is_survey = true; - } - fleet->Prepare(); - population.fleets.push_back(fleet); - } + int i_age_year = year * population.nages + age; + int i_agem1_yearm1 = (year - 1) * population.nages + age - 1; + + population.CalculateMortality(i_age_year, year, age); + population.CalculateNumbersAA(i_age_year, i_agem1_yearm1, age); + } + + virtual void TearDown() {} + + fims_popdy::Population population; + int id_g = 0; + int nyears = 30; + int nseasons = 1; + int nages = 12; + int nfleets = 2; + + int year = 4; + int age = 6; + int i_age_year = year * nages + age; + int i_agem1_yearm1 = (year - 1) * nages + age - 1; +}; + +class PopulationPrepareTestFixture : public testing::Test { + protected: + void SetUp() override { + population.id_g = id_g; + population.nyears = nyears; + population.nseasons = nseasons; + population.nages = nages; + population.nfleets = nfleets; + + // C++ code to set up true values for log_Fmort, and log_q: + int seed = 1234; + std::default_random_engine generator(seed); + + // log_Fmort + double log_Fmort_min = fims_math::log(0.1); + double log_Fmort_max = fims_math::log(2.3); + std::uniform_real_distribution log_Fmort_distribution( + log_Fmort_min, log_Fmort_max); - population.Initialize(nyears, nseasons, nages); + // log_q + double log_q_min = fims_math::log(0.1); + double log_q_max = fims_math::log(1); + std::uniform_real_distribution log_q_distribution(log_q_min, + log_q_max); - for (int i = 0; i < nages; i++) - { - population.ages[i] = i + 1; - } + // Make a shared pointer to selectivity and fleet because + // fleet object needs a shared pointer in fleet.hpp + // (std::shared_ptr > selectivity;) + // and population object needs a shared pointer in population.hpp + // (std::vector > > fleets;) - // log_naa - double log_init_naa_min = 10.0; - double log_init_naa_max = 12.0; - std::uniform_real_distribution log_naa_distribution( - log_init_naa_min, log_init_naa_max); - for (int i = 0; i < nages; i++) - { - population.log_init_naa[i] = log_naa_distribution(generator); + for (int i = 0; i < nfleets; i++) { + auto fleet = std::make_shared>(); + auto selectivity = + std::make_shared>(); + selectivity->inflection_point = 7; + selectivity->slope = 0.5; + + fleet->Initialize(nyears, nages); + fleet->selectivity = selectivity; + fleet->log_q = log_q_distribution(generator); + for (int year = 0; year < nyears; year++) { + fleet->log_Fmort[year] = log_Fmort_distribution(generator); } - - // log_M - double log_M_min = fims_math::log(0.1); - double log_M_max = fims_math::log(0.3); - std::uniform_real_distribution log_M_distribution(log_M_min, - log_M_max); - for (int i = 0; i < nyears * nages; i++) - { - population.log_M[i] = log_M_distribution(generator); + if (i == 0) { + fleet->is_survey = true; } + fleet->Prepare(); + population.fleets.push_back(fleet); + } - // numbers_at_age - double numbers_at_age_min = fims_math::exp(10.0); - double numbers_at_age_max = fims_math::exp(12.0); - std::uniform_real_distribution numbers_at_age_distribution( - numbers_at_age_min, numbers_at_age_max); - for (int i = 0; i < (nyears + 1) * nages; i++) - { - population.numbers_at_age[i] = numbers_at_age_distribution(generator); - } + population.Initialize(nyears, nseasons, nages); - // weight_at_age - double weight_at_age_min = 0.5; - double weight_at_age_max = 12.0; - - std::shared_ptr> growth = - std::make_shared>(); - std::uniform_real_distribution weight_at_age_distribution( - weight_at_age_min, weight_at_age_max); - for (int i = 0; i < nages; i++) - { - growth->ewaa[static_cast(population.ages[i])] = - weight_at_age_distribution(generator); - } + for (int i = 0; i < nages; i++) { + population.ages[i] = i + 1; + } - population.growth = growth; + // log_naa + double log_init_naa_min = 10.0; + double log_init_naa_max = 12.0; + std::uniform_real_distribution log_naa_distribution( + log_init_naa_min, log_init_naa_max); + for (int i = 0; i < nages; i++) { + population.log_init_naa[i] = log_naa_distribution(generator); + } - population.Prepare(); + // log_M + double log_M_min = fims_math::log(0.1); + double log_M_max = fims_math::log(0.3); + std::uniform_real_distribution log_M_distribution(log_M_min, + log_M_max); + for (int i = 0; i < nyears * nages; i++) { + population.log_M[i] = log_M_distribution(generator); } - virtual void TearDown() {} + // numbers_at_age + double numbers_at_age_min = fims_math::exp(10.0); + double numbers_at_age_max = fims_math::exp(12.0); + std::uniform_real_distribution numbers_at_age_distribution( + numbers_at_age_min, numbers_at_age_max); + for (int i = 0; i < (nyears + 1) * nages; i++) { + population.numbers_at_age[i] = numbers_at_age_distribution(generator); + } + + // weight_at_age + double weight_at_age_min = 0.5; + double weight_at_age_max = 12.0; + + std::shared_ptr> growth = + std::make_shared>(); + std::uniform_real_distribution weight_at_age_distribution( + weight_at_age_min, weight_at_age_max); + for (int i = 0; i < nages; i++) { + growth->ewaa[static_cast(population.ages[i])] = + weight_at_age_distribution(generator); + } + + population.growth = growth; + + population.Prepare(); + } + + virtual void TearDown() {} - fims_popdy::Population population; - int id_g = 0; - int nyears = 30; - int nseasons = 1; - int nages = 12; - int nfleets = 2; - }; -} // namespace \ No newline at end of file + fims_popdy::Population population; + int id_g = 0; + int nyears = 30; + int nseasons = 1; + int nages = 12; + int nfleets = 2; +}; +} // namespace \ No newline at end of file