2019-07-22 07:53:58 +00:00
|
|
|
# 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.
|
|
|
|
|
2019-10-23 02:20:06 +00:00
|
|
|
import ../stint, unittest, stew/byteutils, test_helpers
|
2019-10-09 03:37:30 +00:00
|
|
|
|
|
|
|
template chkToBytesLE(chk: untyped, bits: int, hex: string) =
|
|
|
|
let x = fromHex(StUint[bits], hex)
|
|
|
|
chk toBytes(x, littleEndian).toHex() == x.dumpHex(littleEndian)
|
|
|
|
|
|
|
|
template chkToBytesBE(chk: untyped, bits: int, hex: string) =
|
|
|
|
let x = fromHex(StUint[bits], hex)
|
|
|
|
chk toBytes(x, bigEndian).toHex() == x.dumpHex(bigEndian)
|
|
|
|
|
|
|
|
|
|
|
|
template chkFromBytesBE(chk: untyped, bits: int, hex: string) =
|
|
|
|
let x = fromHex(StUint[bits], hex)
|
2023-06-12 14:07:15 +00:00
|
|
|
let z = fromBytesBE(StUint[bits], toByteArrayBE(x))
|
2019-10-09 03:37:30 +00:00
|
|
|
chk z == x
|
|
|
|
|
|
|
|
template chkFromBytesLE(chk: untyped, bits: int, hex: string) =
|
|
|
|
let x = fromHex(StUint[bits], hex)
|
2023-06-20 12:45:49 +00:00
|
|
|
let z = fromBytesLE(StUint[bits], toByteArrayLE(x))
|
2019-10-09 03:37:30 +00:00
|
|
|
chk z == x
|
|
|
|
|
|
|
|
template chkEndians(chkFunc, tst, name: untyped) =
|
|
|
|
tst astToStr(name).substr(3):
|
2023-06-20 12:45:49 +00:00
|
|
|
name(chkFunc, 64, "abcdef1234567890")
|
2019-10-09 03:37:30 +00:00
|
|
|
name(chkFunc, 128, "abcdef1234567890abcdef1234567890")
|
|
|
|
name(chkFunc, 256, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890")
|
|
|
|
|
|
|
|
template testEndians(chkFunc, tst: untyped) =
|
2023-06-20 12:45:49 +00:00
|
|
|
chkEndians(chkFunc, tst, chkToBytesLE)
|
2019-10-09 03:37:30 +00:00
|
|
|
chkEndians(chkFunc, tst, chkToBytesBE)
|
2023-06-20 12:45:49 +00:00
|
|
|
chkEndians(chkFunc, tst, chkFromBytesLE)
|
|
|
|
chkEndians(chkFunc, tst, chkFromBytesBE)
|
2019-10-09 03:37:30 +00:00
|
|
|
|
|
|
|
static:
|
2019-10-23 02:20:06 +00:00
|
|
|
testEndians(ctCheck, ctTest)
|
2019-07-22 07:53:58 +00:00
|
|
|
|
|
|
|
suite "Testing endians":
|
|
|
|
test "Endians give sane results":
|
|
|
|
|
|
|
|
check:
|
2023-06-12 14:07:15 +00:00
|
|
|
1.u128.toByteArrayBE() ==
|
2019-07-22 07:53:58 +00:00
|
|
|
[0'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
|
|
|
|
|
2023-06-20 12:45:49 +00:00
|
|
|
1.u128.toByteArrayLE() ==
|
|
|
|
[1'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
2019-07-22 07:53:58 +00:00
|
|
|
|
|
|
|
1.u128 == UInt128.fromBytesBE(
|
|
|
|
[0'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
|
|
|
|
|
2023-06-20 12:45:49 +00:00
|
|
|
1.u128 == UInt128.fromBytesLE(
|
|
|
|
[1'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
2019-10-09 03:37:30 +00:00
|
|
|
|
|
|
|
testEndians(check, test)
|