mirror of
https://github.com/status-im/nim-stint.git
synced 2025-02-19 18:38:13 +00:00
Add comparison operators + tests + fix shr forward declaration
This commit is contained in:
parent
f044d8a476
commit
834ff19a25
@ -4,9 +4,11 @@
|
||||
import ./uint_type,
|
||||
./uint_init,
|
||||
./uint_bitwise_ops,
|
||||
./uint_binary_ops
|
||||
./uint_binary_ops,
|
||||
./uint_comparison
|
||||
|
||||
export uint_type,
|
||||
uint_init,
|
||||
uint_bitwise_ops,
|
||||
uint_binary_ops
|
||||
uint_binary_ops,
|
||||
uint_comparison
|
@ -2,7 +2,8 @@
|
||||
# Distributed under the MIT License (license terms are at http://opensource.org/licenses/MIT).
|
||||
|
||||
import ./private/utils,
|
||||
uint_type
|
||||
uint_type,
|
||||
uint_comparison
|
||||
|
||||
proc `+=`*[T: MpUint](x: var T, y: T) {.noSideEffect.}=
|
||||
## In-place addition for multi-precision unsigned int
|
||||
|
@ -25,7 +25,7 @@ proc `xor`*(x, y: MpUint): MpUint {.noInit, noSideEffect, inline.}=
|
||||
result.lo = x.lo xor y.lo
|
||||
result.hi = x.hi xor y.hi
|
||||
|
||||
proc `shl`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}
|
||||
proc `shr`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}
|
||||
# Forward declaration
|
||||
|
||||
proc `shl`*[T: MpUint](x: T, y: SomeInteger): T {.noInit, noSideEffect.}=
|
||||
|
13
src/uint_comparison.nim
Normal file
13
src/uint_comparison.nim
Normal file
@ -0,0 +1,13 @@
|
||||
# Copyright (c) 2018 Status Research & Development GmbH
|
||||
# Distributed under the MIT License (license terms are at http://opensource.org/licenses/MIT).
|
||||
|
||||
import ./uint_type
|
||||
|
||||
proc `<`*[T: MpUint](x, y: T): bool {.noSideEffect, noInit, inline.}=
|
||||
(x.hi < y.hi) or ((x.hi == y.hi) and x.lo < y.lo)
|
||||
|
||||
proc `<=`*[T: MpUint](x, y: T): bool {.noSideEffect, noInit, inline.}=
|
||||
# Lower or equal comparison for multi-precision integers
|
||||
if x == y:
|
||||
return true
|
||||
x < y
|
@ -3,4 +3,5 @@
|
||||
|
||||
import test_endianness,
|
||||
test_addsub,
|
||||
test_mul
|
||||
test_mul,
|
||||
test_comparison
|
34
tests/test_comparison.nim
Normal file
34
tests/test_comparison.nim
Normal file
@ -0,0 +1,34 @@
|
||||
# Copyright (c) 2018 Status Research & Development GmbH
|
||||
# Distributed under the MIT License (license terms are at http://opensource.org/licenses/MIT).
|
||||
|
||||
import ../src/mpint, unittest
|
||||
|
||||
suite "Testing comparison operators":
|
||||
let
|
||||
a = initMpUint(10, uint8)
|
||||
b = initMpUint(15, uint8)
|
||||
c = 150'u16
|
||||
|
||||
test "< operator":
|
||||
check: a < b
|
||||
check: not (a + b < b)
|
||||
check: not (a + a + a < b + b)
|
||||
check: not (a * b < cast[Mpuint[uint8]](c))
|
||||
|
||||
test "<= operator":
|
||||
check: a <= b
|
||||
check: not (a + b <= b)
|
||||
check: a + a + a <= b + b
|
||||
check: a * b <= cast[Mpuint[uint8]](c)
|
||||
|
||||
test "> operator":
|
||||
check: b > a
|
||||
check: not (b > a + b)
|
||||
check: not (b + b > a + a + a)
|
||||
check: not (cast[Mpuint[uint8]](c) > a * b)
|
||||
|
||||
test ">= operator":
|
||||
check: b >= a
|
||||
check: not (b >= a + b)
|
||||
check: b + b >= a + a + a
|
||||
check: cast[Mpuint[uint8]](c) >= a * b
|
Loading…
x
Reference in New Issue
Block a user