2018-04-25 19:21:25 +00:00
|
|
|
# Stint
|
2018-03-02 10:48:08 +00:00
|
|
|
# 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.
|
2018-02-16 12:54:38 +00:00
|
|
|
|
2018-04-25 15:50:53 +00:00
|
|
|
import ../src/stint, unittest
|
2018-02-16 12:54:38 +00:00
|
|
|
|
2018-04-25 19:21:25 +00:00
|
|
|
suite "Testing unsigned int bitwise operations":
|
2018-04-25 11:36:56 +00:00
|
|
|
let a = 100'i16.stuint(16)
|
2018-02-16 12:54:38 +00:00
|
|
|
|
|
|
|
let b = a * a
|
|
|
|
let z = 10000'u16
|
2018-03-28 18:45:39 +00:00
|
|
|
assert cast[uint16](b) == z, "Test cannot proceed, something is wrong with the multiplication implementation"
|
2018-02-16 12:54:38 +00:00
|
|
|
|
2018-04-20 09:13:47 +00:00
|
|
|
|
2018-04-25 11:36:56 +00:00
|
|
|
let u = 10000.stuint(64)
|
2018-04-20 09:13:47 +00:00
|
|
|
let v = 10000'u64
|
|
|
|
let clz = 30
|
|
|
|
|
2018-02-16 12:54:38 +00:00
|
|
|
test "Shift left - by less than half the size of the integer":
|
|
|
|
check: cast[uint16](b) == z # Sanity check
|
|
|
|
check: cast[uint16](b shl 2) == z shl 2
|
|
|
|
|
|
|
|
test "Shift left - by more than half the size of the integer":
|
|
|
|
check: cast[uint16](b) == z # Sanity check
|
|
|
|
check: cast[uint16](b shl 10) == z shl 10
|
|
|
|
|
2018-04-20 09:13:47 +00:00
|
|
|
check: cast[uint64](u shl clz) == v shl clz
|
|
|
|
|
2018-02-16 12:54:38 +00:00
|
|
|
test "Shift left - by half the size of the integer":
|
|
|
|
check: cast[uint16](b) == z # Sanity check
|
|
|
|
check: cast[uint16](b shl 8) == z shl 8
|
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
block: # Testing shl for nested UintImpl
|
|
|
|
let p2_64 = UintImpl[uint64](hi:1, lo:0)
|
2018-04-25 11:36:56 +00:00
|
|
|
let p = 1.stuint(128) shl 64
|
2018-04-20 09:13:47 +00:00
|
|
|
|
2018-04-25 10:52:00 +00:00
|
|
|
check: p == cast[StUint[128]](p2_64)
|
2018-04-20 09:13:47 +00:00
|
|
|
|
2018-02-16 12:54:38 +00:00
|
|
|
test "Shift right - by less than half the size of the integer":
|
|
|
|
check: cast[uint16](b) == z # Sanity check
|
|
|
|
check: cast[uint16](b shr 2) == z shr 2
|
|
|
|
|
|
|
|
test "Shift right - by more than half the size of the integer":
|
|
|
|
check: cast[uint16](b) == z # Sanity check
|
|
|
|
check: cast[uint16](b shr 10) == z shr 10
|
|
|
|
|
|
|
|
test "Shift right - by half the size of the integer":
|
|
|
|
check: cast[uint16](b) == z # Sanity check
|
|
|
|
check: cast[uint16](b shr 8) == z shr 8
|