From 8af38da097b8358cb273baa37c748120461c718e Mon Sep 17 00:00:00 2001
From: pacien
Date: Fri, 30 Nov 2018 16:57:32 +0100
Subject: isolate bit IO
---
tests/tbitio.nim | 152 ++++++++++++++++++++++++++++++++++++++++++++++
tests/tbitreader.nim | 84 -------------------------
tests/tbitwriter.nim | 85 --------------------------
tests/thuffmandecoder.nim | 2 +-
tests/thuffmanencoder.nim | 2 +-
tests/thuffmantree.nim | 2 +-
tests/trawblock.nim | 2 +-
tests/tstreamblock.nim | 2 +-
8 files changed, 157 insertions(+), 174 deletions(-)
create mode 100644 tests/tbitio.nim
delete mode 100644 tests/tbitreader.nim
delete mode 100644 tests/tbitwriter.nim
(limited to 'tests')
diff --git a/tests/tbitio.nim b/tests/tbitio.nim
new file mode 100644
index 0000000..0391974
--- /dev/null
+++ b/tests/tbitio.nim
@@ -0,0 +1,152 @@
+# gzip-like LZSS compressor
+# Copyright (C) 2018 Pacien TRAN-GIRARD
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+
+import unittest, streams, sugar, sequtils
+import integers
+import bitio/bitreader, bitio/bitwriter
+
+suite "bitreader":
+ test "readBool":
+ let stream = newStringStream()
+ defer: stream.close()
+ stream.write(0b1001_1111'u8)
+ stream.write(0b0110_0000'u8)
+ stream.setPosition(0)
+
+ let bitReader = stream.bitReader()
+ check lc[bitReader.readBool() | (_ <- 0..<16), bool] == @[
+ true, true, true, true, true, false, false, true,
+ false, false, false, false, false, true, true, false]
+
+ expect IOError: discard bitReader.readBool()
+ check bitReader.atEnd()
+
+ test "readBits":
+ let stream = newStringStream()
+ defer: stream.close()
+ stream.write(0xF00F'u16)
+ stream.write(0x0FFF'u16)
+ stream.setPosition(0)
+
+ let bitReader = stream.bitReader()
+ check bitReader.readBits(8, uint8) == 0x0F'u8
+ check bitReader.readBits(16, uint16) == 0xFFF0'u16
+ check bitReader.readBits(8, uint8) == 0x0F'u8
+
+ expect RangeError: discard bitReader.readBits(9, uint8)
+ 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()
+ stream.write(0x0F00_F0FF_F0F0_F0F0'u64)
+ stream.setPosition(0)
+
+ let bitReader = stream.bitReader()
+ check bitReader.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0])
+ check bitReader.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F])
+ check bitReader.atEnd()
+
+suite "bitwriter":
+ test "flush":
+ let stream = newStringStream()
+ defer: stream.close()
+ let bitWriter = stream.bitWriter()
+
+ bitWriter.writeBool(true)
+ stream.setPosition(0)
+ expect IOError: discard stream.peekUint8()
+
+ bitWriter.flush()
+ stream.setPosition(0)
+ check stream.readUint8() == 0x01'u8
+ check stream.atEnd()
+
+ bitWriter.flush()
+ check stream.atEnd()
+
+ test "writeBool":
+ let stream = newStringStream()
+ defer: stream.close()
+
+ let bitWriter = stream.bitWriter()
+ let booleanValues = @[
+ true, true, true, true, true, false, false, true,
+ false, false, false, false, false, true, true, false,
+ true, true, false, true]
+ for b in booleanValues: bitWriter.writeBool(b)
+ bitWriter.flush()
+
+ stream.setPosition(0)
+ check stream.readUint8() == 0b1001_1111'u8
+ check stream.readUint8() == 0b0110_0000'u8
+ check stream.readUint8() == 0b0000_1011'u8
+ expect IOError: discard stream.readUint8()
+ check stream.atEnd()
+
+ test "writeBits":
+ let stream = newStringStream()
+ defer: stream.close()
+
+ let bitWriter = stream.bitWriter()
+ bitWriter.writeBits(4, 0xF00F'u16)
+ bitWriter.writeBits(16, 0xF00F'u16)
+ bitWriter.writeBits(16, 0xFFFF'u16)
+ bitWriter.flush()
+
+ stream.setPosition(0)
+ check stream.readUint16() == 0x00FF'u16
+ check stream.readUint16() == 0xFFFF'u16
+ check stream.readUint8() == 0x0F'u8
+ expect IOError: discard stream.readUint8()
+ check stream.atEnd()
+
+ test "writeSeq":
+ let stream = newStringStream()
+ defer: stream.close()
+
+ let bitWriter = stream.bitWriter()
+ bitWriter.writeSeq(32, @[0xF0F0'u16, 0xF0F0])
+ bitWriter.writeSeq(28, @[0xFF'u8, 0xF0, 0x00, 0xFF])
+ bitWriter.flush()
+
+ stream.setPosition(0)
+ check stream.readUint64() == 0x0F00_F0FF_F0F0_F0F0'u64
+ check stream.atEnd()
diff --git a/tests/tbitreader.nim b/tests/tbitreader.nim
deleted file mode 100644
index 294f6c9..0000000
--- a/tests/tbitreader.nim
+++ /dev/null
@@ -1,84 +0,0 @@
-# gzip-like LZSS compressor
-# Copyright (C) 2018 Pacien TRAN-GIRARD
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see .
-
-import unittest, streams, sugar, sequtils
-import integers, bitreader
-
-suite "bitreader":
- test "readBool":
- let stream = newStringStream()
- defer: stream.close()
- stream.write(0b1001_1111'u8)
- stream.write(0b0110_0000'u8)
- stream.setPosition(0)
-
- let bitReader = stream.bitReader()
- check lc[bitReader.readBool() | (_ <- 0..<16), bool] == @[
- true, true, true, true, true, false, false, true,
- false, false, false, false, false, true, true, false]
-
- expect IOError: discard bitReader.readBool()
- check bitReader.atEnd()
-
- test "readBits":
- let stream = newStringStream()
- defer: stream.close()
- stream.write(0xF00F'u16)
- stream.write(0x0FFF'u16)
- stream.setPosition(0)
-
- let bitReader = stream.bitReader()
- check bitReader.readBits(8, uint8) == 0x0F'u8
- check bitReader.readBits(16, uint16) == 0xFFF0'u16
- check bitReader.readBits(8, uint8) == 0x0F'u8
-
- expect RangeError: discard bitReader.readBits(9, uint8)
- 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()
- stream.write(0x0F00_F0FF_F0F0_F0F0'u64)
- stream.setPosition(0)
-
- let bitReader = stream.bitReader()
- check bitReader.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0])
- check bitReader.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F])
- check bitReader.atEnd()
diff --git a/tests/tbitwriter.nim b/tests/tbitwriter.nim
deleted file mode 100644
index 2c570af..0000000
--- a/tests/tbitwriter.nim
+++ /dev/null
@@ -1,85 +0,0 @@
-# gzip-like LZSS compressor
-# Copyright (C) 2018 Pacien TRAN-GIRARD
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see .
-
-import unittest, streams
-import integers, bitwriter
-
-suite "bitwriter":
- test "flush":
- let stream = newStringStream()
- defer: stream.close()
- let bitWriter = stream.bitWriter()
-
- bitWriter.writeBool(true)
- stream.setPosition(0)
- expect IOError: discard stream.peekUint8()
-
- bitWriter.flush()
- stream.setPosition(0)
- check stream.readUint8() == 0x01'u8
- check stream.atEnd()
-
- bitWriter.flush()
- check stream.atEnd()
-
- test "writeBool":
- let stream = newStringStream()
- defer: stream.close()
-
- let bitWriter = stream.bitWriter()
- let booleanValues = @[
- true, true, true, true, true, false, false, true,
- false, false, false, false, false, true, true, false,
- true, true, false, true]
- for b in booleanValues: bitWriter.writeBool(b)
- bitWriter.flush()
-
- stream.setPosition(0)
- check stream.readUint8() == 0b1001_1111'u8
- check stream.readUint8() == 0b0110_0000'u8
- check stream.readUint8() == 0b0000_1011'u8
- expect IOError: discard stream.readUint8()
- check stream.atEnd()
-
- test "writeBits":
- let stream = newStringStream()
- defer: stream.close()
-
- let bitWriter = stream.bitWriter()
- bitWriter.writeBits(4, 0xF00F'u16)
- bitWriter.writeBits(16, 0xF00F'u16)
- bitWriter.writeBits(16, 0xFFFF'u16)
- bitWriter.flush()
-
- stream.setPosition(0)
- check stream.readUint16() == 0x00FF'u16
- check stream.readUint16() == 0xFFFF'u16
- check stream.readUint8() == 0x0F'u8
- expect IOError: discard stream.readUint8()
- check stream.atEnd()
-
- test "writeSeq":
- let stream = newStringStream()
- defer: stream.close()
-
- let bitWriter = stream.bitWriter()
- bitWriter.writeSeq(32, @[0xF0F0'u16, 0xF0F0])
- bitWriter.writeSeq(28, @[0xFF'u8, 0xF0, 0x00, 0xFF])
- bitWriter.flush()
-
- stream.setPosition(0)
- check stream.readUint64() == 0x0F00_F0FF_F0F0_F0F0'u64
- check stream.atEnd()
diff --git a/tests/thuffmandecoder.nim b/tests/thuffmandecoder.nim
index d4d81b4..9b44e9d 100644
--- a/tests/thuffmandecoder.nim
+++ b/tests/thuffmandecoder.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import unittest, streams
-import bitreader, bitwriter
+import bitio/bitreader, bitio/bitwriter
import huffmantree, huffmandecoder
suite "huffdecoder":
diff --git a/tests/thuffmanencoder.nim b/tests/thuffmanencoder.nim
index 04318b7..9c46eed 100644
--- a/tests/thuffmanencoder.nim
+++ b/tests/thuffmanencoder.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import unittest, streams, tables
-import bitreader, bitwriter
+import bitio/bitreader, bitio/bitwriter
import huffmantree, huffmanencoder
suite "huffencoder":
diff --git a/tests/thuffmantree.nim b/tests/thuffmantree.nim
index bc6a505..467fac5 100644
--- a/tests/thuffmantree.nim
+++ b/tests/thuffmantree.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import unittest, streams, sequtils, tables, heapqueue
-import bitreader, bitwriter, huffmantree
+import bitio/bitreader, bitio/bitwriter, huffmantree
suite "huffmantree":
let tree = huffmanBranch(
diff --git a/tests/trawblock.nim b/tests/trawblock.nim
index 2544199..0271e33 100644
--- a/tests/trawblock.nim
+++ b/tests/trawblock.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import unittest, streams
-import bitreader, bitwriter, rawblock
+import bitio/bitreader, bitio/bitwriter, rawblock
suite "rawblock":
test "serialise":
diff --git a/tests/tstreamblock.nim b/tests/tstreamblock.nim
index 89f69e0..57eaf3a 100644
--- a/tests/tstreamblock.nim
+++ b/tests/tstreamblock.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import unittest, streams
-import bitreader, bitwriter, streamblock
+import bitio/bitreader, bitio/bitwriter, streamblock
suite "streamblock":
test "serialise":
--
cgit v1.2.3