| 1 |
test_that("with_seed works as expected", { |
| 2 |
expect_identical( |
| 3 |
with_preserve_seed(runif(10)), |
| 4 |
runif(10)) |
| 5 |
expect_identical( |
| 6 |
with_preserve_seed(runif(10)), |
| 7 |
with_preserve_seed(runif(10))) |
| 8 |
expect_identical( |
| 9 |
with_seed(1, runif(10)), |
| 10 |
with_seed(1, runif(10))) |
| 11 |
expect_false(with_seed(1, runif(1)) == runif(1)) |
| 12 |
expect_false( |
| 13 |
with_seed(sample.int(.Machine$integer.max, 1), runif(1)) == |
| 14 |
with_seed(sample.int(.Machine$integer.max, 1), runif(1)) |
| 15 |
) |
| 16 |
}) |
| 17 |
|
| 18 |
test_that("local_seed works as expected", { |
| 19 |
x <- numeric() |
| 20 |
y <- numeric() |
| 21 |
local({ |
| 22 |
local_preserve_seed() |
| 23 |
x <<- runif(10) |
| 24 |
}) |
| 25 |
expect_identical(x, runif(10)) |
| 26 |
|
| 27 |
local({ |
| 28 |
local_preserve_seed() |
| 29 |
x <<- runif(10) |
| 30 |
}) |
| 31 |
|
| 32 |
local({ |
| 33 |
local_preserve_seed() |
| 34 |
y <<- runif(10) |
| 35 |
}) |
| 36 |
|
| 37 |
expect_identical(x, y) |
| 38 |
|
| 39 |
local({ |
| 40 |
local_seed(1) |
| 41 |
x <<- runif(10) |
| 42 |
}) |
| 43 |
|
| 44 |
local({ |
| 45 |
local_seed(1) |
| 46 |
y <<- runif(10) |
| 47 |
}) |
| 48 |
|
| 49 |
expect_identical(x, y) |
| 50 |
|
| 51 |
local({ |
| 52 |
local_seed(1) |
| 53 |
x <<- runif(1) |
| 54 |
}) |
| 55 |
expect_false(x == runif(1)) |
| 56 |
|
| 57 |
x_seed <- sample.int(.Machine$integer.max, 1) |
| 58 |
y_seed <- sample.int(.Machine$integer.max, 1) |
| 59 |
|
| 60 |
local({ |
| 61 |
local_seed(x_seed) |
| 62 |
x <<- runif(1) |
| 63 |
}) |
| 64 |
|
| 65 |
local({ |
| 66 |
local_seed(y_seed) |
| 67 |
y <<- runif(1) |
| 68 |
}) |
| 69 |
expect_false(x == y) |
| 70 |
}) |
| 71 |
|
| 72 |
test_that("local_seed doesn't modify `.Random.seed` (#286)", { |
| 73 |
f1 <- function(n, seed) { |
| 74 |
local_seed(seed) |
| 75 |
rnorm(n) |
| 76 |
} |
| 77 |
set.seed(42) |
| 78 |
seed3 <- .Random.seed |
| 79 |
rslt3 <- f1(5, 41) |
| 80 |
expect_identical(seed3, .Random.seed) |
| 81 |
}) |
| 82 |
|
| 83 |
test_that("with_preserve_seed preserves empty seed", { |
| 84 |
rm_seed() |
| 85 |
with_preserve_seed(runif(1)) |
| 86 |
expect_false(has_seed()) |
| 87 |
}) |
| 88 |
|
| 89 |
test_that("local_preserve_seed preserves empty seed", { |
| 90 |
rm_seed() |
| 91 |
local({ |
| 92 |
local_preserve_seed() |
| 93 |
runif(1) |
| 94 |
}) |
| 95 |
expect_false(has_seed()) |
| 96 |
}) |
| 97 |
|
| 98 |
test_that("local_seed preserves empty seed (#286)", { |
| 99 |
rm_seed() |
| 100 |
local({ |
| 101 |
local_seed(1) |
| 102 |
runif(1) |
| 103 |
}) |
| 104 |
# `.Random.seed` must be truly absent, not present-but-`NULL`. A `NULL` |
| 105 |
# value reads as no seed to `has_seed()` (it checks `mode = "integer"`), |
| 106 |
# but makes the next RNG draw warn that `.Random.seed` is not an integer. |
| 107 |
expect_false(exists(".Random.seed", globalenv(), inherits = FALSE)) |
| 108 |
expect_no_warning(runif(1)) |
| 109 |
}) |
| 110 |
|
| 111 |
test_that("RNGkind is also respected", { |
| 112 |
old <- RNGkind() |
| 113 |
on.exit(do.call("RNGkind", as.list(old))) |
| 114 |
|
| 115 |
|
| 116 |
# Uniform RNG |
| 117 |
|
| 118 |
my_seed <- sample.int(.Machine$integer.max, 1) |
| 119 |
rng_kind <- "Mersenne-Twister" |
| 120 |
alt_rng_kind <- "L'Ecuyer-CMRG" |
| 121 |
|
| 122 |
RNGkind(rng_kind) |
| 123 |
set.seed(my_seed) |
| 124 |
reference <- runif(10) |
| 125 |
|
| 126 |
RNGkind(alt_rng_kind) |
| 127 |
set.seed(my_seed) |
| 128 |
alt_reference <- runif(10) |
| 129 |
|
| 130 |
RNGkind(rng_kind) |
| 131 |
expect_equal( |
| 132 |
with_seed(my_seed, { |
| 133 |
expect_equal(RNGkind()[[1]], rng_kind) |
| 134 |
runif(10) |
| 135 |
}), |
| 136 |
reference |
| 137 |
) |
| 138 |
expect_equal(RNGkind()[[1]], rng_kind) |
| 139 |
|
| 140 |
RNGkind(alt_rng_kind) |
| 141 |
expect_equal( |
| 142 |
with_seed(my_seed, { |
| 143 |
expect_equal(RNGkind()[[1]], alt_rng_kind) |
| 144 |
runif(10) |
| 145 |
}), |
| 146 |
alt_reference |
| 147 |
) |
| 148 |
expect_equal(RNGkind()[[1]], alt_rng_kind) |
| 149 |
|
| 150 |
RNGkind(rng_kind) |
| 151 |
expect_equal( |
| 152 |
with_seed( |
| 153 |
my_seed, |
| 154 |
{ |
| 155 |
expect_equal(RNGkind()[[1]], alt_rng_kind) |
| 156 |
runif(10) |
| 157 |
}, |
| 158 |
.rng_kind = alt_rng_kind |
| 159 |
), |
| 160 |
alt_reference |
| 161 |
) |
| 162 |
expect_equal(RNGkind()[[1]], rng_kind) |
| 163 |
|
| 164 |
|
| 165 |
# Normal RNG |
| 166 |
|
| 167 |
rng_norm_kind <- "Box-Muller" |
| 168 |
alt_rng_norm_kind <- "Kinderman-Ramage" |
| 169 |
|
| 170 |
RNGkind(normal.kind = rng_norm_kind) |
| 171 |
set.seed(my_seed) |
| 172 |
norm_reference <- rnorm(10) |
| 173 |
|
| 174 |
RNGkind(normal.kind = alt_rng_norm_kind) |
| 175 |
set.seed(my_seed) |
| 176 |
alt_norm_reference <- rnorm(10) |
| 177 |
|
| 178 |
expect_equal( |
| 179 |
with_seed(my_seed, rnorm(10)), |
| 180 |
alt_norm_reference |
| 181 |
) |
| 182 |
|
| 183 |
expect_equal( |
| 184 |
with_seed( |
| 185 |
my_seed, |
| 186 |
.rng_normal_kind = alt_rng_norm_kind, |
| 187 |
rnorm(10) |
| 188 |
), |
| 189 |
alt_norm_reference |
| 190 |
) |
| 191 |
|
| 192 |
expect_equal( |
| 193 |
with_seed( |
| 194 |
my_seed, |
| 195 |
.rng_normal_kind = rng_norm_kind, |
| 196 |
rnorm(10) |
| 197 |
), |
| 198 |
norm_reference |
| 199 |
) |
| 200 |
|
| 201 |
expect_equal( |
| 202 |
with_preserve_seed(rnorm(10)), |
| 203 |
with_preserve_seed(rnorm(10)) |
| 204 |
) |
| 205 |
|
| 206 |
# Beware: Box-Muller generates pairs, that state is lost across set.seed()! |
| 207 |
RNGkind(normal.kind = rng_norm_kind) |
| 208 |
rnorm(1) |
| 209 |
expect_failure(expect_equal( |
| 210 |
with_preserve_seed(rnorm(10)), |
| 211 |
with_preserve_seed(rnorm(10)) |
| 212 |
)) |
| 213 |
}) |
| 214 |
|
| 215 |
test_that("can restore to `sample.kind = 'Rounding'` (#167)", { |
| 216 |
expect_warning(regexp = NA, local({ |
| 217 |
withr::local_rng_version("3.5.0") |
| 218 |
withr::local_seed(42) |
| 219 |
})) |
| 220 |
}) |