Skip to content

Commit 0a7ede3

Browse files
committed
Add tests for random streams
1 parent 24e84f3 commit 0a7ede3

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tests/testthat/test-utils.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ test_that("Setting the private seed explicitly results in identical values", {
5252
expect_identical(id7, id8)
5353
})
5454

55+
test_that("Private and 'public' random streams are independent and work the same", {
56+
set.seed(0)
57+
public <- c(runif(1), runif(1), runif(1))
58+
withPrivateSeed(set.seed(0))
59+
private <- c(withPrivateSeed(runif(1)), withPrivateSeed(runif(1)), withPrivateSeed(runif(1)))
60+
expect_identical(public, private)
61+
62+
# Interleaved calls to runif() with private and public streams
63+
set.seed(0)
64+
withPrivateSeed(set.seed(0))
65+
public <- numeric()
66+
private <- numeric()
67+
public[1] <- runif(1)
68+
private[1] <- withPrivateSeed(runif(1))
69+
private[2] <- withPrivateSeed(runif(1))
70+
public[2] <- runif(1)
71+
public[3] <- runif(1)
72+
private[3] <- withPrivateSeed(runif(1))
73+
expect_identical(public, private)
74+
})
75+
5576
test_that("need() works as expected", {
5677

5778
# These are all falsy

0 commit comments

Comments
 (0)