Skip to content

Commit

Permalink
Merge pull request #37 from bladder-ca/develop
Browse files Browse the repository at this point in the history
adding user-supplied RNG-based tests to vdraw_sc_step_regular_cpp()
  • Loading branch information
ttrikalin committed Apr 5, 2024
2 parents ce12cde + 9521d4d commit 4fab37c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Language: es
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Suggests:
rlecuyer,
testthat,
withr
Config/Needs/website: rmarkdown
Expand Down
8 changes: 0 additions & 8 deletions src/nhppp.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ Rcpp::NumericMatrix vztdraw_sc_step_regular2(
const bool atmost1
);

// Rcpp::NumericMatrix vdraw_intensity_step_regular(
// const Rcpp::Function & lambda,
// const Rcpp::NumericMatrix & rate_maj,
// const bool is_cumulative,
// const Rcpp::NumericMatrix & range_t,
// const double tol,
// const bool atmost1);

Rcpp::NumericMatrix vdraw_intensity_step_regular(
const Rcpp::Function & lambda,
const Rcpp::NumericMatrix & rate_maj,
Expand Down
57 changes: 57 additions & 0 deletions tests/testthat/test-vdraw_intensity_step_regular_cpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,60 @@ test_that("vdraw_intensity_step_regular_cpp() works with subinterval", {
))
check_ppp_sample_validity(Z, t_min = 2.3, t_max = 4.8, atmost1 = FALSE)
})


test_that("vdraw_intensity_step_regular_cpp() uses user-supplied RNGs", {
l <- matrix(rep(1, 50), ncol = 5)
L <- mat_cumsum_columns(l)

seed <- rep(1, 6)
rlecuyer::.lec.SetPackageSeed(seed)
rlecuyer::.lec.CreateStream(c("a", "b"))

res <- list()

# seed the old.kind generator (default ) hereon "R"
set.seed(123)
res[["R0"]] <- vdraw_sc_step_regular_cpp(Lambda_matrix = L, range_t = c(100, 110))
set.seed(123)
res[["R1"]] <- vdraw_sc_step_regular_cpp(Lambda_matrix = L, range_t = c(100, 110))
expect_equal(res[["R0"]], res[["R1"]])

# activate "a" and reseed "R" -- If the function uses the "R", the test will fail
set.seed(123)
old.kind <- rlecuyer::.lec.CurrentStream("a")
res[["a0"]] <- vdraw_sc_step_regular_cpp(Lambda_matrix = L, range_t = c(100, 110))
rlecuyer::.lec.CurrentStreamEnd(old.kind)
expect_false(identical(res[["R0"]], res[["a0"]]))

# activate "b", again, re-seed "R"
set.seed(123)
old.kind <- rlecuyer::.lec.CurrentStream("b")
res[["b0"]] <- vdraw_sc_step_regular_cpp(Lambda_matrix = L, range_t = c(100, 110))
rlecuyer::.lec.CurrentStreamEnd(old.kind)
expect_false(identical(res[["R0"]], res[["b0"]]))
expect_false(identical(res[["b0"]], res[["a0"]]))

# reset the RNGs -- but advance the "R"
burn <- runif(10000)
rm("burn")
rlecuyer::.lec.ResetStartStream("a")
rlecuyer::.lec.ResetStartStream("b")

# activate "a" -- do not reseed "R"
old.kind <- rlecuyer::.lec.CurrentStream("a")
res[["a1"]] <- vdraw_sc_step_regular_cpp(Lambda_matrix = L, range_t = c(100, 110))
rlecuyer::.lec.CurrentStreamEnd(old.kind)
expect_equal(res[["a0"]], res[["a1"]])
expect_false(identical(res[["R0"]], res[["a1"]]))

# activate "b" -- do not reseed "R"
old.kind <- rlecuyer::.lec.CurrentStream("b")
res[["b1"]] <- vdraw_sc_step_regular_cpp(Lambda_matrix = L, range_t = c(100, 110))
rlecuyer::.lec.CurrentStreamEnd(old.kind)
expect_equal(res[["b0"]], res[["b1"]])
expect_false(identical(res[["R0"]], res[["b1"]]))
expect_false(identical(res[["a1"]], res[["b1"]]))

rlecuyer::.lec.exit()
})

0 comments on commit 4fab37c

Please sign in to comment.