aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-11-30 16:57:32 +0100
committerpacien2018-11-30 17:03:23 +0100
commit8af38da097b8358cb273baa37c748120461c718e (patch)
tree403397acf75540e76cbed619deaaa027da33a91f
parentb03508ea5e20370de26c6faf23bbbdd4e89ab1a9 (diff)
downloadgziplike-8af38da097b8358cb273baa37c748120461c718e.tar.gz
isolate bit IO
-rw-r--r--src/bitio/bitreader.nim (renamed from src/bitreader.nim)2
-rw-r--r--src/bitio/bitwriter.nim (renamed from src/bitwriter.nim)2
-rw-r--r--src/huffmandecoder.nim2
-rw-r--r--src/huffmanencoder.nim2
-rw-r--r--src/huffmantree.nim2
-rw-r--r--src/lzssblock.nim2
-rw-r--r--src/main.nim2
-rw-r--r--src/rawblock.nim2
-rw-r--r--src/streamblock.nim2
-rw-r--r--tests/tbitio.nim (renamed from tests/tbitreader.nim)70
-rw-r--r--tests/tbitwriter.nim85
-rw-r--r--tests/thuffmandecoder.nim2
-rw-r--r--tests/thuffmanencoder.nim2
-rw-r--r--tests/thuffmantree.nim2
-rw-r--r--tests/trawblock.nim2
-rw-r--r--tests/tstreamblock.nim2
16 files changed, 83 insertions, 100 deletions
diff --git a/src/bitreader.nim b/src/bitio/bitreader.nim
index baa8bf8..e4ad225 100644
--- a/src/bitreader.nim
+++ b/src/bitio/bitreader.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import streams 17import streams
18import integers 18import ../integers
19 19
20type BitReader* = ref object 20type BitReader* = ref object
21 stream: Stream 21 stream: Stream
diff --git a/src/bitwriter.nim b/src/bitio/bitwriter.nim
index 4fe499a..f1b44ca 100644
--- a/src/bitwriter.nim
+++ b/src/bitio/bitwriter.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import streams 17import streams
18import integers 18import ../integers
19 19
20type BitWriter* = ref object 20type BitWriter* = ref object
21 stream: Stream 21 stream: Stream
diff --git a/src/huffmandecoder.nim b/src/huffmandecoder.nim
index 4df712a..5cf4ca5 100644
--- a/src/huffmandecoder.nim
+++ b/src/huffmandecoder.nim
@@ -14,7 +14,7 @@
14# You should have received a copy of the GNU Affero General Public License 14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import huffmantree, bitreader 17import huffmantree, bitio/bitreader
18 18
19type HuffmanDecoder*[T: SomeUnsignedInt] = object 19type HuffmanDecoder*[T: SomeUnsignedInt] = object
20 tree: HuffmanTreeNode[T] 20 tree: HuffmanTreeNode[T]
diff --git a/src/huffmanencoder.nim b/src/huffmanencoder.nim
index 60c3d46..05ed64f 100644
--- a/src/huffmanencoder.nim
+++ b/src/huffmanencoder.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import tables 17import tables
18import integers, huffmantree, bitwriter 18import integers, huffmantree, bitio/bitwriter
19 19
20type HuffmanEncoder*[T, U: SomeUnsignedInt] = object 20type HuffmanEncoder*[T, U: SomeUnsignedInt] = object
21 codebook: TableRef[T, U] 21 codebook: TableRef[T, U]
diff --git a/src/huffmantree.nim b/src/huffmantree.nim
index 0266dfb..6208ecf 100644
--- a/src/huffmantree.nim
+++ b/src/huffmantree.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import tables, heapqueue 17import tables, heapqueue
18import integers, bitreader, bitwriter 18import integers, bitio/bitreader, bitio/bitwriter
19 19
20const valueLengthFieldBitLength* = 6 # 64 20const valueLengthFieldBitLength* = 6 # 64
21 21
diff --git a/src/lzssblock.nim b/src/lzssblock.nim
index be8c4f0..317e768 100644
--- a/src/lzssblock.nim
+++ b/src/lzssblock.nim
@@ -14,7 +14,7 @@
14# You should have received a copy of the GNU Affero General Public License 14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import bitreader, bitwriter 17import bitio/bitreader, bitio/bitwriter
18 18
19type LzssBlock* = object 19type LzssBlock* = object
20 discard 20 discard
diff --git a/src/main.nim b/src/main.nim
index 4fc137f..450f52d 100644
--- a/src/main.nim
+++ b/src/main.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import os, streams, sugar 17import os, streams, sugar
18import bitreader, bitwriter, streamblock 18import bitio/bitreader, bitio/bitwriter, streamblock
19 19
20proc transform*(operation: (BitReader, BitWriter) -> void, input, output: string) = 20proc transform*(operation: (BitReader, BitWriter) -> void, input, output: string) =
21 let inputStream = openFileStream(input, fmRead) 21 let inputStream = openFileStream(input, fmRead)
diff --git a/src/rawblock.nim b/src/rawblock.nim
index 4a83b1d..b9a1e63 100644
--- a/src/rawblock.nim
+++ b/src/rawblock.nim
@@ -14,7 +14,7 @@
14# You should have received a copy of the GNU Affero General Public License 14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import integers, bitreader, bitwriter 17import integers, bitio/bitreader, bitio/bitwriter
18 18
19const maxDataBitLength = high(uint16).int 19const maxDataBitLength = high(uint16).int
20const bitLengthFieldBitLength = 2 * wordBitLength 20const bitLengthFieldBitLength = 2 * wordBitLength
diff --git a/src/streamblock.nim b/src/streamblock.nim
index 403687e..ff649b1 100644
--- a/src/streamblock.nim
+++ b/src/streamblock.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import sequtils 17import sequtils
18import integers, bitreader, bitwriter 18import integers, bitio/bitreader, bitio/bitwriter
19import rawblock, lzssblock 19import rawblock, lzssblock
20 20
21type BlockKind* = enum 21type BlockKind* = enum
diff --git a/tests/tbitreader.nim b/tests/tbitio.nim
index 294f6c9..0391974 100644
--- a/tests/tbitreader.nim
+++ b/tests/tbitio.nim
@@ -15,7 +15,8 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import unittest, streams, sugar, sequtils 17import unittest, streams, sugar, sequtils
18import integers, bitreader 18import integers
19import bitio/bitreader, bitio/bitwriter
19 20
20suite "bitreader": 21suite "bitreader":
21 test "readBool": 22 test "readBool":
@@ -82,3 +83,70 @@ suite "bitreader":
82 check bitReader.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0]) 83 check bitReader.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0])
83 check bitReader.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F]) 84 check bitReader.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F])
84 check bitReader.atEnd() 85 check bitReader.atEnd()
86
87suite "bitwriter":
88 test "flush":
89 let stream = newStringStream()
90 defer: stream.close()
91 let bitWriter = stream.bitWriter()
92
93 bitWriter.writeBool(true)
94 stream.setPosition(0)
95 expect IOError: discard stream.peekUint8()
96
97 bitWriter.flush()
98 stream.setPosition(0)
99 check stream.readUint8() == 0x01'u8
100 check stream.atEnd()
101
102 bitWriter.flush()
103 check stream.atEnd()
104
105 test "writeBool":
106 let stream = newStringStream()
107 defer: stream.close()
108
109 let bitWriter = stream.bitWriter()
110 let booleanValues = @[
111 true, true, true, true, true, false, false, true,
112 false, false, false, false, false, true, true, false,
113 true, true, false, true]
114 for b in booleanValues: bitWriter.writeBool(b)
115 bitWriter.flush()
116
117 stream.setPosition(0)
118 check stream.readUint8() == 0b1001_1111'u8
119 check stream.readUint8() == 0b0110_0000'u8
120 check stream.readUint8() == 0b0000_1011'u8
121 expect IOError: discard stream.readUint8()
122 check stream.atEnd()
123
124 test "writeBits":
125 let stream = newStringStream()
126 defer: stream.close()
127
128 let bitWriter = stream.bitWriter()
129 bitWriter.writeBits(4, 0xF00F'u16)
130 bitWriter.writeBits(16, 0xF00F'u16)
131 bitWriter.writeBits(16, 0xFFFF'u16)
132 bitWriter.flush()
133
134 stream.setPosition(0)
135 check stream.readUint16() == 0x00FF'u16
136 check stream.readUint16() == 0xFFFF'u16
137 check stream.readUint8() == 0x0F'u8
138 expect IOError: discard stream.readUint8()
139 check stream.atEnd()
140
141 test "writeSeq":
142 let stream = newStringStream()
143 defer: stream.close()
144
145 let bitWriter = stream.bitWriter()
146 bitWriter.writeSeq(32, @[0xF0F0'u16, 0xF0F0])
147 bitWriter.writeSeq(28, @[0xFF'u8, 0xF0, 0x00, 0xFF])
148 bitWriter.flush()
149
150 stream.setPosition(0)
151 check stream.readUint64() == 0x0F00_F0FF_F0F0_F0F0'u64
152 check stream.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 @@
1# gzip-like LZSS compressor
2# Copyright (C) 2018 Pacien TRAN-GIRARD
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as
6# published by the Free Software Foundation, either version 3 of the
7# License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17import unittest, streams
18import integers, bitwriter
19
20suite "bitwriter":
21 test "flush":
22 let stream = newStringStream()