Skip to content

Commit 9ec06b3

Browse files
committed
test(reproducibility): Test seeded reproducibility with threading
relates to #58
1 parent 41fe7ee commit 9ec06b3

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
test_that("Setting seed in R works", {
2+
set.seed(13)
3+
rpf_fit1 <- rpf(mpg ~ wt + cyl, data = mtcars)
4+
pred1 <- predict(rpf_fit1, mtcars[1:5, ])
5+
6+
set.seed(13)
7+
rpf_fit2 <- rpf(mpg ~ wt + cyl, data = mtcars)
8+
pred2 <- predict(rpf_fit2, mtcars[1:5, ])
9+
10+
# No seed set, so should be different
11+
rpf_fit3 <- rpf(mpg ~ wt + cyl, data = mtcars)
12+
pred3 <- predict(rpf_fit3, mtcars[1:5, ])
13+
14+
expect_equal(pred1, pred2)
15+
expect_failure(expect_equal(pred1, pred3))
16+
})
17+
18+
test_that("Rcpp RNG/R RNG interference", {
19+
set.seed(1)
20+
r11 <- runif(1)
21+
r12 <- runif(1)
22+
23+
set.seed(1)
24+
r21 <- runif(1)
25+
rpf_fit <- rpf(mpg ~ wt + cyl, data = mtcars)
26+
r22 <- runif(1)
27+
28+
# If this fails R is broken
29+
expect_equal(r11, r21)
30+
# If this fails Rcpp does not properly affect R's RNG
31+
expect_failure(expect_equal(r12, r22))
32+
})
33+
34+
35+
test_that("Seeding from R works with threading", {
36+
set.seed(13)
37+
rpf_fit1 <- rpf(mpg ~ wt + cyl, nthreads = 2, data = mtcars)
38+
pred1 <- predict(rpf_fit1, mtcars[1:5, ])
39+
40+
set.seed(13)
41+
rpf_fit2 <- rpf(mpg ~ wt + cyl, nthreads = 2, data = mtcars)
42+
pred2 <- predict(rpf_fit2, mtcars[1:5, ])
43+
44+
expect_equal(pred1, pred2)
45+
})

tests/testthat/test-rpf.R

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,20 @@ test_that("Unsupported interface", {
3737
})
3838

3939

40-
# Randomness --------------------------------------------------------------
41-
42-
test_that("Setting seed in R works", {
43-
set.seed(13)
44-
rpf_fit1 <- rpf(mpg ~ wt + cyl, data = mtcars)
45-
pred1 <- predict(rpf_fit1, mtcars[1:5, ])
46-
47-
set.seed(13)
48-
rpf_fit2 <- rpf(mpg ~ wt + cyl, data = mtcars)
49-
pred2 <- predict(rpf_fit2, mtcars[1:5, ])
50-
51-
# No seed set, so should be different
52-
rpf_fit3 <- rpf(mpg ~ wt + cyl, data = mtcars)
53-
pred3 <- predict(rpf_fit3, mtcars[1:5, ])
54-
55-
expect_equal(pred1, pred2)
56-
expect_failure(expect_equal(pred1, pred3))
57-
})
58-
59-
test_that("Rcpp RNG/R RNG interference", {
60-
set.seed(1)
61-
r11 <- runif(1)
62-
r12 <- runif(1)
63-
64-
set.seed(1)
65-
r21 <- runif(1)
66-
rpf_fit <- rpf(mpg ~ wt + cyl, data = mtcars)
67-
r22 <- runif(1)
68-
69-
# If this fails R is broken
70-
expect_equal(r11, r21)
71-
# If this fails Rcpp does not properly affect R's RNG
72-
expect_failure(expect_equal(r12, r22))
73-
})
74-
75-
7640
# Parameter sets/combinations ---------------------------------------------
7741

7842
test_that("Setting max_interaction = 0 works", {
7943
set.seed(100)
80-
rpf_fit0 <- rpf(mpg ~ ., data = mtcars[1:20,], max_interaction = 0)
81-
pred0 <- predict(rpf_fit0, new_data = mtcars[21:32,])
44+
rpf_fit0 <- rpf(mpg ~ ., data = mtcars[1:20, ], max_interaction = 0)
45+
pred0 <- predict(rpf_fit0, new_data = mtcars[21:32, ])
8246

8347
set.seed(100)
84-
rpf_fit10 <- rpf(mpg ~ ., data = mtcars[1:20,], max_interaction = 10)
85-
pred10 <- predict(rpf_fit10, new_data = mtcars[21:32,])
48+
rpf_fit10 <- rpf(mpg ~ ., data = mtcars[1:20, ], max_interaction = 10)
49+
pred10 <- predict(rpf_fit10, new_data = mtcars[21:32, ])
8650

8751
set.seed(100)
88-
rpf_fit_default <- rpf(mpg ~ ., data = mtcars[1:20,])
89-
pred_default <- predict(rpf_fit_default, new_data = mtcars[21:32,])
52+
rpf_fit_default <- rpf(mpg ~ ., data = mtcars[1:20, ])
53+
pred_default <- predict(rpf_fit_default, new_data = mtcars[21:32, ])
9054

9155
# Sanity: pred of default (max_interaction = 1) should be different
9256
expect_failure(expect_equal(pred_default, pred0))

0 commit comments

Comments
 (0)