Skip to content

Commit

Permalink
Basic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed May 3, 2024
1 parent c63c757 commit 56e23e3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
2 changes: 1 addition & 1 deletion inst/include/dust2/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class dust_cpu {
rng_(n_particles_, seed, deterministic) {
// TODO: above, filter states need adding here too.
if (dt != 1) {
throw std::runtime_error("Assuming dt = 1 for now");
throw std::runtime_error("Requiring dt = 1 for now");
}
}

Expand Down
2 changes: 1 addition & 1 deletion inst/include/dust2/r/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace r {

inline void check_scalar(cpp11::sexp x, const char * name) {
if (LENGTH(x) != 1) {
cpp11::stop("'%s' must be scalar", name);
cpp11::stop("'%s' must be a scalar", name);
}
}

Expand Down
98 changes: 98 additions & 0 deletions tests/testthat/test-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,101 @@ test_that("can run simple walk model", {
expect_equal(s, colSums(r$normal(3, 0, 1)))
expect_equal(dust2_cpu_walk_time(ptr), 3)
})


test_that("can set model state from a vector", {
pars <- list(sd = 1, random_initial = TRUE)
obj <- dust2_cpu_walk_alloc(pars, 0, 1, 10, 42, FALSE)
ptr <- obj[[1]]
s <- runif(10)
expect_null(dust2_cpu_walk_set_state(ptr, s))
expect_equal(dust2_cpu_walk_state(ptr), s)

expect_null(dust2_cpu_walk_run_steps(ptr, 3))

r <- mcstate2::mcstate_rng$new(seed = 42, n_streams = 10)
expect_equal(dust2_cpu_walk_state(ptr),
colSums(r$normal(3, 0, 1)) + s)
})


test_that("can set model state from initial conditions", {
pars <- list(sd = 1, random_initial = TRUE)
obj <- dust2_cpu_walk_alloc(pars, 0, 1, 10, 42, FALSE)
ptr <- obj[[1]]
expect_null(dust2_cpu_walk_set_state_initial(ptr))
r <- mcstate2::mcstate_rng$new(seed = 42, n_streams = 10)
expect_equal(dust2_cpu_walk_state(ptr),
drop(r$normal(1, 0, 1)))
})


## Not 100% sure we'll keep this, but we will see. This is only a
## feature of the walk model and not dust though.
test_that("can set model state from initial conditions with empty version", {
pars <- list(sd = 1)
obj <- dust2_cpu_walk_alloc(pars, 0, 1, 10, 42, FALSE)
ptr <- obj[[1]]
expect_null(dust2_cpu_walk_set_state_initial(ptr))
expect_equal(dust2_cpu_walk_state(ptr),
rep(0, 10))
})


test_that("can run deterministically", {
pars <- list(sd = 1)
obj <- dust2_cpu_walk_alloc(pars, 0, 1, 10, 42, TRUE)
ptr <- obj[[1]]
expect_null(dust2_cpu_walk_run_steps(ptr, 3))
expect_equal(dust2_cpu_walk_state(ptr),
rep(0, 10))
})


test_that("require that dt is 1 for now", {
pars <- list(sd = 1, random_initial = TRUE)
expect_error(
dust2_cpu_walk_alloc(pars, 0, 0.5, 10, 42, FALSE),
"Requiring dt = 1 for now",
fixed = TRUE)
})


## Not a test at all of the model, but of the error handling in
## r/helpers.hpp
test_that("validate inputs", {
pars <- list(sd = 1, random_initial = TRUE)
expect_error(
dust2_cpu_walk_alloc(pars, 0:3, 1, 10, 42, FALSE),
"'time' must be a scalar")

expect_identical(
dust2_cpu_walk_time(dust2_cpu_walk_alloc(pars, 5L, 1, 10, 42, FALSE)[[1]]),
5.0)
expect_identical(
dust2_cpu_walk_time(dust2_cpu_walk_alloc(pars, 5, 1, 10, 42, FALSE)[[1]]),
5.0)
expect_error(
dust2_cpu_walk_alloc(pars, "5", 1, 10, 42, FALSE),
"'time' must be scalar numeric")

expect_identical(
dust2_cpu_walk_state(dust2_cpu_walk_alloc(pars, 5, 1, 10, 42, FALSE)[[1]]),
rep(0, 10))
expect_identical(
dust2_cpu_walk_state(dust2_cpu_walk_alloc(pars, 5, 1, 10L, 42, FALSE)[[1]]),
rep(0, 10))
expect_error(
dust2_cpu_walk_alloc(pars, 5, 1, 9.5, 42, FALSE),
"'n_particles' must be integer-like")
expect_error(
dust2_cpu_walk_alloc(pars, 5, 1, "10", 42, FALSE),
"'n_particles' must be scalar integer")
expect_error(
dust2_cpu_walk_alloc(pars, 5, 1, -5, 42, FALSE),
"'n_particles' must be non-negative")

expect_error(
dust2_cpu_walk_alloc(pars, 5, 1, 10, 42, 1),
"'deterministic' must be scalar logical")
})

0 comments on commit 56e23e3

Please sign in to comment.