diff --git a/hardy.nim b/hardy.nim index 92d5105..cce022d 100644 --- a/hardy.nim +++ b/hardy.nim @@ -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 + diff --git a/hardy.nimble b/hardy.nimble index a176efe..dcab1e7 100644 --- a/hardy.nimble +++ b/hardy.nimble @@ -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" diff --git a/hardy/datatypes.nim b/hardy/datatypes.nim index a215fa9..ad72c0b 100644 --- a/hardy/datatypes.nim +++ b/hardy/datatypes.nim @@ -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) diff --git a/tests/all_tests.nim b/tests/all_tests.nim index 4255e6e..f1dcf1e 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -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: + 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: - discard + 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