Skip to content

Commit

Permalink
Expand testing
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed May 13, 2024
1 parent 9f227e0 commit bfa947e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inst/include/dust2/r/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ std::vector<real_type> check_time_sequence(real_type time_start,
cpp11::stop("Expected 'time[%d]' to be integer-like", i + 1);
}
if (t <= prev) {
cpp11::stop("Expected 'time[%d]' (%d) to larger than the previous value (%d)",
cpp11::stop("Expected 'time[%d]' (%d) to be larger than the previous value (%d)",
i + 1, static_cast<int>(prev), static_cast<int>(t));
}
prev = t;
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,25 @@ test_that("can run run unfilter on structured model", {
ptr <- obj[[1]]
expect_equal(dust2_cpu_sir_unfilter_run(ptr, NULL, FALSE), f(pars))
})


test_that("validate time for filter", {
pars <- list(beta = 0.1, gamma = 0.2, N = 1000, I0 = 10, exp_noise = 1e6)
time_start <- 0
time <- as.integer(c(4, 8, 12, 16))
data <- lapply(1:4, function(i) list(incidence = i))
dt <- 1

expect_error(
dust2_cpu_sir_unfilter_alloc(pars, 5, time, dt, data, 0),
"Expected 'time[1]' (5) to be larger than the previous value (4)",
fixed = TRUE)
expect_error(
dust2_cpu_sir_unfilter_alloc(pars, 0, time + c(0, 0, .1, 0), dt, data, 0),
"Expected 'time[3]' to be integer-like",
fixed = TRUE)
expect_error(
dust2_cpu_sir_unfilter_alloc(pars, 0, as.character(time), dt, data, 0),
"'time' must be a numeric vector",
fixed = TRUE)
})
20 changes: 20 additions & 0 deletions tests/testthat/test-sir.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ test_that("validate data size on compare", {
dust2_cpu_sir_compare_data(ptr, vector("list", 3), TRUE),
"'data' must have length 4")
})


test_that("can update parameters", {
base <- list(beta = 0.1, gamma = 0.2, N = 1000, I0 = 10, exp_noise = 1e6)
update <- list(beta = 0.3, gamma = 0.2)
combined <- modifyList(base, update)

obj1 <- dust2_cpu_sir_alloc(base, 0, 1, 10, 0, 42, FALSE)
ptr1 <- obj1[[1]]
expect_null(dust2_cpu_sir_update_pars(ptr1, update, FALSE))
expect_null(dust2_cpu_sir_run_steps(ptr1, 10))

obj2 <- dust2_cpu_sir_alloc(combined, 0, 1, 10, 0, 42, FALSE)
ptr2 <- obj2[[1]]
expect_null(dust2_cpu_sir_run_steps(ptr2, 10))

expect_equal(
dust2_cpu_sir_state(ptr2, FALSE),
dust2_cpu_sir_state(ptr1, FALSE))
})

0 comments on commit bfa947e

Please sign in to comment.