mirror of
https://github.com/status-im/nim-stint.git
synced 2025-02-16 17:07:23 +00:00
* Add serialization for decimal and hex * Fix carry bug in signed add * Add parsing test * Improve highlow for int, remove most_significant_word_mut * make conversion toString compile * Add division corner case test * Remove a buggy division shortcut * Fix decimal string conversion * Fix hex dumping * Fix power of 2 division (what was I thinking?) * Add hexdump test * Move runtime check to compile-time * Fix static assert check * more compile-time asserts * Fix parsing of negative hex numbers, add test_io to the suite * dump default to bigEndian, split toString in Stint and Stuint * Add (failing) tests with big ints conversion * Temporarily remove all the noInit pragma
100 lines
2.7 KiB
Nim
100 lines
2.7 KiB
Nim
# Stint
|
|
# 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.
|
|
|
|
import ../src/stint, unittest, strutils
|
|
|
|
suite "Testing input and output procedures":
|
|
test "Creation from decimal strings":
|
|
block:
|
|
let a = parse(Stint[64], "123456789")
|
|
let b = 123456789.stint(64)
|
|
|
|
check: a == b
|
|
check: 123456789'i64 == cast[int64](a)
|
|
|
|
block:
|
|
let a = parse(Stuint[64], "123456789")
|
|
let b = 123456789.stuint(64)
|
|
|
|
check: a == b
|
|
check: 123456789'u64 == cast[uint64](a)
|
|
|
|
block:
|
|
let a = parse(Stint[64], "-123456789")
|
|
let b = (-123456789).stint(64)
|
|
|
|
check: a == b
|
|
check: -123456789'i64 == cast[int64](a)
|
|
|
|
test "Creation from hex strings":
|
|
block:
|
|
let a = parse(Stint[64], "0xFF", 16)
|
|
let b = 255.stint(64)
|
|
|
|
check: a == b
|
|
check: 255'i64 == cast[int64](a)
|
|
|
|
block:
|
|
let a = parse(Stuint[64], "0xFF", 16)
|
|
let b = 255.stuint(64)
|
|
|
|
check: a == b
|
|
check: 255'u64 == cast[uint64](a)
|
|
|
|
block:
|
|
let a = parse(Stint[16], "0xFFFF", 16)
|
|
let b = (-1'i16).stint(16)
|
|
|
|
check: a == b
|
|
check: -1'i16 == cast[int16](a)
|
|
|
|
test "Conversion to decimal strings":
|
|
block:
|
|
let a = 1234567891234567890.stint(128)
|
|
check: a.toString == "1234567891234567890"
|
|
|
|
block:
|
|
let a = 1234567891234567890.stuint(128)
|
|
check: a.toString == "1234567891234567890"
|
|
|
|
block:
|
|
let a = (-1234567891234567890).stint(128)
|
|
check: a.toString == "-1234567891234567890"
|
|
|
|
test "Conversion to hex strings":
|
|
block:
|
|
let a = 0x1234567890ABCDEF.stint(128)
|
|
check: a.toString(base = 16).toUpperAscii == "1234567890ABCDEF"
|
|
|
|
block:
|
|
let a = 0x1234567890ABCDEF.stuint(128)
|
|
check: a.toString(base = 16).toUpperAscii == "1234567890ABCDEF"
|
|
|
|
# TODO: negative hex
|
|
|
|
test "Hex dump":
|
|
block:
|
|
let a = 0x1234'i32.stint(32)
|
|
check: a.dumpHex(bigEndian).toUpperAscii == "00001234"
|
|
|
|
block:
|
|
let a = 0x1234'i32.stint(32)
|
|
check: a.dumpHex(littleEndian).toUpperAscii == "34120000"
|
|
|
|
test "Back and forth bigint conversion consistency":
|
|
block:
|
|
let s = "1234567890123456789012345678901234567890123456789"
|
|
let a = parse(StInt[512], s)
|
|
check: a.toString == s
|
|
|
|
block:
|
|
let s = "1234567890123456789012345678901234567890123456789"
|
|
let a = parse(StUInt[512], s)
|
|
check: a.toString == s
|