diff options
author | Tomohiro <gpuppur@gmail.com> | 2021-09-02 21:12:14 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-02 14:12:14 +0200 |
commit | 7c8ea490a2be36d171d362c43212195ea8ce489f (patch) | |
tree | 0c3a54a233e999ddf008ddbe99c4ca6311cded40 /tests/stdlib | |
parent | e0ef859130f429df1891e31a85955daa753346b4 (diff) | |
download | Nim-7c8ea490a2be36d171d362c43212195ea8ce489f.tar.gz |
Fix initrand to avoid random number sequences overlapping (#18744)
* Fix initrand to avoid random number sequences overlapping * Minor fix * Fix compile error on js backend * Disable new test for js backend * Minor fix * tempfiles module uses random.initRand() * Remove unused module import from lib/std/tempfiles.nim * Initialize baseState in initRand() * Run tests/stdlib/trandom.nim from tests/test_nimscript.nims * baseState is initialized only with sysrand.urandom and quit if failed * Add comments
Diffstat (limited to 'tests/stdlib')
-rw-r--r-- | tests/stdlib/trandom.nim | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/tests/stdlib/trandom.nim b/tests/stdlib/trandom.nim index 39ccca85b..61e858f86 100644 --- a/tests/stdlib/trandom.nim +++ b/tests/stdlib/trandom.nim @@ -23,9 +23,10 @@ proc main() = doAssert a in [[0,1], [1,0]] doAssert rand(0) == 0 - doAssert sample("a") == 'a' + when not defined(nimscript): + doAssert sample("a") == 'a' - when compileOption("rangeChecks"): + when compileOption("rangeChecks") and not defined(nimscript): doAssertRaises(RangeDefect): discard rand(-1) @@ -92,7 +93,7 @@ block: # random int block: # again gives new numbers var rand1 = rand(1000000) - when not defined(js): + when not (defined(js) or defined(nimscript)): os.sleep(200) var rand2 = rand(1000000) @@ -122,7 +123,7 @@ block: # random float block: # again gives new numbers var rand1: float = rand(1000000.0) - when not defined(js): + when not (defined(js) or defined(nimscript)): os.sleep(200) var rand2: float = rand(1000000.0) @@ -248,3 +249,26 @@ block: # bug #17670 type UInt48 = range[0'u64..2'u64^48-1] let x = rand(UInt48) doAssert x is UInt48 + +block: # bug #17898 + # Checks whether `initRand()` generates unique states. + # size should be 2^64, but we don't have time and space. + + # Disable this test for js until js gets proper skipRandomNumbers. + when not defined(js): + const size = 1000 + var + rands: array[size, Rand] + randSet: HashSet[Rand] + for i in 0..<size: + rands[i] = initRand() + randSet.incl rands[i] + + doAssert randSet.len == size + + # Checks random number sequences overlapping. + const numRepeat = 100 + for i in 0..<size: + for j in 0..<numRepeat: + discard rands[i].next + doAssert rands[i] notin randSet |