-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(seeds): add testthat + test that seeds ensure consistency betwee…
…n runs
- Loading branch information
1 parent
6dda511
commit d3e3ef6
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
}) |