2018-04-25 19:21:25 +00:00
|
|
|
# Stint
|
2023-06-14 02:41:01 +00:00
|
|
|
# Copyright 2018-2023 Status Research & Development GmbH
|
2018-03-02 10:48:08 +00:00
|
|
|
# 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.
|
2018-02-16 16:48:54 +00:00
|
|
|
|
2019-10-23 02:20:06 +00:00
|
|
|
import ../stint, unittest, test_helpers
|
2018-02-16 16:48:54 +00:00
|
|
|
|
2019-10-22 10:44:43 +00:00
|
|
|
template chkLT(chk: untyped, a, b: string, bits: int) =
|
2022-02-24 19:09:53 +00:00
|
|
|
chk fromHex(StUint[bits], a) < fromHex(StUint[bits], b)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
2022-02-24 19:09:53 +00:00
|
|
|
template chkNotLT(chk: untyped, a, b: string, bits: int) =
|
|
|
|
chk (not(fromHex(StUint[bits], b) < fromHex(StUint[bits], a)))
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
template chkLTE(chk: untyped, a, b: string, bits: int) =
|
2022-02-24 19:09:53 +00:00
|
|
|
chk fromHex(StUint[bits], a) <= fromHex(StUint[bits], b)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
2022-02-24 19:09:53 +00:00
|
|
|
template chkNotLTE(chk: untyped, a, b: string, bits: int) =
|
|
|
|
chk (not(fromHex(StUint[bits], b) <= fromHex(StUint[bits], a)))
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
template chkEQ(chk: untyped, a, b: string, bits: int) =
|
2022-02-24 19:09:53 +00:00
|
|
|
chk fromHex(StUint[bits], a) == fromHex(StUint[bits], b)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
2022-02-24 19:09:53 +00:00
|
|
|
template chkNotEQ(chk: untyped, a, b: string, bits: int) =
|
|
|
|
chk (not(fromHex(StUint[bits], a) == fromHex(StUint[bits], b)))
|
2019-10-22 10:44:43 +00:00
|
|
|
|
2022-02-24 19:09:53 +00:00
|
|
|
template chkIsZero(chk: untyped, a: string, bits: int) =
|
|
|
|
chk fromHex(StUint[bits], a).isZero()
|
2019-10-22 10:44:43 +00:00
|
|
|
|
2022-02-24 19:09:53 +00:00
|
|
|
template chkNotIsZero(chk: untyped, a: string, bits: int) =
|
|
|
|
chk (not fromHex(StUint[bits], a).isZero())
|
2019-10-22 10:44:43 +00:00
|
|
|
|
2022-02-24 19:09:53 +00:00
|
|
|
template chkIsOdd(chk: untyped, a: string, bits: int) =
|
|
|
|
chk fromHex(StUint[bits], a).isOdd()
|
2019-10-22 10:44:43 +00:00
|
|
|
|
2022-02-24 19:09:53 +00:00
|
|
|
template chkNotIsOdd(chk: untyped, a: string, bits: int) =
|
|
|
|
chk (not fromHex(StUint[bits], a).isOdd())
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
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)
|
2023-06-14 02:41:01 +00:00
|
|
|
chkLT(chk, "FFFFF", "FFFFFFFF", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
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`":
|
2022-02-24 19:09:53 +00:00
|
|
|
chkNotLT(chk, "0", "F", 64)
|
|
|
|
chkNotLT(chk, "F", "FF", 64)
|
|
|
|
chkNotLT(chk, "FF", "FFF", 64)
|
|
|
|
chkNotLT(chk, "FFFF", "FFFFF", 64)
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotLT(chk, "FFFFF", "FFFFFFFF", 64)
|
2022-02-24 19:09:53 +00:00
|
|
|
|
|
|
|
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)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
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)
|
2023-06-14 02:41:01 +00:00
|
|
|
chkLTE(chk, "FFFFFFFF", "FFFFFFFF", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
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`":
|
2022-02-24 19:09:53 +00:00
|
|
|
chkNotLTE(chk, "0", "F", 64)
|
|
|
|
chkNotLTE(chk, "F", "FF", 64)
|
|
|
|
chkNotLTE(chk, "FF", "FFF", 64)
|
|
|
|
chkNotLTE(chk, "FFFF", "FFFFF", 64)
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotLTE(chk, "FFFFF", "FFFFFFFF", 64)
|
2022-02-24 19:09:53 +00:00
|
|
|
|
|
|
|
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)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
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)
|
2023-06-14 02:41:01 +00:00
|
|
|
chkEQ(chk, "FFFFFFFF", "FFFFFFFF", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
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`":
|
2022-02-24 19:09:53 +00:00
|
|
|
chkNotEQ(chk, "0", "F", 64)
|
|
|
|
chkNotEQ(chk, "F", "FF", 64)
|
|
|
|
chkNotEQ(chk, "FF", "FFF", 64)
|
|
|
|
chkNotEQ(chk, "FFFF", "FFFFF", 64)
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotEQ(chk, "FFFFF", "FFFFFFFF", 64)
|
2022-02-24 19:09:53 +00:00
|
|
|
|
|
|
|
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)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
tst "operator `isZero`":
|
2023-06-14 02:41:01 +00:00
|
|
|
chkIsZero(chk, "0", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkIsZero(chk, "0", 128)
|
|
|
|
chkIsZero(chk, "0", 256)
|
|
|
|
|
|
|
|
tst "operator not `isZero`":
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotIsZero(chk, "4", 64)
|
2022-02-24 19:09:53 +00:00
|
|
|
chkNotIsZero(chk, "5", 128)
|
|
|
|
chkNotIsZero(chk, "6", 256)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
tst "operator `isOdd`":
|
2023-06-14 02:41:01 +00:00
|
|
|
chkIsOdd(chk, "1", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkIsOdd(chk, "1", 128)
|
|
|
|
chkIsOdd(chk, "1", 256)
|
|
|
|
|
2023-06-14 02:41:01 +00:00
|
|
|
chkIsOdd(chk, "FFFFFF", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkIsOdd(chk, "FFFFFFFFFFFFFFF", 128)
|
|
|
|
chkIsOdd(chk, "FFFFFFFFFFFFFFFFFF", 256)
|
|
|
|
|
|
|
|
tst "operator not `isOdd`":
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotIsOdd(chk, "0", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkNotIsOdd(chk, "0", 128)
|
|
|
|
chkNotIsOdd(chk, "0", 256)
|
|
|
|
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotIsOdd(chk, "4", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkNotIsOdd(chk, "4", 128)
|
|
|
|
chkNotIsOdd(chk, "4", 256)
|
|
|
|
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotIsOdd(chk, "FFFFFA", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkNotIsOdd(chk, "FFFFFFFFFFFFFFA", 128)
|
|
|
|
chkNotIsOdd(chk, "FFFFFFFFFFFFFFFFFA", 256)
|
|
|
|
|
|
|
|
tst "operator `isEven`":
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotIsOdd(chk, "0", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkNotIsOdd(chk, "0", 128)
|
|
|
|
chkNotIsOdd(chk, "0", 256)
|
|
|
|
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotIsOdd(chk, "4", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkNotIsOdd(chk, "4", 128)
|
|
|
|
chkNotIsOdd(chk, "4", 256)
|
|
|
|
|
2023-06-14 02:41:01 +00:00
|
|
|
chkNotIsOdd(chk, "FFFFFA", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkNotIsOdd(chk, "FFFFFFFFFFFFFFA", 128)
|
|
|
|
chkNotIsOdd(chk, "FFFFFFFFFFFFFFFFFA", 256)
|
|
|
|
|
|
|
|
tst "operator not `isEven`":
|
2023-06-14 02:41:01 +00:00
|
|
|
chkIsOdd(chk, "1", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkIsOdd(chk, "1", 128)
|
|
|
|
chkIsOdd(chk, "1", 256)
|
|
|
|
|
2023-06-14 02:41:01 +00:00
|
|
|
chkIsOdd(chk, "FFFFFF", 64)
|
2019-10-22 10:44:43 +00:00
|
|
|
chkIsOdd(chk, "FFFFFFFFFFFFFFF", 128)
|
|
|
|
chkIsOdd(chk, "FFFFFFFFFFFFFFFFFF", 256)
|
|
|
|
|
|
|
|
static:
|
2019-10-23 02:20:06 +00:00
|
|
|
testComparison(ctCheck, ctTest)
|
2019-10-22 10:44:43 +00:00
|
|
|
|
|
|
|
suite "Wider unsigned int comparison coverage":
|
|
|
|
testComparison(check, test)
|
|
|
|
|
2018-04-25 19:21:25 +00:00
|
|
|
suite "Testing unsigned int comparison operators":
|
2018-02-16 16:48:54 +00:00
|
|
|
let
|
2023-06-14 02:41:01 +00:00
|
|
|
a = 10.stuint(64)
|
|
|
|
b = 15.stuint(64)
|
2023-06-14 02:52:56 +00:00
|
|
|
c = 150'u64
|
2018-04-25 11:36:56 +00:00
|
|
|
d = 4.stuint(128) shl 64
|
|
|
|
e = 4.stuint(128)
|
|
|
|
f = 4.stuint(128) shl 65
|
2018-02-16 16:48:54 +00:00
|
|
|
|
|
|
|
test "< operator":
|
2018-04-21 10:12:05 +00:00
|
|
|
check:
|
|
|
|
a < b
|
|
|
|
not (a + b < b)
|
|
|
|
not (a + a + a < b + b)
|
2023-06-14 02:41:01 +00:00
|
|
|
not (a * b < cast[StUint[64]](c))
|
2018-04-21 10:12:05 +00:00
|
|
|
e < d
|
|
|
|
d < f
|
2018-02-16 16:48:54 +00:00
|
|
|
|
|
|
|
test "<= operator":
|
2018-04-21 10:12:05 +00:00
|
|
|
check:
|
|
|
|
a <= b
|
|
|
|
not (a + b <= b)
|
|
|
|
a + a + a <= b + b
|
2023-06-14 02:41:01 +00:00
|
|
|
a * b <= cast[StUint[64]](c)
|
2018-04-21 10:12:05 +00:00
|
|
|
e <= d
|
|
|
|
d <= f
|
2018-02-16 16:48:54 +00:00
|
|
|
|
|
|
|
test "> operator":
|
2018-04-21 10:12:05 +00:00
|
|
|
check:
|
|
|
|
b > a
|
|
|
|
not (b > a + b)
|
|
|
|
not (b + b > a + a + a)
|
2023-06-14 02:41:01 +00:00
|
|
|
not (cast[StUint[64]](c) > a * b)
|
2018-04-21 10:12:05 +00:00
|
|
|
d > e
|
|
|
|
f > d
|
2018-02-16 16:48:54 +00:00
|
|
|
|
|
|
|
test ">= operator":
|
2018-04-21 10:12:05 +00:00
|
|
|
check:
|
|
|
|
b >= a
|
|
|
|
not (b >= a + b)
|
|
|
|
b + b >= a + a + a
|
2023-06-14 02:41:01 +00:00
|
|
|
cast[StUint[64]](c) >= a * b
|
2018-04-21 10:12:05 +00:00
|
|
|
d >= e
|
|
|
|
f >= d
|
2018-05-16 08:41:46 +00:00
|
|
|
|
|
|
|
test "isOdd/isEven":
|
|
|
|
check:
|
|
|
|
a.isEven
|
|
|
|
not a.isOdd
|
|
|
|
b.isOdd
|
|
|
|
not b.isEven
|
2020-06-13 15:10:26 +00:00
|
|
|
# c.isEven
|
2023-06-14 02:41:01 +00:00
|
|
|
# not c.isOdd
|