aboutsummaryrefslogtreecommitdiff
path: root/tests/testthat/tests.R
blob: 59dc0043710e8119f2804edba66e9f9ebd28e8bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
library(SyncRNG)
context("SyncRNG unit tests")

test_that("test_randi", {
  s <- SyncRNG(seed=123456)
  expect_equal(s$randi(), 959852049)
  expect_equal(s$randi(), 2314333085)
  expect_equal(s$randi(), 2255782734)
  expect_equal(s$randi(), 2921461239)
  expect_equal(s$randi(), 1024197102)
})

test_that("test_rand", {
  s <- SyncRNG(seed=123456)
  expect_equal(s$rand(), 959852049 /(2**32))
  expect_equal(s$rand(), 2314333085/(2**32))
  expect_equal(s$rand(), 2255782734/(2**32))
  expect_equal(s$rand(), 2921461239/(2**32))
  expect_equal(s$rand(), 1024197102/(2**32))
})

test_that("test_randbelow", {
  s <- SyncRNG(seed=123456)
  expect_equal(s$randbelow(5), 4)
  expect_equal(s$randbelow(5), 0)
  expect_equal(s$randbelow(5), 4)
  expect_equal(s$randbelow(5), 4)
  expect_equal(s$randbelow(5), 2)
})

test_that("test_shuffle", {
  s <- SyncRNG(seed=123456)
  x <- c(1, 2, 3, 4, 5)
  y <- s$shuffle(x)
  expect_equal(y, c(3, 4, 1, 2, 5))
})

test_that("test_first_1000", {
  s <- SyncRNG(seed=0)
  fileName <- "./first_1000_seed_0.txt"
  conn <- file(fileName, open="r")
  linn <- readLines(conn)
  for (i in 1:length(linn)) {
	  exp <- as.numeric(linn[i])
	  expect_equal(exp, s$randi())
  }
  close(conn)
})

printf <- function(...) invisible(cat(sprintf(...)));

test_that("test_first_1000_unif", {
  set.seed(0, 'user', 'user')
  fileName <- "./first_1000_seed_0.txt"
  conn <- file(fileName, open="r")
  linn <- readLines(conn)
  for (i in 1:length(linn)) {
	  exp <- as.numeric(linn[i])
      u <- as.numeric(runif(1)*(2**32 - 1))
	  expect_equal(exp, u)
  }
  close(conn)
})