fix compile-time hash, port to unittest2

* 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
This commit is contained in:
Jacek Sieka 2023-09-27 10:55:57 +02:00
parent 711cda4456
commit 0101f4c449
No known key found for this signature in database
GPG Key ID: A1B09461ABB656B8
33 changed files with 2729 additions and 2962 deletions

View File

@ -1,3 +1,5 @@
mode = ScriptMode.Verbose
packageName = "stint"
version = "2.0.0"
author = "Status Research & Development GmbH"
@ -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#static-test"
let nimc = getEnv("NIMC", "nim") # Which nim compiler to use
let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js)
@ -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"
@ -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"

View File

@ -35,10 +35,8 @@ template wordType*(_: type SomeBigInteger): type =
Word
template hash*(num: StUint|StInt): Hash =
# TODO:
# `hashData` is not particularly efficient.
# Explore better hashing solutions in nim-stew.
hashData(unsafeAddr num, sizeof num)
mixin hash, limbs
hash(num.limbs)
{.pop.}

View File

@ -38,6 +38,6 @@ import
test_bugfix,
test_features
when defined(cpp):
when defined(cpp) and not defined(unittest2Static):
import
test_vs_intx

View File

@ -1 +1,2 @@
switch("warning", "BareExcept:off")
switch("define", "unittest2Static")

View File

@ -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)":

View File

@ -9,10 +9,11 @@
import
# Standard library
std/[unittest, times],
std/times,
# Internal
../stint,
# Test utilities
unittest2,
../helpers/prng_unsafe
const Iters = 50000
@ -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)

View File

@ -1,6 +1,6 @@
# 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).
@ -8,7 +8,7 @@
import
# Standard library
std/[unittest, times, strutils],
std/[unittest2, times, strutils],
# Third-party
gmp, stew/byteutils,
# Internal
@ -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
@ -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)
@ -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
@ -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)
@ -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
@ -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)

View File

@ -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)

View File

@ -7,9 +7,9 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, strutils, test_helpers
import ../stint, unittest2, strutils
template chkStuintToStuint(chk: untyped, N, bits: static[int]) =
template chkStuintToStuint(N, bits: static[int]) =
block:
let x = StUint[N].high
let y = stuint(0, N)
@ -20,13 +20,13 @@ template chkStuintToStuint(chk: untyped, N, bits: static[int]) =
let zz = stuint(z, bits)
when N <= bits:
chk $x == $xx
check $x == $xx
else:
chk $xx == $(StUint[bits].high)
chk $y == $yy
chk $z == $zz
check $xx == $(StUint[bits].high)
check $y == $yy
check $z == $zz
template chkStintToStuint(chk, handleErr: untyped, N, bits: static[int]) =
template chkStintToStuint(N, bits: static[int]) =
block:
let w = StInt[N].low
let x = StInt[N].high
@ -34,25 +34,25 @@ template chkStintToStuint(chk, handleErr: untyped, N, bits: static[int]) =
let z = stint(1, N)
let v = stint(-1, N)
handleErr OverflowDefect:
expect OverflowDefect:
discard stuint(w, bits)
let xx = stuint(x, bits)
let yy = stuint(y, bits)
let zz = stuint(z, bits)
handleErr OverflowDefect:
expect OverflowDefect:
discard stuint(v, bits)
when N <= bits:
chk $x == $xx
check $x == $xx
else:
chk $xx == $(StUint[bits].high)
check $xx == $(StUint[bits].high)
chk $y == $yy
chk $z == $zz
check $y == $yy
check $z == $zz
template chkStintToStint(chk, handleErr: untyped, N, bits: static[int]) =
template chkStintToStint(N, bits: static[int]) =
block:
let y = stint(0, N)
let z = stint(1, N)
@ -65,24 +65,24 @@ template chkStintToStint(chk, handleErr: untyped, N, bits: static[int]) =
when bits >= N:
let x = StInt[N].high
let xx = stint(x, bits)
chk $x == $xx
check $x == $xx
else:
let x = fromHex(StInt[N], toHex(StInt[bits].high))
let xx = stint(x, bits)
chk $x == $xx
check $x == $xx
chk $y == $yy
chk $z == $zz
chk $v == $vv
check $y == $yy
check $z == $zz
check $v == $vv
let w = StInt[N].low
when bits < N:
handleErr RangeDefect:
expect RangeDefect:
discard stint(w, bits)
else:
let ww = stint(w, bits)
chk $w == $ww
check $w == $ww
let m = stint(int32.low, N)
let n = stint(int64.low, N)
@ -90,10 +90,10 @@ template chkStintToStint(chk, handleErr: untyped, N, bits: static[int]) =
let mm = stint(m, bits)
let nn = stint(n, bits)
chk $m == $mm
chk $n == $nn
check $m == $mm
check $n == $nn
template chkStuintToStint(chk, handleErr: untyped, N, bits: static[int]) =
template chkStuintToStint(N, bits: static[int]) =
block:
let y = stuint(0, N)
let z = stuint(1, N)
@ -103,107 +103,108 @@ template chkStuintToStint(chk, handleErr: untyped, N, bits: static[int]) =
let zz = stint(z, bits)
when bits <= N:
handleErr RangeDefect:
expect RangeDefect:
discard stint(v, bits)
else:
let vv = stint(v, bits)
chk v.toHex == vv.toHex
check v.toHex == vv.toHex
chk $y == $yy
chk $z == $zz
check $y == $yy
check $z == $zz
template testConversion(chk, tst, handleErr: untyped) =
tst "stuint to stuint":
chkStuintToStuint(chk, 64, 64)
chkStuintToStuint(chk, 64, 128)
chkStuintToStuint(chk, 64, 256)
chkStuintToStuint(chk, 64, 512)
chkStuintToStuint(chk, 128, 64)
chkStuintToStuint(chk, 128, 128)
chkStuintToStuint(chk, 128, 256)
chkStuintToStuint(chk, 128, 512)
suite "Testing conversion between big integers":
test "stuint to stuint":
chkStuintToStuint(64, 64)
chkStuintToStuint(64, 128)
chkStuintToStuint(64, 256)
chkStuintToStuint(64, 512)
chkStuintToStuint(chk, 256, 64)
chkStuintToStuint(chk, 256, 128)
chkStuintToStuint(chk, 256, 256)
chkStuintToStuint(chk, 256, 512)
chkStuintToStuint(128, 64)
chkStuintToStuint(128, 128)
chkStuintToStuint(128, 256)
chkStuintToStuint(128, 512)
chkStuintToStuint(chk, 512, 64)
chkStuintToStuint(chk, 512, 128)
chkStuintToStuint(chk, 512, 256)
chkStuintToStuint(chk, 512, 512)
chkStuintToStuint(256, 64)
chkStuintToStuint(256, 128)
chkStuintToStuint(256, 256)
chkStuintToStuint(256, 512)
tst "stint to stuint":
chkStintToStuint(chk, handleErr, 64, 64)
chkStintToStuint(chk, handleErr, 64, 128)
chkStintToStuint(chk, handleErr, 64, 256)
chkStintToStuint(chk, handleErr, 64, 512)
chkStuintToStuint(512, 64)
chkStuintToStuint(512, 128)
chkStuintToStuint(512, 256)
chkStuintToStuint(512, 512)
chkStintToStuint(chk, handleErr, 128, 64)
chkStintToStuint(chk, handleErr, 128, 128)
chkStintToStuint(chk, handleErr, 128, 256)
chkStintToStuint(chk, handleErr, 128, 512)
test "stint to stuint":
chkStintToStuint(64, 64)
chkStintToStuint(64, 128)
chkStintToStuint(64, 256)
chkStintToStuint(64, 512)
chkStintToStuint(chk, handleErr, 256, 64)
chkStintToStuint(chk, handleErr, 256, 128)
chkStintToStuint(chk, handleErr, 256, 256)
chkStintToStuint(chk, handleErr, 256, 512)
chkStintToStuint(128, 64)
chkStintToStuint(128, 128)
chkStintToStuint(128, 256)
chkStintToStuint(128, 512)
chkStintToStuint(chk, handleErr, 512, 64)
chkStintToStuint(chk, handleErr, 512, 128)
chkStintToStuint(chk, handleErr, 512, 256)
chkStintToStuint(chk, handleErr, 512, 512)
chkStintToStuint(256, 64)
chkStintToStuint(256, 128)
chkStintToStuint(256, 256)
chkStintToStuint(256, 512)
tst "stint to stint":
chkStintToStint(chk, handleErr, 64, 64)
chkStintToStint(chk, handleErr, 64, 128)
chkStintToStint(chk, handleErr, 64, 256)
chkStintToStint(chk, handleErr, 64, 512)
chkStintToStuint(512, 64)
chkStintToStuint(512, 128)
chkStintToStuint(512, 256)
chkStintToStuint(512, 512)
chkStintToStint(chk, handleErr, 128, 64)
chkStintToStint(chk, handleErr, 128, 128)
chkStintToStint(chk, handleErr, 128, 256)
chkStintToStint(chk, handleErr, 128, 512)
test "stint to stint":
chkStintToStint(64, 64)
chkStintToStint(64, 128)
chkStintToStint(64, 256)
chkStintToStint(64, 512)
chkStintToStint(chk, handleErr, 256, 64)
chkStintToStint(chk, handleErr, 256, 128)
chkStintToStint(chk, handleErr, 256, 256)
chkStintToStint(chk, handleErr, 256, 512)
chkStintToStint(128, 64)
chkStintToStint(128, 128)
chkStintToStint(128, 256)
chkStintToStint(128, 512)
chkStintToStint(chk, handleErr, 512, 64)
chkStintToStint(chk, handleErr, 512, 128)
chkStintToStint(chk, handleErr, 512, 256)
chkStintToStint(chk, handleErr, 512, 512)
chkStintToStint(256, 64)
chkStintToStint(256, 128)
chkStintToStint(256, 256)
chkStintToStint(256, 512)
tst "stuint to stint":
chkStuintToStint(chk, handleErr, 64, 64)
chkStuintToStint(chk, handleErr, 64, 128)
chkStuintToStint(chk, handleErr, 64, 256)
chkStuintToStint(chk, handleErr, 64, 512)
chkStintToStint(512, 64)
chkStintToStint(512, 128)
chkStintToStint(512, 256)
chkStintToStint(512, 512)
chkStuintToStint(chk, handleErr, 128, 64)
chkStuintToStint(chk, handleErr, 128, 128)
chkStuintToStint(chk, handleErr, 128, 256)
chkStuintToStint(chk, handleErr, 128, 512)
test "stuint to stint":
chkStuintToStint(64, 64)
chkStuintToStint(64, 128)
chkStuintToStint(64, 256)
chkStuintToStint(64, 512)
chkStuintToStint(chk, handleErr, 256, 64)
chkStuintToStint(chk, handleErr, 256, 128)
chkStuintToStint(chk, handleErr, 256, 256)
chkStuintToStint(chk, handleErr, 256, 512)
chkStuintToStint(128, 64)
chkStuintToStint(128, 128)
chkStuintToStint(128, 256)
chkStuintToStint(128, 512)
chkStuintToStint(chk, handleErr, 512, 64)
chkStuintToStint(chk, handleErr, 512, 128)
chkStuintToStint(chk, handleErr, 512, 256)
chkStuintToStint(chk, handleErr, 512, 512)
chkStuintToStint(256, 64)
chkStuintToStint(256, 128)
chkStuintToStint(256, 256)
chkStuintToStint(256, 512)
static:
testConversion(ctCheck, ctTest, ctExpect)
chkStuintToStint(512, 64)
chkStuintToStint(512, 128)
chkStuintToStint(512, 256)
chkStuintToStint(512, 512)
proc main() =
# Nim GC protests we are using too much global variables
# so put it in a proc
suite "Testing conversion between big integers":
testConversion(check, test, expect)
# static:
# testConversion(ctCheck, ctTest, ctExpect)
main()
# proc main() =
# # Nim GC protests we are using too much global variables
# # so put it in a proc
# suite "Testing conversion between big integers":
# testConversion(check, test, expect)
# main()

View File

@ -8,7 +8,7 @@
import
../stint,
unittest
unittest2
template reject(code: untyped) =
static: assert(not compiles(code))

View File

@ -1,20 +0,0 @@
# this is some template to help mimicking unittest at compile time
# perhaps we can implement a real compile time unittest?
template ctCheck*(cond: untyped) =
doAssert(cond)
template ctTest*(name: string, body: untyped) =
block:
body
echo "[OK] compile time ", name
template ctExpect*(errTypes, body: untyped) =
try:
body
except errTypes:
discard
except CatchableError:
doAssert(false, "unexpected error")
except Defect:
doAssert(false, "unexpected defect")

View File

@ -7,160 +7,149 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkAddition(chk, a, b, c, bits: untyped) =
template checkAddition(a, b, c, bits: untyped) =
block:
let x = stint(a, bits)
let y = stint(b, bits)
chk x + y == stint(c, bits)
check x + y == stint(c, bits)
template chkInplaceAddition(chk, a, b, c, bits: untyped) =
template checkInplaceAddition(a, b, c, bits: untyped) =
block:
var x = stint(a, bits)
x += stint(b, bits)
chk x == stint(c, bits)
check x == stint(c, bits)
template chkSubstraction(chk, a, b, c, bits: untyped) =
template checkSubstraction(a, b, c, bits: untyped) =
block:
let x = stint(a, bits)
let y = stint(b, bits)
chk x - y == stint(c, bits)
check x - y == stint(c, bits)
template chkInplaceSubstraction(chk, a, b, c, bits: untyped) =
template checkInplaceSubstraction(a, b, c, bits: untyped) =
block:
var x = stint(a, bits)
x -= stint(b, bits)
chk x == stint(c, bits)
check x == stint(c, bits)
template chkNegation(chk, a, b, bits: untyped) =
chk -stint(a, bits) == stint(b, bits)
template checkNegation(a, b, bits: untyped) =
check -stint(a, bits) == stint(b, bits)
template chkAbs(chk, a, b, bits: untyped) =
chk stint(a, bits).abs() == stint(b, bits)
template checkAbs(a, b, bits: untyped) =
check stint(a, bits).abs() == stint(b, bits)
template testAddSub(chk, tst: untyped) =
tst "addition":
chkAddition(chk, 0'i8, 0'i8, 0'i8, 128)
chkAddition(chk, high(int8) - 17'i8, 17'i8, high(int8), 128)
chkAddition(chk, low(int8) + 17'i8, 17'i8, low(int8) + 34'i8, 128)
chkAddition(chk, high(int16) - 17'i16, 17'i16, high(int16), 128)
chkAddition(chk, low(int16) + 17'i16, 17'i16, low(int16) + 34'i16, 128)
chkAddition(chk, high(int32) - 17'i32, 17'i32, high(int32), 128)
chkAddition(chk, low(int32) + 17'i32, 17'i32, low(int32) + 34'i32, 128)
chkAddition(chk, high(int64) - 17'i64, 17'i64, high(int64), 128)
chkAddition(chk, low(int64) + 17'i64, 17'i64, low(int64) + 34'i64, 128)
suite "Wider signed int addsub coverage":
test "addition":
checkAddition(0'i8, 0'i8, 0'i8, 128)
checkAddition(high(int8) - 17'i8, 17'i8, high(int8), 128)
checkAddition(low(int8) + 17'i8, 17'i8, low(int8) + 34'i8, 128)
checkAddition(high(int16) - 17'i16, 17'i16, high(int16), 128)
checkAddition(low(int16) + 17'i16, 17'i16, low(int16) + 34'i16, 128)
checkAddition(high(int32) - 17'i32, 17'i32, high(int32), 128)
checkAddition(low(int32) + 17'i32, 17'i32, low(int32) + 34'i32, 128)
checkAddition(high(int64) - 17'i64, 17'i64, high(int64), 128)
checkAddition(low(int64) + 17'i64, 17'i64, low(int64) + 34'i64, 128)
tst "inplace addition":
chkInplaceAddition(chk, 0'i8, 0'i8, 0'i8, 128)
chkInplaceAddition(chk, high(int8) - 17'i8, 17'i8, high(int8), 128)
chkInplaceAddition(chk, low(int8) + 17'i8, 17'i8, low(int8) + 34'i8, 128)
chkInplaceAddition(chk, high(int16) - 17'i16, 17'i16, high(int16), 128)
chkInplaceAddition(chk, low(int16) + 17'i16, 17'i16, low(int16) + 34'i16, 128)
chkInplaceAddition(chk, high(int32) - 17'i32, 17'i32, high(int32), 128)
chkInplaceAddition(chk, low(int32) + 17'i32, 17'i32, low(int32) + 34'i32, 128)
chkInplaceAddition(chk, high(int64) - 17'i64, 17'i64, high(int64), 128)
chkInplaceAddition(chk, low(int64) + 17'i64, 17'i64, low(int64) + 34'i64, 128)
test "inplace addition":
checkInplaceAddition(0'i8, 0'i8, 0'i8, 128)
checkInplaceAddition(high(int8) - 17'i8, 17'i8, high(int8), 128)
checkInplaceAddition(low(int8) + 17'i8, 17'i8, low(int8) + 34'i8, 128)
checkInplaceAddition(high(int16) - 17'i16, 17'i16, high(int16), 128)
checkInplaceAddition(low(int16) + 17'i16, 17'i16, low(int16) + 34'i16, 128)
checkInplaceAddition(high(int32) - 17'i32, 17'i32, high(int32), 128)
checkInplaceAddition(low(int32) + 17'i32, 17'i32, low(int32) + 34'i32, 128)
checkInplaceAddition(high(int64) - 17'i64, 17'i64, high(int64), 128)
checkInplaceAddition(low(int64) + 17'i64, 17'i64, low(int64) + 34'i64, 128)
tst "substraction":
chkSubstraction(chk, 0'i8, 0'i8, 0'i8, 128)
chkSubstraction(chk, high(int8) - 17'i8, 17'i8, high(int8) - 34'i8, 128)
chkSubstraction(chk, -high(int8), -high(int8), 0'i8, 128)
chkSubstraction(chk, high(int16) - 17'i16, 17'i16, high(int16) - 34'i16, 128)
chkSubstraction(chk, -high(int16), -high(int16), 0'i16, 128)
chkSubstraction(chk, high(int32) - 17'i32, 17'i32, high(int32) - 34'i32, 128)
chkSubstraction(chk, -high(int32), -high(int32), 0'i32, 128)
chkSubstraction(chk, high(int64) - 17'i64, 17'i64, high(int64) - 34'i64, 128)
chkSubstraction(chk, -high(int64), -high(int64), 0'i64, 128)
test "substraction":
checkSubstraction(0'i8, 0'i8, 0'i8, 128)
checkSubstraction(high(int8) - 17'i8, 17'i8, high(int8) - 34'i8, 128)
checkSubstraction(-high(int8), -high(int8), 0'i8, 128)
checkSubstraction(high(int16) - 17'i16, 17'i16, high(int16) - 34'i16, 128)
checkSubstraction(-high(int16), -high(int16), 0'i16, 128)
checkSubstraction(high(int32) - 17'i32, 17'i32, high(int32) - 34'i32, 128)
checkSubstraction(-high(int32), -high(int32), 0'i32, 128)
checkSubstraction(high(int64) - 17'i64, 17'i64, high(int64) - 34'i64, 128)
checkSubstraction(-high(int64), -high(int64), 0'i64, 128)
tst "inplace substraction":
chkInplaceSubstraction(chk, 0'i8, 0'i8, 0'i8, 128)
chkInplaceSubstraction(chk, high(int8) - 17'i8, 17'i8, high(int8) - 34'i8, 128)
chkInplaceSubstraction(chk, -high(int8), -high(int8), 0'i8, 128)
chkInplaceSubstraction(chk, high(int16) - 17'i16, 17'i16, high(int16) - 34'i16, 128)
chkInplaceSubstraction(chk, -high(int16), -high(int16), 0'i16, 128)
chkInplaceSubstraction(chk, high(int32) - 17'i32, 17'i32, high(int32) - 34'i32, 128)
chkInplaceSubstraction(chk, -high(int32), -high(int32), 0'i32, 128)
chkInplaceSubstraction(chk, high(int64) - 17'i64, 17'i64, high(int64) - 34'i64, 128)
chkInplaceSubstraction(chk, -high(int64), -high(int64), 0'i64, 128)
test "inplace substraction":
checkInplaceSubstraction(0'i8, 0'i8, 0'i8, 128)
checkInplaceSubstraction(high(int8) - 17'i8, 17'i8, high(int8) - 34'i8, 128)
checkInplaceSubstraction(-high(int8), -high(int8), 0'i8, 128)
checkInplaceSubstraction(high(int16) - 17'i16, 17'i16, high(int16) - 34'i16, 128)
checkInplaceSubstraction(-high(int16), -high(int16), 0'i16, 128)
checkInplaceSubstraction(high(int32) - 17'i32, 17'i32, high(int32) - 34'i32, 128)
checkInplaceSubstraction(-high(int32), -high(int32), 0'i32, 128)
checkInplaceSubstraction(high(int64) - 17'i64, 17'i64, high(int64) - 34'i64, 128)
checkInplaceSubstraction(-high(int64), -high(int64), 0'i64, 128)
tst "negation":
chkNegation(chk, 0, 0, 128)
chkNegation(chk, 128, -128, 128)
chkNegation(chk, 127, -127, 128)
chkNegation(chk, 32768, -32768, 128)
chkNegation(chk, 32767, -32767, 128)
chkNegation(chk, 2147483648, -2147483648, 128)
chkNegation(chk, 2147483647, -2147483647, 128)
test "negation":
checkNegation(0, 0, 128)
checkNegation(128, -128, 128)
checkNegation(127, -127, 128)
checkNegation(32768, -32768, 128)
checkNegation(32767, -32767, 128)
checkNegation(2147483648, -2147483648, 128)
checkNegation(2147483647, -2147483647, 128)
let x = int64.high.i128
chk -x == -9223372036854775807'i64.i128
check -x == -9223372036854775807'i64.i128
let y = int64.low.i128
let z = int64.high.i128 + 1.i128
chk -y == z
check -y == z
tst "absolute integer":
chkAbs(chk, 0, 0, 128)
chkAbs(chk, -127, 127, 128)
chkAbs(chk, -32767, 32767, 128)
chkAbs(chk, -1, 1, 128)
chkAbs(chk, 1, 1, 128)
chkAbs(chk, 127, 127, 128)
chkAbs(chk, 32767, 32767, 128)
chkAbs(chk, -2147483647, 2147483647, 128)
chkAbs(chk, 2147483647, 2147483647, 128)
chkAbs(chk, -9223372036854775807, 9223372036854775807, 128)
chkAbs(chk, 9223372036854775807, 9223372036854775807, 128)
test "absolute integer":
checkAbs(0, 0, 128)
checkAbs(-127, 127, 128)
checkAbs(-32767, 32767, 128)
checkAbs(-1, 1, 128)
checkAbs(1, 1, 128)
checkAbs(127, 127, 128)
checkAbs(32767, 32767, 128)
checkAbs(-2147483647, 2147483647, 128)
checkAbs(2147483647, 2147483647, 128)
checkAbs(-9223372036854775807, 9223372036854775807, 128)
checkAbs(9223372036854775807, 9223372036854775807, 128)
static:
testAddSub(ctCheck, ctTest)
suite "Testing signed addition implementation":
test "In-place addition gives expected result":
proc main() =
# Nim GC protests we are using too much global variables
# so put it in a proc
suite "Wider signed int addsub coverage":
testAddSub(check, test)
var a = 20182018.stint(256)
let b = 20172017.stint(256)
suite "Testing signed addition implementation":
test "In-place addition gives expected result":
a += b
var a = 20182018.stint(256)
let b = 20172017.stint(256)
check a == (20182018'i64 + 20172017'i64).i256
a += b
test "Addition gives expected result":
check a == (20182018'i64 + 20172017'i64).i256
let a = 20182018.stint(256)
let b = 20172017.stint(256)
test "Addition gives expected result":
check a+b == (20182018'i64 + 20172017'i64).i256
let a = 20182018.stint(256)
let b = 20172017.stint(256)
test "When the low half overflows, it is properly carried":
# uint8 (low half) overflow at 255
let a = 100.stint(256)
let b = 100.stint(256)
check a+b == (20182018'i64 + 20172017'i64).i256
check a+b == 200.i256
test "When the low half overflows, it is properly carried":
# uint8 (low half) overflow at 255
let a = 100.stint(256)
let b = 100.stint(256)
suite "Testing signed substraction implementation":
test "In-place substraction gives expected result":
check a+b == 200.i256
var a = 20182018.stint(256)
let b = 20172017.stint(256)
suite "Testing signed substraction implementation":
test "In-place substraction gives expected result":
a -= b
var a = 20182018.stint(256)
let b = 20172017.stint(256)
check a == (20182018'i64 - 20172017'i64).i256
a -= b
test "Substraction gives expected result":
check a == (20182018'i64 - 20172017'i64).i256
let a = 20182018.stint(256)
let b = 20172017.stint(256)
test "Substraction gives expected result":
let a = 20182018.stint(256)
let b = 20172017.stint(256)
check: a-b == (20182018'i64 - 20172017'i64).i256
main()
check: a-b == (20182018'i64 - 20172017'i64).i256

View File

@ -7,129 +7,119 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkNot(chk: untyped, a, b: distinct SomeInteger, bits: int) =
chk stint(a, bits).not() == stint(b, bits)
template chkNot(a, b: string, bits: int) =
check fromHex(StInt[bits], a).not() == fromHex(StInt[bits], b)
template chkNot(chk: untyped, a, b: string, bits: int) =
chk fromHex(StInt[bits], a).not() == fromHex(StInt[bits], b)
template chkOr(a, b, c: string, bits: int) =
check (fromHex(StInt[bits], a) or fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkOr(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StInt[bits], a) or fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkAnd(a, b, c: string, bits: int) =
check (fromHex(StInt[bits], a) and fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkAnd(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StInt[bits], a) and fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkXor(a, b, c: string, bits: int) =
check (fromHex(StInt[bits], a) xor fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkXor(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StInt[bits], a) xor fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkShl(a: string, b: SomeInteger, c: string, bits: int) =
check (fromHex(StInt[bits], a) shl b) == fromHex(StInt[bits], c)
template chkShl(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(StInt[bits], a) shl b) == fromHex(StInt[bits], c)
template chkShr(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(StInt[bits], a) shr b) == fromHex(StInt[bits], c)
template testBitwise(chk, tst: untyped) =
# TODO: see issue #95
#chkShl(chk, "0F", 128, "00", 128)
#chkShl(chk, "0F", 256, "00", 256)
#chkShr(chk, "F0000000000000000000000000000000", 128, "00", 128)
tst "operator `not`":
chkNot(chk, "0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNot(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "0", 128)
chkNot(chk, "F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0", "0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F", 128)
chkNot(chk, "FFFFFFFFFFFF00000000000000000000", "000000000000FFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `or`":
chkOr(chk, "00", "FF", "000000000000000000000000000000FF", 128)
chkOr(chk, "FF", "00", "000000000000000000000000000000FF", 128)
chkOr(chk, "F0", "0F", "000000000000000000000000000000FF", 128)
chkOr(chk, "00", "00", "00000000000000000000000000000000", 128)
chkOr(chk, "FF00", "0F00", "0000000000000000000000000000FF00", 128)
chkOr(chk, "00FF00FF", "000F000F", "00000000000000000000000000FF00FF", 128)
chkOr(chk, "00000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", 128)
tst "operator `and`":
chkAnd(chk, "00", "FF", "00000000000000000000000000000000", 128)
chkAnd(chk, "FF", "00", "00000000000000000000000000000000", 128)
chkAnd(chk, "F0", "0F", "00000000000000000000000000000000", 128)
chkAnd(chk, "00", "00", "00000000000000000000000000000000", 128)
chkAnd(chk, "FF00", "0F00", "00000000000000000000000000000F00", 128)
chkAnd(chk, "00FF00FF", "000F000F", "000000000000000000000000000F000F", 128)
chkAnd(chk, "F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "F0000000000000000000000000FF00FF", 128)
tst "operator `xor`":
chkXor(chk, "00", "FF", "000000000000000000000000000000FF", 128)
chkXor(chk, "FF", "00", "000000000000000000000000000000FF", 128)
chkXor(chk, "F0", "0F", "000000000000000000000000000000FF", 128)
chkXor(chk, "00", "00", "00000000000000000000000000000000", 128)
chkXor(chk, "FF00", "0F00", "0000000000000000000000000000F000", 128)
chkXor(chk, "00FF00FF", "000F000F", "00000000000000000000000000F000F0", 128)
chkXor(chk, "F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "0F0F0000000000000000000000000000", 128)
tst "operator `shl`":
chkShl(chk, "0F", 4, "F0", 128)
chkShl(chk, "F0", 4, "F00", 128)
chkShl(chk, "F0", 3, "780", 128)
chkShl(chk, "F000", 3, "78000", 128)
chkShl(chk, "F0000000", 3, "780000000", 128)
chkShl(chk, "F000000000000000", 3, "78000000000000000", 128)
chkShl(chk, "F0000000000000000000000000000000", 3, "80000000000000000000000000000000", 128)
chkShl(chk, "0F", 33, "1E00000000", 128)
chkShl(chk, "0F", 65, "1E0000000000000000", 128)
chkShl(chk, "0F", 97, "1E000000000000000000000000", 128)
chkShl(chk, "0F", 127, "80000000000000000000000000000000", 128)
chkShl(chk, "0F", 4, "F0", 256)
chkShl(chk, "F0", 4, "F00", 256)
chkShl(chk, "F0", 3, "780", 256)
chkShl(chk, "F000", 3, "78000", 256)
chkShl(chk, "F0000000", 3, "780000000", 256)
chkShl(chk, "F000000000000000", 3, "78000000000000000", 256)
chkShl(chk, "F0000000000000000000000000000000", 3, "780000000000000000000000000000000", 256)
chkShl(chk, "0F", 33, "1E00000000", 256)
chkShl(chk, "0F", 65, "1E0000000000000000", 256)
chkShl(chk, "0F", 97, "1E000000000000000000000000", 256)
chkShl(chk, "0F", 128, "0F00000000000000000000000000000000", 256)
chkShl(chk, "0F", 129, "1E00000000000000000000000000000000", 256)
chkShl(chk, "0F", 255, "8000000000000000000000000000000000000000000000000000000000000000", 256)
tst "operator `shr`":
chkShr(chk, "0F", 4, "00", 128)
chkShr(chk, "F0", 4, "0F", 128)
chkShr(chk, "F0", 3, "1E", 128)
chkShr(chk, "F000", 3, "1E00", 128)
chkShr(chk, "F0000000", 3, "1E000000", 128)
chkShr(chk, "F000000000000000", 3, "1E00000000000000", 128)
chkShr(chk, "F0000000000000000000000000000000", 127, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkShr(chk, "F0000000000000000000000000000000", 33, "FFFFFFFFF80000000000000000000000", 128)
chkShr(chk, "F0000000000000000000000000000000", 65, "FFFFFFFFFFFFFFFFF800000000000000", 128)
chkShr(chk, "F0000000000000000000000000000000", 97, "FFFFFFFFFFFFFFFFFFFFFFFFF8000000", 128)
chkShr(chk, "0F", 4, "00", 256)
chkShr(chk, "F0", 4, "0F", 256)
chkShr(chk, "F0", 3, "1E", 256)
chkShr(chk, "F000", 3, "1E00", 256)
chkShr(chk, "F0000000", 3, "1E000000", 256)
chkShr(chk, "F000000000000000", 3, "1E00000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 255, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 33, "FFFFFFFFF8000000000000000000000000000000000000000000000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 65, "FFFFFFFFFFFFFFFFF80000000000000000000000000000000000000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 129, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8000000000000000000000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 233, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF80000", 256)
static:
testBitwise(ctCheck, ctTest)
template chkShr(a: string, b: SomeInteger, c: string, bits: int) =
check (fromHex(StInt[bits], a) shr b) == fromHex(StInt[bits], c)
suite "Wider signed int bitwise coverage":
testBitwise(check, test)
# TODO: see issue #95
#chkShl("0F", 128, "00", 128)
#chkShl("0F", 256, "00", 256)
#chkShr("F0000000000000000000000000000000", 128, "00", 128)
test "operator `not`":
chkNot("0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNot("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "0", 128)
chkNot("F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0", "0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F", 128)
chkNot("FFFFFFFFFFFF00000000000000000000", "000000000000FFFFFFFFFFFFFFFFFFFF", 128)
test "operator `or`":
chkOr("00", "FF", "000000000000000000000000000000FF", 128)
chkOr("FF", "00", "000000000000000000000000000000FF", 128)
chkOr("F0", "0F", "000000000000000000000000000000FF", 128)
chkOr("00", "00", "00000000000000000000000000000000", 128)
chkOr("FF00", "0F00", "0000000000000000000000000000FF00", 128)
chkOr("00FF00FF", "000F000F", "00000000000000000000000000FF00FF", 128)
chkOr("00000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", 128)
test "operator `and`":
chkAnd("00", "FF", "00000000000000000000000000000000", 128)
chkAnd("FF", "00", "00000000000000000000000000000000", 128)
chkAnd("F0", "0F", "00000000000000000000000000000000", 128)
chkAnd("00", "00", "00000000000000000000000000000000", 128)
chkAnd("FF00", "0F00", "00000000000000000000000000000F00", 128)
chkAnd("00FF00FF", "000F000F", "000000000000000000000000000F000F", 128)
chkAnd("F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "F0000000000000000000000000FF00FF", 128)
test "operator `xor`":
chkXor("00", "FF", "000000000000000000000000000000FF", 128)
chkXor("FF", "00", "000000000000000000000000000000FF", 128)
chkXor("F0", "0F", "000000000000000000000000000000FF", 128)
chkXor("00", "00", "00000000000000000000000000000000", 128)
chkXor("FF00", "0F00", "0000000000000000000000000000F000", 128)
chkXor("00FF00FF", "000F000F", "00000000000000000000000000F000F0", 128)
chkXor("F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "0F0F0000000000000000000000000000", 128)
test "operator `shl`":
chkShl("0F", 4, "F0", 128)
chkShl("F0", 4, "F00", 128)
chkShl("F0", 3, "780", 128)
chkShl("F000", 3, "78000", 128)
chkShl("F0000000", 3, "780000000", 128)
chkShl("F000000000000000", 3, "78000000000000000", 128)
chkShl("F0000000000000000000000000000000", 3, "80000000000000000000000000000000", 128)
chkShl("0F", 33, "1E00000000", 128)
chkShl("0F", 65, "1E0000000000000000", 128)
chkShl("0F", 97, "1E000000000000000000000000", 128)
chkShl("0F", 127, "80000000000000000000000000000000", 128)
chkShl("0F", 4, "F0", 256)
chkShl("F0", 4, "F00", 256)
chkShl("F0", 3, "780", 256)
chkShl("F000", 3, "78000", 256)
chkShl("F0000000", 3, "780000000", 256)
chkShl("F000000000000000", 3, "78000000000000000", 256)
chkShl("F0000000000000000000000000000000", 3, "780000000000000000000000000000000", 256)
chkShl("0F", 33, "1E00000000", 256)
chkShl("0F", 65, "1E0000000000000000", 256)
chkShl("0F", 97, "1E000000000000000000000000", 256)
chkShl("0F", 128, "0F00000000000000000000000000000000", 256)
chkShl("0F", 129, "1E00000000000000000000000000000000", 256)
chkShl("0F", 255, "8000000000000000000000000000000000000000000000000000000000000000", 256)
test "operator `shr`":
chkShr("0F", 4, "00", 128)
chkShr("F0", 4, "0F", 128)
chkShr("F0", 3, "1E", 128)
chkShr("F000", 3, "1E00", 128)
chkShr("F0000000", 3, "1E000000", 128)
chkShr("F000000000000000", 3, "1E00000000000000", 128)
chkShr("F0000000000000000000000000000000", 127, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkShr("F0000000000000000000000000000000", 33, "FFFFFFFFF80000000000000000000000", 128)
chkShr("F0000000000000000000000000000000", 65, "FFFFFFFFFFFFFFFFF800000000000000", 128)
chkShr("F0000000000000000000000000000000", 97, "FFFFFFFFFFFFFFFFFFFFFFFFF8000000", 128)
chkShr("0F", 4, "00", 256)
chkShr("F0", 4, "0F", 256)
chkShr("F0", 3, "1E", 256)
chkShr("F000", 3, "1E00", 256)
chkShr("F0000000", 3, "1E000000", 256)
chkShr("F000000000000000", 3, "1E00000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 255, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 33, "FFFFFFFFF8000000000000000000000000000000000000000000000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 65, "FFFFFFFFFFFFFFFFF80000000000000000000000000000000000000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 129, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8000000000000000000000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 233, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF80000", 256)
suite "Testing signed int bitwise operations":
test "Shift Left":
@ -137,7 +127,7 @@ suite "Testing signed int bitwise operations":
for i in 1..255:
let x = 1.i256 shl i
y = y shl 1
check cast[stint.UInt256](x) == y
check x.toBytesLE() == y.toBytesLE()
test "Shift Right on positive int":
const leftMost = 1.i256 shl 254
@ -145,7 +135,7 @@ suite "Testing signed int bitwise operations":
for i in 1..255:
let x = leftMost shr i
y = y shr 1
check x == cast[stint.Int256](y)
check x.toBytesLE() == y.toBytesLE()
test "Shift Right on negative int":
const
@ -155,7 +145,7 @@ suite "Testing signed int bitwise operations":
for i in 1..255:
let x = leftMostI shr i
y = (y shr 1) or leftMostU
check x == cast[stint.Int256](y)
check x.toBytesLE() == y.toBytesLE()
test "Compile time shift":
const
@ -170,8 +160,8 @@ suite "Testing signed int bitwise operations":
b = (high(stint.UInt256) shl 10) shr 10
c = (high(stint.Int256) shl 10) shr 10
check a != cast[stint.Int256](b)
check c != cast[stint.Int256](b)
check a.toBytesLE() != b.toBytesLE()
check c.toBytesLE() != b.toBytesLE()
check c == a
#[

View File

@ -7,333 +7,321 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkLT(chk: untyped, a, b: string, bits: int) =
chk fromHex(StInt[bits], a) < fromHex(StInt[bits], b)
template chkLT(a, b: string, bits: int) =
check fromHex(StInt[bits], a) < fromHex(StInt[bits], b)
template chkNotLT(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StInt[bits], b) < fromHex(StInt[bits], a)))
template chkNotLT(a, b: string, bits: int) =
check (not(fromHex(StInt[bits], b) < fromHex(StInt[bits], a)))
template chkLTE(chk: untyped, a, b: string, bits: int) =
chk fromHex(StInt[bits], a) <= fromHex(StInt[bits], b)
template chkLTE(a, b: string, bits: int) =
check fromHex(StInt[bits], a) <= fromHex(StInt[bits], b)
template chkNotLTE(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StInt[bits], b) <= fromHex(StInt[bits], a)))
template chkNotLTE(a, b: string, bits: int) =
check (not(fromHex(StInt[bits], b) <= fromHex(StInt[bits], a)))
template chkEQ(chk: untyped, a, b: string, bits: int) =
chk fromHex(StInt[bits], a) == fromHex(StInt[bits], b)
template chkEQ(a, b: string, bits: int) =
check fromHex(StInt[bits], a) == fromHex(StInt[bits], b)
template chkNotEQ(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StInt[bits], a) == fromHex(StInt[bits], b)))
template chkNotEQ(a, b: string, bits: int) =
check (not(fromHex(StInt[bits], a) == fromHex(StInt[bits], b)))
template chkIsZero(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isZero()
template chkIsZero(a: string, bits: int) =
check fromHex(StInt[bits], a).isZero()
template chkNotIsZero(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isZero())
template chkNotIsZero(a: string, bits: int) =
check (not fromHex(StInt[bits], a).isZero())
template chkIsNegative(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isNegative()
template chkIsNegative(a: string, bits: int) =
check fromHex(StInt[bits], a).isNegative()
template chkNotIsNegative(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isNegative())
template chkNotIsNegative(a: string, bits: int) =
check (not fromHex(StInt[bits], a).isNegative())
template chkIsOdd(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isOdd()
template chkIsOdd(a: string, bits: int) =
check fromHex(StInt[bits], a).isOdd()
template chkNotIsOdd(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isOdd())
template chkNotIsOdd(a: string, bits: int) =
check (not fromHex(StInt[bits], a).isOdd())
template chkIsEven(chk: untyped, a: string, bits: int) =
chk fromHex(StInt[bits], a).isEven()
template chkIsEven(a: string, bits: int) =
check fromHex(StInt[bits], a).isEven()
template chkNotIsEven(chk: untyped, a: string, bits: int) =
chk (not fromHex(StInt[bits], a).isEven())
template chkNotIsEven(a: string, bits: int) =
check (not fromHex(StInt[bits], a).isEven())
template testComparison(chk, tst: untyped) =
tst "operator `LT`":
chk 0.i128 < 1.i128
chk -1.i128 < 1.i128
chk -1.i128 < 0.i128
chk Int128.low < Int128.high
chk -2.i128 < -1.i128
chk 1.i128 < 2.i128
chk 10000.i128 < Int128.high
chk Int128.low < 10000.i128
suite "Wider signed int comparison coverage":
test "operator `LT`":
check 0.i128 < 1.i128
check -1.i128 < 1.i128
check -1.i128 < 0.i128
check Int128.low < Int128.high
check -2.i128 < -1.i128
check 1.i128 < 2.i128
check 10000.i128 < Int128.high
check Int128.low < 10000.i128
chk 0.i256 < 1.i256
chk -1.i256 < 1.i256
chk -1.i256 < 0.i256
chk Int256.low < Int256.high
chk -2.i256 < -1.i256
chk 1.i256 < 2.i256
check 0.i256 < 1.i256
check -1.i256 < 1.i256
check -1.i256 < 0.i256
check Int256.low < Int256.high
check -2.i256 < -1.i256
check 1.i256 < 2.i256
chkLT(chk, "0", "F", 128)
chkLT(chk, "F", "FF", 128)
chkLT(chk, "FF", "FFF", 128)
chkLT(chk, "FFFF", "FFFFF", 128)
chkLT(chk, "FFFFF", "FFFFFFFF", 128)
chkLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLT(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLT("0", "F", 128)
chkLT("F", "FF", 128)
chkLT("FF", "FFF", 128)
chkLT("FFFF", "FFFFF", 128)
chkLT("FFFFF", "FFFFFFFF", 128)
chkLT("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLT("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `GT`":
chk 1.i128 > 0.i128
chk 1.i128 > -1.i128
chk 0.i128 > -1.i128
chk Int128.high > Int128.low
chk -1.i128 > -2.i128
chk 2.i128 > 1.i128
test "operator `GT`":
check 1.i128 > 0.i128
check 1.i128 > -1.i128
check 0.i128 > -1.i128
check Int128.high > Int128.low
check -1.i128 > -2.i128
check 2.i128 > 1.i128
chk 1.i256 > 0.i256
chk 1.i256 > -1.i256
chk 0.i256 > -1.i256
chk Int256.high > Int256.low
chk -1.i256 > -2.i256
chk 2.i256 > 1.i256
check 1.i256 > 0.i256
check 1.i256 > -1.i256
check 0.i256 > -1.i256
check Int256.high > Int256.low
check -1.i256 > -2.i256
check 2.i256 > 1.i256
chkNotLT(chk, "0", "F", 128)
chkNotLT(chk, "F", "FF", 128)
chkNotLT(chk, "FF", "FFF", 128)
chkNotLT(chk, "FFFF", "FFFFF", 128)
chkNotLT(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLT(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLT("0", "F", 128)
chkNotLT("F", "FF", 128)
chkNotLT("FF", "FFF", 128)
chkNotLT("FFFF", "FFFFF", 128)
chkNotLT("FFFFF", "FFFFFFFF", 128)
chkNotLT("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLT("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `LTE`":
chk 0.i128 <= 1.i128
chk -1.i128 <= 1.i128
chk -1.i128 <= 0.i128
chk Int128.low <= Int128.high
chk -2.i128 <= -1.i128
chk 1.i128 <= 2.i128
chk 10000.i128 <= Int128.high
chk Int128.low <= 10000.i128
chk Int128.low <= Int128.low
chk Int128.high <= Int128.high
chk 10000.i128 <= 10000.i128
test "operator `LTE`":
check 0.i128 <= 1.i128
check -1.i128 <= 1.i128
check -1.i128 <= 0.i128
check Int128.low <= Int128.high
check -2.i128 <= -1.i128
check 1.i128 <= 2.i128
check 10000.i128 <= Int128.high
check Int128.low <= 10000.i128
check Int128.low <= Int128.low
check Int128.high <= Int128.high
check 10000.i128 <= 10000.i128
chk 0.i256 <= 1.i256
chk -1.i256 <= 1.i256
chk -1.i256 <= 0.i256
chk Int256.low <= Int256.high
chk -2.i256 <= -1.i256
chk 1.i256 <= 2.i256
chk 10000.i256 <= Int256.high
chk Int256.low <= 10000.i256
chk Int256.low <= Int256.low
chk Int256.high <= Int256.high
chk 10000.i256 <= 10000.i256
check 0.i256 <= 1.i256
check -1.i256 <= 1.i256
check -1.i256 <= 0.i256
check Int256.low <= Int256.high
check -2.i256 <= -1.i256
check 1.i256 <= 2.i256
check 10000.i256 <= Int256.high
check Int256.low <= 10000.i256
check Int256.low <= Int256.low
check Int256.high <= Int256.high
check 10000.i256 <= 10000.i256
chkLTE(chk, "0", "F", 128)
chkLTE(chk, "F", "FF", 128)
chkLTE(chk, "FF", "FFF", 128)
chkLTE(chk, "FFFF", "FFFFF", 128)
chkLTE(chk, "FFFFF", "FFFFFFFF", 128)
chkLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLTE("0", "F", 128)
chkLTE("F", "FF", 128)
chkLTE("FF", "FFF", 128)
chkLTE("FFFF", "FFFFF", 128)
chkLTE("FFFFF", "FFFFFFFF", 128)
chkLTE("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLTE("FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLTE("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `GTE`":
chk 1.i128 >= 0.i128
chk 1.i128 >= -1.i128
chk 0.i128 >= -1.i128
chk Int128.high >= Int128.low
chk -1.i128 >= -2.i128
chk 2.i128 >= 1.i128
chk Int128.high >= 10000.i128
chk 10000.i128 >= Int128.low
chk Int128.low >= Int128.low
chk Int128.high >= Int128.high
chk 10000.i128 >= 10000.i128
test "operator `GTE`":
check 1.i128 >= 0.i128
check 1.i128 >= -1.i128
check 0.i128 >= -1.i128
check Int128.high >= Int128.low
check -1.i128 >= -2.i128
check 2.i128 >= 1.i128
check Int128.high >= 10000.i128
check 10000.i128 >= Int128.low
check Int128.low >= Int128.low
check Int128.high >= Int128.high
check 10000.i128 >= 10000.i128
chk 1.i256 >= 0.i256
chk 1.i256 >= -1.i256
chk 0.i256 >= -1.i256
chk Int256.high >= Int256.low
chk -1.i256 >= -2.i256
chk 2.i256 >= 1.i256
chk Int256.high >= 10000.i256
chk 10000.i256 >= Int256.low
chk Int256.low >= Int256.low
chk Int256.high >= Int256.high
chk 10000.i256 >= 10000.i256
check 1.i256 >= 0.i256
check 1.i256 >= -1.i256
check 0.i256 >= -1.i256
check Int256.high >= Int256.low
check -1.i256 >= -2.i256
check 2.i256 >= 1.i256
check Int256.high >= 10000.i256
check 10000.i256 >= Int256.low
check Int256.low >= Int256.low
check Int256.high >= Int256.high
check 10000.i256 >= 10000.i256
chkNotLTE(chk, "0", "F", 128)
chkNotLTE(chk, "F", "FF", 128)
chkNotLTE(chk, "FF", "FFF", 128)
chkNotLTE(chk, "FFFF", "FFFFF", 128)
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLTE("0", "F", 128)
chkNotLTE("F", "FF", 128)
chkNotLTE("FF", "FFF", 128)
chkNotLTE("FFFF", "FFFFF", 128)
chkNotLTE("FFFFF", "FFFFFFFF", 128)
chkNotLTE("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotLTE("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `EQ`":
chk 0.i128 == 0.i128
chk 1.i128 == 1.i128
chk -1.i128 == -1.i128
chk Int128.high == Int128.high
chk Int128.low == Int128.low
test "operator `EQ`":
check 0.i128 == 0.i128
check 1.i128 == 1.i128
check -1.i128 == -1.i128
check Int128.high == Int128.high
check Int128.low == Int128.low
chk 0.i256 == 0.i256
chk 1.i256 == 1.i256
chk -1.i256 == -1.i256
chk Int256.high == Int256.high
chk Int256.low == Int256.low
check 0.i256 == 0.i256
check 1.i256 == 1.i256
check -1.i256 == -1.i256
check Int256.high == Int256.high
check Int256.low == Int256.low
chkEQ(chk, "0", "0", 128)
chkEQ(chk, "F", "F", 128)
chkEQ(chk, "FF", "FF", 128)
chkEQ(chk, "FFFF", "FFFF", 128)
chkEQ(chk, "FFFFF", "FFFFF", 128)
chkEQ(chk, "FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkEQ("0", "0", 128)
chkEQ("F", "F", 128)
chkEQ("FF", "FF", 128)
chkEQ("FFFF", "FFFF", 128)
chkEQ("FFFFF", "FFFFF", 128)
chkEQ("FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `EQ`":
chk Int128.low != Int128.high
chk Int128.high != Int128.low
chk 0.i256 != 1.i256
chk 1.i256 != 0.i256
chk 1.i256 != -1.i256
chk -1.i256 != 1.i256
test "operator not `EQ`":
check Int128.low != Int128.high
check Int128.high != Int128.low
check 0.i256 != 1.i256
check 1.i256 != 0.i256
check 1.i256 != -1.i256
check -1.i256 != 1.i256
chkNotEQ(chk, "0", "F", 128)
chkNotEQ(chk, "F", "FF", 128)
chkNotEQ(chk, "FF", "FFF", 128)
chkNotEQ(chk, "FFFF", "FFFFF", 128)
chkNotEQ(chk, "FFFFF", "FFAFFFFF", 128)
chkNotEQ(chk, "FFFFFFFFFFF", "AFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotEQ("0", "F", 128)
chkNotEQ("F", "FF", 128)
chkNotEQ("FF", "FFF", 128)
chkNotEQ("FFFF", "FFFFF", 128)
chkNotEQ("FFFFF", "FFAFFFFF", 128)
chkNotEQ("FFFFFFFFFFF", "AFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `isZero`":
chkIsZero(chk, "0", 128)
chkIsZero(chk, "0", 256)
test "operator `isZero`":
chkIsZero("0", 128)
chkIsZero("0", 256)
tst "operator not `isZero`":
chkNotIsZero(chk, "5", 128)
chkNotIsZero(chk, "6", 256)
test "operator not `isZero`":
chkNotIsZero("5", 128)
chkNotIsZero("6", 256)
chkNotIsZero(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotIsZero(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 256)
chkNotIsZero("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNotIsZero("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 256)
tst "operator `isNegative`":
chkIsNegative(chk, "F0000000000000000000000000000000", 128)
chkIsNegative(chk, "F000000000000000000000000000000000000000000000000000000000000000", 256)
test "operator `isNegative`":
chkIsNegative("F0000000000000000000000000000000", 128)
chkIsNegative("F000000000000000000000000000000000000000000000000000000000000000", 256)
chkIsNegative(chk, "A5000000000000000000000000000000", 128)
chkIsNegative(chk, "A600000000000000000000000000000000000000000000000000000000000000", 256)
chkIsNegative("A5000000000000000000000000000000", 128)
chkIsNegative("A600000000000000000000000000000000000000000000000000000000000000", 256)
tst "operator not `isNegative`":
chkNotIsNegative(chk, "0", 128)
chkNotIsNegative(chk, "0", 256)
test "operator not `isNegative`":
chkNotIsNegative("0", 128)
chkNotIsNegative("0", 256)
chkNotIsNegative(chk, "5", 128)
chkNotIsNegative(chk, "6", 256)
chkNotIsNegative("5", 128)
chkNotIsNegative("6", 256)
chkNotIsNegative(chk, "75000000000000000000000000000000", 128)
chkNotIsNegative(chk, "7600000000000000000000000000000000000000000000000000000000000000", 256)
chkNotIsNegative("75000000000000000000000000000000", 128)
chkNotIsNegative("7600000000000000000000000000000000000000000000000000000000000000", 256)
tst "operator `isOdd`":
chkIsOdd(chk, "1", 128)
chkIsOdd(chk, "1", 256)
test "operator `isOdd`":
chkIsOdd("1", 128)
chkIsOdd("1", 256)
chkIsOdd(chk, "FFFFFFFFFFFFFFF", 128)
chkIsOdd(chk, "FFFFFFFFFFFFFFFFFF", 256)
chkIsOdd("FFFFFFFFFFFFFFF", 128)
chkIsOdd("FFFFFFFFFFFFFFFFFF", 256)
tst "operator not `isOdd`":
chkNotIsOdd(chk, "0", 128)
chkNotIsOdd(chk, "0", 256)
test "operator not `isOdd`":
chkNotIsOdd("0", 128)
chkNotIsOdd("0", 256)
chkNotIsOdd(chk, "4", 128)
chkNotIsOdd(chk, "4", 256)
chkNotIsOdd("4", 128)
chkNotIsOdd("4", 256)
chkNotIsOdd(chk, "FFFFFFFFFFFFFFA", 128)
chkNotIsOdd(chk, "FFFFFFFFFFFFFFFFFA", 256)
chkNotIsOdd("FFFFFFFFFFFFFFA", 128)
chkNotIsOdd("FFFFFFFFFFFFFFFFFA", 256)
tst "operator `isEven`":
chkIsEven(chk, "0", 128)
chkIsEven(chk, "0", 256)
test "operator `isEven`":
chkIsEven("0", 128)
chkIsEven("0", 256)
chkIsEven(chk, "4", 128)
chkIsEven(chk, "4", 256)
chkIsEven("4", 128)
chkIsEven("4", 256)
chkIsEven(chk, "FFFFFFFFFFFFFFA", 128)
chkIsEven(chk, "FFFFFFFFFFFFFFFFFA", 256)
chkIsEven("FFFFFFFFFFFFFFA", 128)
chkIsEven("FFFFFFFFFFFFFFFFFA", 256)
tst "operator not `isEven`":
chkNotIsEven(chk, "1", 128)
chkNotIsEven(chk, "1", 256)
test "operator not `isEven`":
chkNotIsEven("1", 128)
chkNotIsEven("1", 256)
chkNotIsEven(chk, "FFFFFFFFFFFFFFF", 128)
chkNotIsEven(chk, "FFFFFFFFFFFFFFFFFF", 256)
chkNotIsEven("FFFFFFFFFFFFFFF", 128)
chkNotIsEven("FFFFFFFFFFFFFFFFFF", 256)
tst "isOne":
test "isOne":
let x = 1.i128
chk x.isOne
check x.isOne
let y = 1.i256
chk y.isOne
check y.isOne
static:
testComparison(ctCheck, ctTest)
suite "Signed int - Testing comparison operators":
const
a = 10.i256
b = 15.i256
c = 150.i256
proc main() =
# Nim GC protests we are using too much global variables
# so put it in a proc
test "< operator":
check:
a < b
not (a + b < b)
not (a + a + a < b + b)
-c < c
-c < a
-b < -a
not(-b < -b)
suite "Wider signed int comparison coverage":
testComparison(check, test)
test "<= operator":
check:
a <= b
not (a + b <= b)
a + a + a <= b + b
-c <= c
-c <= a
-b <= -a
-b <= -b
suite "Signed int - Testing comparison operators":
let
a = 10.i256
b = 15.i256
c = 150.i256
test "> operator":
check:
b > a
not (b > a + b)
not (b + b > a + a + a)
c > -c
a > -c
b > -c
not(-b > -b)
test "< operator":
check:
a < b
not (a + b < b)
not (a + a + a < b + b)
-c < c
-c < a
-b < -a
not(-b < -b)
test ">= operator":
check:
b >= a
not (b >= a + b)
b + b >= a + a + a
c >= -c
a >= -c
b >= -c
-b >= -b
test "<= operator":
check:
a <= b
not (a + b <= b)
a + a + a <= b + b
-c <= c
-c <= a
-b <= -a
-b <= -b
test "> operator":
check:
b > a
not (b > a + b)
not (b + b > a + a + a)
c > -c
a > -c
b > -c
not(-b > -b)
test ">= operator":
check:
b >= a
not (b >= a + b)
b + b >= a + a + a
c >= -c
a >= -c
b >= -c
-b >= -b
test "isOdd/isEven":
check:
a.isEven
not a.isOdd
b.isOdd
not b.isEven
c.isEven
not c.isOdd
main()
test "isOdd/isEven":
check:
a.isEven
not a.isOdd
b.isOdd
not b.isEven
c.isEven
not c.isOdd

View File

@ -7,10 +7,10 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest2
suite "Testing signed int byte representation":
test "Byte representation conforms to the platform endianness":
runtimeTest "Byte representation conforms to the platform endianness":
block:
let a = 20182018.stint(64)
let b = 20182018'i64

View File

@ -1,80 +0,0 @@
# Stint
# Copyright 2018 Status Research & Development GmbH
# Licensed under either of
#
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, stew/byteutils, test_helpers
template chkToBytesLE(chk: untyped, bits: int, hex: string) =
let x = fromHex(StInt[bits], hex)
chk toBytes(x, littleEndian).toHex() == x.dumpHex(littleEndian)
template chkToBytesBE(chk: untyped, bits: int, hex: string) =
let x = fromHex(StInt[bits], hex)
chk toBytes(x, bigEndian).toHex() == x.dumpHex(bigEndian)
template chkFromBytesBE(chk: untyped, bits: int, hex: string) =
let x = fromHex(StInt[bits], hex)
let z = fromBytesBE(StInt[bits], toByteArrayBE(x))
chk z == x
template chkFromBytesLE(chk: untyped, bits: int, hex: string) =
let x = fromHex(StInt[bits], hex)
let z = fromBytesLE(StInt[bits], toByteArrayLE(x))
chk z == x
template chkEndians(chkFunc, tst, name: untyped) =
tst astToStr(name).substr(3):
name(chkFunc, 64, "abcdef1234567890")
name(chkFunc, 128, "abcdef1234567890abcdef1234567890")
name(chkFunc, 256, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890")
template testEndians(chkFunc, tst: untyped) =
chkEndians(chkFunc, tst, chkToBytesLE)
chkEndians(chkFunc, tst, chkToBytesBE)
chkEndians(chkFunc, tst, chkFromBytesLE)
chkEndians(chkFunc, tst, chkFromBytesBE)
static:
testEndians(ctCheck, ctTest)
suite "Testing endians":
test "Endians give sane results":
check:
1.i128.toByteArrayBE() ==
[0'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
1.i128.toByteArrayLE() ==
[1'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1.i128 == Int128.fromBytesBE(
[0'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
1.i128 == Int128.fromBytesLE(
[1'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
-1.i128.toByteArrayBE() ==
[255'u8, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
-2.i128.toByteArrayBE() ==
[255'u8, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254]
-1.i128.toByteArrayLE() ==
[255'u8, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
-2.i128.toByteArrayLE() ==
[254'u8, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
-2.i128 == Int128.fromBytesBE(
[255'u8, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254])
-2.i128 == Int128.fromBytesLE(
[254'u8, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255])
testEndians(check, test)

View File

@ -7,40 +7,34 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, math, test_helpers
import ../stint, unittest2, math
template chkPow(chk: untyped, a, b, c: string, bits: int) =
chk pow(fromHex(StInt[bits], a), fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
check pow(fromHex(StInt[bits], a), fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkPow(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk pow(fromHex(StInt[bits], a), b) == fromHex(StInt[bits], c)
check pow(fromHex(StInt[bits], a), b) == fromHex(StInt[bits], c)
template testExp(chk, tst: untyped) =
tst "signed BigInt BigInt Pow":
suite "Wider signed int exp coverage":
test "signed BigInt BigInt Pow":
chkPow(chk, "F", "2", "E1", 128)
chkPow(chk, "FF", "2", "FE01", 128)
chkPow(chk, "FF", "3", "FD02FF", 128)
chkPow(chk, "FFF", "3", "FFD002FFF", 128)
chkPow(chk, "FFFFF", "3", "ffffd00002fffff", 128)
chk pow(-10.i128, 2.i128) == 100.i128
chk pow(-10.i128, 3.i128) == -1000.i128
check pow(-10.i128, 2.i128) == 100.i128
check pow(-10.i128, 3.i128) == -1000.i128
tst "signed BigInt Natural Pow":
test "signed BigInt Natural Pow":
chkPow(chk, "F", 2, "E1", 128)
chkPow(chk, "FF", 2, "FE01", 128)
chkPow(chk, "FF", 3, "FD02FF", 128)
chkPow(chk, "FFF", 3, "FFD002FFF", 128)
chkPow(chk, "FFFFF", 3, "ffffd00002fffff", 128)
chk pow(-10.i128, 2) == 100.i128
chk pow(-10.i128, 3) == -1000.i128
static:
testExp(ctCheck, ctTest)
suite "Wider signed int exp coverage":
testExp(check, test)
check pow(-10.i128, 2) == 100.i128
check pow(-10.i128, 3) == -1000.i128
suite "Testing signed exponentiation":
test "Simple exponentiation 5^3":
@ -51,8 +45,8 @@ suite "Testing signed exponentiation":
u = a.stint(64)
check:
cast[uint64](u.pow(b)) == a ^ b
cast[uint64](u.pow(b.stint(64))) == a ^ b
truncate(u.pow(b), uint64) == a ^ b
truncate(u.pow(b.stint(64)), uint64) == a ^ b
test "12 ^ 34 == 4922235242952026704037113243122008064":
# https://www.wolframalpha.com/input/?i=12+%5E+34

View File

@ -7,51 +7,45 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template testInitialization(chk, tst: untyped) =
tst "zero one":
suite "Signed integer initialization":
test "zero one":
var a: StInt[128]
a.setZero
chk a == 0.i128
check a == 0.i128
var b: StInt[256]
b.setZero
chk b == 0.i256
check b == 0.i256
var aa: StInt[128]
aa.setOne
chk aa == 1.i128
check aa == 1.i128
var bb: StInt[256]
bb.setOne
chk bb == 1.i256
check bb == 1.i256
var xx = StInt[128].zero
chk xx == 0.i128
check xx == 0.i128
var yy = StInt[256].zero
chk yy == 0.i256
check yy == 0.i256
var uu = StInt[128].one
chk uu == 1.i128
check uu == 1.i128
var vv = StInt[256].one
chk vv == 1.i256
check vv == 1.i256
tst "hi lo":
test "hi lo":
let x = Int128.high
var z = UInt128.high
z.clearBit(z.bits - 1)
chk x.impl == z
check x.impl == z
let xx = Int128.low
var zz = UInt128.low
zz.setBit(z.bits - 1)
chk xx.impl == zz
static:
testInitialization(ctCheck, ctTest)
suite "Signed integer initialization":
testInitialization(check, test)
check xx.impl == zz

View File

@ -7,49 +7,43 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, math, test_helpers
import ../stint, unittest2, math
template chkAddMod(chk: untyped, a, b, m, c: string, bits: int) =
chk addmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b), fromHex(StInt[bits], m)) == fromHex(StInt[bits], c)
check addmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b), fromHex(StInt[bits], m)) == fromHex(StInt[bits], c)
template chkMulMod(chk: untyped, a, b, m, c: string, bits: int) =
chk mulmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b), fromHex(StInt[bits], m)) == fromHex(StInt[bits], c)
check mulmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b), fromHex(StInt[bits], m)) == fromHex(StInt[bits], c)
template chkPowMod(chk: untyped, a, b, m, c: string, bits: int) =
chk powmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b), fromHex(StInt[bits], m)) == fromHex(StInt[bits], c)
check powmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b), fromHex(StInt[bits], m)) == fromHex(StInt[bits], c)
template testModArith(chk, tst: untyped) =
tst "addmod":
suite "Wider unsigned Modular arithmetic coverage":
test "addmod":
chkAddMod(chk, "F", "F", "7", "2", 128)
chkAddMod(chk, "AAAA", "AA", "F", "0", 128)
chkAddMod(chk, "BBBB", "AAAA", "9", "3", 128)
chkAddMod(chk, "BBBBBBBB", "AAAAAAAA", "9", "6", 128)
chkAddMod(chk, "BBBBBBBBBBBBBBBB", "AAAAAAAAAAAAAAAA", "9", "3", 128)
chk addmod(-5.i128, -5.i128, 3.i128) == -1.i128
chk addmod(5.i128, -9.i128, 3.i128) == -1.i128
chk addmod(-5.i128, 9.i128, 3.i128) == 1.i128
check addmod(-5.i128, -5.i128, 3.i128) == -1.i128
check addmod(5.i128, -9.i128, 3.i128) == -1.i128
check addmod(-5.i128, 9.i128, 3.i128) == 1.i128
tst "submod":
chk submod(10.i128, 5.i128, 3.i128) == 2.i128
chk submod(-6.i128, -5.i128, 3.i128) == -1.i128
chk submod(5.i128, -9.i128, 3.i128) == 2.i128
chk submod(-5.i128, 9.i128, 3.i128) == -2.i128
test "submod":
check submod(10.i128, 5.i128, 3.i128) == 2.i128
check submod(-6.i128, -5.i128, 3.i128) == -1.i128
check submod(5.i128, -9.i128, 3.i128) == 2.i128
check submod(-5.i128, 9.i128, 3.i128) == -2.i128
tst "mulmod":
chk mulmod(10.i128, 5.i128, 3.i128) == 2.i128
chk mulmod(-7.i128, -5.i128, 3.i128) == 2.i128
chk mulmod(6.i128, -9.i128, 4.i128) == -2.i128
chk mulmod(-5.i128, 7.i128, 3.i128) == -2.i128
test "mulmod":
check mulmod(10.i128, 5.i128, 3.i128) == 2.i128
check mulmod(-7.i128, -5.i128, 3.i128) == 2.i128
check mulmod(6.i128, -9.i128, 4.i128) == -2.i128
check mulmod(-5.i128, 7.i128, 3.i128) == -2.i128
tst "powmod":
chk powmod(10.i128, 5.i128, 3.i128) == 1.i128
chk powmod(-7.i128, 4.i128, 3.i128) == 1.i128
chk powmod(-7.i128, 3.i128, 3.i128) == -1.i128
chk powmod(5.i128, 9.i128, 3.i128) == 2.i128
chk powmod(-5.i128, 9.i128, 3.i128) == -2.i128
static:
testModArith(ctCheck, ctTest)
suite "Wider unsigned Modular arithmetic coverage":
testModArith(check, test)
test "powmod":
check powmod(10.i128, 5.i128, 3.i128) == 1.i128
check powmod(-7.i128, 4.i128, 3.i128) == 1.i128
check powmod(-7.i128, 3.i128, 3.i128) == -1.i128
check powmod(5.i128, 9.i128, 3.i128) == 2.i128
check powmod(-5.i128, 9.i128, 3.i128) == -2.i128

View File

@ -7,120 +7,114 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkMul(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StInt[bits], a) * fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkMul(a, b, c: string, bits: int) =
check (fromHex(StInt[bits], a) * fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkDiv(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StInt[bits], a) div fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkDiv(a, b, c: string, bits: int) =
check (fromHex(StInt[bits], a) div fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkMod(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StInt[bits], a) mod fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkMod(a, b, c: string, bits: int) =
check (fromHex(StInt[bits], a) mod fromHex(StInt[bits], b)) == fromHex(StInt[bits], c)
template chkMod(chk: untyped, a, b, c: int, bits: int) =
chk (stint(a, bits) mod stint(b, bits)) == stint(c, bits)
template chkMod(a, b, c: int, bits: int) =
check (stint(a, bits) mod stint(b, bits)) == stint(c, bits)
template chkDivMod(chk: untyped, a, b, c, d: string, bits: int) =
chk divmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b)) == (fromHex(StInt[bits], c), fromHex(StInt[bits], d))
template chkDivMod(a, b, c, d: string, bits: int) =
check divmod(fromHex(StInt[bits], a), fromHex(StInt[bits], b)) == (fromHex(StInt[bits], c), fromHex(StInt[bits], d))
template testMuldiv(chk, tst: untyped) =
tst "operator `mul`":
chkMul(chk, "0", "3", "0", 128)
chkMul(chk, "1", "3", "3", 128)
chkMul(chk, "F0", "3", "2D0", 128)
chkMul(chk, "F000", "3", "2D000", 128)
chkMul(chk, "F0000000", "3", "2D0000000", 128)
chkMul(chk, "F000000000000000", "3", "2D000000000000000", 128)
chkMul(chk, "F0000000000000000000000000000000", "3", "D0000000000000000000000000000000", 128)
chkMul(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "1", 128)
suite "Wider signed int muldiv coverage":
test "operator `mul`":
chkMul("0", "3", "0", 128)
chkMul("1", "3", "3", 128)
chkMul("F0", "3", "2D0", 128)
chkMul("F000", "3", "2D000", 128)
chkMul("F0000000", "3", "2D0000000", 128)
chkMul("F000000000000000", "3", "2D000000000000000", 128)
chkMul("F0000000000000000000000000000000", "3", "D0000000000000000000000000000000", 128)
chkMul("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "1", 128)
tst "operator `div`":
chkDiv(chk, "0", "3", "0", 64)
chkDiv(chk, "1", "3", "0", 64)
chkDiv(chk, "3", "3", "1", 64)
chkDiv(chk, "3", "1", "3", 64)
chkDiv(chk, "FF", "3", "55", 64)
chkDiv(chk, "0F", "FF", "0", 64)
chkDiv(chk, "FF", "FF", "1", 64)
chkDiv(chk, "FFFF", "3", "5555", 64)
chkDiv(chk, "0F", "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFF1", 64)
chkDiv(chk, "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFF", "1", 64)
test "operator `div`":
chkDiv("0", "3", "0", 64)
chkDiv("1", "3", "0", 64)
chkDiv("3", "3", "1", 64)
chkDiv("3", "1", "3", 64)
chkDiv("FF", "3", "55", 64)
chkDiv("0F", "FF", "0", 64)
chkDiv("FF", "FF", "1", 64)
chkDiv("FFFF", "3", "5555", 64)
chkDiv("0F", "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFF1", 64)
chkDiv("FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFF", "1", 64)
chkDiv(chk, "0", "3", "0", 128)
chkDiv(chk, "1", "3", "0", 128)
chkDiv(chk, "3", "3", "1", 128)
chkDiv(chk, "3", "1", "3", 128)
chkDiv(chk, "FF", "3", "55", 128)
chkDiv(chk, "0F", "FF", "0", 128)
chkDiv(chk, "FF", "FF", "1", 128)
chkDiv(chk, "FFFF", "3", "5555", 128)
chkDiv(chk, "0F", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1", 128)
chkDiv(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "1", 128)
chkDiv("0", "3", "0", 128)
chkDiv("1", "3", "0", 128)
chkDiv("3", "3", "1", 128)
chkDiv("3", "1", "3", 128)
chkDiv("FF", "3", "55", 128)
chkDiv("0F", "FF", "0", 128)
chkDiv("FF", "FF", "1", 128)
chkDiv("FFFF", "3", "5555", 128)
chkDiv("0F", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1", 128)
chkDiv("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "1", 128)
tst "operator `mod`":
chkMod(chk, "0", "3", "0", 64)
chkMod(chk, "1", "3", "1", 64)
chkMod(chk, "3", "3", "0", 64)
chkMod(chk, "3", "1", "0", 64)
chkMod(chk, "FFFFFFFFFFFFFFFF", "3", "FFFFFFFFFFFFFFFF", 64)
chkMod(chk, "FFFFFFFFFFFFFFFF", "4", "FFFFFFFFFFFFFFFF", 64)
chkMod(chk, "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "0", 64)
chkMod(chk, "0F", "FFFFFFFFFFFFFFFC", "3", 64)
test "operator `mod`":
chkMod("0", "3", "0", 64)
chkMod("1", "3", "1", 64)
chkMod("3", "3", "0", 64)
chkMod("3", "1", "0", 64)
chkMod("FFFFFFFFFFFFFFFF", "3", "FFFFFFFFFFFFFFFF", 64)
chkMod("FFFFFFFFFFFFFFFF", "4", "FFFFFFFFFFFFFFFF", 64)
chkMod("FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "0", 64)
chkMod("0F", "FFFFFFFFFFFFFFFC", "3", 64)
chkMod(chk, "0", "3", "0", 128)
chkMod(chk, "1", "3", "1", 128)
chkMod(chk, "3", "3", "0", 128)
chkMod(chk, "3", "1", "0", 128)
chkMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "4", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "0", 128)
chkMod(chk, "0F", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", "3", 128)
chkMod("0", "3", "0", 128)
chkMod("1", "3", "1", 128)
chkMod("3", "3", "0", 128)
chkMod("3", "1", "0", 128)
chkMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "4", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "0", 128)
chkMod("0F", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", "3", 128)
tst "operator `divmod`":
chkDivMod(chk, "0", "3", "0", "0", 64)
chkDivMod(chk, "1", "3", "0", "1", 64)
chkDivMod(chk, "3", "3", "1", "0", 64)
chkDivMod(chk, "3", "1", "3", "0", 64)
chkDivMod(chk, "FFFFFFFFFFFFFFFF", "3", "0", "FFFFFFFFFFFFFFFF", 64)
chkDivMod(chk, "FFFFFFFFFFFFFFFF", "4", "0", "FFFFFFFFFFFFFFFF", 64)
chkDivMod(chk, "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "1", "0", 64)
chkDivMod(chk, "0F", "FFFFFFFFFFFFFFFC", "FFFFFFFFFFFFFFFD", "3", 64)
test "operator `divmod`":
chkDivMod("0", "3", "0", "0", 64)
chkDivMod("1", "3", "0", "1", 64)
chkDivMod("3", "3", "1", "0", 64)
chkDivMod("3", "1", "3", "0", 64)
chkDivMod("FFFFFFFFFFFFFFFF", "3", "0", "FFFFFFFFFFFFFFFF", 64)
chkDivMod("FFFFFFFFFFFFFFFF", "4", "0", "FFFFFFFFFFFFFFFF", 64)
chkDivMod("FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "1", "0", 64)
chkDivMod("0F", "FFFFFFFFFFFFFFFC", "FFFFFFFFFFFFFFFD", "3", 64)
chkDivMod(chk, "0", "3", "0", "0", 128)
chkDivMod(chk, "1", "3", "0", "1", 128)
chkDivMod(chk, "3", "3", "1", "0", 128)
chkDivMod(chk, "3", "1", "3", "0", 128)
chkDivMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkDivMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "4", "0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkDivMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "1", "0", 128)
chkDivMod(chk, "0F", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD", "3", 128)
chkDivMod("0", "3", "0", "0", 128)
chkDivMod("1", "3", "0", "1", 128)
chkDivMod("3", "3", "1", "0", 128)
chkDivMod("3", "1", "3", "0", 128)
chkDivMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkDivMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "4", "0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkDivMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "1", "0", 128)
chkDivMod("0F", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD", "3", 128)
tst "issues with Nim v1.0.2":
test "issues with Nim v1.0.2":
block:
let x = -2.stint(256)
let y = 3200566678774828.stint(256)
let z = -6401133357549656.stint(256)
chk x * y == z
check x * y == z
chkMod(chk, -3, 7, -3, 64)
chkMod(chk, -3, 5, -3, 64)
chkMod(chk, -13, 5, -3, 64)
chkMod(chk, 7, 5, 2, 64)
chkMod(chk, -7, 5, -2, 64)
chkMod(chk, 7, -5, 2, 64)
chkMod(chk, -7, -5, -2, 64)
chkMod(-3, 7, -3, 64)
chkMod(-3, 5, -3, 64)
chkMod(-13, 5, -3, 64)
chkMod(7, 5, 2, 64)
chkMod(-7, 5, -2, 64)
chkMod(7, -5, 2, 64)
chkMod(-7, -5, -2, 64)
chkMod(chk, 2, 5, 2, 64)
chkMod(chk, -2, 5, -2, 64)
chkMod(chk, 2, -5, 2, 64)
chkMod(chk, -2, -5, -2, 64)
static:
testMuldiv(ctCheck, ctTest)
suite "Wider signed int muldiv coverage":
testMuldiv(check, test)
chkMod(2, 5, 2, 64)
chkMod(-2, 5, -2, 64)
chkMod(2, -5, 2, 64)
chkMod(-2, -5, -2, 64)
suite "Testing signed int multiplication implementation":
test "Multiplication with result fitting in low half":
@ -128,14 +122,14 @@ suite "Testing signed int multiplication implementation":
let a = 10000.stint(64)
let b = 10000.stint(64)
check: cast[int64](a*b) == 100_000_000'i64 # need 27-bits
check: truncate(a*b, int64) == 100_000_000'i64 # need 27-bits
test "Multiplication with result overflowing low half":
let a = 1_000_000.stint(64)
let b = 1_000_000.stint(64)
check: cast[int64](a*b) == 1_000_000_000_000'i64 # need 40 bits
check: truncate(a*b, int64) == 1_000_000_000_000'i64 # need 40 bits
test "Multiplication with result fitting in low half - opposite signs":
@ -143,8 +137,8 @@ suite "Testing signed int multiplication implementation":
let b = 10000.stint(64)
check:
cast[int64](a*b) == -100_000_000'i64 # need 27-bits
cast[int64](b*a) == -100_000_000'i64
truncate(a*b, int64) == -100_000_000'i64 # need 27-bits
truncate(b*a, int64) == -100_000_000'i64
test "Multiplication with result overflowing low half - opposite signs":
@ -152,23 +146,27 @@ suite "Testing signed int multiplication implementation":
let a = -1_000_000.stint(64)
let b = 1_000_000.stint(64)
check:
cast[int64](a*b) == -1_000_000_000_000'i64 # need 40 bits
cast[int64](b*a) == -1_000_000_000_000'i64
when sizeof(int) == 8:
check:
truncate(a*b, int64) == -1_000_000_000_000'i64 # need 40 bits
truncate(b*a, int64) == -1_000_000_000_000'i64
else:
discard # TODO https://github.com/status-im/nim-stint/issues/144
# TODO truncate fails here
test "Multiplication with result fitting in low half - both negative":
let a = -10000.stint(64)
let b = -10000.stint(64)
check: cast[int64](a*b) == 100_000_000'i64 # need 27-bits
check: truncate(a*b, int64) == 100_000_000'i64 # need 27-bits
test "Multiplication with result overflowing low half - both negative":
let a = -1_000_000.stint(64)
let b = -1_000_000.stint(64)
check: cast[int64](a*b) == 1_000_000_000_000'i64 # need 40 bits
check: truncate(a*b, int64) == 1_000_000_000_000'i64 # need 40 bits
suite "Testing signed int division and modulo implementation":
test "Divmod(100, 13) returns the correct result":
@ -177,8 +175,8 @@ suite "Testing signed int division and modulo implementation":
let b = 13.stint(64)
let qr = divmod(a, b)
check: cast[int64](qr.quot) == 7'i64
check: cast[int64](qr.rem) == 9'i64
check: truncate(qr.quot, int64) == 7'i64
check: truncate(qr.rem, int64) == 9'i64
test "Divmod(-100, 13) returns the correct result":
@ -186,8 +184,8 @@ suite "Testing signed int division and modulo implementation":
let b = 13.stint(64)
let qr = divmod(a, b)
check: cast[int64](qr.quot) == -100'i64 div 13
check: cast[int64](qr.rem) == -100'i64 mod 13
check: truncate(qr.quot, int64) == -100'i64 div 13
check: truncate(qr.rem, int64) == -100'i64 mod 13
test "Divmod(100, -13) returns the correct result":
@ -195,8 +193,8 @@ suite "Testing signed int division and modulo implementation":
let b = -13.stint(64)
let qr = divmod(a, b)
check: cast[int64](qr.quot) == 100'i64 div -13
check: cast[int64](qr.rem) == 100'i64 mod -13
check: truncate(qr.quot, int64) == 100'i64 div -13
check: truncate(qr.rem, int64) == 100'i64 mod -13
test "Divmod(-100, -13) returns the correct result":
@ -204,8 +202,8 @@ suite "Testing signed int division and modulo implementation":
let b = -13.stint(64)
let qr = divmod(a, b)
check: cast[int64](qr.quot) == -100'i64 div -13
check: cast[int64](qr.rem) == -100'i64 mod -13
check: truncate(qr.quot, int64) == -100'i64 div -13
check: truncate(qr.rem, int64) == -100'i64 mod -13
test "Divmod(2^64, 3) returns the correct result":
let a = 1.stint(128) shl 64
@ -221,13 +219,13 @@ suite "Testing signed int division and modulo implementation":
r == 1'u64.i128
test "Divmod(1234567891234567890, 10) returns the correct result":
let a = cast[StInt[64]](1234567891234567890'i64)
let b = cast[StInt[64]](10'i64)
let a = stint(1234567891234567890'i64, 64)
let b = stint(10'i64, 64)
let qr = divmod(a, b)
let q = cast[int64](qr.quot)
let r = cast[int64](qr.rem)
let q = truncate(qr.quot, int64)
let r = truncate(qr.rem, int64)
check: q == 123456789123456789'i64
check: r == 0'i64

View File

@ -7,102 +7,96 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template testSignedness(chk, tst: untyped) =
tst "positive sign":
suite "Signed integer signedness":
test "positive sign":
let a = stint(1, 128)
chk a.sign > 0
chk a.isNegative == false
check a.sign > 0
check a.isNegative == false
let b = stint(uint32.high, 128)
chk b.sign > 0
chk b.isNegative == false
check b.sign > 0
check b.isNegative == false
let c = stint(uint64.high, 128)
chk c.sign > 0
chk c.isNegative == false
check c.sign > 0
check c.isNegative == false
let aa = stint(1, 256)
chk aa.sign > 0
chk aa.isNegative == false
check aa.sign > 0
check aa.isNegative == false
let bb = stint(uint32.high, 256)
chk bb.sign > 0
chk bb.isNegative == false
check bb.sign > 0
check bb.isNegative == false
let cc = stint(uint64.high, 256)
chk cc.sign > 0
chk cc.isNegative == false
check cc.sign > 0
check cc.isNegative == false
var zz: StInt[128]
zz.setOne
chk zz.sign > 0
chk zz.isNegative == false
check zz.sign > 0
check zz.isNegative == false
let yy = StInt[128].one
chk yy.sign > 0
chk yy.isNegative == false
check yy.sign > 0
check yy.isNegative == false
tst "zero sign":
test "zero sign":
let a = stint(0, 128)
chk a.sign == 0
chk a.isNegative == false
check a.sign == 0
check a.isNegative == false
let aa = stint(0, 256)
chk aa.sign == 0
chk aa.isNegative == false
check aa.sign == 0
check aa.isNegative == false
var zz: StInt[128]
zz.setZero
chk zz.sign == 0
chk zz.isNegative == false
check zz.sign == 0
check zz.isNegative == false
let yy = StInt[128].zero
chk yy.sign == 0
chk yy.isNegative == false
check yy.sign == 0
check yy.isNegative == false
tst "negative sign":
test "negative sign":
let a = stint(-1, 128)
chk a.sign < 0
chk a.isNegative == true
check a.sign < 0
check a.isNegative == true
let aa = stint(-1, 256)
chk aa.sign < 0
chk aa.isNegative == true
check aa.sign < 0
check aa.isNegative == true
let zz = -1.i128
chk zz.sign < 0
chk zz.isNegative == true
check zz.sign < 0
check zz.isNegative == true
let yy = -1.i256
chk yy.sign < 0
chk yy.isNegative == true
check yy.sign < 0
check yy.isNegative == true
tst "abs":
test "abs":
let a = -1.i128
let aa = a.abs
chk aa == 1.i128
check aa == 1.i128
let b = -1.i256
let bb = b.abs
chk bb == 1.i256
check bb == 1.i256
tst "negate":
test "negate":
var a = -1.i128
a.negate
chk a == 1.i128
check a == 1.i128
var b = -1.i256
b.negate
chk b == 1.i256
check b == 1.i256
let c = -1.i256
let d = -c
chk d == 1.i256
static:
testSignedness(ctCheck, ctTest)
suite "Signed integer signedness":
testSignedness(check, test)
check d == 1.i256

File diff suppressed because it is too large Load Diff

View File

@ -7,194 +7,188 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkAddition(chk, a, b, c, bits: untyped) =
template chkAddition(a, b, c, bits: untyped) =
block:
let x = stuint(a, bits)
let y = stuint(b, bits)
chk x + y == stuint(c, bits)
check x + y == stuint(c, bits)
template chkInplaceAddition(chk, a, b, c, bits: untyped) =
template chkInplaceAddition(a, b, c, bits: untyped) =
block:
var x = stuint(a, bits)
x += stuint(b, bits)
chk x == stuint(c, bits)
check x == stuint(c, bits)
template chkSubstraction(chk, a, b, c, bits: untyped) =
template chkSubstraction(a, b, c, bits: untyped) =
block:
let x = stuint(a, bits)
let y = stuint(b, bits)
chk x - y == stuint(c, bits)
check x - y == stuint(c, bits)
template chkInplaceSubstraction(chk, a, b, c, bits: untyped) =
template chkInplaceSubstraction(a, b, c, bits: untyped) =
block:
var x = stuint(a, bits)
x -= stuint(b, bits)
chk x == stuint(c, bits)
template testAddSub(chk, tst: untyped) =
tst "addition":
#[chkAddition(chk, 0'u8, 0'u8, 0'u8, 8)
chkAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 8)
chkAddition(chk, low(uint8), 17'u8, low(uint8) + 17'u8, 8)
chkAddition(chk, 0'u8, 0'u8, 0'u8, 16)
chkAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 16)
chkAddition(chk, low(uint8), 17'u8, low(uint8) + 17'u8, 16)
chkAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 16)
chkAddition(chk, low(uint16), 17'u16, low(uint16) + 17'u16, 16)
chkAddition(chk, 0'u8, 0'u8, 0'u8, 32)
chkAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 32)
chkAddition(chk, low(uint8), 17'u8, low(uint8) + 17'u8, 32)
chkAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 32)
chkAddition(chk, low(uint16), 17'u16, low(uint16) + 17'u16, 32)
chkAddition(chk, high(uint32) - 17'u32, 17'u32, high(uint32), 32)
chkAddition(chk, low(uint32), 17'u32, low(uint32) + 17'u32, 32)
chkAddition(chk, 0'u8, 0'u8, 0'u8, 64)
chkAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 64)
chkAddition(chk, low(uint8), 17'u8, low(uint8) + 17'u8, 64)
chkAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 64)
chkAddition(chk, low(uint16), 17'u16, low(uint16) + 17'u16, 64)
chkAddition(chk, high(uint32) - 17'u32, 17'u32, high(uint32), 64)
chkAddition(chk, low(uint32), 17'u32, low(uint32) + 17'u32, 64)
chkAddition(chk, high(uint64) - 17'u64, 17'u64, high(uint64), 64)
chkAddition(chk, low(uint64), 17'u64, low(uint64) + 17'u64, 64)]#
chkAddition(chk, 0'u8, 0'u8, 0'u8, 128)
chkAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 128)
chkAddition(chk, low(uint8), 17'u8, low(uint8) + 17'u8, 128)
chkAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 128)
chkAddition(chk, low(uint16), 17'u16, low(uint16) + 17'u16, 128)
chkAddition(chk, high(uint32) - 17'u32, 17'u32, high(uint32), 128)
chkAddition(chk, low(uint32), 17'u32, low(uint32) + 17'u32, 128)
chkAddition(chk, high(uint64) - 17'u64, 17'u64, high(uint64), 128)
chkAddition(chk, low(uint64), 17'u64, low(uint64) + 17'u64, 128)
tst "inplace addition":
#[chkInplaceAddition(chk, 0'u8, 0'u8, 0'u8, 8)
chkInplaceAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 8)
chkInplaceAddition(chk, low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 8)
chkInplaceAddition(chk, 0'u8, 0'u8, 0'u8, 16)
chkInplaceAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 16)
chkInplaceAddition(chk, low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 16)
chkInplaceAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 16)
chkInplaceAddition(chk, low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 16)
chkInplaceAddition(chk, 0'u8, 0'u8, 0'u8, 32)
chkInplaceAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 32)
chkInplaceAddition(chk, low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 32)
chkInplaceAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 32)
chkInplaceAddition(chk, low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 32)
chkInplaceAddition(chk, high(uint32) - 17'u32, 17'u32, high(uint32), 32)
chkInplaceAddition(chk, low(uint32) + 17'u32, 17'u32, low(uint32) + 34'u32, 32)
chkInplaceAddition(chk, 0'u8, 0'u8, 0'u8, 64)
chkInplaceAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 64)
chkInplaceAddition(chk, low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 64)
chkInplaceAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 64)
chkInplaceAddition(chk, low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 64)
chkInplaceAddition(chk, high(uint32) - 17'u32, 17'u32, high(uint32), 64)
chkInplaceAddition(chk, low(uint32) + 17'u32, 17'u32, low(uint32) + 34'u32, 64)
chkInplaceAddition(chk, high(uint64) - 17'u64, 17'u64, high(uint64), 64)
chkInplaceAddition(chk, low(uint64) + 17'u64, 17'u64, low(uint64) + 34'u64, 64)]#
chkInplaceAddition(chk, 0'u8, 0'u8, 0'u8, 128)
chkInplaceAddition(chk, high(uint8) - 17'u8, 17'u8, high(uint8), 128)
chkInplaceAddition(chk, low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 128)
chkInplaceAddition(chk, high(uint16) - 17'u16, 17'u16, high(uint16), 128)
chkInplaceAddition(chk, low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 128)
chkInplaceAddition(chk, high(uint32) - 17'u32, 17'u32, high(uint32), 128)
chkInplaceAddition(chk, low(uint32) + 17'u32, 17'u32, low(uint32) + 34'u32, 128)
chkInplaceAddition(chk, high(uint64) - 17'u64, 17'u64, high(uint64), 128)
chkInplaceAddition(chk, low(uint64) + 17'u64, 17'u64, low(uint64) + 34'u64, 128)
tst "substraction":
#[chkSubstraction(chk, 0'u8, 0'u8, 0'u8, 8)
chkSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 8)
chkSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 8)
chkSubstraction(chk, 0'u8, 0'u8, 0'u8, 16)
chkSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 16)
chkSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 16)
chkSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 16)
chkSubstraction(chk, low(uint16) + 17'u16, 17'u16, low(uint16), 16)
chkSubstraction(chk, 0'u8, 0'u8, 0'u8, 32)
chkSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 32)
chkSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 32)
chkSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 32)
chkSubstraction(chk, low(uint16) + 17'u16, 17'u16, low(uint16), 32)
chkSubstraction(chk, high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 32)
chkSubstraction(chk, low(uint32) + 17'u32, 17'u32, low(uint32), 32)
chkSubstraction(chk, 0'u8, 0'u8, 0'u8, 64)
chkSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 64)
chkSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 64)
chkSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 64)
chkSubstraction(chk, low(uint16) + 17'u16, 17'u16, low(uint16), 64)
chkSubstraction(chk, high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 64)
chkSubstraction(chk, low(uint32) + 17'u32, 17'u32, low(uint32), 64)
chkSubstraction(chk, high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 64)
chkSubstraction(chk, low(uint64) + 17'u64, 17'u64, low(uint64), 64)]#
chkSubstraction(chk, 0'u8, 0'u8, 0'u8, 128)
chkSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 128)
chkSubstraction(chk, high(uint8), high(uint8), 0'u8, 128)
chkSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 128)
chkSubstraction(chk, high(uint16), high(uint16), 0'u16, 128)
chkSubstraction(chk, high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 128)
chkSubstraction(chk, high(uint32), high(uint32), 0'u32, 128)
chkSubstraction(chk, high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 128)
chkSubstraction(chk, high(uint64), high(uint64), 0'u64, 128)
tst "inplace substraction":
#[chkInplaceSubstraction(chk, 0'u8, 0'u8, 0'u8, 8)
chkInplaceSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 8)
chkInplaceSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 8)
chkInplaceSubstraction(chk, 0'u8, 0'u8, 0'u8, 16)
chkInplaceSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 16)
chkInplaceSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 16)
chkInplaceSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 16)
chkInplaceSubstraction(chk, low(uint16) + 17'u16, 17'u16, low(uint16), 16)
chkInplaceSubstraction(chk, 0'u8, 0'u8, 0'u8, 32)
chkInplaceSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 32)
chkInplaceSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 32)
chkInplaceSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 32)
chkInplaceSubstraction(chk, low(uint16) + 17'u16, 17'u16, low(uint16), 32)
chkInplaceSubstraction(chk, high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 32)
chkInplaceSubstraction(chk, low(uint32) + 17'u32, 17'u32, low(uint32), 32)
chkInplaceSubstraction(chk, 0'u8, 0'u8, 0'u8, 64)
chkInplaceSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 64)
chkInplaceSubstraction(chk, low(uint8) + 17'u8, 17'u8, low(uint8), 64)
chkInplaceSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 64)
chkInplaceSubstraction(chk, low(uint16) + 17'u16, 17'u16, low(uint16), 64)
chkInplaceSubstraction(chk, high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 64)
chkInplaceSubstraction(chk, low(uint32) + 17'u32, 17'u32, low(uint32), 64)
chkInplaceSubstraction(chk, high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 64)
chkInplaceSubstraction(chk, low(uint64) + 17'u64, 17'u64, low(uint64), 64)]#
chkInplaceSubstraction(chk, 0'u8, 0'u8, 0'u8, 128)
chkInplaceSubstraction(chk, high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 128)
chkInplaceSubstraction(chk, high(uint8), high(uint8), 0'u8, 128)
chkInplaceSubstraction(chk, high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 128)
chkInplaceSubstraction(chk, high(uint16), high(uint16), 0'u16, 128)
chkInplaceSubstraction(chk, high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 128)
chkInplaceSubstraction(chk, high(uint32), high(uint32), 0'u32, 128)
chkInplaceSubstraction(chk, high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 128)
chkInplaceSubstraction(chk, high(uint64), high(uint64), 0'u64, 128)
static:
testAddSub(ctCheck, ctTest)
check x == stuint(c, bits)
suite "Wider unsigned int addsub coverage":
testAddSub(check, test)
test "addition":
#[chkAddition(0'u8, 0'u8, 0'u8, 8)
chkAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 8)
chkAddition(low(uint8), 17'u8, low(uint8) + 17'u8, 8)
chkAddition(0'u8, 0'u8, 0'u8, 16)
chkAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 16)
chkAddition(low(uint8), 17'u8, low(uint8) + 17'u8, 16)
chkAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 16)
chkAddition(low(uint16), 17'u16, low(uint16) + 17'u16, 16)
chkAddition(0'u8, 0'u8, 0'u8, 32)
chkAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 32)
chkAddition(low(uint8), 17'u8, low(uint8) + 17'u8, 32)
chkAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 32)
chkAddition(low(uint16), 17'u16, low(uint16) + 17'u16, 32)
chkAddition(high(uint32) - 17'u32, 17'u32, high(uint32), 32)
chkAddition(low(uint32), 17'u32, low(uint32) + 17'u32, 32)
chkAddition(0'u8, 0'u8, 0'u8, 64)
chkAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 64)
chkAddition(low(uint8), 17'u8, low(uint8) + 17'u8, 64)
chkAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 64)
chkAddition(low(uint16), 17'u16, low(uint16) + 17'u16, 64)
chkAddition(high(uint32) - 17'u32, 17'u32, high(uint32), 64)
chkAddition(low(uint32), 17'u32, low(uint32) + 17'u32, 64)
chkAddition(high(uint64) - 17'u64, 17'u64, high(uint64), 64)
chkAddition(low(uint64), 17'u64, low(uint64) + 17'u64, 64)]#
chkAddition(0'u8, 0'u8, 0'u8, 128)
chkAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 128)
chkAddition(low(uint8), 17'u8, low(uint8) + 17'u8, 128)
chkAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 128)
chkAddition(low(uint16), 17'u16, low(uint16) + 17'u16, 128)
chkAddition(high(uint32) - 17'u32, 17'u32, high(uint32), 128)
chkAddition(low(uint32), 17'u32, low(uint32) + 17'u32, 128)
chkAddition(high(uint64) - 17'u64, 17'u64, high(uint64), 128)
chkAddition(low(uint64), 17'u64, low(uint64) + 17'u64, 128)
test "inplace addition":
#[chkInplaceAddition(0'u8, 0'u8, 0'u8, 8)
chkInplaceAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 8)
chkInplaceAddition(low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 8)
chkInplaceAddition(0'u8, 0'u8, 0'u8, 16)
chkInplaceAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 16)
chkInplaceAddition(low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 16)
chkInplaceAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 16)
chkInplaceAddition(low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 16)
chkInplaceAddition(0'u8, 0'u8, 0'u8, 32)
chkInplaceAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 32)
chkInplaceAddition(low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 32)
chkInplaceAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 32)
chkInplaceAddition(low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 32)
chkInplaceAddition(high(uint32) - 17'u32, 17'u32, high(uint32), 32)
chkInplaceAddition(low(uint32) + 17'u32, 17'u32, low(uint32) + 34'u32, 32)
chkInplaceAddition(0'u8, 0'u8, 0'u8, 64)
chkInplaceAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 64)
chkInplaceAddition(low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 64)
chkInplaceAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 64)
chkInplaceAddition(low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 64)
chkInplaceAddition(high(uint32) - 17'u32, 17'u32, high(uint32), 64)
chkInplaceAddition(low(uint32) + 17'u32, 17'u32, low(uint32) + 34'u32, 64)
chkInplaceAddition(high(uint64) - 17'u64, 17'u64, high(uint64), 64)
chkInplaceAddition(low(uint64) + 17'u64, 17'u64, low(uint64) + 34'u64, 64)]#
chkInplaceAddition(0'u8, 0'u8, 0'u8, 128)
chkInplaceAddition(high(uint8) - 17'u8, 17'u8, high(uint8), 128)
chkInplaceAddition(low(uint8) + 17'u8, 17'u8, low(uint8) + 34'u8, 128)
chkInplaceAddition(high(uint16) - 17'u16, 17'u16, high(uint16), 128)
chkInplaceAddition(low(uint16) + 17'u16, 17'u16, low(uint16) + 34'u16, 128)
chkInplaceAddition(high(uint32) - 17'u32, 17'u32, high(uint32), 128)
chkInplaceAddition(low(uint32) + 17'u32, 17'u32, low(uint32) + 34'u32, 128)
chkInplaceAddition(high(uint64) - 17'u64, 17'u64, high(uint64), 128)
chkInplaceAddition(low(uint64) + 17'u64, 17'u64, low(uint64) + 34'u64, 128)
test "substraction":
#[chkSubstraction(0'u8, 0'u8, 0'u8, 8)
chkSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 8)
chkSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 8)
chkSubstraction(0'u8, 0'u8, 0'u8, 16)
chkSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 16)
chkSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 16)
chkSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 16)
chkSubstraction(low(uint16) + 17'u16, 17'u16, low(uint16), 16)
chkSubstraction(0'u8, 0'u8, 0'u8, 32)
chkSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 32)
chkSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 32)
chkSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 32)
chkSubstraction(low(uint16) + 17'u16, 17'u16, low(uint16), 32)
chkSubstraction(high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 32)
chkSubstraction(low(uint32) + 17'u32, 17'u32, low(uint32), 32)
chkSubstraction(0'u8, 0'u8, 0'u8, 64)
chkSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 64)
chkSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 64)
chkSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 64)
chkSubstraction(low(uint16) + 17'u16, 17'u16, low(uint16), 64)
chkSubstraction(high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 64)
chkSubstraction(low(uint32) + 17'u32, 17'u32, low(uint32), 64)
chkSubstraction(high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 64)
chkSubstraction(low(uint64) + 17'u64, 17'u64, low(uint64), 64)]#
chkSubstraction(0'u8, 0'u8, 0'u8, 128)
chkSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 128)
chkSubstraction(high(uint8), high(uint8), 0'u8, 128)
chkSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 128)
chkSubstraction(high(uint16), high(uint16), 0'u16, 128)
chkSubstraction(high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 128)
chkSubstraction(high(uint32), high(uint32), 0'u32, 128)
chkSubstraction(high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 128)
chkSubstraction(high(uint64), high(uint64), 0'u64, 128)
test "inplace substraction":
#[chkInplaceSubstraction(0'u8, 0'u8, 0'u8, 8)
chkInplaceSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 8)
chkInplaceSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 8)
chkInplaceSubstraction(0'u8, 0'u8, 0'u8, 16)
chkInplaceSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 16)
chkInplaceSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 16)
chkInplaceSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 16)
chkInplaceSubstraction(low(uint16) + 17'u16, 17'u16, low(uint16), 16)
chkInplaceSubstraction(0'u8, 0'u8, 0'u8, 32)
chkInplaceSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 32)
chkInplaceSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 32)
chkInplaceSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 32)
chkInplaceSubstraction(low(uint16) + 17'u16, 17'u16, low(uint16), 32)
chkInplaceSubstraction(high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 32)
chkInplaceSubstraction(low(uint32) + 17'u32, 17'u32, low(uint32), 32)
chkInplaceSubstraction(0'u8, 0'u8, 0'u8, 64)
chkInplaceSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 64)
chkInplaceSubstraction(low(uint8) + 17'u8, 17'u8, low(uint8), 64)
chkInplaceSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 64)
chkInplaceSubstraction(low(uint16) + 17'u16, 17'u16, low(uint16), 64)
chkInplaceSubstraction(high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 64)
chkInplaceSubstraction(low(uint32) + 17'u32, 17'u32, low(uint32), 64)
chkInplaceSubstraction(high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 64)
chkInplaceSubstraction(low(uint64) + 17'u64, 17'u64, low(uint64), 64)]#
chkInplaceSubstraction(0'u8, 0'u8, 0'u8, 128)
chkInplaceSubstraction(high(uint8) - 17'u8, 17'u8, high(uint8) - 34'u8, 128)
chkInplaceSubstraction(high(uint8), high(uint8), 0'u8, 128)
chkInplaceSubstraction(high(uint16) - 17'u16, 17'u16, high(uint16) - 34'u16, 128)
chkInplaceSubstraction(high(uint16), high(uint16), 0'u16, 128)
chkInplaceSubstraction(high(uint32) - 17'u32, 17'u32, high(uint32) - 34'u32, 128)
chkInplaceSubstraction(high(uint32), high(uint32), 0'u32, 128)
chkInplaceSubstraction(high(uint64) - 17'u64, 17'u64, high(uint64) - 34'u64, 128)
chkInplaceSubstraction(high(uint64), high(uint64), 0'u64, 128)
#[
suite "Testing unsigned int addition implementation":

View File

@ -7,214 +7,208 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkCountOnes(chk: untyped, bits: int) =
template chkCountOnes(bits: int) =
block:
var x = 0.stuint(bits)
chk x.countOnes == 0
check x.countOnes == 0
for i in 1 .. bits:
x = x shl 1
x = x or 1.stuint(bits)
chk x.countOnes == i
check x.countOnes == i
template chkParity(chk: untyped, bits: int) =
template chkParity(bits: int) =
block:
var x = 0.stuint(bits)
chk x.parity == 0
check x.parity == 0
for i in 1 .. bits:
x = x shl 1
x = x or 1.stuint(bits)
chk x.parity == (i mod 2)
check x.parity == (i mod 2)
template chkFirstOne(chk: untyped, bits: int) =
template chkFirstOne(bits: int) =
block:
var x = 0.stuint(bits)
chk x.firstOne == 0
check x.firstOne == 0
x = x + 1
chk x.firstOne == 1
check x.firstOne == 1
for i in 2 .. bits:
x = x shl 1
chk x.firstOne == i
check x.firstOne == i
template chkLeadingZeros(chk: untyped, bits: int) =
template chkLeadingZeros(bits: int) =
block:
var x = 0.stuint(bits)
chk x.leadingZeros == bits
check x.leadingZeros == bits
x = x + 1
chk x.leadingZeros == (bits-1)
check x.leadingZeros == (bits-1)
for i in 2 .. bits:
x = x shl 1
chk x.leadingZeros == (bits-i)
check x.leadingZeros == (bits-i)
template chkTrailingZeros(chk: untyped, bits: int) =
template chkTrailingZeros(bits: int) =
block:
var x = 0.stuint(bits)
chk x.trailingZeros == bits
check x.trailingZeros == bits
x = x + 1
chk x.trailingZeros == 0
check x.trailingZeros == 0
for i in 1 .. bits:
x = x shl 1
chk x.trailingZeros == i
template testBitOps(chk, tst: untyped) =
tst "countOnes":
chk countOnes(0b01000100'u8.stuint(8)) == 2
chk countOnes(0b01000100'u8.stuint(16)) == 2
chk countOnes(0b01000100_01000100'u16.stuint(16)) == 4
chk countOnes(0b01000100'u8.stuint(32)) == 2
chk countOnes(0b01000100_01000100'u16.stuint(32)) == 4
chk countOnes(0b01000100_01000100_01000100_01000100'u32.stuint(32)) == 8
chk countOnes(0b01000100'u8.stuint(64)) == 2
chk countOnes(0b01000100_01000100'u16.stuint(64)) == 4
chk countOnes(0b01000100_01000100_01000100_01000100'u32.stuint(64)) == 8
chk countOnes(0b01000100_01000100_01000100_01000100_01000100_01000100_01000100_01000100'u64.stuint(64)) == 16
chk countOnes(0b01000100'u8.stuint(128)) == 2
chk countOnes(0b01000100_01000100'u16.stuint(128)) == 4
chk countOnes(0b01000100_01000100_01000100_01000100'u32.stuint(128)) == 8
chk countOnes(0b01000100_01000100_01000100_01000100_01000100_01000100_01000100_01000100'u64.stuint(128)) == 16
chk countOnes(0b01000100'u8.stuint(128) shl 100) == 2
chkCountOnes(chk, 128)
chkCountOnes(chk, 256)
tst "parity":
chk parity(0b00000001'u8.stuint(8)) == 1
chk parity(0b00000011'u8.stuint(8)) == 0
chk parity(0b00000001'u8.stuint(16)) == 1
chk parity(0b00000011'u8.stuint(16)) == 0
chk parity(0b00000001_00000001'u16.stuint(16)) == 0
chk parity(0b00000011_00000001'u16.stuint(16)) == 1
chk parity(0b00000001'u8.stuint(32)) == 1
chk parity(0b00000011'u8.stuint(32)) == 0
chk parity(0b00000001_00000001'u16.stuint(32)) == 0
chk parity(0b00000011_00000001'u16.stuint(32)) == 1
chk parity(0b00000001_00000001_00000001_00000001'u32.stuint(32)) == 0
chk parity(0b00000011_00000001_00000001_00000001'u32.stuint(32)) == 1
chk parity(0b00000001'u8.stuint(64)) == 1
chk parity(0b00000011'u8.stuint(64)) == 0
chk parity(0b00000001_00000001'u16.stuint(64)) == 0
chk parity(0b00000011_00000001'u16.stuint(64)) == 1
chk parity(0b00000001_00000001_00000001_00000001'u32.stuint(64)) == 0
chk parity(0b00000011_00000001_00000001_00000001'u32.stuint(64)) == 1
chk parity(0b00000001_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(64)) == 0
chk parity(0b00000011_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(64)) == 1
chk parity(0b00000001'u8.stuint(128)) == 1
chk parity(0b00000011'u8.stuint(128)) == 0
chk parity(0b00000001_00000001'u16.stuint(128)) == 0
chk parity(0b00000011_00000001'u16.stuint(128)) == 1
chk parity(0b00000001_00000001_00000001_00000001'u32.stuint(128)) == 0
chk parity(0b00000011_00000001_00000001_00000001'u32.stuint(128)) == 1
chk parity(0b00000001_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(128)) == 0
chk parity(0b00000011_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(128)) == 1
chk parity(0b00000001'u8.stuint(128)) == 1
chk parity(0b00000001'u8.stuint(128) shl 100) == 1
chkParity(chk, 128)
chkParity(chk, 256)
tst "firstOne":
chk firstOne(0b00000010'u8.stuint(8)) == 2
chk firstOne(0b00000010'u8.stuint(16)) == 2
chk firstOne(0b00000010_00000000'u16.stuint(16)) == 10
chk firstOne(0b00000010'u8.stuint(32)) == 2
chk firstOne(0b00000010_00000000'u16.stuint(32)) == 10
chk firstOne(0b00000010_00000000_00000000_00000000'u32.stuint(32)) == 26
chk firstOne(0b00000010'u8.stuint(64)) == 8*0+2
chk firstOne(0b00000010_00000000'u16.stuint(64)) == 8*1+2
chk firstOne(0b00000010_00000000_00000000_00000000'u32.stuint(64)) == 8*3+2
chk firstOne(0b00000010_00000000_00000000_00000000_00000000_00000000_00000000_00000000'u64.stuint(64)) == 8*7+2
chk firstOne(0b00000010'u8.stuint(128)) == 2
chk firstOne(0b00000010'u8.stuint(128) shl 100) == 102
chk firstOne(0'u8.stuint(128)) == 0
chkFirstOne(chk, 128)
chkFirstOne(chk, 256)
tst "leadingZeros":
chk leadingZeros(0'u8.stuint(8)) == 8*1
chk leadingZeros(0b00010000'u8.stuint(8)) == 3
chk leadingZeros(0'u8.stuint(16)) == 8*2
chk leadingZeros(0b00010000'u8.stuint(16)) == 8*1+3
chk leadingZeros(0'u16.stuint(16)) == 8*2
chk leadingZeros(0b00000000_00010000'u16.stuint(16)) == 8*1+3
chk leadingZeros(0'u8.stuint(32)) == 8*4
chk leadingZeros(0b00010000'u8.stuint(32)) == 8*3+3
chk leadingZeros(0'u16.stuint(32)) == 8*4
chk leadingZeros(0b00000000_00010000'u16.stuint(32)) == 8*3+3
chk leadingZeros(0'u32.stuint(32)) == 8*4
chk leadingZeros(0b00000000_00000000_00000000_00010000'u32.stuint(32)) == 8*3+3
chk leadingZeros(0'u8.stuint(64)) == 8*8
chk leadingZeros(0b00010000'u8.stuint(64)) == 8*7+3
chk leadingZeros(0'u16.stuint(64)) == 8*8
chk leadingZeros(0b00000000_00010000'u16.stuint(64)) == 8*7+3
chk leadingZeros(0'u32.stuint(64)) == 8*8
chk leadingZeros(0b00000000_00000000_00000000_00010000'u32.stuint(64)) == 8*7+3
chk leadingZeros(0'u64.stuint(64)) == 8*8
chk leadingZeros(0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00010000'u64.stuint(64)) == 8*7+3
chk leadingZeros(0'u8.stuint(128)) == 128
chk leadingZeros(0b00100000'u8.stuint(128)) == 128 - 6
chk leadingZeros(0b00100000'u8.stuint(128) shl 100) == 128 - 106
chkLeadingZeros(chk, 128)
chkLeadingZeros(chk, 256)
tst "trailingZeros":
chk trailingZeros(0'u8.stuint(8)) == 8*1
chk trailingZeros(0b00010000'u8.stuint(8)) == 4
chk trailingZeros(0'u8.stuint(16)) == 8*2
chk trailingZeros(0b00010000'u8.stuint(16)) == 8*0+4
chk trailingZeros(0'u16.stuint(16)) == 8*2
chk trailingZeros(0b00010000_00000000'u16.stuint(16)) == 8*1+4
chk trailingZeros(0'u8.stuint(32)) == 8*4
chk trailingZeros(0b00010000'u8.stuint(32)) == 8*0+4
chk trailingZeros(0'u16.stuint(32)) == 8*4
chk trailingZeros(0b00010000_00000000'u16.stuint(32)) == 8*1+4
chk trailingZeros(0'u32.stuint(32)) == 8*4
chk trailingZeros(0b00010000_00000000_00000000_00000000'u32.stuint(32)) == 8*3+4
chk trailingZeros(0'u8.stuint(64)) == 8*8
chk trailingZeros(0b00010000'u8.stuint(64)) == 8*0+4
chk trailingZeros(0'u16.stuint(64)) == 8*8
chk trailingZeros(0b00010000_00000000'u16.stuint(64)) == 8*1+4
chk trailingZeros(0'u32.stuint(64)) == 8*8
chk trailingZeros(0b00010000_00000000_00000000_00000000'u32.stuint(64)) == 8*3+4
chk trailingZeros(0'u64.stuint(64)) == 8*8
chk trailingZeros(0b00010000_00000000_00000000_00000000_00000000_00000000_00000000_00000000'u64.stuint(64)) == 8*7+4
chk trailingZeros(0b00100000'u8.stuint(128)) == 5
chk trailingZeros(0b00100000'u8.stuint(128) shl 100) == 105
chk trailingZeros(0'u8.stuint(128)) == 128
chkTrailingZeros(chk, 128)
chkTrailingZeros(chk, 256)
static:
testBitOps(ctCheck, ctTest)
check x.trailingZeros == i
suite "Testing bitops2":
testBitOps(check, test)
test "countOnes":
check countOnes(0b01000100'u8.stuint(8)) == 2
check countOnes(0b01000100'u8.stuint(16)) == 2
check countOnes(0b01000100_01000100'u16.stuint(16)) == 4
check countOnes(0b01000100'u8.stuint(32)) == 2
check countOnes(0b01000100_01000100'u16.stuint(32)) == 4
check countOnes(0b01000100_01000100_01000100_01000100'u32.stuint(32)) == 8
check countOnes(0b01000100'u8.stuint(64)) == 2
check countOnes(0b01000100_01000100'u16.stuint(64)) == 4
check countOnes(0b01000100_01000100_01000100_01000100'u32.stuint(64)) == 8
check countOnes(0b01000100_01000100_01000100_01000100_01000100_01000100_01000100_01000100'u64.stuint(64)) == 16
check countOnes(0b01000100'u8.stuint(128)) == 2
check countOnes(0b01000100_01000100'u16.stuint(128)) == 4
check countOnes(0b01000100_01000100_01000100_01000100'u32.stuint(128)) == 8
check countOnes(0b01000100_01000100_01000100_01000100_01000100_01000100_01000100_01000100'u64.stuint(128)) == 16
check countOnes(0b01000100'u8.stuint(128) shl 100) == 2
chkCountOnes(128)
chkCountOnes(256)
test "parity":
check parity(0b00000001'u8.stuint(8)) == 1
check parity(0b00000011'u8.stuint(8)) == 0
check parity(0b00000001'u8.stuint(16)) == 1
check parity(0b00000011'u8.stuint(16)) == 0
check parity(0b00000001_00000001'u16.stuint(16)) == 0
check parity(0b00000011_00000001'u16.stuint(16)) == 1
check parity(0b00000001'u8.stuint(32)) == 1
check parity(0b00000011'u8.stuint(32)) == 0
check parity(0b00000001_00000001'u16.stuint(32)) == 0
check parity(0b00000011_00000001'u16.stuint(32)) == 1
check parity(0b00000001_00000001_00000001_00000001'u32.stuint(32)) == 0
check parity(0b00000011_00000001_00000001_00000001'u32.stuint(32)) == 1
check parity(0b00000001'u8.stuint(64)) == 1
check parity(0b00000011'u8.stuint(64)) == 0
check parity(0b00000001_00000001'u16.stuint(64)) == 0
check parity(0b00000011_00000001'u16.stuint(64)) == 1
check parity(0b00000001_00000001_00000001_00000001'u32.stuint(64)) == 0
check parity(0b00000011_00000001_00000001_00000001'u32.stuint(64)) == 1
check parity(0b00000001_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(64)) == 0
check parity(0b00000011_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(64)) == 1
check parity(0b00000001'u8.stuint(128)) == 1
check parity(0b00000011'u8.stuint(128)) == 0
check parity(0b00000001_00000001'u16.stuint(128)) == 0
check parity(0b00000011_00000001'u16.stuint(128)) == 1
check parity(0b00000001_00000001_00000001_00000001'u32.stuint(128)) == 0
check parity(0b00000011_00000001_00000001_00000001'u32.stuint(128)) == 1
check parity(0b00000001_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(128)) == 0
check parity(0b00000011_00000001_00000001_00000001_00000001_00000001_00000001_00000001'u64.stuint(128)) == 1
check parity(0b00000001'u8.stuint(128)) == 1
check parity(0b00000001'u8.stuint(128) shl 100) == 1
chkParity(128)
chkParity(256)
test "firstOne":
check firstOne(0b00000010'u8.stuint(8)) == 2
check firstOne(0b00000010'u8.stuint(16)) == 2
check firstOne(0b00000010_00000000'u16.stuint(16)) == 10
check firstOne(0b00000010'u8.stuint(32)) == 2
check firstOne(0b00000010_00000000'u16.stuint(32)) == 10
check firstOne(0b00000010_00000000_00000000_00000000'u32.stuint(32)) == 26
check firstOne(0b00000010'u8.stuint(64)) == 8*0+2
check firstOne(0b00000010_00000000'u16.stuint(64)) == 8*1+2
check firstOne(0b00000010_00000000_00000000_00000000'u32.stuint(64)) == 8*3+2
check firstOne(0b00000010_00000000_00000000_00000000_00000000_00000000_00000000_00000000'u64.stuint(64)) == 8*7+2
check firstOne(0b00000010'u8.stuint(128)) == 2
check firstOne(0b00000010'u8.stuint(128) shl 100) == 102
check firstOne(0'u8.stuint(128)) == 0
chkFirstOne(128)
chkFirstOne(256)
test "leadingZeros":
check leadingZeros(0'u8.stuint(8)) == 8*1
check leadingZeros(0b00010000'u8.stuint(8)) == 3
check leadingZeros(0'u8.stuint(16)) == 8*2
check leadingZeros(0b00010000'u8.stuint(16)) == 8*1+3
check leadingZeros(0'u16.stuint(16)) == 8*2
check leadingZeros(0b00000000_00010000'u16.stuint(16)) == 8*1+3
check leadingZeros(0'u8.stuint(32)) == 8*4
check leadingZeros(0b00010000'u8.stuint(32)) == 8*3+3
check leadingZeros(0'u16.stuint(32)) == 8*4
check leadingZeros(0b00000000_00010000'u16.stuint(32)) == 8*3+3
check leadingZeros(0'u32.stuint(32)) == 8*4
check leadingZeros(0b00000000_00000000_00000000_00010000'u32.stuint(32)) == 8*3+3
check leadingZeros(0'u8.stuint(64)) == 8*8
check leadingZeros(0b00010000'u8.stuint(64)) == 8*7+3
check leadingZeros(0'u16.stuint(64)) == 8*8
check leadingZeros(0b00000000_00010000'u16.stuint(64)) == 8*7+3
check leadingZeros(0'u32.stuint(64)) == 8*8
check leadingZeros(0b00000000_00000000_00000000_00010000'u32.stuint(64)) == 8*7+3
check leadingZeros(0'u64.stuint(64)) == 8*8
check leadingZeros(0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00010000'u64.stuint(64)) == 8*7+3
check leadingZeros(0'u8.stuint(128)) == 128
check leadingZeros(0b00100000'u8.stuint(128)) == 128 - 6
check leadingZeros(0b00100000'u8.stuint(128) shl 100) == 128 - 106
chkLeadingZeros(128)
chkLeadingZeros(256)
test "trailingZeros":
check trailingZeros(0'u8.stuint(8)) == 8*1
check trailingZeros(0b00010000'u8.stuint(8)) == 4
check trailingZeros(0'u8.stuint(16)) == 8*2
check trailingZeros(0b00010000'u8.stuint(16)) == 8*0+4
check trailingZeros(0'u16.stuint(16)) == 8*2
check trailingZeros(0b00010000_00000000'u16.stuint(16)) == 8*1+4
check trailingZeros(0'u8.stuint(32)) == 8*4
check trailingZeros(0b00010000'u8.stuint(32)) == 8*0+4
check trailingZeros(0'u16.stuint(32)) == 8*4
check trailingZeros(0b00010000_00000000'u16.stuint(32)) == 8*1+4
check trailingZeros(0'u32.stuint(32)) == 8*4
check trailingZeros(0b00010000_00000000_00000000_00000000'u32.stuint(32)) == 8*3+4
check trailingZeros(0'u8.stuint(64)) == 8*8
check trailingZeros(0b00010000'u8.stuint(64)) == 8*0+4
check trailingZeros(0'u16.stuint(64)) == 8*8
check trailingZeros(0b00010000_00000000'u16.stuint(64)) == 8*1+4
check trailingZeros(0'u32.stuint(64)) == 8*8
check trailingZeros(0b00010000_00000000_00000000_00000000'u32.stuint(64)) == 8*3+4
check trailingZeros(0'u64.stuint(64)) == 8*8
check trailingZeros(0b00010000_00000000_00000000_00000000_00000000_00000000_00000000_00000000'u64.stuint(64)) == 8*7+4
check trailingZeros(0b00100000'u8.stuint(128)) == 5
check trailingZeros(0b00100000'u8.stuint(128) shl 100) == 105
check trailingZeros(0'u8.stuint(128)) == 128
chkTrailingZeros(128)
chkTrailingZeros(256)

View File

@ -7,309 +7,300 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkNot(chk: untyped, a, b: distinct SomeInteger, bits: int) =
chk stuint(a, bits).not() == stuint(b, bits)
template chkNot(a, b: string, bits: int) =
check fromHex(StUint[bits], a).not() == fromHex(StUint[bits], b)
template chkNot(chk: untyped, a, b: string, bits: int) =
chk fromHex(StUint[bits], a).not() == fromHex(StUint[bits], b)
template chkOr(a, b, c: string, bits: int) =
check (fromHex(StUint[bits], a) or fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkOr(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StUint[bits], a) or fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkAnd(a, b, c: string, bits: int) =
check (fromHex(StUint[bits], a) and fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkAnd(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StUint[bits], a) and fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkXor(a, b, c: string, bits: int) =
check (fromHex(StUint[bits], a) xor fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkXor(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StUint[bits], a) xor fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkShl(a: string, b: SomeInteger, c: string, bits: int) =
check (fromHex(StUint[bits], a) shl b) == fromHex(StUint[bits], c)
template chkShl(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(StUint[bits], a) shl b) == fromHex(StUint[bits], c)
template chkShr(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(StUint[bits], a) shr b) == fromHex(StUint[bits], c)
template testBitwise(chk, tst: untyped) =
# TODO: see issue #95
#chkShl(chk, "0F", 8, "00", 8)
#chkShl(chk, "0F", 16, "00", 16)
#chkShl(chk, "0F", 32, "00", 32)
#chkShl(chk, "0F", 64, "00", 64)
#chkShl(chk, "0F", 128, "00", 128)
#chkShl(chk, "0F", 256, "00", 256)
#
#chkShr(chk, "F0", 8, "00", 8)
#chkShr(chk, "F000", 16, "00", 16)
#chkShr(chk, "F0000000", 32, "00", 32)
#chkShr(chk, "F000000000000000", 64, "00", 64)
#chkShr(chk, "F0000000000000000000000000000000", 128, "00", 128)
tst "operator `not`":
#[chkNot(chk, 0'u8, not 0'u8, 8)
chkNot(chk, high(uint8), not high(uint8), 8)
chkNot(chk, "F0", "0F", 8)
chkNot(chk, "0F", "F0", 8)
chkNot(chk, 0'u8, not 0'u16, 16)
chkNot(chk, 0'u16, not 0'u16, 16)
chkNot(chk, high(uint8), not uint16(high(uint8)), 16)
chkNot(chk, high(uint16), not high(uint16), 16)
chkNot(chk, "F0", "FF0F", 16)
chkNot(chk, "0F", "FFF0", 16)
chkNot(chk, "FF00", "00FF", 16)
chkNot(chk, "00FF", "FF00", 16)
chkNot(chk, "0FF0", "F00F", 16)
chkNot(chk, 0'u8, not 0'u32, 32)
chkNot(chk, 0'u16, not 0'u32, 32)
chkNot(chk, 0'u32, not 0'u32, 32)
chkNot(chk, high(uint8), not uint32(high(uint8)), 32)
chkNot(chk, high(uint16), not uint32(high(uint16)), 32)
chkNot(chk, high(uint32), not high(uint32), 32)
chkNot(chk, "F0", "FFFFFF0F", 32)
chkNot(chk, "0F", "FFFFFFF0", 32)
chkNot(chk, "FF00", "FFFF00FF", 32)
chkNot(chk, "00FF", "FFFFFF00", 32)
chkNot(chk, "0000FFFF", "FFFF0000", 32)
chkNot(chk, "00FFFF00", "FF0000FF", 32)
chkNot(chk, "0F0F0F0F", "F0F0F0F0", 32)
chkNot(chk, 0'u8, not 0'u64, 64)
chkNot(chk, 0'u16, not 0'u64, 64)
chkNot(chk, 0'u32, not 0'u64, 64)
chkNot(chk, 0'u64, not 0'u64, 64)
chkNot(chk, high(uint8), not uint64(high(uint8)), 64)
chkNot(chk, high(uint16), not uint64(high(uint16)), 64)
chkNot(chk, high(uint32), not uint64(high(uint32)), 64)
chkNot(chk, high(uint64), not high(uint64), 64)]#
chkNot(chk, "0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNot(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "0", 128)
chkNot(chk, "F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0", "0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F", 128)
chkNot(chk, "FFFFFFFFFFFF00000000000000000000", "000000000000FFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `or`":
#[chkOr(chk, "00", "FF", "FF", 8)
chkOr(chk, "FF", "00", "FF", 8)
chkOr(chk, "F0", "0F", "FF", 8)
chkOr(chk, "00", "00", "00", 8)
chkOr(chk, "00", "FF", "00FF", 16)
chkOr(chk, "FF", "00", "00FF", 16)
chkOr(chk, "F0", "0F", "00FF", 16)
chkOr(chk, "00", "00", "0000", 16)
chkOr(chk, "FF00", "0F00", "FF00", 16)
chkOr(chk, "00", "FF", "000000FF", 32)
chkOr(chk, "FF", "00", "000000FF", 32)
chkOr(chk, "F0", "0F", "000000FF", 32)
chkOr(chk, "00", "00", "00000000", 32)
chkOr(chk, "FF00", "0F00", "0000FF00", 32)
chkOr(chk, "00FF00FF", "000F000F", "00FF00FF", 32)
chkOr(chk, "00", "FF", "00000000000000FF", 64)
chkOr(chk, "FF", "00", "00000000000000FF", 64)
chkOr(chk, "F0", "0F", "00000000000000FF", 64)
chkOr(chk, "00", "00", "0000000000000000", 64)
chkOr(chk, "FF00", "0F00", "000000000000FF00", 64)
chkOr(chk, "00FF00FF", "000F000F", "0000000000FF00FF", 64)]#
chkOr(chk, "00", "FF", "000000000000000000000000000000FF", 128)
chkOr(chk, "FF", "00", "000000000000000000000000000000FF", 128)
chkOr(chk, "F0", "0F", "000000000000000000000000000000FF", 128)
chkOr(chk, "00", "00", "00000000000000000000000000000000", 128)
chkOr(chk, "FF00", "0F00", "0000000000000000000000000000FF00", 128)
chkOr(chk, "00FF00FF", "000F000F", "00000000000000000000000000FF00FF", 128)
chkOr(chk, "00000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", 128)
tst "operator `and`":
#[chkAnd(chk, "00", "FF", "00", 8)
chkAnd(chk, "FF", "00", "00", 8)
chkAnd(chk, "F0", "0F", "00", 8)
chkAnd(chk, "00", "00", "00", 8)
chkAnd(chk, "0F", "0F", "0F", 8)
chkAnd(chk, "FF", "FF", "FF", 8)
chkAnd(chk, "00", "FF", "0000", 16)
chkAnd(chk, "FF", "00", "0000", 16)
chkAnd(chk, "F0", "0F", "0000", 16)
chkAnd(chk, "00", "00", "0000", 16)
chkAnd(chk, "FF00", "0F00", "0F00", 16)
chkAnd(chk, "00", "FF", "00000000", 32)
chkAnd(chk, "FF", "00", "00000000", 32)
chkAnd(chk, "F0", "0F", "00000000", 32)
chkAnd(chk, "00", "00", "00000000", 32)
chkAnd(chk, "FF00", "0F00", "00000F00", 32)
chkAnd(chk, "00FF00FF", "000F000F", "000F000F", 32)
chkAnd(chk, "00", "FF", "0000000000000000", 64)
chkAnd(chk, "FF", "00", "0000000000000000", 64)
chkAnd(chk, "F0", "0F", "0000000000000000", 64)
chkAnd(chk, "00", "00", "0000000000000000", 64)
chkAnd(chk, "FF00", "0F00", "0000000000000F00", 64)
chkAnd(chk, "00FF00FF", "000F000F", "00000000000F000F", 64)]#
chkAnd(chk, "00", "FF", "00000000000000000000000000000000", 128)
chkAnd(chk, "FF", "00", "00000000000000000000000000000000", 128)
chkAnd(chk, "F0", "0F", "00000000000000000000000000000000", 128)
chkAnd(chk, "00", "00", "00000000000000000000000000000000", 128)
chkAnd(chk, "FF00", "0F00", "00000000000000000000000000000F00", 128)
chkAnd(chk, "00FF00FF", "000F000F", "000000000000000000000000000F000F", 128)
chkAnd(chk, "F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "F0000000000000000000000000FF00FF", 128)
tst "operator `xor`":
#[chkXor(chk, "00", "FF", "FF", 8)
chkXor(chk, "FF", "00", "FF", 8)
chkXor(chk, "F0", "0F", "FF", 8)
chkXor(chk, "00", "00", "00", 8)
chkXor(chk, "0F", "0F", "00", 8)
chkXor(chk, "FF", "FF", "00", 8)
chkXor(chk, "00", "FF", "00FF", 16)
chkXor(chk, "FF", "00", "00FF", 16)
chkXor(chk, "F0", "0F", "00FF", 16)
chkXor(chk, "00", "00", "0000", 16)
chkXor(chk, "FF00", "0F00", "F000", 16)
chkXor(chk, "00", "FF", "000000FF", 32)
chkXor(chk, "FF", "00", "000000FF", 32)
chkXor(chk, "F0", "0F", "000000FF", 32)
chkXor(chk, "00", "00", "00000000", 32)
chkXor(chk, "FF00", "0F00", "0000F000", 32)
chkXor(chk, "00FF00FF", "000F000F", "00F000F0", 32)
chkXor(chk, "00", "FF", "00000000000000FF", 64)
chkXor(chk, "FF", "00", "00000000000000FF", 64)
chkXor(chk, "F0", "0F", "00000000000000FF", 64)
chkXor(chk, "00", "00", "0000000000000000", 64)
chkXor(chk, "FF00", "0F00", "000000000000F000", 64)
chkXor(chk, "00FF00FF", "000F000F", "0000000000F000F0", 64)]#
chkXor(chk, "00", "FF", "000000000000000000000000000000FF", 128)
chkXor(chk, "FF", "00", "000000000000000000000000000000FF", 128)
chkXor(chk, "F0", "0F", "000000000000000000000000000000FF", 128)
chkXor(chk, "00", "00", "00000000000000000000000000000000", 128)
chkXor(chk, "FF00", "0F00", "0000000000000000000000000000F000", 128)
chkXor(chk, "00FF00FF", "000F000F", "00000000000000000000000000F000F0", 128)
chkXor(chk, "F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "0F0F0000000000000000000000000000", 128)
tst "operator `shl`":
#[chkShl(chk, "0F", 4, "F0", 8)
chkShl(chk, "F0", 4, "00", 8)
chkShl(chk, "F0", 3, "80", 8)
chkShl(chk, "0F", 7, "80", 8)
chkShl(chk, "0F", 4, "F0", 16)
chkShl(chk, "F0", 4, "F00", 16)
chkShl(chk, "F0", 3, "780", 16)
chkShl(chk, "F000", 3, "8000", 16)
chkShl(chk, "0F", 15, "8000", 16)
chkShl(chk, "0F", 4, "F0", 32)
chkShl(chk, "F0", 4, "F00", 32)
chkShl(chk, "F0", 3, "780", 32)
chkShl(chk, "F000", 3, "78000", 32)
chkShl(chk, "F0000000", 3, "80000000", 32)
chkShl(chk, "0F", 31, "80000000", 32)
chkShl(chk, "0F", 4, "F0", 64)
chkShl(chk, "F0", 4, "F00", 64)
chkShl(chk, "F0", 3, "780", 64)
chkShl(chk, "F000", 3, "78000", 64)
chkShl(chk, "F0000000", 3, "780000000", 64)
chkShl(chk, "F000000000000000", 3, "8000000000000000", 64)
chkShl(chk, "0F", 63, "8000000000000000", 64)
chkShl(chk, "0F", 5, "1E0", 64)
chkShl(chk, "0F", 9, "1E00", 64)
chkShl(chk, "0F", 17, "1E0000", 64)
chkShl(chk, "0F", 33, "1E00000000", 64)]#
chkShl(chk, "0F", 4, "F0", 128)
chkShl(chk, "F0", 4, "F00", 128)
chkShl(chk, "F0", 3, "780", 128)
chkShl(chk, "F000", 3, "78000", 128)
chkShl(chk, "F0000000", 3, "780000000", 128)
chkShl(chk, "F000000000000000", 3, "78000000000000000", 128)
chkShl(chk, "F0000000000000000000000000000000", 3, "80000000000000000000000000000000", 128)
chkShl(chk, "0F", 33, "1E00000000", 128)
chkShl(chk, "0F", 65, "1E0000000000000000", 128)
chkShl(chk, "0F", 97, "1E000000000000000000000000", 128)
chkShl(chk, "0F", 127, "80000000000000000000000000000000", 128)
chkShl(chk, "0F", 4, "F0", 256)
chkShl(chk, "F0", 4, "F00", 256)
chkShl(chk, "F0", 3, "780", 256)
chkShl(chk, "F000", 3, "78000", 256)
chkShl(chk, "F0000000", 3, "780000000", 256)
chkShl(chk, "F000000000000000", 3, "78000000000000000", 256)
chkShl(chk, "F0000000000000000000000000000000", 3, "780000000000000000000000000000000", 256)
chkShl(chk, "0F", 33, "1E00000000", 256)
chkShl(chk, "0F", 65, "1E0000000000000000", 256)
chkShl(chk, "0F", 97, "1E000000000000000000000000", 256)
chkShl(chk, "0F", 128, "0F00000000000000000000000000000000", 256)
chkShl(chk, "0F", 129, "1E00000000000000000000000000000000", 256)
chkShl(chk, "0F", 255, "8000000000000000000000000000000000000000000000000000000000000000", 256)
tst "operator `shr`":
#[chkShr(chk, "0F", 4, "00", 8)
chkShr(chk, "F0", 4, "0F", 8)
chkShr(chk, "F0", 3, "1E", 8)
chkShr(chk, "F0", 7, "01", 8)
chkShr(chk, "0F", 4, "00", 16)
chkShr(chk, "F0", 4, "0F", 16)
chkShr(chk, "F000", 3, "1E00", 16)
chkShr(chk, "F000", 15, "0001", 16)
chkShr(chk, "0F", 4, "00", 32)
chkShr(chk, "F0", 4, "0F", 32)
chkShr(chk, "F0", 3, "1E", 32)
chkShr(chk, "F0000000", 3, "1E000000", 32)
chkShr(chk, "F0000000", 31, "00000001", 32)
chkShr(chk, "0F", 4, "00", 64)
chkShr(chk, "F0", 4, "0F", 64)
chkShr(chk, "F0", 3, "1E", 64)
chkShr(chk, "F000", 3, "1E00", 64)
chkShr(chk, "F0000000", 3, "1E000000", 64)
chkShr(chk, "F000000000000000", 63, "0000000000000001", 64)]#
chkShr(chk, "0F", 4, "00", 128)
chkShr(chk, "F0", 4, "0F", 128)
chkShr(chk, "F0", 3, "1E", 128)
chkShr(chk, "F000", 3, "1E00", 128)
chkShr(chk, "F0000000", 3, "1E000000", 128)
chkShr(chk, "F000000000000000", 3, "1E00000000000000", 128)
chkShr(chk, "F0000000000000000000000000000000", 127, "00000000000000000000000000000001", 128)
chkShr(chk, "F0000000000000000000000000000000", 33, "00000000780000000000000000000000", 128)
chkShr(chk, "F0000000000000000000000000000000", 65, "00000000000000007800000000000000", 128)
chkShr(chk, "F0000000000000000000000000000000", 97, "00000000000000000000000078000000", 128)
chkShr(chk, "0F", 4, "00", 256)
chkShr(chk, "F0", 4, "0F", 256)
chkShr(chk, "F0", 3, "1E", 256)
chkShr(chk, "F000", 3, "1E00", 256)
chkShr(chk, "F0000000", 3, "1E000000", 256)
chkShr(chk, "F000000000000000", 3, "1E00000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 255, "1", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 33, "0000000078000000000000000000000000000000000000000000000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 65, "0000000000000000780000000000000000000000000000000000000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 129, "0000000000000000000000000000000078000000000000000000000000000000", 256)
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 233, "780000", 256)
static:
testBitwise(ctCheck, ctTest)
template chkShr(a: string, b: SomeInteger, c: string, bits: int) =
check (fromHex(StUint[bits], a) shr b) == fromHex(StUint[bits], c)
suite "Wider unsigned int bitwise coverage":
testBitwise(check, test)
# TODO: see issue #95
#chkShl("0F", 8, "00", 8)
#chkShl("0F", 16, "00", 16)
#chkShl("0F", 32, "00", 32)
#chkShl("0F", 64, "00", 64)
#chkShl("0F", 128, "00", 128)
#chkShl("0F", 256, "00", 256)
#
#chkShr("F0", 8, "00", 8)
#chkShr("F000", 16, "00", 16)
#chkShr("F0000000", 32, "00", 32)
#chkShr("F000000000000000", 64, "00", 64)
#chkShr("F0000000000000000000000000000000", 128, "00", 128)
test "operator `not`":
#[chkNot(0'u8, not 0'u8, 8)
chkNot(high(uint8), not high(uint8), 8)
chkNot("F0", "0F", 8)
chkNot("0F", "F0", 8)
chkNot(0'u8, not 0'u16, 16)
chkNot(0'u16, not 0'u16, 16)
chkNot(high(uint8), not uint16(high(uint8)), 16)
chkNot(high(uint16), not high(uint16), 16)
chkNot("F0", "FF0F", 16)
chkNot("0F", "FFF0", 16)
chkNot("FF00", "00FF", 16)
chkNot("00FF", "FF00", 16)
chkNot("0FF0", "F00F", 16)
chkNot(0'u8, not 0'u32, 32)
chkNot(0'u16, not 0'u32, 32)
chkNot(0'u32, not 0'u32, 32)
chkNot(high(uint8), not uint32(high(uint8)), 32)
chkNot(high(uint16), not uint32(high(uint16)), 32)
chkNot(high(uint32), not high(uint32), 32)
chkNot("F0", "FFFFFF0F", 32)
chkNot("0F", "FFFFFFF0", 32)
chkNot("FF00", "FFFF00FF", 32)
chkNot("00FF", "FFFFFF00", 32)
chkNot("0000FFFF", "FFFF0000", 32)
chkNot("00FFFF00", "FF0000FF", 32)
chkNot("0F0F0F0F", "F0F0F0F0", 32)
chkNot(0'u8, not 0'u64, 64)
chkNot(0'u16, not 0'u64, 64)
chkNot(0'u32, not 0'u64, 64)
chkNot(0'u64, not 0'u64, 64)
chkNot(high(uint8), not uint64(high(uint8)), 64)
chkNot(high(uint16), not uint64(high(uint16)), 64)
chkNot(high(uint32), not uint64(high(uint32)), 64)
chkNot(high(uint64), not high(uint64), 64)]#
chkNot("0", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkNot("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "0", 128)
chkNot("F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0", "0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F", 128)
chkNot("FFFFFFFFFFFF00000000000000000000", "000000000000FFFFFFFFFFFFFFFFFFFF", 128)
test "operator `or`":
#[chkOr("00", "FF", "FF", 8)
chkOr("FF", "00", "FF", 8)
chkOr("F0", "0F", "FF", 8)
chkOr("00", "00", "00", 8)
chkOr("00", "FF", "00FF", 16)
chkOr("FF", "00", "00FF", 16)
chkOr("F0", "0F", "00FF", 16)
chkOr("00", "00", "0000", 16)
chkOr("FF00", "0F00", "FF00", 16)
chkOr("00", "FF", "000000FF", 32)
chkOr("FF", "00", "000000FF", 32)
chkOr("F0", "0F", "000000FF", 32)
chkOr("00", "00", "00000000", 32)
chkOr("FF00", "0F00", "0000FF00", 32)
chkOr("00FF00FF", "000F000F", "00FF00FF", 32)
chkOr("00", "FF", "00000000000000FF", 64)
chkOr("FF", "00", "00000000000000FF", 64)
chkOr("F0", "0F", "00000000000000FF", 64)
chkOr("00", "00", "0000000000000000", 64)
chkOr("FF00", "0F00", "000000000000FF00", 64)
chkOr("00FF00FF", "000F000F", "0000000000FF00FF", 64)]#
chkOr("00", "FF", "000000000000000000000000000000FF", 128)
chkOr("FF", "00", "000000000000000000000000000000FF", 128)
chkOr("F0", "0F", "000000000000000000000000000000FF", 128)
chkOr("00", "00", "00000000000000000000000000000000", 128)
chkOr("FF00", "0F00", "0000000000000000000000000000FF00", 128)
chkOr("00FF00FF", "000F000F", "00000000000000000000000000FF00FF", 128)
chkOr("00000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", 128)
test "operator `and`":
#[chkAnd("00", "FF", "00", 8)
chkAnd("FF", "00", "00", 8)
chkAnd("F0", "0F", "00", 8)
chkAnd("00", "00", "00", 8)
chkAnd("0F", "0F", "0F", 8)
chkAnd("FF", "FF", "FF", 8)
chkAnd("00", "FF", "0000", 16)
chkAnd("FF", "00", "0000", 16)
chkAnd("F0", "0F", "0000", 16)
chkAnd("00", "00", "0000", 16)
chkAnd("FF00", "0F00", "0F00", 16)
chkAnd("00", "FF", "00000000", 32)
chkAnd("FF", "00", "00000000", 32)
chkAnd("F0", "0F", "00000000", 32)
chkAnd("00", "00", "00000000", 32)
chkAnd("FF00", "0F00", "00000F00", 32)
chkAnd("00FF00FF", "000F000F", "000F000F", 32)
chkAnd("00", "FF", "0000000000000000", 64)
chkAnd("FF", "00", "0000000000000000", 64)
chkAnd("F0", "0F", "0000000000000000", 64)
chkAnd("00", "00", "0000000000000000", 64)
chkAnd("FF00", "0F00", "0000000000000F00", 64)
chkAnd("00FF00FF", "000F000F", "00000000000F000F", 64)]#
chkAnd("00", "FF", "00000000000000000000000000000000", 128)
chkAnd("FF", "00", "00000000000000000000000000000000", 128)
chkAnd("F0", "0F", "00000000000000000000000000000000", 128)
chkAnd("00", "00", "00000000000000000000000000000000", 128)
chkAnd("FF00", "0F00", "00000000000000000000000000000F00", 128)
chkAnd("00FF00FF", "000F000F", "000000000000000000000000000F000F", 128)
chkAnd("F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "F0000000000000000000000000FF00FF", 128)
test "operator `xor`":
#[chkXor("00", "FF", "FF", 8)
chkXor("FF", "00", "FF", 8)
chkXor("F0", "0F", "FF", 8)
chkXor("00", "00", "00", 8)
chkXor("0F", "0F", "00", 8)
chkXor("FF", "FF", "00", 8)
chkXor("00", "FF", "00FF", 16)
chkXor("FF", "00", "00FF", 16)
chkXor("F0", "0F", "00FF", 16)
chkXor("00", "00", "0000", 16)
chkXor("FF00", "0F00", "F000", 16)
chkXor("00", "FF", "000000FF", 32)
chkXor("FF", "00", "000000FF", 32)
chkXor("F0", "0F", "000000FF", 32)
chkXor("00", "00", "00000000", 32)
chkXor("FF00", "0F00", "0000F000", 32)
chkXor("00FF00FF", "000F000F", "00F000F0", 32)
chkXor("00", "FF", "00000000000000FF", 64)
chkXor("FF", "00", "00000000000000FF", 64)
chkXor("F0", "0F", "00000000000000FF", 64)
chkXor("00", "00", "0000000000000000", 64)
chkXor("FF00", "0F00", "000000000000F000", 64)
chkXor("00FF00FF", "000F000F", "0000000000F000F0", 64)]#
chkXor("00", "FF", "000000000000000000000000000000FF", 128)
chkXor("FF", "00", "000000000000000000000000000000FF", 128)
chkXor("F0", "0F", "000000000000000000000000000000FF", 128)
chkXor("00", "00", "00000000000000000000000000000000", 128)
chkXor("FF00", "0F00", "0000000000000000000000000000F000", 128)
chkXor("00FF00FF", "000F000F", "00000000000000000000000000F000F0", 128)
chkXor("F0000000000000000000000000FF00FF", "FF0F0000000000000000000000FF00FF", "0F0F0000000000000000000000000000", 128)
test "operator `shl`":
#[chkShl("0F", 4, "F0", 8)
chkShl("F0", 4, "00", 8)
chkShl("F0", 3, "80", 8)
chkShl("0F", 7, "80", 8)
chkShl("0F", 4, "F0", 16)
chkShl("F0", 4, "F00", 16)
chkShl("F0", 3, "780", 16)
chkShl("F000", 3, "8000", 16)
chkShl("0F", 15, "8000", 16)
chkShl("0F", 4, "F0", 32)
chkShl("F0", 4, "F00", 32)
chkShl("F0", 3, "780", 32)
chkShl("F000", 3, "78000", 32)
chkShl("F0000000", 3, "80000000", 32)
chkShl("0F", 31, "80000000", 32)
chkShl("0F", 4, "F0", 64)
chkShl("F0", 4, "F00", 64)
chkShl("F0", 3, "780", 64)
chkShl("F000", 3, "78000", 64)
chkShl("F0000000", 3, "780000000", 64)
chkShl("F000000000000000", 3, "8000000000000000", 64)
chkShl("0F", 63, "8000000000000000", 64)
chkShl("0F", 5, "1E0", 64)
chkShl("0F", 9, "1E00", 64)
chkShl("0F", 17, "1E0000", 64)
chkShl("0F", 33, "1E00000000", 64)]#
chkShl("0F", 4, "F0", 128)
chkShl("F0", 4, "F00", 128)
chkShl("F0", 3, "780", 128)
chkShl("F000", 3, "78000", 128)
chkShl("F0000000", 3, "780000000", 128)
chkShl("F000000000000000", 3, "78000000000000000", 128)
chkShl("F0000000000000000000000000000000", 3, "80000000000000000000000000000000", 128)
chkShl("0F", 33, "1E00000000", 128)
chkShl("0F", 65, "1E0000000000000000", 128)
chkShl("0F", 97, "1E000000000000000000000000", 128)
chkShl("0F", 127, "80000000000000000000000000000000", 128)
chkShl("0F", 4, "F0", 256)
chkShl("F0", 4, "F00", 256)
chkShl("F0", 3, "780", 256)
chkShl("F000", 3, "78000", 256)
chkShl("F0000000", 3, "780000000", 256)
chkShl("F000000000000000", 3, "78000000000000000", 256)
chkShl("F0000000000000000000000000000000", 3, "780000000000000000000000000000000", 256)
chkShl("0F", 33, "1E00000000", 256)
chkShl("0F", 65, "1E0000000000000000", 256)
chkShl("0F", 97, "1E000000000000000000000000", 256)
chkShl("0F", 128, "0F00000000000000000000000000000000", 256)
chkShl("0F", 129, "1E00000000000000000000000000000000", 256)
chkShl("0F", 255, "8000000000000000000000000000000000000000000000000000000000000000", 256)
test "operator `shr`":
#[chkShr("0F", 4, "00", 8)
chkShr("F0", 4, "0F", 8)
chkShr("F0", 3, "1E", 8)
chkShr("F0", 7, "01", 8)
chkShr("0F", 4, "00", 16)
chkShr("F0", 4, "0F", 16)
chkShr("F000", 3, "1E00", 16)
chkShr("F000", 15, "0001", 16)
chkShr("0F", 4, "00", 32)
chkShr("F0", 4, "0F", 32)
chkShr("F0", 3, "1E", 32)
chkShr("F0000000", 3, "1E000000", 32)
chkShr("F0000000", 31, "00000001", 32)
chkShr("0F", 4, "00", 64)
chkShr("F0", 4, "0F", 64)
chkShr("F0", 3, "1E", 64)
chkShr("F000", 3, "1E00", 64)
chkShr("F0000000", 3, "1E000000", 64)
chkShr("F000000000000000", 63, "0000000000000001", 64)]#
chkShr("0F", 4, "00", 128)
chkShr("F0", 4, "0F", 128)
chkShr("F0", 3, "1E", 128)
chkShr("F000", 3, "1E00", 128)
chkShr("F0000000", 3, "1E000000", 128)
chkShr("F000000000000000", 3, "1E00000000000000", 128)
chkShr("F0000000000000000000000000000000", 127, "00000000000000000000000000000001", 128)
chkShr("F0000000000000000000000000000000", 33, "00000000780000000000000000000000", 128)
chkShr("F0000000000000000000000000000000", 65, "00000000000000007800000000000000", 128)
chkShr("F0000000000000000000000000000000", 97, "00000000000000000000000078000000", 128)
chkShr("0F", 4, "00", 256)
chkShr("F0", 4, "0F", 256)
chkShr("F0", 3, "1E", 256)
chkShr("F000", 3, "1E00", 256)
chkShr("F0000000", 3, "1E000000", 256)
chkShr("F000000000000000", 3, "1E00000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 255, "1", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 33, "0000000078000000000000000000000000000000000000000000000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 65, "0000000000000000780000000000000000000000000000000000000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 129, "0000000000000000000000000000000078000000000000000000000000000000", 256)
chkShr("F000000000000000000000000000000000000000000000000000000000000000", 233, "780000", 256)
#[
suite "Testing unsigned int bitwise operations":

View File

@ -7,188 +7,182 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkLT(chk: untyped, a, b: string, bits: int) =
chk fromHex(StUint[bits], a) < fromHex(StUint[bits], b)
template chkLT(a, b: string, bits: int) =
check fromHex(StUint[bits], a) < fromHex(StUint[bits], b)
template chkNotLT(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StUint[bits], b) < fromHex(StUint[bits], a)))
template chkNotLT(a, b: string, bits: int) =
check (not(fromHex(StUint[bits], b) < fromHex(StUint[bits], a)))
template chkLTE(chk: untyped, a, b: string, bits: int) =
chk fromHex(StUint[bits], a) <= fromHex(StUint[bits], b)
template chkLTE(a, b: string, bits: int) =
check fromHex(StUint[bits], a) <= fromHex(StUint[bits], b)
template chkNotLTE(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StUint[bits], b) <= fromHex(StUint[bits], a)))
template chkNotLTE(a, b: string, bits: int) =
check (not(fromHex(StUint[bits], b) <= fromHex(StUint[bits], a)))
template chkEQ(chk: untyped, a, b: string, bits: int) =
chk fromHex(StUint[bits], a) == fromHex(StUint[bits], b)
template chkEQ(a, b: string, bits: int) =
check fromHex(StUint[bits], a) == fromHex(StUint[bits], b)
template chkNotEQ(chk: untyped, a, b: string, bits: int) =
chk (not(fromHex(StUint[bits], a) == fromHex(StUint[bits], b)))
template chkNotEQ(a, b: string, bits: int) =
check (not(fromHex(StUint[bits], a) == fromHex(StUint[bits], b)))
template chkIsZero(chk: untyped, a: string, bits: int) =
chk fromHex(StUint[bits], a).isZero()
template chkIsZero(a: string, bits: int) =
check fromHex(StUint[bits], a).isZero()
template chkNotIsZero(chk: untyped, a: string, bits: int) =
chk (not fromHex(StUint[bits], a).isZero())
template chkNotIsZero(a: string, bits: int) =
check (not fromHex(StUint[bits], a).isZero())
template chkIsOdd(chk: untyped, a: string, bits: int) =
chk fromHex(StUint[bits], a).isOdd()
template chkIsOdd(a: string, bits: int) =
check fromHex(StUint[bits], a).isOdd()
template chkNotIsOdd(chk: untyped, a: string, bits: int) =
chk (not fromHex(StUint[bits], a).isOdd())
template testComparison(chk, tst: untyped) =
tst "operator `LT`":
chkLT(chk, "0", "F", 64)
chkLT(chk, "F", "FF", 64)
chkLT(chk, "FF", "FFF", 64)
chkLT(chk, "FFFF", "FFFFF", 64)
chkLT(chk, "FFFFF", "FFFFFFFF", 64)
chkLT(chk, "0", "F", 128)
chkLT(chk, "F", "FF", 128)
chkLT(chk, "FF", "FFF", 128)
chkLT(chk, "FFFF", "FFFFF", 128)
chkLT(chk, "FFFFF", "FFFFFFFF", 128)
chkLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `LT`":
chkNotLT(chk, "0", "F", 64)
chkNotLT(chk, "F", "FF", 64)
chkNotLT(chk, "FF", "FFF", 64)
chkNotLT(chk, "FFFF", "FFFFF", 64)
chkNotLT(chk, "FFFFF", "FFFFFFFF", 64)
chkNotLT(chk, "0", "F", 128)
chkNotLT(chk, "F", "FF", 128)
chkNotLT(chk, "FF", "FFF", 128)
chkNotLT(chk, "FFFF", "FFFFF", 128)
chkNotLT(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLT(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `LTE`":
chkLTE(chk, "0", "F", 64)
chkLTE(chk, "F", "FF", 64)
chkLTE(chk, "FF", "FFF", 64)
chkLTE(chk, "FFFF", "FFFFF", 64)
chkLTE(chk, "FFFFF", "FFFFFFFF", 64)
chkLTE(chk, "FFFFFFFF", "FFFFFFFF", 64)
chkLTE(chk, "0", "F", 128)
chkLTE(chk, "F", "FF", 128)
chkLTE(chk, "FF", "FFF", 128)
chkLTE(chk, "FFFF", "FFFFF", 128)
chkLTE(chk, "FFFFF", "FFFFFFFF", 128)
chkLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLTE(chk, "FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `LTE`":
chkNotLTE(chk, "0", "F", 64)
chkNotLTE(chk, "F", "FF", 64)
chkNotLTE(chk, "FF", "FFF", 64)
chkNotLTE(chk, "FFFF", "FFFFF", 64)
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 64)
chkNotLTE(chk, "0", "F", 128)
chkNotLTE(chk, "F", "FF", 128)
chkNotLTE(chk, "FF", "FFF", 128)
chkNotLTE(chk, "FFFF", "FFFFF", 128)
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 128)
chkNotLTE(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `EQ`":
chkEQ(chk, "0", "0", 64)
chkEQ(chk, "F", "F", 64)
chkEQ(chk, "FF", "FF", 64)
chkEQ(chk, "FFFF", "FFFF", 64)
chkEQ(chk, "FFFFF", "FFFFF", 64)
chkEQ(chk, "FFFFFFFF", "FFFFFFFF", 64)
chkEQ(chk, "0", "0", 128)
chkEQ(chk, "F", "F", 128)
chkEQ(chk, "FF", "FF", 128)
chkEQ(chk, "FFFF", "FFFF", 128)
chkEQ(chk, "FFFFF", "FFFFF", 128)
chkEQ(chk, "FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator not `EQ`":
chkNotEQ(chk, "0", "F", 64)
chkNotEQ(chk, "F", "FF", 64)
chkNotEQ(chk, "FF", "FFF", 64)
chkNotEQ(chk, "FFFF", "FFFFF", 64)
chkNotEQ(chk, "FFFFF", "FFFFFFFF", 64)
chkNotEQ(chk, "0", "F", 128)
chkNotEQ(chk, "F", "FF", 128)
chkNotEQ(chk, "FF", "FFF", 128)
chkNotEQ(chk, "FFFF", "FFFFF", 128)
chkNotEQ(chk, "FFFFF", "FFFFFFFF", 128)
chkNotEQ(chk, "FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
tst "operator `isZero`":
chkIsZero(chk, "0", 64)
chkIsZero(chk, "0", 128)
chkIsZero(chk, "0", 256)
tst "operator not `isZero`":
chkNotIsZero(chk, "4", 64)
chkNotIsZero(chk, "5", 128)
chkNotIsZero(chk, "6", 256)
tst "operator `isOdd`":
chkIsOdd(chk, "1", 64)
chkIsOdd(chk, "1", 128)
chkIsOdd(chk, "1", 256)
chkIsOdd(chk, "FFFFFF", 64)
chkIsOdd(chk, "FFFFFFFFFFFFFFF", 128)
chkIsOdd(chk, "FFFFFFFFFFFFFFFFFF", 256)
tst "operator not `isOdd`":
chkNotIsOdd(chk, "0", 64)
chkNotIsOdd(chk, "0", 128)
chkNotIsOdd(chk, "0", 256)
chkNotIsOdd(chk, "4", 64)
chkNotIsOdd(chk, "4", 128)
chkNotIsOdd(chk, "4", 256)
chkNotIsOdd(chk, "FFFFFA", 64)
chkNotIsOdd(chk, "FFFFFFFFFFFFFFA", 128)
chkNotIsOdd(chk, "FFFFFFFFFFFFFFFFFA", 256)
tst "operator `isEven`":
chkNotIsOdd(chk, "0", 64)
chkNotIsOdd(chk, "0", 128)
chkNotIsOdd(chk, "0", 256)
chkNotIsOdd(chk, "4", 64)
chkNotIsOdd(chk, "4", 128)
chkNotIsOdd(chk, "4", 256)
chkNotIsOdd(chk, "FFFFFA", 64)
chkNotIsOdd(chk, "FFFFFFFFFFFFFFA", 128)
chkNotIsOdd(chk, "FFFFFFFFFFFFFFFFFA", 256)
tst "operator not `isEven`":
chkIsOdd(chk, "1", 64)
chkIsOdd(chk, "1", 128)
chkIsOdd(chk, "1", 256)
chkIsOdd(chk, "FFFFFF", 64)
chkIsOdd(chk, "FFFFFFFFFFFFFFF", 128)
chkIsOdd(chk, "FFFFFFFFFFFFFFFFFF", 256)
static:
testComparison(ctCheck, ctTest)
template chkNotIsOdd(a: string, bits: int) =
check (not fromHex(StUint[bits], a).isOdd())
suite "Wider unsigned int comparison coverage":
testComparison(check, test)
test "operator `LT`":
chkLT("0", "F", 64)
chkLT("F", "FF", 64)
chkLT("FF", "FFF", 64)
chkLT("FFFF", "FFFFF", 64)
chkLT("FFFFF", "FFFFFFFF", 64)
chkLT("0", "F", 128)
chkLT("F", "FF", 128)
chkLT("FF", "FFF", 128)
chkLT("FFFF", "FFFFF", 128)
chkLT("FFFFF", "FFFFFFFF", 128)
chkLT("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
test "operator not `LT`":
chkNotLT("0", "F", 64)
chkNotLT("F", "FF", 64)
chkNotLT("FF", "FFF", 64)
chkNotLT("FFFF", "FFFFF", 64)
chkNotLT("FFFFF", "FFFFFFFF", 64)
chkNotLT("0", "F", 128)
chkNotLT("F", "FF", 128)
chkNotLT("FF", "FFF", 128)
chkNotLT("FFFF", "FFFFF", 128)
chkNotLT("FFFFF", "FFFFFFFF", 128)
chkNotLT("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
test "operator `LTE`":
chkLTE("0", "F", 64)
chkLTE("F", "FF", 64)
chkLTE("FF", "FFF", 64)
chkLTE("FFFF", "FFFFF", 64)
chkLTE("FFFFF", "FFFFFFFF", 64)
chkLTE("FFFFFFFF", "FFFFFFFF", 64)
chkLTE("0", "F", 128)
chkLTE("F", "FF", 128)
chkLTE("FF", "FFF", 128)
chkLTE("FFFF", "FFFFF", 128)
chkLTE("FFFFF", "FFFFFFFF", 128)
chkLTE("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
chkLTE("FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
test "operator not `LTE`":
chkNotLTE("0", "F", 64)
chkNotLTE("F", "FF", 64)
chkNotLTE("FF", "FFF", 64)
chkNotLTE("FFFF", "FFFFF", 64)
chkNotLTE("FFFFF", "FFFFFFFF", 64)
chkNotLTE("0", "F", 128)
chkNotLTE("F", "FF", 128)
chkNotLTE("FF", "FFF", 128)
chkNotLTE("FFFF", "FFFFF", 128)
chkNotLTE("FFFFF", "FFFFFFFF", 128)
chkNotLTE("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
test "operator `EQ`":
chkEQ("0", "0", 64)
chkEQ("F", "F", 64)
chkEQ("FF", "FF", 64)
chkEQ("FFFF", "FFFF", 64)
chkEQ("FFFFF", "FFFFF", 64)
chkEQ("FFFFFFFF", "FFFFFFFF", 64)
chkEQ("0", "0", 128)
chkEQ("F", "F", 128)
chkEQ("FF", "FF", 128)
chkEQ("FFFF", "FFFF", 128)
chkEQ("FFFFF", "FFFFF", 128)
chkEQ("FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
test "operator not `EQ`":
chkNotEQ("0", "F", 64)
chkNotEQ("F", "FF", 64)
chkNotEQ("FF", "FFF", 64)
chkNotEQ("FFFF", "FFFFF", 64)
chkNotEQ("FFFFF", "FFFFFFFF", 64)
chkNotEQ("0", "F", 128)
chkNotEQ("F", "FF", 128)
chkNotEQ("FF", "FFF", 128)
chkNotEQ("FFFF", "FFFFF", 128)
chkNotEQ("FFFFF", "FFFFFFFF", 128)
chkNotEQ("FFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFF", 128)
test "operator `isZero`":
chkIsZero("0", 64)
chkIsZero("0", 128)
chkIsZero("0", 256)
test "operator not `isZero`":
chkNotIsZero("4", 64)
chkNotIsZero("5", 128)
chkNotIsZero("6", 256)
test "operator `isOdd`":
chkIsOdd("1", 64)
chkIsOdd("1", 128)
chkIsOdd("1", 256)
chkIsOdd("FFFFFF", 64)
chkIsOdd("FFFFFFFFFFFFFFF", 128)
chkIsOdd("FFFFFFFFFFFFFFFFFF", 256)
test "operator not `isOdd`":
chkNotIsOdd("0", 64)
chkNotIsOdd("0", 128)
chkNotIsOdd("0", 256)
chkNotIsOdd("4", 64)
chkNotIsOdd("4", 128)
chkNotIsOdd("4", 256)
chkNotIsOdd("FFFFFA", 64)
chkNotIsOdd("FFFFFFFFFFFFFFA", 128)
chkNotIsOdd("FFFFFFFFFFFFFFFFFA", 256)
test "operator `isEven`":
chkNotIsOdd("0", 64)
chkNotIsOdd("0", 128)
chkNotIsOdd("0", 256)
chkNotIsOdd("4", 64)
chkNotIsOdd("4", 128)
chkNotIsOdd("4", 256)
chkNotIsOdd("FFFFFA", 64)
chkNotIsOdd("FFFFFFFFFFFFFFA", 128)
chkNotIsOdd("FFFFFFFFFFFFFFFFFA", 256)
test "operator not `isEven`":
chkIsOdd("1", 64)
chkIsOdd("1", 128)
chkIsOdd("1", 256)
chkIsOdd("FFFFFF", 64)
chkIsOdd("FFFFFFFFFFFFFFF", 128)
chkIsOdd("FFFFFFFFFFFFFFFFFF", 256)
suite "Testing unsigned int comparison operators":
let
const
a = 10.stuint(64)
b = 15.stuint(64)
c = 150'u64
@ -201,7 +195,7 @@ suite "Testing unsigned int comparison operators":
a < b
not (a + b < b)
not (a + a + a < b + b)
not (a * b < cast[StUint[64]](c))
not (a * b < c.stuint(64))
e < d
d < f
@ -210,7 +204,7 @@ suite "Testing unsigned int comparison operators":
a <= b
not (a + b <= b)
a + a + a <= b + b
a * b <= cast[StUint[64]](c)
a * b <= c.stuint(64)
e <= d
d <= f
@ -219,7 +213,7 @@ suite "Testing unsigned int comparison operators":
b > a
not (b > a + b)
not (b + b > a + a + a)
not (cast[StUint[64]](c) > a * b)
not (c.stuint(64) > a * b)
d > e
f > d
@ -228,7 +222,7 @@ suite "Testing unsigned int comparison operators":
b >= a
not (b >= a + b)
b + b >= a + a + a
cast[StUint[64]](c) >= a * b
c.stuint(64) >= a * b
d >= e
f >= d

View File

@ -7,64 +7,58 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkDiv(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StUint[bits], a) div fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkDiv(a, b, c: string, bits: int) =
check (fromHex(StUint[bits], a) div fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkMod(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StUint[bits], a) mod fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkMod(a, b, c: string, bits: int) =
check (fromHex(StUint[bits], a) mod fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkDivMod(chk: untyped, a, b, c, d: string, bits: int) =
chk divmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b)) == (fromHex(StUint[bits], c), fromHex(StUint[bits], d))
template testdivmod(chk, tst: untyped) =
tst "operator `div`":
chkDiv(chk, "0", "3", "0", 128)
chkDiv(chk, "1", "3", "0", 128)
chkDiv(chk, "3", "3", "1", 128)
chkDiv(chk, "3", "1", "3", 128)
chkDiv(chk, "FF", "3", "55", 128)
chkDiv(chk, "FFFF", "3", "5555", 128)
chkDiv(chk, "FFFFFFFF", "3", "55555555", 128)
chkDiv(chk, "FFFFFFFFFFFFFFFF", "3", "5555555555555555", 128)
chkDiv(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "55555555555555555555555555555555", 128)
tst "operator `mod`":
chkMod(chk, "0", "3", "0", 128)
chkMod(chk, "1", "3", "1", 128)
chkMod(chk, "3", "3", "0", 128)
chkMod(chk, "3", "1", "0", 128)
chkMod(chk, "FF", "3", "0", 128)
chkMod(chk, "FF", "4", "3", 128)
chkMod(chk, "FFFF", "3", "0", 128)
chkMod(chk, "FFFF", "17", "8", 128)
chkMod(chk, "FFFFFFFF", "3", "0", 128)
chkMod(chk, "FFFFFFFF", "23", "A", 128)
chkMod(chk, "FFFFFFFF", "27", "15", 128)
chkMod(chk, "FFFFFFFFFFFFFFFF", "27", "F", 128)
chkMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "27", "15", 128)
tst "operator `divmod`":
chkDivMod(chk, "0", "3", "0", "0", 128)
chkDivMod(chk, "1", "3", "0", "1", 128)
chkDivMod(chk, "3", "3", "1", "0", 128)
chkDivMod(chk, "3", "1", "3", "0", 128)
chkDivMod(chk, "FF", "3", "55", "0", 128)
chkDivMod(chk, "FF", "4", "3F", "3", 128)
chkDivMod(chk, "FFFF", "3", "5555", "0", 128)
chkDivMod(chk, "FFFF", "17", "B21", "8", 128)
chkDivMod(chk, "FFFFFFFF", "3", "55555555", "0", 128)
chkDivMod(chk, "FFFFFFFF", "23", "7507507", "0A", 128)
chkDivMod(chk, "FFFFFFFF", "27", "6906906", "15", 128)
chkDivMod(chk, "FFFFFFFFFFFFFFFF", "27", "690690690690690", "F", 128)
chkDivMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "27", "6906906906906906906906906906906", "15", 128)
static:
testdivmod(ctCheck, ctTest)
template chkDivMod(a, b, c, d: string, bits: int) =
check divmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b)) == (fromHex(StUint[bits], c), fromHex(StUint[bits], d))
suite "Wider unsigned int muldiv coverage":
testdivmod(check, test)
test "operator `div`":
chkDiv("0", "3", "0", 128)
chkDiv("1", "3", "0", 128)
chkDiv("3", "3", "1", 128)
chkDiv("3", "1", "3", 128)
chkDiv("FF", "3", "55", 128)
chkDiv("FFFF", "3", "5555", 128)
chkDiv("FFFFFFFF", "3", "55555555", 128)
chkDiv("FFFFFFFFFFFFFFFF", "3", "5555555555555555", 128)
chkDiv("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "55555555555555555555555555555555", 128)
test "operator `mod`":
chkMod("0", "3", "0", 128)
chkMod("1", "3", "1", 128)
chkMod("3", "3", "0", 128)
chkMod("3", "1", "0", 128)
chkMod("FF", "3", "0", 128)
chkMod("FF", "4", "3", 128)
chkMod("FFFF", "3", "0", 128)
chkMod("FFFF", "17", "8", 128)
chkMod("FFFFFFFF", "3", "0", 128)
chkMod("FFFFFFFF", "23", "A", 128)
chkMod("FFFFFFFF", "27", "15", 128)
chkMod("FFFFFFFFFFFFFFFF", "27", "F", 128)
chkMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "27", "15", 128)
test "operator `divmod`":
chkDivMod("0", "3", "0", "0", 128)
chkDivMod("1", "3", "0", "1", 128)
chkDivMod("3", "3", "1", "0", 128)
chkDivMod("3", "1", "3", "0", 128)
chkDivMod("FF", "3", "55", "0", 128)
chkDivMod("FF", "4", "3F", "3", 128)
chkDivMod("FFFF", "3", "5555", "0", 128)
chkDivMod("FFFF", "17", "B21", "8", 128)
chkDivMod("FFFFFFFF", "3", "55555555", "0", 128)
chkDivMod("FFFFFFFF", "23", "7507507", "0A", 128)
chkDivMod("FFFFFFFF", "27", "6906906", "15", 128)
chkDivMod("FFFFFFFFFFFFFFFF", "27", "690690690690690", "F", 128)
chkDivMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "27", "6906906906906906906906906906906", "15", 128)
suite "Testing unsigned int division and modulo implementation":
test "Divmod(100, 13) returns the correct result":

View File

@ -7,10 +7,10 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest2
suite "Testing unsigned int byte representation":
test "Byte representation conforms to the platform endianness":
runtimeTest "Byte representation conforms to the platform endianness":
let a = 20182018.stuint(64)
let b = 20182018'u64

View File

@ -7,43 +7,38 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, stew/byteutils, test_helpers
import ../stint, unittest2, stew/byteutils
template chkToBytesLE(chk: untyped, bits: int, hex: string) =
template chkToBytesLE(bits: int, hex: string) =
let x = fromHex(StUint[bits], hex)
chk toBytes(x, littleEndian).toHex() == x.dumpHex(littleEndian)
check toBytes(x, littleEndian).toHex() == x.dumpHex(littleEndian)
template chkToBytesBE(chk: untyped, bits: int, hex: string) =
template chkToBytesBE(bits: int, hex: string) =
let x = fromHex(StUint[bits], hex)
chk toBytes(x, bigEndian).toHex() == x.dumpHex(bigEndian)
check toBytes(x, bigEndian).toHex() == x.dumpHex(bigEndian)
template chkFromBytesBE(chk: untyped, bits: int, hex: string) =
template chkFromBytesBE(bits: int, hex: string) =
let x = fromHex(StUint[bits], hex)
let z = fromBytesBE(StUint[bits], toByteArrayBE(x))
chk z == x
check z == x
template chkFromBytesLE(chk: untyped, bits: int, hex: string) =
template chkFromBytesLE(bits: int, hex: string) =
let x = fromHex(StUint[bits], hex)
let z = fromBytesLE(StUint[bits], toByteArrayLE(x))
chk z == x
check z == x
template chkEndians(chkFunc, tst, name: untyped) =
tst astToStr(name).substr(3):
name(chkFunc, 64, "abcdef1234567890")
name(chkFunc, 128, "abcdef1234567890abcdef1234567890")
name(chkFunc, 256, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890")
template testEndians(chkFunc, tst: untyped) =
chkEndians(chkFunc, tst, chkToBytesLE)
chkEndians(chkFunc, tst, chkToBytesBE)
chkEndians(chkFunc, tst, chkFromBytesLE)
chkEndians(chkFunc, tst, chkFromBytesBE)
static:
testEndians(ctCheck, ctTest)
template chkEndians(name: untyped) =
test astToStr(name).substr(3):
name(64, "abcdef1234567890")
name(128, "abcdef1234567890abcdef1234567890")
name(256, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890")
suite "Testing endians":
chkEndians(chkToBytesLE)
chkEndians(chkToBytesBE)
chkEndians(chkFromBytesLE)
chkEndians(chkFromBytesBE)
test "Endians give sane results":
check:
@ -58,5 +53,3 @@ suite "Testing endians":
1.u128 == UInt128.fromBytesLE(
[1'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
testEndians(check, test)

View File

@ -7,34 +7,28 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, math, test_helpers
import ../stint, unittest2, math
template chkPow(chk: untyped, a, b, c: string, bits: int) =
chk pow(fromHex(StUint[bits], a), fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkPow(a, b, c: string, bits: int) =
check pow(fromHex(StUint[bits], a), fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template chkPow(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk pow(fromHex(StUint[bits], a), b) == fromHex(StUint[bits], c)
template testExp(chk, tst: untyped) =
tst "BigInt BigInt Pow":
chkPow(chk, "F", "2", "E1", 128)
chkPow(chk, "FF", "2", "FE01", 128)
chkPow(chk, "FF", "3", "FD02FF", 128)
chkPow(chk, "FFF", "3", "FFD002FFF", 128)
chkPow(chk, "FFFFF", "3", "ffffd00002fffff", 128)
tst "BigInt Natural Pow":
chkPow(chk, "F", 2, "E1", 128)
chkPow(chk, "FF", 2, "FE01", 128)
chkPow(chk, "FF", 3, "FD02FF", 128)
chkPow(chk, "FFF", 3, "FFD002FFF", 128)
chkPow(chk, "FFFFF", 3, "ffffd00002fffff", 128)
static:
testExp(ctCheck, ctTest)
template chkPow(a: string, b: SomeInteger, c: string, bits: int) =
check pow(fromHex(StUint[bits], a), b) == fromHex(StUint[bits], c)
suite "Wider unsigned int exp coverage":
testExp(check, test)
test "BigInt BigInt Pow":
chkPow("F", "2", "E1", 128)
chkPow("FF", "2", "FE01", 128)
chkPow("FF", "3", "FD02FF", 128)
chkPow("FFF", "3", "FFD002FFF", 128)
chkPow("FFFFF", "3", "ffffd00002fffff", 128)
test "BigInt Natural Pow":
chkPow("F", 2, "E1", 128)
chkPow("FF", 2, "FE01", 128)
chkPow("FF", 3, "FD02FF", 128)
chkPow("FFF", 3, "FFD002FFF", 128)
chkPow("FFFFF", 3, "ffffd00002fffff", 128)
suite "Testing unsigned exponentiation":
test "Simple exponentiation 5^3":
@ -45,8 +39,8 @@ suite "Testing unsigned exponentiation":
u = a.stuint(64)
check:
cast[uint64](u.pow(b)) == a ^ b
cast[uint64](u.pow(b.stuint(64))) == a ^ b
u.pow(b).truncate(uint64) == a ^ b
u.pow(b.stuint(64)).truncate(uint64) == a ^ b
test "12 ^ 34 == 4922235242952026704037113243122008064":
# https://www.wolframalpha.com/input/?i=12+%5E+34

View File

@ -7,60 +7,54 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkAddMod(chk: untyped, a, b, m, c: string, bits: int) =
chk addmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkAddMod(a, b, m, c: string, bits: int) =
check addmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkSubMod(chk: untyped, a, b, m, c: string, bits: int) =
chk submod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkSubMod(a, b, m, c: string, bits: int) =
check submod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkMulMod(chk: untyped, a, b, m, c: string, bits: int) =
chk mulmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkMulMod(a, b, m, c: string, bits: int) =
check mulmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template chkPowMod(chk: untyped, a, b, m, c: string, bits: int) =
chk powmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
template testModArith(chk, tst: untyped) =
tst "addmod":
chkAddMod(chk, "F", "F", "7", "2", 128)
chkAddMod(chk, "AAAA", "AA", "F", "0", 128)
chkAddMod(chk, "BBBB", "AAAA", "9", "3", 128)
chkAddMod(chk, "BBBBBBBB", "AAAAAAAA", "9", "6", 128)
chkAddMod(chk, "BBBBBBBBBBBBBBBB", "AAAAAAAAAAAAAAAA", "9", "3", 128)
chkAddMod(chk, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "9", "6", 128)
tst "submod":
chkSubMod(chk, "C", "3", "C", "9", 128)
chkSubMod(chk, "1", "3", "C", "A", 128)
chkSubMod(chk, "1", "FFFF", "C", "A", 128)
chkSubMod(chk, "1", "FFFFFFFF", "C", "A", 128)
chkSubMod(chk, "1", "FFFFFFFFFFFFFFFF", "C", "A", 128)
chkSubMod(chk, "1", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C", "A", 128)
tst "mulmod":
chkMulMod(chk, "C", "3", "C", "0", 128)
chkMulMod(chk, "1", "3", "C", "3", 128)
chkMulMod(chk, "1", "FFFF", "C", "3", 128)
chkMulMod(chk, "1", "FFFFFFFF", "C", "3", 128)
chkMulMod(chk, "1", "FFFFFFFFFFFFFFFF", "C", "3", 128)
chkMulMod(chk, "1", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C", "3", 128)
tst "powmod":
chkPowMod(chk, "C", "3", "C", "0", 128)
chkPowMod(chk, "1", "3", "C", "1", 128)
chkPowMod(chk, "1", "FF", "C", "1", 128)
chkPowMod(chk, "FF", "3", "C", "3", 128)
chkPowMod(chk, "FFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFFFFFFFFFF", "3", "C", "3", 128)
chkPowMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "C", "3", 128)
static:
testModArith(ctCheck, ctTest)
template chkPowMod(a, b, m, c: string, bits: int) =
check powmod(fromHex(StUint[bits], a), fromHex(StUint[bits], b), fromHex(StUint[bits], m)) == fromHex(StUint[bits], c)
suite "Wider unsigned Modular arithmetic coverage":
testModArith(check, test)
test "addmod":
chkAddMod("F", "F", "7", "2", 128)
chkAddMod("AAAA", "AA", "F", "0", 128)
chkAddMod("BBBB", "AAAA", "9", "3", 128)
chkAddMod("BBBBBBBB", "AAAAAAAA", "9", "6", 128)
chkAddMod("BBBBBBBBBBBBBBBB", "AAAAAAAAAAAAAAAA", "9", "3", 128)
chkAddMod("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "9", "6", 128)
test "submod":
chkSubMod("C", "3", "C", "9", 128)
chkSubMod("1", "3", "C", "A", 128)
chkSubMod("1", "FFFF", "C", "A", 128)
chkSubMod("1", "FFFFFFFF", "C", "A", 128)
chkSubMod("1", "FFFFFFFFFFFFFFFF", "C", "A", 128)
chkSubMod("1", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C", "A", 128)
test "mulmod":
chkMulMod("C", "3", "C", "0", 128)
chkMulMod("1", "3", "C", "3", 128)
chkMulMod("1", "FFFF", "C", "3", 128)
chkMulMod("1", "FFFFFFFF", "C", "3", 128)
chkMulMod("1", "FFFFFFFFFFFFFFFF", "C", "3", 128)
chkMulMod("1", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "C", "3", 128)
test "powmod":
chkPowMod("C", "3", "C", "0", 128)
chkPowMod("1", "3", "C", "1", 128)
chkPowMod("1", "FF", "C", "1", 128)
chkPowMod("FF", "3", "C", "3", 128)
chkPowMod("FFFF", "3", "C", "3", 128)
chkPowMod("FFFFFFFF", "3", "C", "3", 128)
chkPowMod("FFFFFFFFFFFFFFFF", "3", "C", "3", 128)
chkPowMod("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "C", "3", 128)
suite "Modular arithmetic":
test "Modular addition":

View File

@ -7,51 +7,45 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, test_helpers
import ../stint, unittest2
template chkMul(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(StUint[bits], a) * fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
template testMul(chk, tst: untyped) =
tst "operator `mul`":
#[chkMul(chk, "0", "3", "0", 8)
chkMul(chk, "1", "3", "3", 8)
chkMul(chk, "64", "3", "2C", 8) # overflow
chkMul(chk, "0", "3", "0", 16)
chkMul(chk, "1", "3", "3", 16)
chkMul(chk, "64", "3", "12C", 16)
chkMul(chk, "1770", "46", "68A0", 16) # overflow
chkMul(chk, "0", "3", "0", 32)
chkMul(chk, "1", "3", "3", 32)
chkMul(chk, "64", "3", "12C", 32)
chkMul(chk, "1770", "46", "668A0", 32)
chkMul(chk, "13880", "13880", "7D784000", 32) # overflow
chkMul(chk, "0", "3", "0", 64)
chkMul(chk, "1", "3", "3", 64)
chkMul(chk, "64", "3", "12C", 64)
chkMul(chk, "1770", "46", "668A0", 64)
chkMul(chk, "13880", "13880", "17D784000", 64)
chkMul(chk, "3B9ACA00", "E8D4A51000", "35C9ADC5DEA00000", 64) # overflow]#
chkMul(chk, "0", "3", "0", 128)
chkMul(chk, "1", "3", "3", 128)
chkMul(chk, "64", "3", "12C", 128)
chkMul(chk, "1770", "46", "668A0", 128)
chkMul(chk, "13880", "13880", "17D784000", 128)
chkMul(chk, "3B9ACA00", "E8D4A51000", "3635C9ADC5DEA00000", 128)
chkMul(chk, "25295F0D1", "10", "25295F0D10", 128)
chkMul(chk, "123456789ABCDEF00", "123456789ABCDEF00", "4b66dc33f6acdca5e20890f2a5210000", 128) # overflow
chkMul(chk, "123456789ABCDEF00", "123456789ABCDEF00", "14b66dc33f6acdca5e20890f2a5210000", 256)
static:
testMul(ctCheck, ctTest)
template chkMul(a, b, c: string, bits: int) =
check (fromHex(StUint[bits], a) * fromHex(StUint[bits], b)) == fromHex(StUint[bits], c)
suite "Wider unsigned int muldiv coverage":
testMul(check, test)
test "operator `mul`":
#[chkMul("0", "3", "0", 8)
chkMul("1", "3", "3", 8)
chkMul("64", "3", "2C", 8) # overflow
chkMul("0", "3", "0", 16)
chkMul("1", "3", "3", 16)
chkMul("64", "3", "12C", 16)
chkMul("1770", "46", "68A0", 16) # overflow
chkMul("0", "3", "0", 32)
chkMul("1", "3", "3", 32)
chkMul("64", "3", "12C", 32)
chkMul("1770", "46", "668A0", 32)
chkMul("13880", "13880", "7D784000", 32) # overflow
chkMul("0", "3", "0", 64)
chkMul("1", "3", "3", 64)
chkMul("64", "3", "12C", 64)
chkMul("1770", "46", "668A0", 64)
chkMul("13880", "13880", "17D784000", 64)
chkMul("3B9ACA00", "E8D4A51000", "35C9ADC5DEA00000", 64) # overflow]#
chkMul("0", "3", "0", 128)
chkMul("1", "3", "3", 128)
chkMul("64", "3", "12C", 128)
chkMul("1770", "46", "668A0", 128)
chkMul("13880", "13880", "17D784000", 128)
chkMul("3B9ACA00", "E8D4A51000", "3635C9ADC5DEA00000", 128)
chkMul("25295F0D1", "10", "25295F0D10", 128)
chkMul("123456789ABCDEF00", "123456789ABCDEF00", "4b66dc33f6acdca5e20890f2a5210000", 128) # overflow
chkMul("123456789ABCDEF00", "123456789ABCDEF00", "14b66dc33f6acdca5e20890f2a5210000", 256)
#[
suite "Testing unsigned int multiplication implementation":

View File

@ -9,7 +9,7 @@
import
std/[times, random],
unittest,
unittest2,
./intx/intx_compat,
../stint,
../helpers/prng_unsafe