diff options
author | pacien | 2018-12-03 18:42:23 +0100 |
---|---|---|
committer | pacien | 2018-12-03 18:42:23 +0100 |
commit | 925f8d7176c3d54d435896ec0d2eabda634bbcc8 (patch) | |
tree | 4b921a9d9d91cc0dde386d02feabed9207b51880 /tests/lzsshuffman/tlzsshuffmandecoder.nim | |
parent | b616e8f5773631945962d4b1256f8f2d575e0da1 (diff) | |
download | gziplike-925f8d7176c3d54d435896ec0d2eabda634bbcc8.tar.gz |
merge tests
Diffstat (limited to 'tests/lzsshuffman/tlzsshuffmandecoder.nim')
-rw-r--r-- | tests/lzsshuffman/tlzsshuffmandecoder.nim | 65 |
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 | |||
17 | import unittest, streams | ||
18 | import bitio/bitwriter, bitio/bitreader | ||
19 | import lzss/lzssnode | ||
20 | import huffman/huffmantree, huffman/huffmandecoder | ||
21 | import lzsshuffman/lzsshuffmansymbol, lzsshuffman/lzsshuffmandecoder | ||
22 | |||
23 | suite "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)] | ||