add compiletime test for signed int dumphex
This commit is contained in:
parent
8cc9116d1e
commit
7fe4db62dc
|
@ -0,0 +1,23 @@
|
|||
import ./datatypes
|
||||
|
||||
# this module should be in compiletime_helpers
|
||||
# but the cyclic dependency of compiletime_helpers
|
||||
# and int_bitwise_ops make things complicated
|
||||
|
||||
func convertImpl[T: SomeInteger](x: SomeInteger): T {.compileTime.} =
|
||||
cast[T](x)
|
||||
|
||||
func convertImpl[T: IntImpl|UintImpl](x: IntImpl|UintImpl): T {.compileTime.} =
|
||||
result.hi = convertImpl[type(result.hi)](x.hi)
|
||||
result.lo = x.lo
|
||||
|
||||
func convertImpl[T: Stuint|Stint](x: StUint|StInt): T {.compileTime.} =
|
||||
result.data = convertImpl[type(result.data)](x.data)
|
||||
|
||||
template convert*[T](x: Stuint|Stint|UintImpl|IntImpl|SomeInteger): T =
|
||||
when nimvm:
|
||||
# this is a workaround Nim VM inability to cast
|
||||
# something non integer
|
||||
convertImpl[T](x)
|
||||
else:
|
||||
cast[T](x)
|
|
@ -1,22 +1,8 @@
|
|||
import ./datatypes, ./uint_bitwise_ops, ./bitops2_priv
|
||||
import
|
||||
./datatypes, ./uint_bitwise_ops, ./bitops2_priv, ./int_bitwise_ops,
|
||||
./compiletime_cast
|
||||
|
||||
func convertImpl[T: SomeInteger](x: SomeInteger): T {.compileTime.} =
|
||||
cast[T](x)
|
||||
|
||||
func convertImpl[T: IntImpl|UintImpl](x: IntImpl|UintImpl): T {.compileTime.} =
|
||||
result.hi = convertImpl[type(result.hi)](x.hi)
|
||||
result.lo = x.lo
|
||||
|
||||
func convertImpl[T: Stuint|Stint](x: StUint|StInt): T {.compileTime.} =
|
||||
result.data = convertImpl[type(result.data)](x.data)
|
||||
|
||||
template convert*[T](x: Stuint|Stint|UintImpl|IntImpl|SomeInteger): T =
|
||||
when nimvm:
|
||||
# this is a workaround Nim VM inability to cast
|
||||
# something non integer
|
||||
convertImpl[T](x)
|
||||
else:
|
||||
cast[T](x)
|
||||
export compiletime_cast
|
||||
|
||||
func getByte*(x: SomeInteger, pos: int): byte {.compileTime.} =
|
||||
type DT = type x
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#
|
||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||
|
||||
import ./datatypes, ./bitops2_priv, ./uint_bitwise_ops, ./compiletime_helpers
|
||||
import ./datatypes, ./bitops2_priv, ./uint_bitwise_ops, ./compiletime_cast
|
||||
|
||||
func `not`*(x: IntImpl): IntImpl {.inline.}=
|
||||
## Bitwise complement of unsigned integer x
|
||||
|
|
|
@ -97,13 +97,20 @@ template chkCTvsRT(chk: untyped, num: untyped, bits: int) =
|
|||
const yy = toByteArrayBE(xx)
|
||||
chk y == yy
|
||||
|
||||
template chkDumpHex(chk: untyped, BE, LE: string, bits: int) =
|
||||
template chkDumpHexStuint(chk: untyped, BE, LE: string, bits: int) =
|
||||
block:
|
||||
let data = BE
|
||||
let x = fromHex(Stuint[bits], data)
|
||||
chk dumpHex(x, bigEndian) == data
|
||||
chk dumpHex(x, littleEndian) == LE
|
||||
|
||||
template chkDumpHexStint(chk: untyped, BE, LE: string, bits: int) =
|
||||
block:
|
||||
let data = BE
|
||||
let x = fromHex(Stint[bits], data)
|
||||
chk dumpHex(x, bigEndian) == data
|
||||
chk dumpHex(x, littleEndian) == LE
|
||||
|
||||
template testIO(chk, tst: untyped) =
|
||||
tst "[stuint] Creation from native ints":
|
||||
nativeStuint(chk, 0, 8)
|
||||
|
@ -1002,19 +1009,33 @@ template testIO(chk, tst: untyped) =
|
|||
chkRoundtripBE(chk, "xyzwabcd12345678", 128)
|
||||
chkRoundtripBE(chk, "xyzwabcd12345678kilimanjarohello", 256)
|
||||
|
||||
tst "dumpHex":
|
||||
chkDumpHex(chk, "ab", "ab", 8)
|
||||
tst "[stuint] dumpHex":
|
||||
chkDumpHexStuint(chk, "ab", "ab", 8)
|
||||
|
||||
chkDumpHex(chk, "00ab", "ab00", 16)
|
||||
chkDumpHex(chk, "abcd", "cdab", 16)
|
||||
chkDumpHexStuint(chk, "00ab", "ab00", 16)
|
||||
chkDumpHexStuint(chk, "abcd", "cdab", 16)
|
||||
|
||||
chkDumpHex(chk, "000000ab", "ab000000", 32)
|
||||
chkDumpHex(chk, "3412abcd", "cdab1234", 32)
|
||||
chkDumpHexStuint(chk, "000000ab", "ab000000", 32)
|
||||
chkDumpHexStuint(chk, "3412abcd", "cdab1234", 32)
|
||||
|
||||
chkDumpHex(chk, "00000000000000ab", "ab00000000000000", 64)
|
||||
chkDumpHex(chk, "abcdef0012345678", "7856341200efcdab", 64)
|
||||
chkDumpHexStuint(chk, "00000000000000ab", "ab00000000000000", 64)
|
||||
chkDumpHexStuint(chk, "abcdef0012345678", "7856341200efcdab", 64)
|
||||
|
||||
chkDumpHex(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128)
|
||||
chkDumpHexStuint(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128)
|
||||
|
||||
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, "abcdef0012345678", "7856341200efcdab", 64)
|
||||
|
||||
chkDumpHexStint(chk, "abcdef0012345678abcdef1122334455", "5544332211efcdab7856341200efcdab", 128)
|
||||
|
||||
static:
|
||||
testIO(ctCheck, ctTest)
|
||||
|
|
Loading…
Reference in New Issue