diff options
| -rw-r--r-- | DESCRIPTION | 5 | ||||
| -rwxr-xr-x | test/run_tests.sh | 3 | ||||
| -rw-r--r-- | test/test.R | 43 | ||||
| -rw-r--r-- | test/test.py | 34 | ||||
| -rw-r--r-- | tests/testthat.R | 4 | ||||
| -rw-r--r-- | tests/testthat/first_1000_seed_0.txt (renamed from test/first_1000_seed_0.txt) | 0 | ||||
| -rw-r--r-- | tests/testthat/tests.R | 48 |
7 files changed, 55 insertions, 82 deletions
diff --git a/DESCRIPTION b/DESCRIPTION index ac0ee0c..ee619eb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: SyncRNG -Version: 1.0 -Date: 2015-07-31 +Version: 1.1.0 +Date: 2016-10-12 Title: A Synchronized Tausworthe RNG for R and Python Author: Gertjan van den Burg <gertjanvandenburg@gmail.com> Maintainer: Gertjan van den Burg <gertjanvandenburg@gmail.com> @@ -9,3 +9,4 @@ Description: Random number generation designed for cross-language usage. License: file LICENSE Imports: methods +Suggests: testthat diff --git a/test/run_tests.sh b/test/run_tests.sh deleted file mode 100755 index 4c7c5da..0000000 --- a/test/run_tests.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -paste <(python test.py) <(Rscript test.R) diff --git a/test/test.R b/test/test.R deleted file mode 100644 index 0937f52..0000000 --- a/test/test.R +++ /dev/null @@ -1,43 +0,0 @@ -library(SyncRNG) - -test.randi <- function() -{ - s <- SyncRNG(seed=123456) - for (i in 1:5) - cat(s$randi(), '\n') -} - -test.rand <- function() -{ - s <- SyncRNG(seed=123456) - for (i in 1:5) - cat(sprintf('%.16f\n', s$rand())) -} - -test.randbelow <- function() -{ - s <- SyncRNG(seed=123456) - for (i in 1:5) - cat(s$randbelow(i), '\n') -} - -test.shuffle <- function() -{ - s <- SyncRNG(seed=123456) - x <- c(1:5) - for (i in 1:5) { - y <- s$shuffle(x) - x <- y - cat('[', paste(y, collapse=', '), ']\n', sep='') - } -} - -main <- function() -{ - test.randi() - test.rand() - test.randbelow() - test.shuffle() -} - -main() diff --git a/test/test.py b/test/test.py deleted file mode 100644 index 7d3f9f5..0000000 --- a/test/test.py +++ /dev/null @@ -1,34 +0,0 @@ - -from SyncRNG import SyncRNG - -def test_randi(): - s = SyncRNG(seed=123456) - for i in range(5): - print(s.randi()) - -def test_rand(): - s = SyncRNG(seed=123456) - for i in range(5): - print('%.16f' % s.rand()) - -def test_randbelow(): - s = SyncRNG(seed=123456) - for i in range(5): - print(s.randbelow(i+1)) - -def test_shuffle(): - s = SyncRNG(seed=123456) - x = [1, 2, 3, 4, 5] - for i in range(5): - y = s.shuffle(x) - x = y - print(y) - -def main(): - test_randi() - test_rand() - test_randbelow() - test_shuffle() - -if __name__ == '__main__': - main() diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..b1f5cc8 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(SyncRNG) + +test_check("SyncRNG") diff --git a/test/first_1000_seed_0.txt b/tests/testthat/first_1000_seed_0.txt index 50aef45..50aef45 100644 --- a/test/first_1000_seed_0.txt +++ b/tests/testthat/first_1000_seed_0.txt diff --git a/tests/testthat/tests.R b/tests/testthat/tests.R new file mode 100644 index 0000000..4be1756 --- /dev/null +++ b/tests/testthat/tests.R @@ -0,0 +1,48 @@ +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) +}) |
