make tests more robust; tests should be deterministic, no randomize() calls in tests

This commit is contained in:
Araq 2019-01-13 13:54:44 +01:00
commit 796432ff94
3 changed files with 16 additions and 14 deletions

View file

@ -20,29 +20,29 @@ import sets, tables
suite "random int": suite "random int":
test "there might be some randomness": test "there might be some randomness":
var set = initSet[int](128) var set = initSet[int](128)
randomize()
for i in 1..1000: for i in 1..1000:
incl(set, random(high(int))) incl(set, random(high(int)))
check len(set) == 1000 check len(set) == 1000
test "single number bounds work": test "single number bounds work":
randomize()
var rand: int var rand: int
for i in 1..1000: for i in 1..1000:
rand = random(1000) rand = random(1000)
check rand < 1000 check rand < 1000
check rand > -1 check rand > -1
test "slice bounds work": test "slice bounds work":
randomize()
var rand: int var rand: int
for i in 1..1000: for i in 1..1000:
rand = random(100..1000) rand = random(100..1000)
check rand < 1000 check rand < 1000
check rand >= 100 check rand >= 100
test "randomize() again gives new numbers": test " again gives new numbers":
randomize()
var rand1 = random(1000000) var rand1 = random(1000000)
os.sleep(200) os.sleep(200)
randomize()
var rand2 = random(1000000) var rand2 = random(1000000)
check rand1 != rand2 check rand1 != rand2
@ -50,29 +50,29 @@ suite "random int":
suite "random float": suite "random float":
test "there might be some randomness": test "there might be some randomness":
var set = initSet[float](128) var set = initSet[float](128)
randomize()
for i in 1..100: for i in 1..100:
incl(set, random(1.0)) incl(set, random(1.0))
check len(set) == 100 check len(set) == 100
test "single number bounds work": test "single number bounds work":
randomize()
var rand: float var rand: float
for i in 1..1000: for i in 1..1000:
rand = random(1000.0) rand = random(1000.0)
check rand < 1000.0 check rand < 1000.0
check rand > -1.0 check rand > -1.0
test "slice bounds work": test "slice bounds work":
randomize()
var rand: float var rand: float
for i in 1..1000: for i in 1..1000:
rand = random(100.0..1000.0) rand = random(100.0..1000.0)
check rand < 1000.0 check rand < 1000.0
check rand >= 100.0 check rand >= 100.0
test "randomize() again gives new numbers": test " again gives new numbers":
randomize()
var rand1:float = random(1000000.0) var rand1:float = random(1000000.0)
os.sleep(200) os.sleep(200)
randomize()
var rand2:float = random(1000000.0) var rand2:float = random(1000000.0)
check rand1 != rand2 check rand1 != rand2

View file

@ -93,4 +93,7 @@ else:
doAssert outStr2 == absolutePath(testDir) & "\nx yz\n" doAssert outStr2 == absolutePath(testDir) & "\nx yz\n"
removeDir(testDir) removeDir(testDir)
try:
removeFile(exePath) removeFile(exePath)
except OSError:
discard

View file

@ -50,7 +50,6 @@ test "unittest multiple requires":
import math, random import math, random
from strutils import parseInt from strutils import parseInt
proc defectiveRobot() = proc defectiveRobot() =
randomize()
case random(1..4) case random(1..4)
of 1: raise newException(OSError, "CANNOT COMPUTE!") of 1: raise newException(OSError, "CANNOT COMPUTE!")
of 2: discard parseInt("Hello World!") of 2: discard parseInt("Hello World!")