From 25688cd0aa51f5e28c1e55dca1b3a9a73cb3dafa Mon Sep 17 00:00:00 2001 From: cheatfate Date: Thu, 18 Feb 2021 16:39:36 +0200 Subject: [PATCH] Add hexValue tests. --- chronos/streams/chunkstream.nim | 2 +- tests/testasyncstream.nim | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/chronos/streams/chunkstream.nim b/chronos/streams/chunkstream.nim index d5f439e..bca01ac 100644 --- a/chronos/streams/chunkstream.nim +++ b/chronos/streams/chunkstream.nim @@ -36,7 +36,7 @@ proc LT(x, y: uint32): uint32 {.inline.} = let z = x - y (z xor ((y xor x) and (y xor z))) shr 31 -proc hexValue(c: byte): int = +proc hexValue*(c: byte): int = # This is nim adaptation of # https://github.com/pornin/CTTK/blob/master/src/hex.c#L28-L52 let x = uint32(c) - 0x30'u32 diff --git a/tests/testasyncstream.nim b/tests/testasyncstream.nim index 9bce6bf..12b445f 100644 --- a/tests/testasyncstream.nim +++ b/tests/testasyncstream.nim @@ -629,6 +629,19 @@ suite "ChunkedStream test suite": result = res check waitFor(testVectors2(initTAddress("127.0.0.1:46001"))) == true + test "ChunkedStream hex decoding test": + for i in 0 ..< 256: + let ch = char(i) + case ch + of '0' .. '9': + check hexValue(byte(ch)) == ord(ch) - ord('0') + of 'a' .. 'f': + check hexValue(byte(ch)) == ord(ch) - ord('a') + 10 + of 'A' .. 'F': + check hexValue(byte(ch)) == ord(ch) - ord('A') + 10 + else: + check hexValue(byte(ch)) == -1 + test "ChunkedStream leaks test": check: getTracker("async.stream.reader").isLeaked() == false