mirror of
https://github.com/status-im/nim-stint.git
synced 2025-02-16 17:07:23 +00:00
Don't use single letter function like u(256)
This commit is contained in:
parent
102a1b0f0c
commit
00a634d2f2
@ -13,7 +13,7 @@ import ./private/uint_type
|
||||
|
||||
import typetraits
|
||||
|
||||
func u*[T: SomeInteger](n: T, bits: static[int]): StUint[bits] {.inline.}=
|
||||
func stuint*[T: SomeInteger](n: T, bits: static[int]): StUint[bits] {.inline.}=
|
||||
assert n >= 0.T
|
||||
when result.data is UintImpl:
|
||||
when getSize(n) > bits:
|
||||
|
@ -16,7 +16,7 @@ type
|
||||
|
||||
template make_conv(conv_name: untyped, size: int): untyped =
|
||||
func `convname`*(n: SomeInteger): StUint[size] {.inline, noInit.}=
|
||||
n.u(size)
|
||||
n.stuint(size)
|
||||
|
||||
make_conv(u128, 128)
|
||||
make_conv(u256, 256)
|
||||
|
@ -8,7 +8,7 @@ srcDir = "src"
|
||||
### Dependencies
|
||||
|
||||
# TODO remove test only requirements: https://github.com/nim-lang/nimble/issues/482
|
||||
requires "nim >= 0.18", "https://github.com/alehander42/nim-quicktest >= 0.0.9", "https://github.com/status-im/nim-ttmath#master"
|
||||
requires "nim >= 0.18", "https://github.com/alehander42/nim-quicktest >= 0.0.9"
|
||||
|
||||
proc test(name: string, lang: string = "c") =
|
||||
if not dirExists "build":
|
||||
@ -40,11 +40,13 @@ task test_property_release, "Run random tests (release mode) - test implementati
|
||||
|
||||
task test_property_uint256_debug, "Run random tests (normal mode) vs TTMath on Uint256":
|
||||
# TODO: another reference implementation?
|
||||
# Note that currently importing both Stint and TTMath will crash the compiler for unknown reason.
|
||||
requires "quicktest > 0.0.8"
|
||||
test "property_based", "cpp"
|
||||
|
||||
task test_property_uint256_release, "Run random tests (release mode) vs TTMath on Uint256":
|
||||
# TODO: another reference implementation?
|
||||
# Note that currently importing both Stint and TTMath will crash the compiler for unknown reason.
|
||||
requires "quicktest > 0.0.8"
|
||||
switch("define", "release")
|
||||
test "property_based", "cpp"
|
||||
|
@ -7,6 +7,9 @@
|
||||
#
|
||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||
|
||||
|
||||
# Requires "https://github.com/status-im/nim-ttmath#master"
|
||||
# Note that currently importing both Stint and TTMath will crash the compiler for unknown reason
|
||||
import ../src/mpint, unittest, quicktest, ttmath
|
||||
|
||||
const itercount = 1000
|
||||
|
@ -12,8 +12,8 @@ import ../src/mpint, unittest
|
||||
suite "Testing addition implementation":
|
||||
test "In-place addition gives expected result":
|
||||
|
||||
var a = 20182018.u(64)
|
||||
let b = 20172017.u(64)
|
||||
var a = 20182018.stuint(64)
|
||||
let b = 20172017.stuint(64)
|
||||
|
||||
a += b
|
||||
|
||||
@ -21,23 +21,23 @@ suite "Testing addition implementation":
|
||||
|
||||
test "Addition gives expected result":
|
||||
|
||||
let a = 20182018.u(64)
|
||||
let b = 20172017.u(64)
|
||||
let a = 20182018.stuint(64)
|
||||
let b = 20172017.stuint(64)
|
||||
|
||||
check: cast[uint64](a+b) == 20182018'u64 + 20172017'u64
|
||||
|
||||
test "When the low half overflows, it is properly carried":
|
||||
# uint8 (low half) overflow at 255
|
||||
let a = 100'u16.u(16)
|
||||
let b = 100'u16.u(16)
|
||||
let a = 100'u16.stuint(16)
|
||||
let b = 100'u16.stuint(16)
|
||||
|
||||
check: cast[uint16](a+b) == 200
|
||||
|
||||
test "Full overflow is handled like native unsigned types":
|
||||
# uint16 overflows after 65535
|
||||
let a = 100'u16.u(16)
|
||||
var z = 0'u16.u(16)
|
||||
let o = 36'u16.u(16)
|
||||
let a = 100'u16.stuint(16)
|
||||
var z = 0'u16.stuint(16)
|
||||
let o = 36'u16.stuint(16)
|
||||
|
||||
for _ in 0 ..< 655:
|
||||
z += a
|
||||
@ -54,8 +54,8 @@ suite "Testing addition implementation":
|
||||
suite "Testing substraction implementation":
|
||||
test "In-place substraction gives expected result":
|
||||
|
||||
var a = 20182018.u(64)
|
||||
let b = 20172017.u(64)
|
||||
var a = 20182018.stuint(64)
|
||||
let b = 20172017.stuint(64)
|
||||
|
||||
a -= b
|
||||
|
||||
@ -63,14 +63,14 @@ suite "Testing substraction implementation":
|
||||
|
||||
test "Substraction gives expected result":
|
||||
|
||||
let a = 20182018.u(64)
|
||||
let b = 20172017.u(64)
|
||||
let a = 20182018.stuint(64)
|
||||
let b = 20172017.stuint(64)
|
||||
|
||||
check: cast[uint64](a-b) == 20182018'u64 - 20172017'u64
|
||||
|
||||
test "Full overflow is handled like native unsigned types":
|
||||
# uint16 overflows after 65535
|
||||
let a = 100'u16.u(16)
|
||||
let b = 101'u16.u(16)
|
||||
let a = 100'u16.stuint(16)
|
||||
let b = 101'u16.stuint(16)
|
||||
|
||||
check: cast[uint16](a-b) == high(uint16)
|
||||
|
@ -10,14 +10,14 @@
|
||||
import ../src/mpint, unittest
|
||||
|
||||
suite "Testing bitwise operations":
|
||||
let a = 100'i16.u(16)
|
||||
let a = 100'i16.stuint(16)
|
||||
|
||||
let b = a * a
|
||||
let z = 10000'u16
|
||||
assert cast[uint16](b) == z, "Test cannot proceed, something is wrong with the multiplication implementation"
|
||||
|
||||
|
||||
let u = 10000.u(64)
|
||||
let u = 10000.stuint(64)
|
||||
let v = 10000'u64
|
||||
let clz = 30
|
||||
|
||||
@ -37,7 +37,7 @@ suite "Testing bitwise operations":
|
||||
|
||||
block: # Testing shl for nested UintImpl
|
||||
let p2_64 = UintImpl[uint64](hi:1, lo:0)
|
||||
let p = 1.u(128) shl 64
|
||||
let p = 1.stuint(128) shl 64
|
||||
|
||||
check: p == cast[StUint[128]](p2_64)
|
||||
|
||||
|
@ -11,12 +11,12 @@ import ../src/mpint, unittest
|
||||
|
||||
suite "Testing comparison operators":
|
||||
let
|
||||
a = 10'i16.u(16)
|
||||
b = 15'i16.u(16)
|
||||
a = 10'i16.stuint(16)
|
||||
b = 15'i16.stuint(16)
|
||||
c = 150'u16
|
||||
d = 4.u(128) shl 64
|
||||
e = 4.u(128)
|
||||
f = 4.u(128) shl 65
|
||||
d = 4.stuint(128) shl 64
|
||||
e = 4.stuint(128)
|
||||
f = 4.stuint(128) shl 65
|
||||
|
||||
test "< operator":
|
||||
check:
|
||||
|
@ -11,7 +11,7 @@ import ../src/mpint, unittest
|
||||
|
||||
suite "Testing byte representation":
|
||||
test "Byte representation conforms to the platform endianness":
|
||||
let a = 20182018.u(64)
|
||||
let a = 20182018.stuint(64)
|
||||
let b = 20182018'u64
|
||||
|
||||
type AsBytes = array[8, byte]
|
||||
|
@ -12,23 +12,23 @@ import ../src/mpint, unittest
|
||||
suite "Testing multiplication implementation":
|
||||
test "Multiplication with result fitting in low half":
|
||||
|
||||
let a = 10000.u(64)
|
||||
let b = 10000.u(64)
|
||||
let a = 10000.stuint(64)
|
||||
let b = 10000.stuint(64)
|
||||
|
||||
check: cast[uint64](a*b) == 100_000_000'u64 # need 27-bits
|
||||
|
||||
test "Multiplication with result overflowing low half":
|
||||
|
||||
let a = 1_000_000.u(64)
|
||||
let b = 1_000_000.u(64)
|
||||
let a = 1_000_000.stuint(64)
|
||||
let b = 1_000_000.stuint(64)
|
||||
|
||||
check: cast[uint64](a*b) == 1_000_000_000_000'u64 # need 40 bits
|
||||
|
||||
test "Full overflow is handled like native unsigned types":
|
||||
|
||||
let a = 1_000_000_000.u(64)
|
||||
let b = 1_000_000_000.u(64)
|
||||
let c = 1_000.u(64)
|
||||
let a = 1_000_000_000.stuint(64)
|
||||
let b = 1_000_000_000.stuint(64)
|
||||
let c = 1_000.stuint(64)
|
||||
|
||||
check: cast[uint64](a*b*c) == 1_000_000_000_000_000_000_000'u64 # need 70-bits
|
||||
|
||||
@ -36,16 +36,16 @@ suite "Testing multiplication implementation":
|
||||
suite "Testing division and modulo implementation":
|
||||
test "Divmod(100, 13) returns the correct result":
|
||||
|
||||
let a = 100.u(64)
|
||||
let b = 13.u(64)
|
||||
let a = 100.stuint(64)
|
||||
let b = 13.stuint(64)
|
||||
let qr = divmod(a, b)
|
||||
|
||||
check: cast[uint64](qr.quot) == 7'u64
|
||||
check: cast[uint64](qr.rem) == 9'u64
|
||||
|
||||
test "Divmod(2^64, 3) returns the correct result":
|
||||
let a = 1.u(128) shl 64
|
||||
let b = 3.u(128)
|
||||
let a = 1.stuint(128) shl 64
|
||||
let b = 3.stuint(128)
|
||||
|
||||
let qr = divmod(a, b)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user