Skip to content

Commit d3e3ef6

Browse files
committed
test(seeds): add testthat + test that seeds ensure consistency between runs
1 parent 6dda511 commit d3e3ef6

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ Imports:
2121
magrittr
2222
Suggests:
2323
knitr,
24-
rmarkdown
24+
rmarkdown,
25+
testthat (>= 3.0.0)
2526
VignetteBuilder: knitr
27+
Config/testthat/edition: 3

tests/testthat.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(simulation)
11+
12+
test_check("simulation")

tests/testthat/test-model.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
test_that("the same seed returns the same result", {
2+
3+
# Run model twice using same run number (which will set the seed)
4+
env1 <- model(run_number=0, param=defaults())
5+
env2 <- model(run_number=0, param=defaults())
6+
7+
# Compare output results
8+
expect_identical(
9+
env1 %>% get_mon_arrivals(),
10+
env2 %>% get_mon_arrivals())
11+
expect_identical(
12+
env1 %>% get_mon_resources(),
13+
env2 %>% get_mon_resources())
14+
15+
# Conversely, if run with different run number, expect different
16+
env1 <- model(run_number=0, param=defaults())
17+
env2 <- model(run_number=1, param=defaults())
18+
19+
# Compare output results
20+
expect_failure(expect_identical(
21+
env1 %>% get_mon_arrivals(),
22+
env2 %>% get_mon_arrivals()))
23+
expect_failure(expect_identical(
24+
env1 %>% get_mon_resources(),
25+
env2 %>% get_mon_resources()))
26+
27+
# Repeat experiment, but with multiple replications
28+
envs1 <- trial(param=defaults())
29+
envs2 <- trial(param=defaults())
30+
31+
# Aggregate results from replications in each trial, then compare full trial
32+
expect_identical(
33+
do.call(rbind, lapply(envs1, get_mon_arrivals)),
34+
do.call(rbind, lapply(envs2, get_mon_arrivals)))
35+
expect_identical(
36+
do.call(rbind, lapply(envs1, get_mon_resources)),
37+
do.call(rbind, lapply(envs2, get_mon_resources)))
38+
})

0 commit comments

Comments
 (0)