diff --git a/stint/io.nim b/stint/io.nim index adda587..0af29cc 100644 --- a/stint/io.nim +++ b/stint/io.nim @@ -80,7 +80,7 @@ func toInt*(num: Stint or StUint): int {.inline.}= func toUint*(num: Stint or StUint): uint {.inline.}= # Returns as uint. Result is modulo 2^(sizeof(uint)) - num.data.least_significant_word + num.data.least_significant_word.uint func toInt64*(num: Stint or StUint): int64 {.inline.}= # Returns as int64. diff --git a/tests/test_io.nim b/tests/test_io.nim index 30a80f8..8b0e34f 100644 --- a/tests/test_io.nim +++ b/tests/test_io.nim @@ -7,7 +7,7 @@ # # at your option. This file may not be copied, modified, or distributed except according to those terms. -import ../stint, unittest, strutils +import ../stint, unittest, strutils, math suite "Testing input and output procedures": test "Creation from decimal strings": @@ -106,6 +106,39 @@ suite "Testing input and output procedures": check: a.toString == s check: $a == s + test "toInt, toInt64, toUint, toUint64": + block: + let x = 100.stuint(128) + check: + x.toInt == 100 + x.toInt64 == 100'i64 + x.toUint64 == 100'u64 + x.toUint == 100'u + block: + let x = pow(2.stuint(128), 64) + 1 + check: + # x.toInt == 1 # This is undefined + # x.toInt64 == 1'i64 # This is undefined + x.toUint64 == 1'u64 + x.toUint == 1'u + + test "toInt, toInt64, toUint, toUint64 - word size (32/64-it) specific": + when not defined(stint_test): + # stint_test forces word size of 32-bit + # while stint uses uint64 by default. + block: + let x = pow(2.stuint(128), 32) + 1 + when sizeof(int) == 4: # 32-bit machines + check: + x.toUint == 1'u + x.toUint64 == 2'u64^32 + 1 + else: + check: + x.toUint == 2'u^32 + 1 + x.toUint64 == 2'u64^32 + 1 + else: + echo " Skipped when Stint forces uint32 backend in test mode" + suite "Testing conversion functions: Hex, Bytes, Endianness using secp256k1 curve": let