diff --git a/R/mortality_processes.R b/R/mortality_processes.R index 976383c0..5884c928 100644 --- a/R/mortality_processes.R +++ b/R/mortality_processes.R @@ -19,7 +19,7 @@ create_mortality_process <- function(variables, events, renderer, parameters) { renderer$render('natural_deaths', died$size(), timestep) } else { age <- get_age(variables$birth$get_values(), timestep) - last_deathrate <- match_last_timestep(parameters$deathrate_timesteps, timestep) + last_deathrate <- match_timestep(parameters$deathrate_timesteps, timestep) deathrates <- rep(1, pop) age_groups <- .bincode(age, c(0, parameters$deathrate_agegroups)) in_range <- !is.na(age_groups) diff --git a/R/population_parameters.R b/R/population_parameters.R index 3b08cc3d..89015790 100644 --- a/R/population_parameters.R +++ b/R/population_parameters.R @@ -73,6 +73,6 @@ find_birthrates <- function(pops, age_high, deathrates) { } get_human_population <- function(parameters, timestep) { - last_pop <- match_last_timestep(parameters$human_population_timesteps, timestep) + last_pop <- match_timestep(parameters$human_population_timesteps, timestep) parameters$human_population[last_pop] } diff --git a/R/utils.R b/R/utils.R index f2b3dfd1..343e6309 100644 --- a/R/utils.R +++ b/R/utils.R @@ -62,14 +62,6 @@ rtexp <- function(n, m, t) { itexp(runif(n), m, t) } #'@param t current timestep #'@noRd match_timestep <- function(ts, t) { - min(sum(ts < t) + 1, length(ts)) -} - -#'@title Find index of the latest timestep in vector of timesteps -#'@param ts timesteps, assumed to be sorted and unique -#'@param t current timestep -#'@noRd -match_last_timestep <- function(ts, t) { min(sum(ts <= t), length(ts)) } diff --git a/tests/testthat/test-demography.R b/tests/testthat/test-demography.R index c471fe90..572706c2 100644 --- a/tests/testthat/test-demography.R +++ b/tests/testthat/test-demography.R @@ -55,3 +55,19 @@ test_that('calculate_initial_ages calculates truncated exp custom demographic', mockery::expect_args(mock_rtexp, 2, 2, .75, 50 * 365) expect_setequal(ages, c(25 * 365, 75 * 365, 30 * 365, 80 * 365)) }) + +test_that('match_timestep always gives 0 for constant demography', { + expect_equal(match_timestep(c(0), 0), 1) + expect_equal(match_timestep(c(0), 1), 1) + expect_equal(match_timestep(c(0), 50), 1) +}) + +test_that('match_timestep works on the boundaries', { + expect_equal(match_timestep(c(0, 50, 100), 0), 1) + expect_equal(match_timestep(c(0, 50, 100), 1), 1) + expect_equal(match_timestep(c(0, 50, 100), 49), 1) + expect_equal(match_timestep(c(0, 50, 100), 50), 2) + expect_equal(match_timestep(c(0, 50, 100), 99), 2) + expect_equal(match_timestep(c(0, 50, 100), 100), 3) + expect_equal(match_timestep(c(0, 50, 100), 101), 3) +}) \ No newline at end of file