Skip to content

Commit

Permalink
fix compile-time hash, port to unittest2 (#145)
Browse files Browse the repository at this point in the history
* enable compile-time testing for most of the test suite (some parts
that were skipping things turned out to be bugs)
* port to unittest2 and use its recent compile-time testing support
  • Loading branch information
arnetheduck authored Nov 25, 2024
1 parent 3236fa6 commit 5c5e01c
Show file tree
Hide file tree
Showing 33 changed files with 2,886 additions and 3,117 deletions.
14 changes: 10 additions & 4 deletions stint.nimble
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mode = ScriptMode.Verbose

packageName = "stint"
version = "2.0.0"
author = "Status Research & Development GmbH"
Expand All @@ -8,7 +10,8 @@ skipDirs = @["tests", "benchmarks"]

# TODO test only requirements don't work: https://github.com/nim-lang/nimble/issues/482
requires "nim >= 1.6.12",
"stew"
"stew",
"unittest2 >= 0.2.3"

let nimc = getEnv("NIMC", "nim") # Which nim compiler to use
let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js)
Expand All @@ -35,7 +38,8 @@ proc run(args, path: string) =
proc test(path: string) =
for config in ["", "-d:stintNoIntrinsics"]:
for mode in ["-d:debug", "-d:release"]:
run(config & " " & mode, path)
# Compile-time tests are done separately to speed up full testing
run(config & " " & mode & " -d:unittest2Static=false", path)

task test_internal, "Run tests for internal procs":
test "tests/internal"
Expand All @@ -47,5 +51,7 @@ task test, "Run all tests":
test "tests/internal"
test "tests/all_tests"

# Smoke-test wasm32 compiles
build "--cpu:wasm32 -c", "tests/all_tests"
# Run compile-time tests on both 32 and 64 bits
if lang == "c":
build "--cpu:amd64 -c -d:unittest2Static", "tests/all_tests"
build "--cpu:wasm32 -c -d:unittest2Static", "tests/all_tests"
2 changes: 1 addition & 1 deletion stint/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ template wordType*(_: type SomeBigInteger): type =
Word

template hash*(num: StUint|StInt): Hash =
mixin limbs
mixin hash, limbs
hash(num.limbs)

{.pop.}
Expand Down
2 changes: 1 addition & 1 deletion tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ import
test_bugfix,
test_features

when defined(cpp):
when defined(cpp) and not defined(unittest2Static):
import
test_vs_intx
1 change: 1 addition & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
switch("warning", "BareExcept:off")
switch("define", "unittest2Static")
2 changes: 1 addition & 1 deletion tests/internal_uint_div.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{.used.}

include ../stint/private/uint_div
import unittest
import unittest2

suite "implementation of internal division procecures":
test "Division of 2 words by 1 - specific carry case (issue #30)":
Expand Down
5 changes: 3 additions & 2 deletions tests/t_randomized_divmod.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

import
# Standard library
std/[unittest, times],
std/times,
# Internal
../stint,
# Test utilities
unittest2,
../helpers/prng_unsafe

const Iters = 50000
Expand All @@ -35,7 +36,7 @@ proc test_divmod(bits: static int, iters: int, gen: RandomGen) =
doAssert b.isZero()

template test(bits: static int) =
test "(q, r) = divmod(a, b) <=> a = q*b + r (" & $bits & " bits)":
runtimeTest "(q, r) = divmod(a, b) <=> a = q*b + r (" & $bits & " bits)":
test_divmod(bits, Iters, Uniform)
test_divmod(bits, Iters, HighHammingWeight)
test_divmod(bits, Iters, Long01Sequence)
Expand Down
16 changes: 8 additions & 8 deletions tests/t_randomized_vs_gmp.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Stint
# Copyright (c) 2018-2022 Status Research & Development GmbH
#
#
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import
# Standard library
std/[unittest, times, strutils],
std/[unittest2, times, strutils],
# Third-party
gmp, stew/byteutils,
# Internal
Expand Down Expand Up @@ -50,7 +50,7 @@ proc fromStuint[bits: static int](dst: var mpz_t, src: Stuint[bits]) =
doAssert t2.toOpenArray(0, wordsWritten-1) == t.toOpenArray(t.len-wordsWritten, t.len-1)

proc test_add(bits: static int, iters: int, gen: RandomGen) =

const N = (bits + 7) div 8

var x, y, z, m: mpz_t
Expand All @@ -59,7 +59,7 @@ proc test_add(bits: static int, iters: int, gen: RandomGen) =
mpz_init(z)
mpz_init(m)
mpz_ui_pow_ui(m, 2, bits) # 2^bits

for _ in 0 ..< iters:
let a = rng.random_elem(Stuint[bits], gen)
let b = rng.random_elem(Stuint[bits], gen)
Expand Down Expand Up @@ -97,7 +97,7 @@ template testAddition(bits: static int) =
test_add(bits, Iters, Long01Sequence)

proc test_sub(bits: static int, iters: int, gen: RandomGen) =

const N = (bits + 7) div 8

var x, y, z, m: mpz_t
Expand All @@ -106,7 +106,7 @@ proc test_sub(bits: static int, iters: int, gen: RandomGen) =
mpz_init(z)
mpz_init(m)
mpz_ui_pow_ui(m, 2, bits) # 2^bits

for _ in 0 ..< iters:
let a = rng.random_elem(Stuint[bits], gen)
let b = rng.random_elem(Stuint[bits], gen)
Expand Down Expand Up @@ -144,7 +144,7 @@ template testSubstraction(bits: static int) =
test_sub(bits, Iters, Long01Sequence)

proc test_mul(bits: static int, iters: int, gen: RandomGen) =

const N = (bits + 7) div 8

var x, y, z, m: mpz_t
Expand All @@ -153,7 +153,7 @@ proc test_mul(bits: static int, iters: int, gen: RandomGen) =
mpz_init(z)
mpz_init(m)
mpz_ui_pow_ui(m, 2, bits) # 2^bits

for _ in 0 ..< iters:
let a = rng.random_elem(Stuint[bits], gen)
let b = rng.random_elem(Stuint[bits], gen)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_bugfix.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import ../stint, unittest
import ../stint, unittest2

suite "various bugfix":
test "skipPrefixes bug":
let x = "0b1010101".parse(UInt128, 2)
let z = "0bcdef12345".parse(UInt128, 16)

check x == 0b1010101.u128
check z == 0x0bcdef12345.u128

expect(AssertionDefect):
discard "0bcdef12345".parse(UInt128, 10)
Loading

0 comments on commit 5c5e01c

Please sign in to comment.