Fix issue with `uint` type. (#79)

* Fix issue with `uint` type.
Add tests for `uint` type.

* Add decode tests for `uint` type.
This commit is contained in:
Eugene Kabanov 2021-04-09 19:01:50 +03:00 committed by GitHub
parent ee78822e05
commit ede0651741
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -79,7 +79,8 @@ proc encodedLength*(B: typedesc[Base10], value: SomeUnsignedInt): int8 =
if value < 10000'u16: if value < 10000'u16:
return 4'i8 return 4'i8
5'i8 5'i8
elif type(value) is uint32: elif (type(value) is uint32) or
((type(value) is uint) and (sizeof(uint) == 4)):
const const
P04 = 1_0000'u32 P04 = 1_0000'u32
P05 = 1_0000_0'u32 P05 = 1_0000_0'u32
@ -100,7 +101,8 @@ proc encodedLength*(B: typedesc[Base10], value: SomeUnsignedInt): int8 =
return 5'i8 + (if value >= P05: 1'i8 else: 0'i8) return 5'i8 + (if value >= P05: 1'i8 else: 0'i8)
return 7'i8 + (if value >= P07: 1'i8 else: 0'i8) return 7'i8 + (if value >= P07: 1'i8 else: 0'i8)
9'i8 + (if value >= P09: 1'i8 else: 0'i8) 9'i8 + (if value >= P09: 1'i8 else: 0'i8)
elif type(value) is uint64: elif (type(value) is uint64) or
((type(value) is uint) and (sizeof(uint) == 8)):
const const
P04 = 1_0000'u64 P04 = 1_0000'u64
P05 = 1_0000_0'u64 P05 = 1_0000_0'u64

View File

@ -158,6 +158,44 @@ template testEdge(T: typedesc[SomeUnsignedInt]) =
r3.isErr() r3.isErr()
r4.isErr() r4.isErr()
template testHigh() =
check:
Base10.toString(uint8(high(int8))) == "127"
Base10.toString(high(uint8)) == "255"
Base10.toString(uint16(high(int16))) == "32767"
Base10.toString(high(uint16)) == "65535"
Base10.toString(uint32(high(int32))) == "2147483647"
Base10.toString(high(uint32)) == "4294967295"
Base10.toString(uint64(high(int64))) == "9223372036854775807"
Base10.toString(high(uint64)) == "18446744073709551615"
Base10.decode(uint8, "127").tryGet() == 127'u8
Base10.decode(uint8, "255").tryGet() == 255'u8
Base10.decode(uint16, "32767").tryGet() == 32767'u16
Base10.decode(uint16, "65535").tryGet() == 65535'u16
Base10.decode(uint32, "2147483647").tryGet() == 2147483647'u32
Base10.decode(uint32, "4294967295").tryGet() == 4294967295'u32
Base10.decode(uint64, "9223372036854775807").tryGet() ==
9223372036854775807'u64
Base10.decode(uint64, "18446744073709551615").tryGet() ==
18446744073709551615'u64
when sizeof(uint) == 8:
check:
Base10.toString(uint(high(int))) == "9223372036854775807"
Base10.toString(high(uint)) == "18446744073709551615"
Base10.decode(uint, "9223372036854775807").tryGet() ==
9223372036854775807'u
Base10.decode(uint, "18446744073709551615").tryGet() ==
18446744073709551615'u
elif sizeof(uint) == 4:
check:
Base10.toString(uint(high(int))) == "2147483647"
Base10.toString(high(uint)) == "4294967295"
Base10.decode(uint, "2147483647").tryGet() == 2147483647'u
Base10.decode(uint, "4294967295").tryGet() == 4294967295'u
else:
skip()
suite "Base10 (decimal) test suite": suite "Base10 (decimal) test suite":
test "[uint8] encode/decode/length test": test "[uint8] encode/decode/length test":
testVectors(uint8) testVectors(uint8)
@ -167,6 +205,8 @@ suite "Base10 (decimal) test suite":
testVectors(uint32) testVectors(uint32)
test "[uint64] encode/decode/length test": test "[uint64] encode/decode/length test":
testVectors(uint64) testVectors(uint64)
test "[uint] encode/decode/length test":
testVectors(uint)
test "[uint8] all values comparison test": test "[uint8] all values comparison test":
testValues(uint8) testValues(uint8)
test "[uint16] all values comparison test": test "[uint16] all values comparison test":
@ -175,6 +215,8 @@ suite "Base10 (decimal) test suite":
testValues(uint32) testValues(uint32)
test "[uint64] 100,000 values comparison test": test "[uint64] 100,000 values comparison test":
testValues(uint64) testValues(uint64)
test "[uint] 100,000 values comparison test":
testValues(uint)
test "[uint8] edge cases": test "[uint8] edge cases":
testEdge(uint8) testEdge(uint8)
test "[uint16] edge cases": test "[uint16] edge cases":
@ -183,3 +225,7 @@ suite "Base10 (decimal) test suite":
testEdge(uint32) testEdge(uint32)
test "[uint64] edge cases": test "[uint64] edge cases":
testEdge(uint64) testEdge(uint64)
test "[uint] edge cases":
testEdge(uint)
test "high() values test":
testHigh()