Skip to content

Commit

Permalink
test(seeds): add testthat + test that seeds ensure consistency betwee…
Browse files Browse the repository at this point in the history
…n runs
  • Loading branch information
amyheather committed Jan 15, 2025
1 parent 6dda511 commit d3e3ef6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ Imports:
magrittr
Suggests:
knitr,
rmarkdown
rmarkdown,
testthat (>= 3.0.0)
VignetteBuilder: knitr
Config/testthat/edition: 3
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(simulation)

test_check("simulation")
38 changes: 38 additions & 0 deletions tests/testthat/test-model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test_that("the same seed returns the same result", {

# Run model twice using same run number (which will set the seed)
env1 <- model(run_number=0, param=defaults())
env2 <- model(run_number=0, param=defaults())

# Compare output results
expect_identical(
env1 %>% get_mon_arrivals(),
env2 %>% get_mon_arrivals())
expect_identical(
env1 %>% get_mon_resources(),
env2 %>% get_mon_resources())

# Conversely, if run with different run number, expect different
env1 <- model(run_number=0, param=defaults())
env2 <- model(run_number=1, param=defaults())

# Compare output results
expect_failure(expect_identical(
env1 %>% get_mon_arrivals(),
env2 %>% get_mon_arrivals()))
expect_failure(expect_identical(
env1 %>% get_mon_resources(),
env2 %>% get_mon_resources()))

# Repeat experiment, but with multiple replications
envs1 <- trial(param=defaults())
envs2 <- trial(param=defaults())

# Aggregate results from replications in each trial, then compare full trial
expect_identical(
do.call(rbind, lapply(envs1, get_mon_arrivals)),
do.call(rbind, lapply(envs2, get_mon_arrivals)))
expect_identical(
do.call(rbind, lapply(envs1, get_mon_resources)),
do.call(rbind, lapply(envs2, get_mon_resources)))
})

0 comments on commit d3e3ef6

Please sign in to comment.