This commit is contained in:
jangko 2023-06-14 22:26:44 +07:00
parent 6371b66030
commit 41bfed6bb7
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
7 changed files with 188 additions and 578 deletions

View File

@ -143,15 +143,15 @@ func fromBytesBE*[bits: static int](
## at least sizeof(T) bytes. Native endianess is used which is not ## at least sizeof(T) bytes. Native endianess is used which is not
## portable! (i.e. use fixed-endian byte array or hex for serialization) ## portable! (i.e. use fixed-endian byte array or hex for serialization)
var accum: Word var accum: Word = 0
var accumBits: int var accumBits: int = 0
var dstIdx: int var dstIdx: int
when cpuEndian == littleEndian: # src is bigEndian, CPU is little-endian when cpuEndian == littleEndian: # src is bigEndian, CPU is little-endian
dstIdx = 0 dstIdx = 0
for srcIdx in countdown(x.len-1, 0): for srcIdx in countdown(x.len-1, 0):
let srcByte = x[srcIdx] let srcByte = Word(x[srcIdx])
accum = accum or (srcByte shl accumBits) accum = accum or (srcByte shl accumBits)
accumBits += 8 accumBits += 8
@ -170,7 +170,7 @@ func fromBytesBE*[bits: static int](
dstIdx = result.limbs.len-1 dstIdx = result.limbs.len-1
for srcIdx in countdown(x.len-1, 0): for srcIdx in countdown(x.len-1, 0):
let srcByte = x[srcIdx] let srcByte = Word(x[srcIdx])
accum = accum or (srcByte shl accumBits) accum = accum or (srcByte shl accumBits)
accumBits += 8 accumBits += 8
@ -193,15 +193,15 @@ func fromBytesLE*[bits: static int](
## contain at least sizeof(T) bytes. By default, native endianess is used ## contain at least sizeof(T) bytes. By default, native endianess is used
## which is not portable! (i.e. use fixed-endian byte array or hex for serialization) ## which is not portable! (i.e. use fixed-endian byte array or hex for serialization)
var accum: Word var accum: Word = 0
var accumBits: int var accumBits: int = 0
var dstIdx: int var dstIdx: int
when cpuEndian == littleEndian: # src and CPU are little-endian when cpuEndian == littleEndian: # src and CPU are little-endian
dstIdx = 0 dstIdx = 0
for srcIdx in 0 ..< x.len: for srcIdx in 0 ..< x.len:
let srcByte = x[srcIdx] let srcByte = Word(x[srcIdx])
accum = accum or (srcByte shl accumBits) accum = accum or (srcByte shl accumBits)
accumBits += 8 accumBits += 8
@ -220,7 +220,7 @@ func fromBytesLE*[bits: static int](
dstIdx = result.limbs.len-1 dstIdx = result.limbs.len-1
for srcIdx in 0 ..< x.len: for srcIdx in 0 ..< x.len:
let srcByte = x[srcIdx] let srcByte = Word(x[srcIdx])
accum = accum or (srcByte shl accumBits) accum = accum or (srcByte shl accumBits)
accumBits += 8 accumBits += 8
@ -247,3 +247,34 @@ func fromBytes*[bits: static int](
result = fromBytesLE(T, x) result = fromBytesLE(T, x)
else: else:
result = fromBytesBE(T, x) result = fromBytesBE(T, x)
# Signed integer version of above funcs
func toBytesLE*[bits: static int](src: StInt[bits]):
array[bits div 8, byte] {.inline.} =
toBytesLE(src.imp)
func toBytesBE*[bits: static int](src: StInt[bits]):
array[bits div 8, byte] {.inline.} =
toBytesBE(src.imp)
func toBytes*[bits: static int](x: StInt[bits], endian: Endianness = bigEndian):
array[bits div 8, byte] {.inline.} =
toBytes(x.imp, endian)
func fromBytesBE*[bits: static int](
T: typedesc[StInt[bits]],
x: openArray[byte]): T {.raises: [], noinit, gcsafe, inline.} =
fromBytesBE(type result.imp, x)
func fromBytesLE*[bits: static int](
T: typedesc[StInt[bits]],
x: openArray[byte]): T {.inline.} =
fromBytesLE(type result.imp, x)
func fromBytes*[bits: static int](
T: typedesc[StInt[bits]],
x: openArray[byte],
srcEndian: Endianness = bigEndian): T {.inline.} =
fromBytes(type result.imp, x, srcEndian)

View File

@ -17,9 +17,11 @@ import
from stew/byteutils import toHex from stew/byteutils import toHex
template leastSignificantWord*(a: SomeBigInteger): Word = template leastSignificantWord*(a: SomeBigInteger): Word =
mixin limbs
a.limbs[0] a.limbs[0]
template mostSignificantWord*(a: SomeBigInteger): Word = template mostSignificantWord*(a: SomeBigInteger): Word =
mixin limbs
a.limbs[^1] a.limbs[^1]
template signedWordType*(_: type SomeBigInteger): type = template signedWordType*(_: type SomeBigInteger): type =
@ -57,13 +59,33 @@ func to*(a: SomeInteger, T: typedesc[StInt]): T =
func to*(a: SomeUnsignedInt, T: typedesc[StUint]): T = func to*(a: SomeUnsignedInt, T: typedesc[StUint]): T =
stuint(a, result.bits) stuint(a, result.bits)
func truncate*(num: StInt or StUint, T: typedesc[SomeInteger]): T {.inline.}= func truncate*(num: StUint, T: typedesc[SomeInteger]): T {.inline.}=
## Extract the int, uint, int8-int64 or uint8-uint64 portion of a multi-precision integer. ## Extract the int, uint, int8-int64 or uint8-uint64 portion of a multi-precision integer.
## Note that int and uint are 32-bit on 32-bit platform. ## Note that int and uint are 32-bit on 32-bit platform.
## For unsigned result type, result is modulo 2^(sizeof T in bit) ## For unsigned result type, result is modulo 2^(sizeof T in bit)
## For signed result type, result is undefined if input does not fit in the target type. ## For signed result type, result is undefined if input does not fit in the target type.
result = T(num.leastSignificantWord()) result = T(num.leastSignificantWord())
func truncate*(num: StInt, T: typedesc[SomeInteger]): T {.inline.}=
## Extract the int, uint, int8-int64 or uint8-uint64 portion of a multi-precision integer.
## Note that int and uint are 32-bit on 32-bit platform.
## For unsigned result type, result is modulo 2^(sizeof T in bit)
## For signed result type, result is undefined if input does not fit in the target type.
let n = num.abs
when sizeof(T) > sizeof(Word):
result = T(n.leastSignificantWord())
else:
result = T(n.leastSignificantWord() and Word(T.high))
if num.isNegative:
when T is SomeUnsignedInt:
doAssert(false, "cannot truncate negative number to unsigned integer")
else:
if n.leastSignificantWord() == Word(T.high) + 1:
result = low(T)
else:
result = -result
func stuint*(a: StUint, bits: static[int]): StUint[bits] {.inline.} = func stuint*(a: StUint, bits: static[int]): StUint[bits] {.inline.} =
## unsigned int to unsigned int conversion ## unsigned int to unsigned int conversion
## smaller to bigger bits conversion will have the same value ## smaller to bigger bits conversion will have the same value
@ -175,6 +197,7 @@ func skipPrefixes(current_idx: var int, str: string, radix: range[2..16]) {.inli
doAssert radix == 8, "Parsing mismatch, 0o prefix is only valid for an octal number (base 8)" doAssert radix == 8, "Parsing mismatch, 0o prefix is only valid for an octal number (base 8)"
current_idx = 2 current_idx = 2
elif str[1] in {'b', 'B'}: elif str[1] in {'b', 'B'}:
# this check will fail if we have radix 16 and input "0bcdef12345" which is a valid hex
doAssert radix == 2, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2)" doAssert radix == 2, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2)"
current_idx = 2 current_idx = 2
@ -343,8 +366,8 @@ template hash*(num: StUint|StInt): Hash =
func fromBytesBE*(T: type StUint, ba: openArray[byte], allowPadding: static[bool] = true): T {.noinit, inline.}= func fromBytesBE*(T: type StUint, ba: openArray[byte], allowPadding: static[bool] = true): T {.noinit, inline.}=
result = readUintBE[T.bits](ba) result = readUintBE[T.bits](ba)
when allowPadding: #when allowPadding:
result = result shl ((sizeof(T) - ba.len) * 8) # result = result shl (((sizeof(T) - ba.len) * 8) - 1)
template initFromBytesBE*(x: var StUint, ba: openArray[byte], allowPadding: static[bool] = true) = template initFromBytesBE*(x: var StUint, ba: openArray[byte], allowPadding: static[bool] = true) =
x = fromBytesBE(type x, ba, allowPadding) x = fromBytesBE(type x, ba, allowPadding)

View File

@ -59,6 +59,25 @@ when sizeof(int) == 8 and GCC_Compatible:
type type
uint128*{.importc: "unsigned __int128".} = object uint128*{.importc: "unsigned __int128".} = object
# Accessors
# --------------------------------------------------------
template limbs*(a: StInt): untyped =
# TODO: remove this when we switch to borrow `.`
a.imp.limbs
template `[]`*(a: StInt, i: SomeInteger or BackwardsIndex): Word =
a.imp.limbs[i]
template `[]=`*(a: var StInt, i: SomeInteger or BackwardsIndex, val: Word) =
a.imp.limbs[i] = val
template `[]`*(a: StUint, i: SomeInteger or BackwardsIndex): Word =
a.limbs[i]
template `[]=`*(a: var StUint, i: SomeInteger or BackwardsIndex, val: Word) =
a.limbs[i] = val
# Bithacks # Bithacks
# -------------------------------------------------------- # --------------------------------------------------------
@ -91,25 +110,6 @@ func usedBitsAndWords*(a: openArray[Word]): tuple[bits, words: int] =
{.pop.} {.pop.}
# Accessors
# --------------------------------------------------------
template limbs*(a: StInt): untyped =
# TODO: remove this when we switch to borrow `.`
a.imp.limbs
template `[]`*(a: StInt, i: SomeInteger or BackwardsIndex): Word =
a.imp.limbs[i]
template `[]=`*(a: var StInt, i: SomeInteger or BackwardsIndex, val: Word) =
a.imp.limbs[i] = val
template `[]`*(a: StUint, i: SomeInteger or BackwardsIndex): Word =
a.limbs[i]
template `[]=`*(a: var StUint, i: SomeInteger or BackwardsIndex, val: Word) =
a.limbs[i] = val
# Iterations # Iterations
# -------------------------------------------------------- # --------------------------------------------------------

View File

@ -31,7 +31,7 @@ import
test_int_muldiv, test_int_muldiv,
test_int_exp test_int_exp
#[
import test_io, import
test_conversion test_io#,
]# #test_conversion

View File

@ -101,228 +101,84 @@ template chkStuintToStint(chk: untyped, N, bits: static[int]) =
template testConversion(chk, tst: untyped) = template testConversion(chk, tst: untyped) =
tst "stuint to stuint": tst "stuint to stuint":
chkStuintToStuint(chk, 8, 8)
chkStuintToStuint(chk, 8, 16)
chkStuintToStuint(chk, 8, 32)
chkStuintToStuint(chk, 8, 64)
chkStuintToStuint(chk, 8, 128)
chkStuintToStuint(chk, 8, 256)
chkStuintToStuint(chk, 8, 512)
chkStuintToStuint(chk, 16, 8)
chkStuintToStuint(chk, 16, 16)
chkStuintToStuint(chk, 16, 32)
chkStuintToStuint(chk, 16, 64)
chkStuintToStuint(chk, 16, 128)
chkStuintToStuint(chk, 16, 256)
chkStuintToStuint(chk, 16, 512)
chkStuintToStuint(chk, 32, 8)
chkStuintToStuint(chk, 32, 16)
chkStuintToStuint(chk, 32, 32)
chkStuintToStuint(chk, 32, 64)
chkStuintToStuint(chk, 32, 128)
chkStuintToStuint(chk, 32, 256)
chkStuintToStuint(chk, 32, 512)
chkStuintToStuint(chk, 64, 8)
chkStuintToStuint(chk, 64, 16)
chkStuintToStuint(chk, 64, 32)
chkStuintToStuint(chk, 64, 64) chkStuintToStuint(chk, 64, 64)
chkStuintToStuint(chk, 64, 128) chkStuintToStuint(chk, 64, 128)
chkStuintToStuint(chk, 64, 256) chkStuintToStuint(chk, 64, 256)
chkStuintToStuint(chk, 64, 512) chkStuintToStuint(chk, 64, 512)
chkStuintToStuint(chk, 128, 8)
chkStuintToStuint(chk, 128, 16)
chkStuintToStuint(chk, 128, 32)
chkStuintToStuint(chk, 128, 64) chkStuintToStuint(chk, 128, 64)
chkStuintToStuint(chk, 128, 128) chkStuintToStuint(chk, 128, 128)
chkStuintToStuint(chk, 128, 256) chkStuintToStuint(chk, 128, 256)
chkStuintToStuint(chk, 128, 512) chkStuintToStuint(chk, 128, 512)
chkStuintToStuint(chk, 256, 8)
chkStuintToStuint(chk, 256, 16)
chkStuintToStuint(chk, 256, 32)
chkStuintToStuint(chk, 256, 64) chkStuintToStuint(chk, 256, 64)
chkStuintToStuint(chk, 256, 128) chkStuintToStuint(chk, 256, 128)
chkStuintToStuint(chk, 256, 256) chkStuintToStuint(chk, 256, 256)
chkStuintToStuint(chk, 256, 512) chkStuintToStuint(chk, 256, 512)
chkStuintToStuint(chk, 512, 8)
chkStuintToStuint(chk, 512, 16)
chkStuintToStuint(chk, 512, 32)
chkStuintToStuint(chk, 512, 64) chkStuintToStuint(chk, 512, 64)
chkStuintToStuint(chk, 512, 128) chkStuintToStuint(chk, 512, 128)
chkStuintToStuint(chk, 512, 256) chkStuintToStuint(chk, 512, 256)
chkStuintToStuint(chk, 512, 512) chkStuintToStuint(chk, 512, 512)
tst "stint to stuint": tst "stint to stuint":
chkStintToStuint(chk, 8, 8)
chkStintToStuint(chk, 8, 16)
chkStintToStuint(chk, 8, 32)
chkStintToStuint(chk, 8, 64)
chkStintToStuint(chk, 8, 128)
chkStintToStuint(chk, 8, 256)
chkStintToStuint(chk, 8, 512)
chkStintToStuint(chk, 16, 8)
chkStintToStuint(chk, 16, 16)
chkStintToStuint(chk, 16, 32)
chkStintToStuint(chk, 16, 64)
chkStintToStuint(chk, 16, 128)
chkStintToStuint(chk, 16, 256)
chkStintToStuint(chk, 16, 512)
chkStintToStuint(chk, 32, 8)
chkStintToStuint(chk, 32, 16)
chkStintToStuint(chk, 32, 32)
chkStintToStuint(chk, 32, 64)
chkStintToStuint(chk, 32, 128)
chkStintToStuint(chk, 32, 256)
chkStintToStuint(chk, 32, 512)
chkStintToStuint(chk, 64, 8)
chkStintToStuint(chk, 64, 16)
chkStintToStuint(chk, 64, 32)
chkStintToStuint(chk, 64, 64) chkStintToStuint(chk, 64, 64)
chkStintToStuint(chk, 64, 128) chkStintToStuint(chk, 64, 128)
chkStintToStuint(chk, 64, 256) chkStintToStuint(chk, 64, 256)
chkStintToStuint(chk, 64, 512) chkStintToStuint(chk, 64, 512)
chkStintToStuint(chk, 128, 8)
chkStintToStuint(chk, 128, 16)
chkStintToStuint(chk, 128, 32)
chkStintToStuint(chk, 128, 64) chkStintToStuint(chk, 128, 64)
chkStintToStuint(chk, 128, 128) chkStintToStuint(chk, 128, 128)
chkStintToStuint(chk, 128, 256) chkStintToStuint(chk, 128, 256)
chkStintToStuint(chk, 128, 512) chkStintToStuint(chk, 128, 512)
chkStintToStuint(chk, 256, 8)
chkStintToStuint(chk, 256, 16)
chkStintToStuint(chk, 256, 32)
chkStintToStuint(chk, 256, 64) chkStintToStuint(chk, 256, 64)
chkStintToStuint(chk, 256, 128) chkStintToStuint(chk, 256, 128)
chkStintToStuint(chk, 256, 256) chkStintToStuint(chk, 256, 256)
chkStintToStuint(chk, 256, 512) chkStintToStuint(chk, 256, 512)
chkStintToStuint(chk, 512, 8)
chkStintToStuint(chk, 512, 16)
chkStintToStuint(chk, 512, 32)
chkStintToStuint(chk, 512, 64) chkStintToStuint(chk, 512, 64)
chkStintToStuint(chk, 512, 128) chkStintToStuint(chk, 512, 128)
chkStintToStuint(chk, 512, 256) chkStintToStuint(chk, 512, 256)
chkStintToStuint(chk, 512, 512) chkStintToStuint(chk, 512, 512)
tst "stint to stint": tst "stint to stint":
chkStintToStint(chk, 8, 8)
chkStintToStint(chk, 8, 16)
chkStintToStint(chk, 8, 32)
chkStintToStint(chk, 8, 64)
chkStintToStint(chk, 8, 128)
chkStintToStint(chk, 8, 256)
chkStintToStint(chk, 8, 512)
chkStintToStint(chk, 16, 8)
chkStintToStint(chk, 16, 16)
chkStintToStint(chk, 16, 32)
chkStintToStint(chk, 16, 64)
chkStintToStint(chk, 16, 128)
chkStintToStint(chk, 16, 256)
chkStintToStint(chk, 16, 512)
chkStintToStint(chk, 32, 8)
chkStintToStint(chk, 32, 16)
chkStintToStint(chk, 32, 32)
chkStintToStint(chk, 32, 64)
chkStintToStint(chk, 32, 128)
chkStintToStint(chk, 32, 256)
chkStintToStint(chk, 32, 512)
chkStintToStint(chk, 64, 8)
chkStintToStint(chk, 64, 16)
chkStintToStint(chk, 64, 32)
chkStintToStint(chk, 64, 64) chkStintToStint(chk, 64, 64)
chkStintToStint(chk, 64, 128) chkStintToStint(chk, 64, 128)
chkStintToStint(chk, 64, 256) chkStintToStint(chk, 64, 256)
chkStintToStint(chk, 64, 512) chkStintToStint(chk, 64, 512)
chkStintToStint(chk, 128, 8)
chkStintToStint(chk, 128, 16)
chkStintToStint(chk, 128, 32)
chkStintToStint(chk, 128, 64) chkStintToStint(chk, 128, 64)
chkStintToStint(chk, 128, 128) chkStintToStint(chk, 128, 128)
chkStintToStint(chk, 128, 256) chkStintToStint(chk, 128, 256)
chkStintToStint(chk, 128, 512) chkStintToStint(chk, 128, 512)
chkStintToStint(chk, 256, 8)
chkStintToStint(chk, 256, 16)
chkStintToStint(chk, 256, 32)
chkStintToStint(chk, 256, 64) chkStintToStint(chk, 256, 64)
chkStintToStint(chk, 256, 128) chkStintToStint(chk, 256, 128)
chkStintToStint(chk, 256, 256) chkStintToStint(chk, 256, 256)
chkStintToStint(chk, 256, 512) chkStintToStint(chk, 256, 512)
chkStintToStint(chk, 512, 8)
chkStintToStint(chk, 512, 16)
chkStintToStint(chk, 512, 32)
chkStintToStint(chk, 512, 64) chkStintToStint(chk, 512, 64)
chkStintToStint(chk, 512, 128) chkStintToStint(chk, 512, 128)
chkStintToStint(chk, 512, 256) chkStintToStint(chk, 512, 256)
chkStintToStint(chk, 512, 512) chkStintToStint(chk, 512, 512)
tst "stuint to stint": tst "stuint to stint":
chkStuintToStint(chk, 8, 8)
chkStuintToStint(chk, 8, 16)
chkStuintToStint(chk, 8, 32)
chkStuintToStint(chk, 8, 64)
chkStuintToStint(chk, 8, 128)
chkStuintToStint(chk, 8, 256)
chkStuintToStint(chk, 8, 512)
chkStuintToStint(chk, 16, 8)
chkStuintToStint(chk, 16, 16)
chkStuintToStint(chk, 16, 32)
chkStuintToStint(chk, 16, 64)
chkStuintToStint(chk, 16, 128)
chkStuintToStint(chk, 16, 256)
chkStuintToStint(chk, 16, 512)
chkStuintToStint(chk, 32, 8)
chkStuintToStint(chk, 32, 16)
chkStuintToStint(chk, 32, 32)
chkStuintToStint(chk, 32, 64)
chkStuintToStint(chk, 32, 128)
chkStuintToStint(chk, 32, 256)
chkStuintToStint(chk, 32, 512)
chkStuintToStint(chk, 64, 8)
chkStuintToStint(chk, 64, 16)
chkStuintToStint(chk, 64, 32)
chkStuintToStint(chk, 64, 64) chkStuintToStint(chk, 64, 64)
chkStuintToStint(chk, 64, 128) chkStuintToStint(chk, 64, 128)
chkStuintToStint(chk, 64, 256) chkStuintToStint(chk, 64, 256)
chkStuintToStint(chk, 64, 512) chkStuintToStint(chk, 64, 512)
chkStuintToStint(chk, 128, 8)
chkStuintToStint(chk, 128, 16)
chkStuintToStint(chk, 128, 32)
chkStuintToStint(chk, 128, 64) chkStuintToStint(chk, 128, 64)
chkStuintToStint(chk, 128, 128) chkStuintToStint(chk, 128, 128)
chkStuintToStint(chk, 128, 256) chkStuintToStint(chk, 128, 256)
chkStuintToStint(chk, 128, 512) chkStuintToStint(chk, 128, 512)
chkStuintToStint(chk, 256, 8)
chkStuintToStint(chk, 256, 16)
chkStuintToStint(chk, 256, 32)
chkStuintToStint(chk, 256, 64) chkStuintToStint(chk, 256, 64)
chkStuintToStint(chk, 256, 128) chkStuintToStint(chk, 256, 128)
chkStuintToStint(chk, 256, 256) chkStuintToStint(chk, 256, 256)
chkStuintToStint(chk, 256, 512) chkStuintToStint(chk, 256, 512)
chkStuintToStint(chk, 512, 8)
chkStuintToStint(chk, 512, 16)
chkStuintToStint(chk, 512, 32)
chkStuintToStint(chk, 512, 64) chkStuintToStint(chk, 512, 64)
chkStuintToStint(chk, 512, 128) chkStuintToStint(chk, 512, 128)
chkStuintToStint(chk, 512, 256) chkStuintToStint(chk, 512, 256)

View File

@ -8,3 +8,13 @@ template ctTest*(name: string, body: untyped) =
block: block:
body body
echo "[OK] compile time ", name 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

@ -9,6 +9,16 @@
import ../stint, unittest, strutils, math, test_helpers, tables, std/strformat import ../stint, unittest, strutils, math, test_helpers, tables, std/strformat
import stew/byteutils
func significantBytesBE(val: openArray[byte]): int =
## Returns the number of significant trailing bytes in a big endian
## representation of a number.
# TODO: move that in https://github.com/status-im/nim-byteutils
for i in 0 ..< val.len:
if val[i] != 0:
return val.len - i
return 1
template nativeStuint(chk, nint: untyped, bits: int) = template nativeStuint(chk, nint: untyped, bits: int) =
chk $(nint.stuint(bits)) == $(nint) chk $(nint.stuint(bits)) == $(nint)
@ -111,51 +121,8 @@ template chkDumpHexStint(chk: untyped, BE, LE: string, bits: int) =
chk dumpHex(x, bigEndian) == data chk dumpHex(x, bigEndian) == data
chk dumpHex(x, littleEndian) == LE chk dumpHex(x, littleEndian) == LE
template testIO(chk, tst: untyped) = template testIO(chk, tst, handleErr: untyped) =
tst "[stuint] Creation from native ints": tst "[stuint] Creation from native ints":
nativeStuint(chk, 0, 8)
nativeStuint(chk, 0'u8, 8)
nativeStuint(chk, 0xFF'u16, 8)
nativeStuint(chk, 0xFF'u32, 8)
nativeStuint(chk, 0xFF'u64, 8)
nativeStuint(chk, 0'i8, 8)
nativeStuint(chk, 0xFF'i16, 8)
nativeStuint(chk, 0xFF'i32, 8)
nativeStuint(chk, 0xFF'i64, 8)
nativeStuint(chk, high(uint8), 8)
nativeStuint(chk, low(uint8), 8)
nativeStuint(chk, high(int8), 8)
nativeStuint(chk, 0, 16)
nativeStuint(chk, 0'u8, 16)
nativeStuint(chk, 0xFFFF'u32, 16)
nativeStuint(chk, 0xFFFF'u64, 16)
nativeStuint(chk, 0xFFFF'i32, 16)
nativeStuint(chk, 0xFFFF'i64, 16)
nativeStuint(chk, high(uint8), 16)
nativeStuint(chk, low(uint8), 16)
nativeStuint(chk, high(int8), 16)
nativeStuint(chk, 0'u16, 16)
nativeStuint(chk, high(uint16), 16)
nativeStuint(chk, low(uint16), 16)
nativeStuint(chk, high(int16), 16)
nativeStuint(chk, 0, 32)
nativeStuint(chk, 0'u8, 32)
nativeStuint(chk, 0xFFFFFFFF'u64, 32)
nativeStuint(chk, 0xFFFFFFFF'i64, 32)
nativeStuint(chk, high(uint8), 32)
nativeStuint(chk, low(uint8), 32)
nativeStuint(chk, high(int8), 32)
nativeStuint(chk, 0'u16, 32)
nativeStuint(chk, high(uint16), 32)
nativeStuint(chk, low(uint16), 32)
nativeStuint(chk, high(int16), 32)
nativeStuint(chk, 0'u32, 32)
nativeStuint(chk, high(uint32), 32)
nativeStuint(chk, low(uint32), 32)
nativeStuint(chk, high(int32), 32)
nativeStuint(chk, 0, 64) nativeStuint(chk, 0, 64)
nativeStuint(chk, 0'u8, 64) nativeStuint(chk, 0'u8, 64)
nativeStuint(chk, high(uint8), 64) nativeStuint(chk, high(uint8), 64)
@ -174,11 +141,7 @@ template testIO(chk, tst: untyped) =
nativeStuint(chk, low(uint64), 64) nativeStuint(chk, low(uint64), 64)
nativeStuint(chk, high(int64), 64) nativeStuint(chk, high(int64), 64)
when sizeof(uint) == 4: when sizeof(uint) == 8:
nativeStuint(chk, high(uint), 32)
nativeStuint(chk, low(uint), 32)
nativeStuint(chk, high(int), 32)
else:
nativeStuint(chk, high(uint), 64) nativeStuint(chk, high(uint), 64)
nativeStuint(chk, low(uint), 64) nativeStuint(chk, low(uint), 64)
nativeStuint(chk, high(int), 64) nativeStuint(chk, high(int), 64)
@ -202,36 +165,6 @@ template testIO(chk, tst: untyped) =
nativeStuint(chk, high(int64), 128) nativeStuint(chk, high(int64), 128)
tst "[stint] Creation from native ints": tst "[stint] Creation from native ints":
nativeStint(chk, 0, 8)
nativeStint(chk, 0'u8, 8)
nativeStint(chk, high(int8), 8)
nativeStint(chk, low(int8), 8)
nativeStint(chk, low(uint8), 8)
nativeStint(chk, 0, 16)
nativeStint(chk, 0'u8, 16)
nativeStint(chk, high(int8), 16)
nativeStint(chk, low(int8), 16)
nativeStint(chk, low(uint8), 16)
nativeStint(chk, 0'u16, 16)
nativeStint(chk, high(int16), 16)
nativeStint(chk, low(int16), 16)
nativeStint(chk, low(uint16), 16)
nativeStint(chk, 0, 32)
nativeStint(chk, 0'u8, 32)
nativeStint(chk, high(int8), 32)
nativeStint(chk, low(int8), 32)
nativeStint(chk, low(uint8), 32)
nativeStint(chk, 0'u16, 32)
nativeStint(chk, high(int16), 32)
nativeStint(chk, low(int16), 32)
nativeStint(chk, low(uint16), 32)
nativeStint(chk, 0'u32, 32)
nativeStint(chk, high(int32), 32)
nativeStint(chk, low(int32), 32)
nativeStint(chk, low(uint32), 32)
nativeStint(chk, 0, 64) nativeStint(chk, 0, 64)
nativeStint(chk, 0'u8, 64) nativeStint(chk, 0'u8, 64)
nativeStint(chk, high(int8), 64) nativeStint(chk, high(int8), 64)
@ -250,11 +183,7 @@ template testIO(chk, tst: untyped) =
nativeStint(chk, low(int64), 64) nativeStint(chk, low(int64), 64)
nativeStint(chk, low(uint64), 64) nativeStint(chk, low(uint64), 64)
when sizeof(uint) == 4: when sizeof(uint) == 8:
nativeStint(chk, high(int), 32)
nativeStint(chk, low(int), 32)
nativeStint(chk, low(uint), 32)
else:
nativeStint(chk, high(int), 64) nativeStint(chk, high(int), 64)
nativeStint(chk, low(int), 64) nativeStint(chk, low(int), 64)
nativeStint(chk, low(uint), 64) nativeStint(chk, low(uint), 64)
@ -273,63 +202,12 @@ template testIO(chk, tst: untyped) =
nativeStint(chk, high(int64), 128) nativeStint(chk, high(int64), 128)
nativeStint(chk, low(uint64), 128) nativeStint(chk, low(uint64), 128)
# TODO: bug #92 nativeStint(chk, low(int8), 128)
#nativeStint(chk, low(int8), 128) nativeStint(chk, low(int16), 128)
#nativeStint(chk, low(int16), 128) nativeStint(chk, low(int32), 128)
#nativeStint(chk, low(int32), 128) nativeStint(chk, low(int64), 128)
#nativeStint(chk, low(int64), 128)
tst "[stuint] truncate": tst "[stuint] truncate":
chkTruncateStuint(chk, low(uint8), uint8, 8)
chkTruncateStuint(chk, high(uint8), uint8, 8)
chkTruncateStuint(chk, high(int8), uint8, 8)
chkTruncateStuint(chk, high(int8), int8, 8)
chkTruncateStuint(chk, low(uint8), uint8, 16)
chkTruncateStuint(chk, high(uint8), uint8, 16)
chkTruncateStuint(chk, high(int8), uint8, 16)
chkTruncateStuint(chk, high(int8), int8, 16)
chkTruncateStuint(chk, low(uint8), uint16, 16)
chkTruncateStuint(chk, high(uint8), uint16, 16)
chkTruncateStuint(chk, high(int8), uint16, 16)
chkTruncateStuint(chk, high(int8), int16, 16)
chkTruncateStuint(chk, low(uint16), uint16, 16)
chkTruncateStuint(chk, high(uint16), uint16, 16)
chkTruncateStuint(chk, high(int16), uint16, 16)
chkTruncateStuint(chk, high(int16), int16, 16)
chkTruncateStuint(chk, low(uint8), uint8, 32)
chkTruncateStuint(chk, high(uint8), uint8, 32)
chkTruncateStuint(chk, high(int8), uint8, 32)
chkTruncateStuint(chk, high(int8), int8, 32)
chkTruncateStuint(chk, low(uint8), uint16, 32)
chkTruncateStuint(chk, high(uint8), uint16, 32)
chkTruncateStuint(chk, high(int8), uint16, 32)
chkTruncateStuint(chk, high(int8), int16, 32)
chkTruncateStuint(chk, low(uint16), uint16, 32)
chkTruncateStuint(chk, high(uint16), uint16, 32)
chkTruncateStuint(chk, high(int16), uint16, 32)
chkTruncateStuint(chk, high(int16), int16, 32)
chkTruncateStuint(chk, low(uint8), uint32, 32)
chkTruncateStuint(chk, high(uint8), uint32, 32)
chkTruncateStuint(chk, high(int8), uint32, 32)
chkTruncateStuint(chk, high(int8), int32, 32)
chkTruncateStuint(chk, low(uint16), uint32, 32)
chkTruncateStuint(chk, high(uint16), uint32, 32)
chkTruncateStuint(chk, high(int16), uint32, 32)
chkTruncateStuint(chk, high(int16), int32, 32)
chkTruncateStuint(chk, low(uint32), uint32, 32)
chkTruncateStuint(chk, high(uint32), uint32, 32)
chkTruncateStuint(chk, high(int32), uint32, 32)
chkTruncateStuint(chk, high(int32), int32, 32)
chkTruncateStuint(chk, low(uint8), uint8, 64) chkTruncateStuint(chk, low(uint8), uint8, 64)
chkTruncateStuint(chk, high(uint8), uint8, 64) chkTruncateStuint(chk, high(uint8), uint8, 64)
chkTruncateStuint(chk, high(int8), uint8, 64) chkTruncateStuint(chk, high(int8), uint8, 64)
@ -431,197 +309,150 @@ template testIO(chk, tst: untyped) =
chkTruncateStuint(chk, high(int64), int64, 128) chkTruncateStuint(chk, high(int64), int64, 128)
tst "[stint] truncate": tst "[stint] truncate":
chkTruncateStint(chk, low(uint8), uint8, 8) chkTruncateStint(chk, 10, uint8, 64)
chkTruncateStint(chk, low(int8), int8, 8) chkTruncateStint(chk, 10, int8, 64)
chkTruncateStint(chk, high(int8), uint8, 8) chkTruncateStint(chk, -10, int8, 64)
chkTruncateStint(chk, high(int8), int8, 8)
chkTruncateStint(chk, low(int8), uint8, "0x80", 8)
chkTruncateStint(chk, low(uint8), uint8, 16)
chkTruncateStint(chk, low(int8), int8, 16)
chkTruncateStint(chk, high(int8), uint8, 16)
chkTruncateStint(chk, high(int8), int8, 16)
chkTruncateStint(chk, low(int8), uint8, "0x80", 16)
chkTruncateStint(chk, low(uint8), uint16, 16)
chkTruncateStint(chk, low(int8), int16, 16)
chkTruncateStint(chk, high(int8), uint16, 16)
chkTruncateStint(chk, high(int8), int16, 16)
chkTruncateStint(chk, low(int8), uint16, "0xFF80", 16)
chkTruncateStint(chk, low(uint16), uint16, 16)
chkTruncateStint(chk, low(int16), int16, 16)
chkTruncateStint(chk, high(int16), uint16, 16)
chkTruncateStint(chk, high(int16), int16, 16)
chkTruncateStint(chk, low(int16), uint16, "0x8000", 16)
chkTruncateStint(chk, low(uint8), uint8, 32)
chkTruncateStint(chk, low(int8), int8, 32)
chkTruncateStint(chk, high(int8), uint8, 32)
chkTruncateStint(chk, high(int8), int8, 32)
chkTruncateStint(chk, low(int8), uint8, "0x80", 32)
chkTruncateStint(chk, low(uint8), uint16, 32)
chkTruncateStint(chk, low(int8), int16, 32)
chkTruncateStint(chk, high(int8), uint16, 32)
chkTruncateStint(chk, high(int8), int16, 32)
chkTruncateStint(chk, low(int8), uint16, "0xFF80", 32)
chkTruncateStint(chk, low(uint16), uint16, 32)
chkTruncateStint(chk, low(int16), int16, 32)
chkTruncateStint(chk, high(int16), uint16, 32)
chkTruncateStint(chk, high(int16), int16, 32)
chkTruncateStint(chk, low(int16), uint16, "0x8000", 32)
chkTruncateStint(chk, low(uint8), uint32, 32)
chkTruncateStint(chk, low(int8), int32, 32)
chkTruncateStint(chk, high(int8), uint32, 32)
chkTruncateStint(chk, high(int8), int32, 32)
chkTruncateStint(chk, low(int8), uint32, "0xFFFFFF80", 32)
chkTruncateStint(chk, low(uint16), uint32, 32)
chkTruncateStint(chk, low(int16), int32, 32)
chkTruncateStint(chk, high(int16), uint32, 32)
chkTruncateStint(chk, high(int16), int32, 32)
chkTruncateStint(chk, low(int16), uint32, "0xFFFF8000", 32)
chkTruncateStint(chk, low(uint32), uint32, 32)
chkTruncateStint(chk, low(int32), int32, 32)
chkTruncateStint(chk, high(int32), uint32, 32)
chkTruncateStint(chk, high(int32), int32, 32)
chkTruncateStint(chk, low(int32), uint32, "0x80000000", 32)
chkTruncateStint(chk, low(uint8), uint8, 64) chkTruncateStint(chk, low(uint8), uint8, 64)
chkTruncateStint(chk, low(int8), int8, 64) chkTruncateStint(chk, low(int8), int8, 64)
chkTruncateStint(chk, high(int8), uint8, 64) chkTruncateStint(chk, high(int8), uint8, 64)
chkTruncateStint(chk, high(int8), int8, 64) chkTruncateStint(chk, high(int8), int8, 64)
chkTruncateStint(chk, low(int8), uint8, "0x80", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint8, "0x80", 64)
chkTruncateStint(chk, low(uint8), uint16, 64) chkTruncateStint(chk, low(uint8), uint16, 64)
chkTruncateStint(chk, low(int8), int16, 64) chkTruncateStint(chk, low(int8), int16, 64)
chkTruncateStint(chk, high(int8), uint16, 64) chkTruncateStint(chk, high(int8), uint16, 64)
chkTruncateStint(chk, high(int8), int16, 64) chkTruncateStint(chk, high(int8), int16, 64)
chkTruncateStint(chk, low(int8), uint16, "0xFF80", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint16, "0xFF80", 64)
chkTruncateStint(chk, low(uint16), uint16, 64) chkTruncateStint(chk, low(uint16), uint16, 64)
chkTruncateStint(chk, low(int16), int16, 64) chkTruncateStint(chk, low(int16), int16, 64)
chkTruncateStint(chk, high(int16), uint16, 64) chkTruncateStint(chk, high(int16), uint16, 64)
chkTruncateStint(chk, high(int16), int16, 64) chkTruncateStint(chk, high(int16), int16, 64)
chkTruncateStint(chk, low(int16), uint16, "0x8000", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int16), uint16, "0x8000", 64)
chkTruncateStint(chk, low(uint8), uint32, 64) chkTruncateStint(chk, low(uint8), uint32, 64)
chkTruncateStint(chk, low(int8), int32, 64) chkTruncateStint(chk, low(int8), int32, 64)
chkTruncateStint(chk, high(int8), uint32, 64) chkTruncateStint(chk, high(int8), uint32, 64)
chkTruncateStint(chk, high(int8), int32, 64) chkTruncateStint(chk, high(int8), int32, 64)
chkTruncateStint(chk, low(int8), uint32, "0xFFFFFF80", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint32, "0xFFFFFF80", 64)
chkTruncateStint(chk, low(uint16), uint32, 64) chkTruncateStint(chk, low(uint16), uint32, 64)
chkTruncateStint(chk, low(int16), int32, 64) chkTruncateStint(chk, low(int16), int32, 64)
chkTruncateStint(chk, high(int16), uint32, 64) chkTruncateStint(chk, high(int16), uint32, 64)
chkTruncateStint(chk, high(int16), int32, 64) chkTruncateStint(chk, high(int16), int32, 64)
chkTruncateStint(chk, low(int16), uint32, "0xFFFF8000", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int16), uint32, "0xFFFF8000", 64)
chkTruncateStint(chk, low(uint32), uint32, 64) chkTruncateStint(chk, low(uint32), uint32, 64)
chkTruncateStint(chk, low(int32), int32, 64) chkTruncateStint(chk, low(int32), int32, 64)
chkTruncateStint(chk, high(int32), uint32, 64) chkTruncateStint(chk, high(int32), uint32, 64)
chkTruncateStint(chk, high(int32), int32, 64) chkTruncateStint(chk, high(int32), int32, 64)
chkTruncateStint(chk, low(int32), uint32, "0x80000000", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int32), uint32, "0x80000000", 64)
chkTruncateStint(chk, low(uint8), uint64, 64) chkTruncateStint(chk, low(uint8), uint64, 64)
chkTruncateStint(chk, low(int8), int64, 64) chkTruncateStint(chk, low(int8), int64, 64)
chkTruncateStint(chk, high(int8), uint64, 64) chkTruncateStint(chk, high(int8), uint64, 64)
chkTruncateStint(chk, high(int8), int64, 64) chkTruncateStint(chk, high(int8), int64, 64)
chkTruncateStint(chk, low(int8), uint64, "0xFFFFFFFFFFFFFF80", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint64, "0xFFFFFFFFFFFFFF80", 64)
chkTruncateStint(chk, low(uint16), uint64, 64) chkTruncateStint(chk, low(uint16), uint64, 64)
chkTruncateStint(chk, low(int16), int64, 64) chkTruncateStint(chk, low(int16), int64, 64)
chkTruncateStint(chk, high(int16), uint64, 64) chkTruncateStint(chk, high(int16), uint64, 64)
chkTruncateStint(chk, high(int16), int64, 64) chkTruncateStint(chk, high(int16), int64, 64)
chkTruncateStint(chk, low(int16), uint64, "0xFFFFFFFFFFFF8000", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int16), uint64, "0xFFFFFFFFFFFF8000", 64)
chkTruncateStint(chk, low(uint32), uint64, 64) chkTruncateStint(chk, low(uint32), uint64, 64)
chkTruncateStint(chk, low(int32), int64, 64) chkTruncateStint(chk, low(int32), int64, 64)
chkTruncateStint(chk, high(int32), uint64, 64) chkTruncateStint(chk, high(int32), uint64, 64)
chkTruncateStint(chk, high(int32), int64, 64) chkTruncateStint(chk, high(int32), int64, 64)
chkTruncateStint(chk, low(int32), uint64, "0xFFFFFFFF80000000", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int32), uint64, "0xFFFFFFFF80000000", 64)
chkTruncateStint(chk, low(uint64), uint64, 64) chkTruncateStint(chk, low(uint64), uint64, 64)
chkTruncateStint(chk, low(int64), int64, 64) chkTruncateStint(chk, low(int64), int64, 64)
chkTruncateStint(chk, high(int64), uint64, 64) chkTruncateStint(chk, high(int64), uint64, 64)
chkTruncateStint(chk, high(int64), int64, 64) chkTruncateStint(chk, high(int64), int64, 64)
chkTruncateStint(chk, low(int64), uint64, "0x8000000000000000", 64) handleErr AssertionDefect:
chkTruncateStint(chk, low(int64), uint64, "0x8000000000000000", 64)
chkTruncateStint(chk, low(uint8), uint8, 128) chkTruncateStint(chk, low(uint8), uint8, 128)
#chkTruncateStint(chk, low(int8), int8, 128) # TODO: bug #92 chkTruncateStint(chk, low(int8), int8, 128)
chkTruncateStint(chk, high(int8), uint8, 128) chkTruncateStint(chk, high(int8), uint8, 128)
chkTruncateStint(chk, high(int8), int8, 128) chkTruncateStint(chk, high(int8), int8, 128)
#chkTruncateStint(chk, low(int8), uint8, "0x80", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint8, "0x80", 128)
chkTruncateStint(chk, low(uint8), uint16, 128) chkTruncateStint(chk, low(uint8), uint16, 128)
#chkTruncateStint(chk, low(int8), int16, 128) # TODO: bug #92 chkTruncateStint(chk, low(int8), int16, 128)
chkTruncateStint(chk, high(int8), uint16, 128) chkTruncateStint(chk, high(int8), uint16, 128)
chkTruncateStint(chk, high(int8), int16, 128) chkTruncateStint(chk, high(int8), int16, 128)
#chkTruncateStint(chk, low(int8), uint16, "0xFF80", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint16, "0xFF80", 128)
chkTruncateStint(chk, low(uint16), uint16, 128) chkTruncateStint(chk, low(uint16), uint16, 128)
#chkTruncateStint(chk, low(int16), int16, 128) # TODO: bug #92 chkTruncateStint(chk, low(int16), int16, 128)
chkTruncateStint(chk, high(int16), uint16, 128) chkTruncateStint(chk, high(int16), uint16, 128)
chkTruncateStint(chk, high(int16), int16, 128) chkTruncateStint(chk, high(int16), int16, 128)
#chkTruncateStint(chk, low(int16), uint16, "0x8000", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int16), uint16, "0x8000", 128)
chkTruncateStint(chk, low(uint8), uint32, 128) chkTruncateStint(chk, low(uint8), uint32, 128)
#chkTruncateStint(chk, low(int8), int32, 128) # TODO: bug #92 chkTruncateStint(chk, low(int8), int32, 128)
chkTruncateStint(chk, high(int8), uint32, 128) chkTruncateStint(chk, high(int8), uint32, 128)
chkTruncateStint(chk, high(int8), int32, 128) chkTruncateStint(chk, high(int8), int32, 128)
#chkTruncateStint(chk, low(int8), uint32, "0xFFFFFF80", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint32, "0xFFFFFF80", 128)
chkTruncateStint(chk, low(uint16), uint32, 128) chkTruncateStint(chk, low(uint16), uint32, 128)
#chkTruncateStint(chk, low(int16), int32, 128) # TODO: bug #92 chkTruncateStint(chk, low(int16), int32, 128)
chkTruncateStint(chk, high(int16), uint32, 128) chkTruncateStint(chk, high(int16), uint32, 128)
chkTruncateStint(chk, high(int16), int32, 128) chkTruncateStint(chk, high(int16), int32, 128)
#chkTruncateStint(chk, low(int16), uint32, "0xFFFF8000", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int16), uint32, "0xFFFF8000", 128)
chkTruncateStint(chk, low(uint32), uint32, 128) chkTruncateStint(chk, low(uint32), uint32, 128)
#chkTruncateStint(chk, low(int32), int32, 128) # TODO: bug #92 chkTruncateStint(chk, low(int32), int32, 128)
chkTruncateStint(chk, high(int32), uint32, 128) chkTruncateStint(chk, high(int32), uint32, 128)
chkTruncateStint(chk, high(int32), int32, 128) chkTruncateStint(chk, high(int32), int32, 128)
#chkTruncateStint(chk, low(int32), uint32, "0x80000000", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int32), uint32, "0x80000000", 128)
chkTruncateStint(chk, low(uint8), uint64, 128) chkTruncateStint(chk, low(uint8), uint64, 128)
#chkTruncateStint(chk, low(int8), int64, 128) # TODO: bug #92 chkTruncateStint(chk, low(int8), int64, 128)
chkTruncateStint(chk, high(int8), uint64, 128) chkTruncateStint(chk, high(int8), uint64, 128)
chkTruncateStint(chk, high(int8), int64, 128) chkTruncateStint(chk, high(int8), int64, 128)
#chkTruncateStint(chk, low(int8), uint64, "0xFFFFFFFFFFFFFF80", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int8), uint64, "0xFFFFFFFFFFFFFF80", 128)
chkTruncateStint(chk, low(uint16), uint64, 128) chkTruncateStint(chk, low(uint16), uint64, 128)
#chkTruncateStint(chk, low(int16), int64, 128) # TODO: bug #92 chkTruncateStint(chk, low(int16), int64, 128)
chkTruncateStint(chk, high(int16), uint64, 128) chkTruncateStint(chk, high(int16), uint64, 128)
chkTruncateStint(chk, high(int16), int64, 128) chkTruncateStint(chk, high(int16), int64, 128)
#chkTruncateStint(chk, low(int16), uint64, "0xFFFFFFFFFFFF8000", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int16), uint64, "0xFFFFFFFFFFFF8000", 128)
chkTruncateStint(chk, low(uint32), uint64, 128) chkTruncateStint(chk, low(uint32), uint64, 128)
#chkTruncateStint(chk, low(int32), int64, 128) # TODO: bug #92 chkTruncateStint(chk, low(int32), int64, 128)
chkTruncateStint(chk, high(int32), uint64, 128) chkTruncateStint(chk, high(int32), uint64, 128)
chkTruncateStint(chk, high(int32), int64, 128) chkTruncateStint(chk, high(int32), int64, 128)
#chkTruncateStint(chk, low(int32), uint64, "0xFFFFFFFF80000000", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int32), uint64, "0xFFFFFFFF80000000", 128)
chkTruncateStint(chk, low(uint64), uint64, 128) chkTruncateStint(chk, low(uint64), uint64, 128)
#chkTruncateStint(chk, low(int64), int64, 128) # TODO: bug #92 chkTruncateStint(chk, low(int64), int64, 128)
chkTruncateStint(chk, high(int64), uint64, 128) chkTruncateStint(chk, high(int64), uint64, 128)
chkTruncateStint(chk, high(int64), int64, 128) chkTruncateStint(chk, high(int64), int64, 128)
#chkTruncateStint(chk, low(int64), uint64, "0x8000000000000000", 128) # TODO: bug #92 handleErr AssertionDefect:
chkTruncateStint(chk, low(int64), uint64, "0x8000000000000000", 128)
tst "[stuint] parse - toString roundtrip": tst "[stuint] parse - toString roundtrip":
chkRoundTripBin(chk, chkRoundTripStuint, 8, 1)
chkRoundTripBin(chk, chkRoundTripStuint, 16, 1)
chkRoundTripBin(chk, chkRoundTripStuint, 16, 2)
chkRoundTripBin(chk, chkRoundTripStuint, 32, 1)
chkRoundTripBin(chk, chkRoundTripStuint, 32, 2)
chkRoundTripBin(chk, chkRoundTripStuint, 32, 3)
chkRoundTripBin(chk, chkRoundTripStuint, 32, 4)
chkRoundTripBin(chk, chkRoundTripStuint, 64, 1) chkRoundTripBin(chk, chkRoundTripStuint, 64, 1)
chkRoundTripBin(chk, chkRoundTripStuint, 64, 2) chkRoundTripBin(chk, chkRoundTripStuint, 64, 2)
chkRoundTripBin(chk, chkRoundTripStuint, 64, 3) chkRoundTripBin(chk, chkRoundTripStuint, 64, 3)
@ -648,16 +479,6 @@ template testIO(chk, tst: untyped) =
chkRoundTripBin(chk, chkRoundTripStuint, 128, 15) chkRoundTripBin(chk, chkRoundTripStuint, 128, 15)
chkRoundTripBin(chk, chkRoundTripStuint, 128, 16) chkRoundTripBin(chk, chkRoundTripStuint, 128, 16)
chkRoundTripHex(chk, chkRoundTripStuint, 8, 1)
chkRoundTripHex(chk, chkRoundTripStuint, 16, 1)
chkRoundTripHex(chk, chkRoundTripStuint, 16, 2)
chkRoundTripHex(chk, chkRoundTripStuint, 32, 1)
chkRoundTripHex(chk, chkRoundTripStuint, 32, 2)
chkRoundTripHex(chk, chkRoundTripStuint, 32, 3)
chkRoundTripHex(chk, chkRoundTripStuint, 32, 4)
chkRoundTripHex(chk, chkRoundTripStuint, 64, 1) chkRoundTripHex(chk, chkRoundTripStuint, 64, 1)
chkRoundTripHex(chk, chkRoundTripStuint, 64, 2) chkRoundTripHex(chk, chkRoundTripStuint, 64, 2)
chkRoundTripHex(chk, chkRoundTripStuint, 64, 3) chkRoundTripHex(chk, chkRoundTripStuint, 64, 3)
@ -684,21 +505,6 @@ template testIO(chk, tst: untyped) =
chkRoundTripHex(chk, chkRoundTripStuint, 128, 15) chkRoundTripHex(chk, chkRoundTripStuint, 128, 15)
chkRoundTripHex(chk, chkRoundTripStuint, 128, 16) chkRoundTripHex(chk, chkRoundTripStuint, 128, 16)
chkRoundTripOct(chk, chkRoundTripStuint, 8, 1)
chkRoundTripStuint(chk, "377", 8, 8)
chkRoundTripOct(chk, chkRoundTripStuint, 16, 1)
chkRoundTripOct(chk, chkRoundTripStuint, 16, 2)
chkRoundTripStuint(chk, "377", 16, 8)
chkRoundTripStuint(chk, "177777", 16, 8)
chkRoundTripOct(chk, chkRoundTripStuint, 32, 1)
chkRoundTripOct(chk, chkRoundTripStuint, 32, 2)
chkRoundTripOct(chk, chkRoundTripStuint, 32, 3)
chkRoundTripStuint(chk, "377", 32, 8)
chkRoundTripStuint(chk, "177777", 32, 8)
chkRoundTripStuint(chk, "37777777777", 32, 8)
chkRoundTripOct(chk, chkRoundTripStuint, 64, 1) chkRoundTripOct(chk, chkRoundTripStuint, 64, 1)
chkRoundTripOct(chk, chkRoundTripStuint, 64, 2) chkRoundTripOct(chk, chkRoundTripStuint, 64, 2)
chkRoundTripOct(chk, chkRoundTripStuint, 64, 3) chkRoundTripOct(chk, chkRoundTripStuint, 64, 3)
@ -731,21 +537,6 @@ template testIO(chk, tst: untyped) =
chkRoundTripStuint(chk, "1777777777777777777777", 128, 8) chkRoundTripStuint(chk, "1777777777777777777777", 128, 8)
chkRoundTripStuint(chk, "3777777777777777777777777777777777777777777", 128, 8) chkRoundTripStuint(chk, "3777777777777777777777777777777777777777777", 128, 8)
chkRoundTripDec(chk, chkRoundTripStuint, 8, 1)
chkRoundTripStuint(chk, "255", 8, 10)
chkRoundTripDec(chk, chkRoundTripStuint, 16, 1)
chkRoundTripDec(chk, chkRoundTripStuint, 16, 2)
chkRoundTripStuint(chk, "255", 16, 10)
chkRoundTripStuint(chk, "65535", 16, 10)
chkRoundTripDec(chk, chkRoundTripStuint, 32, 1)
chkRoundTripDec(chk, chkRoundTripStuint, 32, 2)
chkRoundTripDec(chk, chkRoundTripStuint, 32, 3)
chkRoundTripStuint(chk, "255", 32, 10)
chkRoundTripStuint(chk, "65535", 32, 10)
chkRoundTripStuint(chk, "4294967295", 32, 10)
chkRoundTripDec(chk, chkRoundTripStuint, 64, 1) chkRoundTripDec(chk, chkRoundTripStuint, 64, 1)
chkRoundTripDec(chk, chkRoundTripStuint, 64, 2) chkRoundTripDec(chk, chkRoundTripStuint, 64, 2)
chkRoundTripDec(chk, chkRoundTripStuint, 64, 3) chkRoundTripDec(chk, chkRoundTripStuint, 64, 3)
@ -779,19 +570,6 @@ template testIO(chk, tst: untyped) =
chkRoundTripStuint(chk, "340282366920938463463374607431768211455", 128, 10) chkRoundTripStuint(chk, "340282366920938463463374607431768211455", 128, 10)
tst "[stint] parse - toString roundtrip": tst "[stint] parse - toString roundtrip":
chkRoundTripBin(chk, chkRoundTripStint, 8, 1)
chkRoundTripStint(chk, "1" & repeat('0', 7), 8, 2)
chkRoundTripBin(chk, chkRoundTripStint, 16, 1)
chkRoundTripBin(chk, chkRoundTripStint, 16, 2)
chkRoundTripStint(chk, "1" & repeat('0', 15), 16, 2)
chkRoundTripBin(chk, chkRoundTripStint, 32, 1)
chkRoundTripBin(chk, chkRoundTripStint, 32, 2)
chkRoundTripBin(chk, chkRoundTripStint, 32, 3)
chkRoundTripBin(chk, chkRoundTripStint, 32, 4)
chkRoundTripStint(chk, "1" & repeat('0', 31), 32, 2)
chkRoundTripBin(chk, chkRoundTripStint, 64, 1) chkRoundTripBin(chk, chkRoundTripStint, 64, 1)
chkRoundTripBin(chk, chkRoundTripStint, 64, 2) chkRoundTripBin(chk, chkRoundTripStint, 64, 2)
chkRoundTripBin(chk, chkRoundTripStint, 64, 3) chkRoundTripBin(chk, chkRoundTripStint, 64, 3)
@ -820,19 +598,6 @@ template testIO(chk, tst: untyped) =
chkRoundTripBin(chk, chkRoundTripStint, 128, 16) chkRoundTripBin(chk, chkRoundTripStint, 128, 16)
chkRoundTripStint(chk, "1" & repeat('0', 127), 128, 2) chkRoundTripStint(chk, "1" & repeat('0', 127), 128, 2)
chkRoundTripHex(chk, chkRoundTripStint, 8, 1)
chkRoundTripStint(chk, "8" & repeat('0', 1), 8, 16)
chkRoundTripHex(chk, chkRoundTripStint, 16, 1)
chkRoundTripHex(chk, chkRoundTripStint, 16, 2)
chkRoundTripStint(chk, "8" & repeat('0', 3), 16, 16)
chkRoundTripHex(chk, chkRoundTripStint, 32, 1)
chkRoundTripHex(chk, chkRoundTripStint, 32, 2)
chkRoundTripHex(chk, chkRoundTripStint, 32, 3)
chkRoundTripHex(chk, chkRoundTripStint, 32, 4)
chkRoundTripStint(chk, "8" & repeat('0', 7), 32, 16)
chkRoundTripHex(chk, chkRoundTripStint, 64, 1) chkRoundTripHex(chk, chkRoundTripStint, 64, 1)
chkRoundTripHex(chk, chkRoundTripStint, 64, 2) chkRoundTripHex(chk, chkRoundTripStint, 64, 2)
chkRoundTripHex(chk, chkRoundTripStint, 64, 3) chkRoundTripHex(chk, chkRoundTripStint, 64, 3)
@ -861,27 +626,6 @@ template testIO(chk, tst: untyped) =
chkRoundTripHex(chk, chkRoundTripStint, 128, 16) chkRoundTripHex(chk, chkRoundTripStint, 128, 16)
chkRoundTripStint(chk, "8" & repeat('0', 31), 128, 16) chkRoundTripStint(chk, "8" & repeat('0', 31), 128, 16)
chkRoundTripOct(chk, chkRoundTripStint, 8, 1)
chkRoundTripStint(chk, "377", 8, 8)
chkRoundTripStint(chk, "200", 8, 8)
chkRoundTripOct(chk, chkRoundTripStint, 16, 1)
chkRoundTripOct(chk, chkRoundTripStint, 16, 2)
chkRoundTripStint(chk, "377", 16, 8)
chkRoundTripStint(chk, "200", 16, 8)
chkRoundTripStint(chk, "177777", 16, 8)
chkRoundTripStint(chk, "100000", 16, 8)
chkRoundTripOct(chk, chkRoundTripStint, 32, 1)
chkRoundTripOct(chk, chkRoundTripStint, 32, 2)
chkRoundTripOct(chk, chkRoundTripStint, 32, 3)
chkRoundTripStint(chk, "377", 32, 8)
chkRoundTripStint(chk, "200", 32, 8)
chkRoundTripStint(chk, "177777", 32, 8)
chkRoundTripStint(chk, "100000", 32, 8)
chkRoundTripStint(chk, "37777777777", 32, 8)
chkRoundTripStint(chk, "20000000000", 32, 8)
chkRoundTripOct(chk, chkRoundTripStint, 64, 1) chkRoundTripOct(chk, chkRoundTripStint, 64, 1)
chkRoundTripOct(chk, chkRoundTripStint, 64, 2) chkRoundTripOct(chk, chkRoundTripStint, 64, 2)
chkRoundTripOct(chk, chkRoundTripStint, 64, 3) chkRoundTripOct(chk, chkRoundTripStint, 64, 3)
@ -923,32 +667,6 @@ template testIO(chk, tst: untyped) =
chkRoundTripStint(chk, "3777777777777777777777777777777777777777777", 128, 8) chkRoundTripStint(chk, "3777777777777777777777777777777777777777777", 128, 8)
chkRoundTripStint(chk, "2000000000000000000000000000000000000000000", 128, 8) chkRoundTripStint(chk, "2000000000000000000000000000000000000000000", 128, 8)
chkRoundTripDec(chk, chkRoundTripStint, 8, 1)
chkRoundTripStint(chk, "127", 8, 10)
chkRoundTripStint(chk, "-127", 8, 10)
# chkRoundTripStint(chk, "-128", 8, 10) # TODO: not supported yet
chkRoundTripDec(chk, chkRoundTripStint, 16, 1)
chkRoundTripDec(chk, chkRoundTripStint, 16, 2)
chkRoundTripStint(chk, "255", 16, 10)
chkRoundTripStint(chk, "127", 16, 10)
chkRoundTripStint(chk, "-128", 16, 10)
chkRoundTripStint(chk, "32767", 16, 10)
chkRoundTripStint(chk, "-32767", 16, 10)
#chkRoundTripStint(chk, "-32768", 16, 10) # TODO: not supported yet
chkRoundTripDec(chk, chkRoundTripStint, 32, 1)
chkRoundTripDec(chk, chkRoundTripStint, 32, 2)
chkRoundTripDec(chk, chkRoundTripStint, 32, 3)
chkRoundTripStint(chk, "255", 32, 10)
chkRoundTripStint(chk, "127", 32, 10)
chkRoundTripStint(chk, "-128", 32, 10)
chkRoundTripStint(chk, "32767", 32, 10)
chkRoundTripStint(chk, "-32768", 32, 10)
chkRoundTripStint(chk, "65535", 32, 10)
chkRoundTripStint(chk, "-2147483647", 32, 10)
#chkRoundTripStint(chk, "-2147483648", 32, 10) # TODO: not supported yet
chkRoundTripDec(chk, chkRoundTripStint, 64, 1) chkRoundTripDec(chk, chkRoundTripStint, 64, 1)
chkRoundTripDec(chk, chkRoundTripStint, 64, 2) chkRoundTripDec(chk, chkRoundTripStint, 64, 2)
chkRoundTripDec(chk, chkRoundTripStint, 64, 3) chkRoundTripDec(chk, chkRoundTripStint, 64, 3)
@ -966,7 +684,7 @@ template testIO(chk, tst: untyped) =
chkRoundTripStint(chk, "-2147483648", 64, 10) chkRoundTripStint(chk, "-2147483648", 64, 10)
chkRoundTripStint(chk, "4294967295", 64, 10) chkRoundTripStint(chk, "4294967295", 64, 10)
chkRoundTripStint(chk, "-9223372036854775807", 64, 10) chkRoundTripStint(chk, "-9223372036854775807", 64, 10)
#chkRoundTripStint(chk, "-9223372036854775808", 64, 10) # TODO: not supported yet chkRoundTripStint(chk, "-9223372036854775808", 64, 10)
chkRoundTripDec(chk, chkRoundTripStint, 128, 1) chkRoundTripDec(chk, chkRoundTripStint, 128, 1)
chkRoundTripDec(chk, chkRoundTripStint, 128, 2) chkRoundTripDec(chk, chkRoundTripStint, 128, 2)
@ -987,65 +705,37 @@ template testIO(chk, tst: untyped) =
chkRoundTripStint(chk, "4294967295", 128, 10) chkRoundTripStint(chk, "4294967295", 128, 10)
chkRoundTripStint(chk, "18446744073709551615", 128, 10) chkRoundTripStint(chk, "18446744073709551615", 128, 10)
chkRoundTripStint(chk, "-170141183460469231731687303715884105727", 128, 10) chkRoundTripStint(chk, "-170141183460469231731687303715884105727", 128, 10)
#chkRoundTripStint(chk, "-170141183460469231731687303715884105728", 128, 10) # TODO: not supported yet chkRoundTripStint(chk, "-170141183460469231731687303715884105728", 128, 10)
tst "roundtrip initFromBytesBE and toByteArrayBE": tst "roundtrip initFromBytesBE and toByteArrayBE":
chkRoundtripBE(chk, "x", 8)
chkRoundtripBE(chk, "xy", 16)
chkRoundtripBE(chk, "xyzw", 32)
chkRoundtripBE(chk, "xyzwabcd", 64) chkRoundtripBE(chk, "xyzwabcd", 64)
chkRoundtripBE(chk, "xyzwabcd12345678", 128) chkRoundtripBE(chk, "xyzwabcd12345678", 128)
chkRoundtripBE(chk, "xyzwabcd12345678kilimanjarohello", 256) chkRoundtripBE(chk, "xyzwabcd12345678kilimanjarohello", 256)
tst "[stuint] dumpHex": tst "[stuint] dumpHex":
chkDumpHexStuint(chk, "ab", "ab", 8)
chkDumpHexStuint(chk, "00ab", "ab00", 16)
chkDumpHexStuint(chk, "abcd", "cdab", 16)
chkDumpHexStuint(chk, "000000ab", "ab000000", 32)
chkDumpHexStuint(chk, "3412abcd", "cdab1234", 32)
chkDumpHexStuint(chk, "00000000000000ab", "ab00000000000000", 64) chkDumpHexStuint(chk, "00000000000000ab", "ab00000000000000", 64)
chkDumpHexStuint(chk, "abcdef0012345678", "7856341200efcdab", 64) chkDumpHexStuint(chk, "abcdef0012345678", "7856341200efcdab", 64)
chkDumpHexStuint(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128) chkDumpHexStuint(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128)
tst "[stint] dumpHex": tst "[stint] dumpHex":
chkDumpHexStint(chk, "ab", "ab", 8)
chkDumpHexStint(chk, "00ab", "ab00", 16)
chkDumpHexStint(chk, "abcd", "cdab", 16)
chkDumpHexStint(chk, "000000ab", "ab000000", 32)
chkDumpHexStint(chk, "3412abcd", "cdab1234", 32)
chkDumpHexStint(chk, "00000000000000ab", "ab00000000000000", 64) chkDumpHexStint(chk, "00000000000000ab", "ab00000000000000", 64)
chkDumpHexStint(chk, "abcdef0012345678", "7856341200efcdab", 64) chkDumpHexStint(chk, "abcdef0012345678", "7856341200efcdab", 64)
chkDumpHexStint(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128) chkDumpHexStint(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128)
static: static:
testIO(ctCheck, ctTest) testIO(ctCheck, ctTest, ctExpect)
proc main() = proc main() =
# Nim GC protests we are using too much global variables # Nim GC protests we are using too much global variables
# so put it in a proc # so put it in a proc
suite "Testing input and output procedures": suite "Testing input and output procedures":
testIO(check, test) testIO(check, test, expect)
# dumpHex # dumpHex
test "toByteArrayBE CT vs RT": test "toByteArrayBE CT vs RT":
chkCTvsRT(check, 0xab'u8, 8)
chkCTvsRT(check, 0xab'u16, 16)
chkCTvsRT(check, 0xabcd'u16, 16)
chkCTvsRT(check, 0xab'u32, 32)
chkCTvsRT(check, 0xabcd'u32, 32)
chkCTvsRT(check, 0xabcdef12'u32, 32)
chkCTvsRT(check, 0xab'u64, 64) chkCTvsRT(check, 0xab'u64, 64)
chkCTvsRT(check, 0xabcd'u64, 64) chkCTvsRT(check, 0xabcd'u64, 64)
chkCTvsRT(check, 0xabcdef12'u64, 64) chkCTvsRT(check, 0xabcdef12'u64, 64)
@ -1120,11 +810,11 @@ proc main() =
check: uint64(i) == cast[uint64](a) check: uint64(i) == cast[uint64](a)
block: block:
let a = "0o177777".parse(StInt[16], 8) let a = "0o1777777777777777777777".parse(StInt[64], 8)
let b = (-1'i16).stint(16) let b = (-1'i16).stint(64)
check: a == b check: a == b
check: -1'i16 == cast[int16](a) check: -1'i16 == cast[int64](a)
test "Creation from hex strings": test "Creation from hex strings":
block: block:
@ -1153,20 +843,20 @@ proc main() =
check: a == a3 check: a == a3
block: block:
let a = "0xFFFF".parse(StInt[16], 16) let a = "0xFFFFFFFFFFFFFFFF".parse(StInt[64], 16)
let b = (-1'i16).stint(16) let b = (-1'i16).stint(64)
check: a == b check: a == b
check: -1'i16 == cast[int16](a) check: -1'i16 == cast[int64](a)
block: block:
let a = "0b1234abcdef".parse(StInt[64], 16) let a = "0c1234abcdef".parse(StInt[64], 16)
let b = "0x0b1234abcdef".parse(StInt[64], 16) let b = "0x0c1234abcdef".parse(StInt[64], 16)
let c = 0x0b1234abcdef.stint(64) let c = 0x0c1234abcdef.stint(64)
check: a == b check: a == b
check: a == c check: a == c
test "Conversion to decimal strings": test "Conversion to decimal strings":
block: block:
let a = 1234567891234567890.stint(128) let a = 1234567891234567890.stint(128)
@ -1196,12 +886,12 @@ proc main() =
test "Hex dump": test "Hex dump":
block: block:
let a = 0x1234'i32.stint(32) let a = 0x1234'i32.stint(64)
check: a.dumpHex(bigEndian).toUpperAscii == "00001234" check: a.dumpHex(bigEndian).toUpperAscii == "0000000000001234"
block: block:
let a = 0x1234'i32.stint(32) let a = 0x1234'i32.stint(64)
check: a.dumpHex(littleEndian).toUpperAscii == "34120000" check: a.dumpHex(littleEndian).toUpperAscii == "3412000000000000"
test "Back and forth bigint conversion consistency": test "Back and forth bigint conversion consistency":
block: block:
@ -1252,8 +942,8 @@ proc main() =
test "Parsing an unexpected 0x prefix for a decimal string is a CatchableError and not a defect": test "Parsing an unexpected 0x prefix for a decimal string is a CatchableError and not a defect":
let s = "0x123456" let s = "0x123456"
expect(ValueError): expect(AssertionDefect):
let value = parse(s, StUint[256], 10) discard parse(s, StUint[256], 10)
suite "Testing conversion functions: Hex, Bytes, Endianness using secp256k1 curve": suite "Testing conversion functions: Hex, Bytes, Endianness using secp256k1 curve":