aboutsummaryrefslogtreecommitdiff
path: root/tests/lzsshuffman/tlzsshuffmandecoder.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lzsshuffman/tlzsshuffmandecoder.nim')
-rw-r--r--tests/lzsshuffman/tlzsshuffmandecoder.nim65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/lzsshuffman/tlzsshuffmandecoder.nim b/tests/lzsshuffman/tlzsshuffmandecoder.nim
new file mode 100644
index 0000000..b8cd589
--- /dev/null
+++ b/tests/lzsshuffman/tlzsshuffmandecoder.nim
@@ -0,0 +1,65 @@
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 bitio/bitwriter, bitio/bitreader
19import lzss/lzssnode
20import huffman/huffmantree, huffman/huffmandecoder
21import lzsshuffman/lzsshuffmansymbol, lzsshuffman/lzsshuffmandecoder
22
23suite "lzsshuffmandecoder":
24 test "readChain (empty)":
25 let symbolTree = huffmanLeaf(endSymbol.uint16)
26 let positionTree = huffmanLeaf(0'u16)
27 let stream = newStringStream()
28 defer: stream.close()
29 stream.write(0'u8) # eof
30 stream.setPosition(0)
31 let bitReader = stream.bitReader()
32 let result = readChain(bitReader, symbolTree.decoder(), positionTree.decoder(), 32_000)
33 check result.len == 0
34
35 test "readChain (minimal)":
36 let symbolTree = huffmanBranch(
37 huffmanBranch(
38 huffmanLeaf(0'u16),
39 huffmanLeaf(1'u16)),
40 huffmanBranch(
41 huffmanLeaf(257'u16),
42 huffmanBranch(
43 huffmanLeaf(2'u16),
44 huffmanLeaf(256'u16))))
45 let positionTree = huffmanBranch(
46 huffmanLeaf(3'u16),
47 huffmanLeaf(4'u16))
48 let stream = newStringStream()
49 defer: stream.close()
50 let bitWriter = stream.bitWriter()
51 bitWriter.writeBits(2, 0b00'u8)
52 bitWriter.writeBits(2, 0b10'u8)
53 bitWriter.writeBits(3, 0b011'u8)
54 bitWriter.writeBits(2, 0b01'u8)
55 bitWriter.writeBits(1, 0b0'u8)
56 bitWriter.writeBits(2, 0b01'u8)
57 bitWriter.writeBits(1, 0b1'u8)
58 bitWriter.writeBits(3, 0b111'u8)
59 bitWriter.flush()
60 stream.setPosition(0)
61 let bitReader = stream.bitReader()
62 let result = readChain(bitReader, symbolTree.decoder(), positionTree.decoder(), 32_000)
63 check result == [
64 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
65 lzssReference(3, 3), lzssReference(3, 4)]