From 680c0a3c94f0bb84a2773bc9a95dc5399b6925fb Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 25 Nov 2018 16:45:35 +0100 Subject: Fix bitreader look-ahead overflow --- tests/tbitreader.nim | 23 +++++++++++++++++++++++ tests/tintegers.nim | 7 +++++++ 2 files changed, 30 insertions(+) (limited to 'tests') diff --git a/tests/tbitreader.nim b/tests/tbitreader.nim index 8285f63..294f6c9 100644 --- a/tests/tbitreader.nim +++ b/tests/tbitreader.nim @@ -49,6 +49,29 @@ suite "bitreader": expect IOError: discard bitReader.readBits(16, uint16) check bitReader.atEnd() + test "readBits (look-ahead overflow)": + let stream = newStringStream() + defer: stream.close() + stream.write(0xAB'u8) + stream.setPosition(0) + + let bitReader = stream.bitReader() + check bitReader.readBits(4, uint16) == 0x000B'u16 + check bitReader.readBits(4, uint16) == 0x000A'u16 + check bitReader.atEnd() + + test "readBits (from buffer composition)": + let stream = newStringStream() + defer: stream.close() + stream.write(0xABCD'u16) + stream.setPosition(0) + + let bitReader = stream.bitReader() + check bitReader.readBits(4, uint16) == 0x000D'u16 + check bitReader.readBits(8, uint16) == 0x00BC'u16 + check bitReader.readBits(4, uint16) == 0x000A'u16 + check bitReader.atEnd() + test "readSeq": let stream = newStringStream() defer: stream.close() diff --git a/tests/tintegers.nim b/tests/tintegers.nim index c77abec..956e4aa 100644 --- a/tests/tintegers.nim +++ b/tests/tintegers.nim @@ -27,6 +27,13 @@ suite "integers": check truncateToUint8(0x00FA'u16) == 0xFA'u8 check truncateToUint8(0xFFFA'u16) == 0xFA'u8 + test "leastSignificantBits": + check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8 + check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8 + check leastSignificantBits(0xFF'u8, 10) == 0xFF'u8 + check leastSignificantBits(0xFFFF'u16, 16) == 0xFFFF'u16 + check leastSignificantBits(0xFFFF'u16, 8) == 0x00FF'u16 + test "chunks iterator": check toSeq(chunks(70, uint32)) == @[(0, 32), (1, 32), (2, 6)] check toSeq(chunks(32, uint16)) == @[(0, 16), (1, 16)] -- cgit v1.2.3