remove some noises in tests (#16448)
This commit is contained in:
parent
357729639f
commit
271f68259b
17 changed files with 140 additions and 181 deletions
|
|
@ -1,18 +1,6 @@
|
|||
discard """
|
||||
action: run
|
||||
output: '''
|
||||
|
||||
[Suite] random int
|
||||
|
||||
[Suite] random float
|
||||
|
||||
[Suite] cumsum
|
||||
|
||||
[Suite] random sample
|
||||
|
||||
[Suite] ^
|
||||
'''
|
||||
matrix:"; -d:nimTmathCase2 -d:danger --passc:-ffast-math"
|
||||
matrix:"; -d:nimTmathCase2 -d:danger --passc:-ffast-math"
|
||||
"""
|
||||
|
||||
# xxx: fix bugs for js then add: targets:"c js"
|
||||
|
|
@ -21,22 +9,22 @@ import math, random, os
|
|||
import unittest
|
||||
import sets, tables
|
||||
|
||||
suite "random int":
|
||||
test "there might be some randomness":
|
||||
block: # random int
|
||||
block: # there might be some randomness
|
||||
var set = initHashSet[int](128)
|
||||
|
||||
for i in 1..1000:
|
||||
incl(set, rand(high(int)))
|
||||
check len(set) == 1000
|
||||
test "single number bounds work":
|
||||
|
||||
block: # single number bounds work
|
||||
var rand: int
|
||||
for i in 1..1000:
|
||||
rand = rand(1000)
|
||||
check rand < 1000
|
||||
check rand > -1
|
||||
test "slice bounds work":
|
||||
|
||||
block: # slice bounds work
|
||||
var rand: int
|
||||
for i in 1..1000:
|
||||
rand = rand(100..1000)
|
||||
|
|
@ -45,8 +33,8 @@ suite "random int":
|
|||
else:
|
||||
check rand < 1000
|
||||
check rand >= 100
|
||||
test " again gives new numbers":
|
||||
|
||||
block: # again gives new numbers
|
||||
var rand1 = rand(1000000)
|
||||
when not defined(js):
|
||||
os.sleep(200)
|
||||
|
|
@ -55,28 +43,29 @@ suite "random int":
|
|||
check rand1 != rand2
|
||||
|
||||
|
||||
suite "random float":
|
||||
test "there might be some randomness":
|
||||
block: # random float
|
||||
block: # there might be some randomness
|
||||
var set = initHashSet[float](128)
|
||||
|
||||
for i in 1..100:
|
||||
incl(set, rand(1.0))
|
||||
check len(set) == 100
|
||||
test "single number bounds work":
|
||||
|
||||
block: # single number bounds work
|
||||
var rand: float
|
||||
for i in 1..1000:
|
||||
rand = rand(1000.0)
|
||||
check rand < 1000.0
|
||||
check rand > -1.0
|
||||
test "slice bounds work":
|
||||
|
||||
block: # slice bounds work
|
||||
var rand: float
|
||||
for i in 1..1000:
|
||||
rand = rand(100.0..1000.0)
|
||||
check rand < 1000.0
|
||||
check rand >= 100.0
|
||||
test " again gives new numbers":
|
||||
|
||||
block: # again gives new numbers
|
||||
|
||||
var rand1:float = rand(1000000.0)
|
||||
when not defined(js):
|
||||
|
|
@ -85,27 +74,27 @@ suite "random float":
|
|||
var rand2:float = rand(1000000.0)
|
||||
check rand1 != rand2
|
||||
|
||||
suite "cumsum":
|
||||
test "cumsum int seq return":
|
||||
block: # cumsum
|
||||
block: # cumsum int seq return
|
||||
let counts = [ 1, 2, 3, 4 ]
|
||||
check counts.cumsummed == [ 1, 3, 6, 10 ]
|
||||
|
||||
test "cumsum float seq return":
|
||||
block: # cumsum float seq return
|
||||
let counts = [ 1.0, 2.0, 3.0, 4.0 ]
|
||||
check counts.cumsummed == [ 1.0, 3.0, 6.0, 10.0 ]
|
||||
|
||||
test "cumsum int in-place":
|
||||
block: # cumsum int in-place
|
||||
var counts = [ 1, 2, 3, 4 ]
|
||||
counts.cumsum
|
||||
check counts == [ 1, 3, 6, 10 ]
|
||||
|
||||
test "cumsum float in-place":
|
||||
block: # cumsum float in-place
|
||||
var counts = [ 1.0, 2.0, 3.0, 4.0 ]
|
||||
counts.cumsum
|
||||
check counts == [ 1.0, 3.0, 6.0, 10.0 ]
|
||||
|
||||
suite "random sample":
|
||||
test "non-uniform array sample unnormalized int CDF":
|
||||
block: # random sample
|
||||
block: # "non-uniform array sample unnormalized int CDF
|
||||
let values = [ 10, 20, 30, 40, 50 ] # values
|
||||
let counts = [ 4, 3, 2, 1, 0 ] # weights aka unnormalized probabilities
|
||||
var histo = initCountTable[int]()
|
||||
|
|
@ -127,7 +116,7 @@ suite "random sample":
|
|||
let stdDev = sqrt(n * p * (1.0 - p))
|
||||
check abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev
|
||||
|
||||
test "non-uniform array sample normalized float CDF":
|
||||
block: # non-uniform array sample normalized float CDF
|
||||
let values = [ 10, 20, 30, 40, 50 ] # values
|
||||
let counts = [ 0.4, 0.3, 0.2, 0.1, 0 ] # probabilities
|
||||
var histo = initCountTable[int]()
|
||||
|
|
@ -146,8 +135,8 @@ suite "random sample":
|
|||
# NOTE: like unnormalized int CDF test, P(wholeTestFails) =~ 0.01.
|
||||
check abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev
|
||||
|
||||
suite "^":
|
||||
test "compiles for valid types":
|
||||
block: # ^
|
||||
block: # compiles for valid types
|
||||
check: compiles(5 ^ 2)
|
||||
check: compiles(5.5 ^ 2)
|
||||
check: compiles(5.5 ^ 2.int8)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue