enable randomized divmod tests

This commit is contained in:
jangko 2023-06-23 07:46:16 +07:00
parent c5955f7e97
commit 7fc30a8f1c
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
5 changed files with 13 additions and 10 deletions

View File

@ -118,7 +118,7 @@ func random_unsafe*[T: SomeInteger](rng: var RngState, inclRange: Slice[T]): T =
# Containers # Containers
# ------------------------------------------------------------ # ------------------------------------------------------------
func sample_unsafe*[T](rng: var RngState, src: openarray[T]): T = func sample_unsafe*[T](rng: var RngState, src: openArray[T]): T =
## Return a random sample from an array ## Return a random sample from an array
result = src[rng.random_unsafe(uint32 src.len)] result = src[rng.random_unsafe(uint32 src.len)]
@ -206,7 +206,7 @@ type
HighHammingWeight HighHammingWeight
Long01Sequence Long01Sequence
func random_elem*(rng: var RngState, T: typedesc, gen: RandomGen): T {.inline, noInit.} = func random_elem*(rng: var RngState, T: typedesc, gen: RandomGen): T {.inline, noinit.} =
case gen case gen
of Uniform: of Uniform:
result = rng.random_unsafe(T) result = rng.random_unsafe(T)

View File

@ -30,8 +30,9 @@ task test_public_api, "Run all tests - prod implementation (StUint[64] = uint64"
task test_uint256_ttmath, "Run random tests Uint256 vs TTMath": task test_uint256_ttmath, "Run random tests Uint256 vs TTMath":
requires "https://github.com/alehander42/nim-quicktest >= 0.18.0", "https://github.com/status-im/nim-ttmath" requires "https://github.com/alehander42/nim-quicktest >= 0.18.0", "https://github.com/status-im/nim-ttmath"
switch("define", "release") putEnv("TEST_LANG", "cpp")
test "uint256_ttmath", "cpp" switch("define", "release")
test "", "tests/test_uint256_ttmath"
task test, "Run all tests": task test, "Run all tests":
exec "nimble test_internal" exec "nimble test_internal"

View File

@ -34,4 +34,6 @@ import
import import
test_io, test_io,
test_conversion test_conversion,
t_randomized_divmod

View File

@ -25,15 +25,15 @@ echo "t_randomized_divmod xoshiro512** seed: ", seed
proc test_divmod(bits: static int, iters: int, gen: RandomGen) = proc test_divmod(bits: static int, iters: int, gen: RandomGen) =
for _ in 0 ..< iters: for _ in 0 ..< iters:
let a = rng.random_elem(Stuint[bits], gen) let a = rng.random_elem(StUint[bits], gen)
let b = rng.random_elem(Stuint[bits], gen) let b = rng.random_elem(StUint[bits], gen)
try: try:
let (q, r) = divmod(a, b) let (q, r) = divmod(a, b)
doAssert a == q*b + r doAssert a == q*b + r
except DivByZeroDefect: except DivByZeroDefect:
doAssert b.isZero() doAssert b.isZero()
template test(bits: static int) = template test(bits: static int) =
test "(q, r) = divmod(a, b) <=> a = q*b + r (" & $bits & " bits)": test "(q, r) = divmod(a, b) <=> a = q*b + r (" & $bits & " bits)":
test_divmod(bits, Iters, Uniform) test_divmod(bits, Iters, Uniform)
@ -43,4 +43,4 @@ template test(bits: static int) =
suite "Randomized division and modulo checks": suite "Randomized division and modulo checks":
test(128) test(128)
test(256) test(256)
test(512) test(512)

View File

@ -1,4 +1,4 @@
import ../stint, ttmath import ../stint, ttmath/ttmath
export ttmath export ttmath
template asSt*(val: UInt): auto = template asSt*(val: UInt): auto =