Initial tests for bitwise operator

This commit is contained in:
mratsim 2018-12-01 16:32:21 +01:00
parent 1cc299e82e
commit b43e289780
4 changed files with 57 additions and 11 deletions

View File

@ -4,3 +4,7 @@
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import hardy/[ct_primitives, datatypes]
export ct_primitives, datatypes

View File

@ -10,13 +10,9 @@ requires "nim >= 0.18.0"
### Helper functions
proc test(name: string, defaultLang = "c") =
# TODO, don't forget to change defaultLang to `cpp` if the project requires C++
if not dirExists "build":
mkDir "build"
if not dirExists "nimcache":
mkDir "nimcache"
--run
--nimcache: "nimcache"
switch("out", ("./build/" & name))
setCommand defaultLang, "tests/" & name & ".nim"

View File

@ -34,5 +34,11 @@ template hard*(x: static int, T: type BaseUint): HardBase[T] =
## For int literals
(HardBase[T])(x)
template hard*(x: BaseUint): HardBase[type x] =
func hard*[T: BaseUint](x: T): HardBase[T] {.inline.}=
(HardBase[T])(x)
func `$`*[T](x: HardBase[T]): string {.inline.} =
$T(x)
func `$`*(x: HardBool): string {.inline.} =
$bool(x)

View File

@ -5,12 +5,52 @@
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import unittest,
import unittest, random,
../hardy
suite "Your first test suite":
test "Your first test":
block: # independant block of subtest
discard
template undistinct[T](x: HardBase[T]): T =
T(x)
suite "Hardened unsigned integers":
test "High - getting the biggest representable number":
check:
high(HardBase[byte]).undistinct == 0xFF.byte
high(HardBase[uint8]).undistinct == 0xFF.uint8
high(HardBase[uint16]).undistinct == 0xFFFF.uint16
high(HardBase[uint32]).undistinct == 0xFFFFFFFF.uint32
high(HardBase[uint64]).undistinct == 0xFFFFFFFF_FFFFFFFF.uint64
test "bitwise `and`, `or`, `xor`, `not`":
var x1 = rand(high(int)).uint64
var y1 = rand(high(int)).uint64
var x2 = rand(high(int)).uint64
var y2 = rand(high(int)).uint64
var x3 = rand(high(int)).uint64
var y3 = rand(high(int)).uint64
template bitwise_check(op: untyped): untyped =
block:
discard
check:
op(hard(0'u32), hard(0'u32)).undistinct == op(0'u32, 0'u32)
op(hard(0'u32), hard(1'u32)).undistinct == op(0'u32, 1'u32)
op(hard(1234'u64), hard(5678'u64)).undistinct == op(1234'u64, 5678'u64)
op(x1.hard, y1.hard).undistinct == op(x1, y1)
op(x2.hard, y2.hard).undistinct == op(x2, y2)
op(x3.hard, y3.hard).undistinct == op(x3, y3)
bitwise_check(`and`)
bitwise_check(`or`)
bitwise_check(`xor`)
block:
check:
not(hard(0'u32)).undistinct == not 0'u32
not(hard(1'u32)).undistinct == not 1'u32
not(hard(1234'u64)).undistinct == not 1234'u64
not(hard(5678'u64)).undistinct == not 5678'u32
not(hard(x1)).undistinct == not x1
not(hard(x2)).undistinct == not x2
not(hard(x3)).undistinct == not x3
not(hard(y1)).undistinct == not y1
not(hard(y2)).undistinct == not y2
not(hard(y3)).undistinct == not y3