cosmetics changes

This commit is contained in:
andri lim 2019-10-23 09:20:06 +07:00 committed by zah
parent e58eb117c8
commit 8cc9116d1e
14 changed files with 97 additions and 80 deletions

9
tests/test_helpers.nim Normal file
View File

@ -0,0 +1,9 @@
# 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) =
body
echo "[OK] compile time ", name

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkAddition(chk, a, b, c, bits: untyped) =
block:
@ -39,10 +39,6 @@ template chkNegation(chk, a, b, bits: untyped) =
template chkAbs(chk, a, b, bits: untyped) =
chk stint(a, bits).abs() == stint(b, bits)
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testAddSub(chk, tst: untyped) =
tst "addition":
chkAddition(chk, 0'i8, 0'i8, 0'i8, 8)
@ -286,7 +282,7 @@ template testAddSub(chk, tst: untyped) =
chkAbs(chk, 9223372036854775807, 9223372036854775807, 128)
static:
testAddSub(doAssert, ctTest)
testAddSub(ctCheck, ctTest)
suite "Wider signed int addsub coverage":
testAddSub(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkNot(chk: untyped, a, b: distinct SomeInteger, bits: int) =
chk stint(a, bits).not() == stint(b, bits)
@ -30,10 +30,6 @@ template chkShl(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
template chkShr(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(Stint[bits], a) shr b) == fromHex(Stint[bits], c)
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testBitwise(chk, tst: untyped) =
# TODO: see issue #95
@ -310,7 +306,7 @@ template testBitwise(chk, tst: untyped) =
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 233, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF80000", 256)
static:
testBitwise(doAssert, ctTest)
testBitwise(ctCheck, ctTest)
suite "Wider signed int bitwise coverage":
testBitwise(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkLT(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stint[bits], a) < fromHex(Stint[bits], b)
@ -51,10 +51,6 @@ template chkisEven(chk: untyped, a: string, bits: int) =
template chknotisEven(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stint[bits], a).isEven())
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testComparison(chk, tst: untyped) =
tst "operator `LT`":
chkLT(chk, "0", "F", 8)
@ -377,7 +373,7 @@ template testComparison(chk, tst: untyped) =
chkIsOdd(chk, "FFFFFFFFFFFFFFFFFF", 256)
static:
testComparison(doAssert, ctTest)
testComparison(ctCheck, ctTest)
suite "Wider signed int comparison coverage":
testComparison(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkMul(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stint[bits], a) * fromHex(Stint[bits], b)) == fromHex(Stint[bits], c)
@ -21,10 +21,6 @@ template chkMod(chk: untyped, a, b, c: string, bits: int) =
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 ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testMuldiv(chk, tst: untyped) =
tst "operator `mul`":
chkMul(chk, "0", "3", "0", 8)
@ -209,11 +205,11 @@ template testMuldiv(chk, tst: untyped) =
chkDivMod(chk, "0F", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD", "3", 128)
static:
testMuldiv(doAssert, ctTest)
testMuldiv(ctCheck, ctTest)
suite "Wider signed int muldiv coverage":
testMuldiv(check, test)
#[
suite "Testing signed int multiplication implementation":
test "Multiplication with result fitting in low half":
@ -324,4 +320,3 @@ suite "Testing signed int division and modulo implementation":
check: q == 123456789123456789'i64
check: r == 0'i64
]#

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, strutils, math
import ../stint, unittest, strutils, math, test_helpers
template nativeStuint(chk, nint: untyped, bits: int) =
chk $(nint.stuint(bits)) == $(nint)
@ -104,10 +104,6 @@ template chkDumpHex(chk: untyped, BE, LE: string, bits: int) =
chk dumpHex(x, bigEndian) == data
chk dumpHex(x, littleEndian) == LE
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testIO(chk, tst: untyped) =
tst "[stuint] Creation from native ints":
nativeStuint(chk, 0, 8)
@ -559,6 +555,67 @@ template testIO(chk, tst: untyped) =
chkTruncateStint(chk, high(int64), int64, 64)
chkTruncateStint(chk, low(int64), uint64, "0x8000000000000000", 64)
chkTruncateStint(chk, low(uint8), uint8, 128)
#chkTruncateStint(chk, low(int8), int8, 128) # TODO: bug #92
chkTruncateStint(chk, high(int8), uint8, 128)
chkTruncateStint(chk, high(int8), int8, 128)
#chkTruncateStint(chk, low(int8), uint8, "0x80", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint8), uint16, 128)
#chkTruncateStint(chk, low(int8), int16, 128) # TODO: bug #92
chkTruncateStint(chk, high(int8), uint16, 128)
chkTruncateStint(chk, high(int8), int16, 128)
#chkTruncateStint(chk, low(int8), uint16, "0xFF80", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint16), uint16, 128)
#chkTruncateStint(chk, low(int16), int16, 128) # TODO: bug #92
chkTruncateStint(chk, high(int16), uint16, 128)
chkTruncateStint(chk, high(int16), int16, 128)
#chkTruncateStint(chk, low(int16), uint16, "0x8000", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint8), uint32, 128)
#chkTruncateStint(chk, low(int8), int32, 128) # TODO: bug #92
chkTruncateStint(chk, high(int8), uint32, 128)
chkTruncateStint(chk, high(int8), int32, 128)
#chkTruncateStint(chk, low(int8), uint32, "0xFFFFFF80", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint16), uint32, 128)
#chkTruncateStint(chk, low(int16), int32, 128) # TODO: bug #92
chkTruncateStint(chk, high(int16), uint32, 128)
chkTruncateStint(chk, high(int16), int32, 128)
#chkTruncateStint(chk, low(int16), uint32, "0xFFFF8000", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint32), uint32, 128)
#chkTruncateStint(chk, low(int32), int32, 128) # TODO: bug #92
chkTruncateStint(chk, high(int32), uint32, 128)
chkTruncateStint(chk, high(int32), int32, 128)
#chkTruncateStint(chk, low(int32), uint32, "0x80000000", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint8), uint64, 128)
#chkTruncateStint(chk, low(int8), int64, 128) # TODO: bug #92
chkTruncateStint(chk, high(int8), uint64, 128)
chkTruncateStint(chk, high(int8), int64, 128)
#chkTruncateStint(chk, low(int8), uint64, "0xFFFFFFFFFFFFFF80", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint16), uint64, 128)
#chkTruncateStint(chk, low(int16), int64, 128) # TODO: bug #92
chkTruncateStint(chk, high(int16), uint64, 128)
chkTruncateStint(chk, high(int16), int64, 128)
#chkTruncateStint(chk, low(int16), uint64, "0xFFFFFFFFFFFF8000", 128) # TODO: bug #92
chkTruncateStint(chk, low(uint32), uint64, 128)
#chkTruncateStint(chk, low(int32), int64, 128) # TODO: bug #92
chkTruncateStint(chk, high(int32), uint64, 128)
chkTruncateStint(chk, high(int32), int64, 128)
#chkTruncateStint(chk, low(int32), uint64, "0xFFFFFFFF80000000", 128) # TODO: bug #92
when (NimMajor, NimMinor, NimPatch) >= (1, 0, 0):
chkTruncateStint(chk, low(uint64), uint64, 128)
#chkTruncateStint(chk, low(int64), int64, 128) # TODO: bug #92
chkTruncateStint(chk, high(int64), uint64, 128)
chkTruncateStint(chk, high(int64), int64, 128)
#chkTruncateStint(chk, low(int64), uint64, "0x8000000000000000", 128) # TODO: bug #92
tst "[stuint] parse - toString roundtrip":
chkRoundTripBin(chk, chkRoundTripStuint, 8, 1)
@ -960,7 +1017,7 @@ template testIO(chk, tst: untyped) =
chkDumpHex(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128)
static:
testIO(doAssert, ctTest)
testIO(ctCheck, ctTest)
proc main() =
# Nim GC protests we are using too much global variables

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkAddition(chk, a, b, c, bits: untyped) =
block:
@ -33,10 +33,6 @@ template chkInplaceSubstraction(chk, a, b, c, bits: untyped) =
x -= stuint(b, bits)
chk x == stuint(c, bits)
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testAddSub(chk, tst: untyped) =
tst "addition":
chkAddition(chk, 0'u8, 0'u8, 0'u8, 8)
@ -203,7 +199,7 @@ template testAddSub(chk, tst: untyped) =
chkInplaceSubstraction(chk, high(uint64), high(uint64), 0'u64, 128)
static:
testAddSub(doAssert, ctTest)
testAddSub(ctCheck, ctTest)
suite "Wider unsigned int addsub coverage":
testAddSub(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkCountOnes(chk: untyped, bits: int) =
block:
@ -62,10 +62,6 @@ template chkTrailingZeros(chk: untyped, bits: int) =
x = x shl 1
chk x.trailingZeros == i
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testBitOps(chk, tst: untyped) =
tst "countOnes":
chk countOnes(0b01000100'u8.stuint(8)) == 2
@ -218,7 +214,7 @@ template testBitOps(chk, tst: untyped) =
chkTrailingZeros(chk, 256)
static:
testBitOps(doAssert, ctTest)
testBitOps(ctCheck, ctTest)
suite "Testing bitops2":
testBitOps(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkNot(chk: untyped, a, b: distinct SomeInteger, bits: int) =
chk stuint(a, bits).not() == stuint(b, bits)
@ -30,10 +30,6 @@ template chkShl(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
template chkShr(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) shr b) == fromHex(Stuint[bits], c)
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testBitwise(chk, tst: untyped) =
# TODO: see issue #95
@ -311,7 +307,7 @@ template testBitwise(chk, tst: untyped) =
chkShr(chk, "F000000000000000000000000000000000000000000000000000000000000000", 233, "780000", 256)
static:
testBitwise(doAssert, ctTest)
testBitwise(ctCheck, ctTest)
suite "Wider unsigned int bitwise coverage":
testBitwise(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkLT(chk: untyped, a, b: string, bits: int) =
chk fromHex(Stuint[bits], a) < fromHex(Stuint[bits], b)
@ -45,10 +45,6 @@ template chkisEven(chk: untyped, a: string, bits: int) =
template chknotisEven(chk: untyped, a: string, bits: int) =
chk (not fromHex(Stuint[bits], a).isEven())
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testComparison(chk, tst: untyped) =
tst "operator `LT`":
chkLT(chk, "0", "F", 8)
@ -306,7 +302,7 @@ template testComparison(chk, tst: untyped) =
chkIsOdd(chk, "FFFFFFFFFFFFFFFFFF", 256)
static:
testComparison(doAssert, ctTest)
testComparison(ctCheck, ctTest)
suite "Wider unsigned int comparison coverage":
testComparison(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, stew/byteutils
import ../stint, unittest, stew/byteutils, test_helpers
template chkSwapBytes(chk: untyped, bits: int, hex: string) =
# dumpHex already do the job to swap the output if
@ -73,12 +73,8 @@ template testEndians(chkFunc, tst: untyped) =
chkEndians(chkFunc, tst, chkFromToLE)
chkEndians(chkFunc, tst, chkFromToBE)
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
static:
testEndians(doAssert, ctTest)
testEndians(ctCheck, ctTest)
suite "Testing endians":
test "Endians give sane results":

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, math
import ../stint, unittest, math, test_helpers
template chkPow(chk: untyped, a, b, c: string, bits: int) =
chk pow(fromHex(Stuint[bits], a), fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
@ -15,10 +15,6 @@ template chkPow(chk: untyped, a, b, c: string, bits: int) =
template chkPow(chk: untyped, a: string, b: SomeInteger, c: string, bits: int) =
chk pow(fromHex(Stuint[bits], a), b) == fromHex(Stuint[bits], c)
template ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testExp(chk, tst: untyped) =
tst "BigInt BigInt Pow":
chkPow(chk, "F", "2", "E1", 8)
@ -63,7 +59,7 @@ template testExp(chk, tst: untyped) =
chkPow(chk, "FFFFF", 3, "ffffd00002fffff", 128)
static:
testExp(doAssert, ctTest)
testExp(ctCheck, ctTest)
suite "Wider unsigned int exp coverage":
testExp(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest, math
import ../stint, unittest, math, test_helpers
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)
@ -21,10 +21,6 @@ template chkMulmod(chk: untyped, a, b, m, c: string, bits: int) =
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 ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testModArith(chk, tst: untyped) =
tst "addmod":
chkAddMod(chk, "F", "F", "7", "2", 8)
@ -141,7 +137,7 @@ template testModArith(chk, tst: untyped) =
chkPowMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "3", "C", "3", 128)
static:
testModArith(doAssert, ctTest)
testModArith(ctCheck, ctTest)
suite "Wider unsigned Modular arithmetic coverage":
testModArith(check, test)

View File

@ -7,7 +7,7 @@
#
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import ../stint, unittest
import ../stint, unittest, test_helpers
template chkMul(chk: untyped, a, b, c: string, bits: int) =
chk (fromHex(Stuint[bits], a) * fromHex(Stuint[bits], b)) == fromHex(Stuint[bits], c)
@ -21,10 +21,6 @@ template chkMod(chk: untyped, a, b, c: string, bits: int) =
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 ctTest(name: string, body: untyped) =
body
echo "[OK] compile time ", name
template testMuldiv(chk, tst: untyped) =
tst "operator `mul`":
chkMul(chk, "0", "3", "0", 8)
@ -216,7 +212,7 @@ template testMuldiv(chk, tst: untyped) =
chkDivMod(chk, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "27", "6906906906906906906906906906906", "15", 128)
static:
testMuldiv(doAssert, ctTest)
testMuldiv(ctCheck, ctTest)
suite "Wider unsigned int muldiv coverage":
testMuldiv(check, test)