aboutsummaryrefslogtreecommitdiff
path: root/tests/lzsshuffman/tlzsshuffmanencoder.nim
diff options
context:
space:
mode:
authorpacien2018-12-03 18:42:23 +0100
committerpacien2018-12-03 18:42:23 +0100
commit925f8d7176c3d54d435896ec0d2eabda634bbcc8 (patch)
tree4b921a9d9d91cc0dde386d02feabed9207b51880 /tests/lzsshuffman/tlzsshuffmanencoder.nim
parentb616e8f5773631945962d4b1256f8f2d575e0da1 (diff)
downloadgziplike-925f8d7176c3d54d435896ec0d2eabda634bbcc8.tar.gz
merge tests
Diffstat (limited to 'tests/lzsshuffman/tlzsshuffmanencoder.nim')
-rw-r--r--tests/lzsshuffman/tlzsshuffmanencoder.nim66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/lzsshuffman/tlzsshuffmanencoder.nim b/tests/lzsshuffman/tlzsshuffmanencoder.nim
new file mode 100644
index 0000000..100b5f4
--- /dev/null
+++ b/tests/lzsshuffman/tlzsshuffmanencoder.nim
@@ -0,0 +1,66 @@
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, lzss/lzsschain
20import huffman/huffmantree, huffman/huffmanencoder
21import lzsshuffman/lzsshuffmansymbol, lzsshuffman/lzsshuffmanencoder
22
23suite "lzsshuffmanencoder":
24 test "writeChain (empty)":
25 let chain = lzssChain(newSeq[LzssNode]())
26 let symbolTree = huffmanLeaf(endSymbol.uint16)
27 let positionTree = huffmanLeaf(0'u16)
28 let stream = newStringStream()
29 defer: stream.close()
30 let bitWriter = stream.bitWriter()
31 writeChain(chain, symbolTree.encoder(uint16), positionTree.encoder(uint16), bitWriter)
32 bitWriter.flush()
33 stream.setPosition(0)
34 check stream.atEnd()
35
36 test "writeChain (minimal)":
37 let chain = lzssChain([
38 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
39 lzssReference(3, 3), lzssReference(3, 4)])
40 let symbolTree = huffmanBranch(
41 huffmanBranch(
42 huffmanLeaf(0'u16),
43 huffmanLeaf(1'u16)),
44 huffmanBranch(
45 huffmanLeaf(257'u16),
46 huffmanBranch(
47 huffmanLeaf(2'u16),
48 huffmanLeaf(256'u16))))
49 let positionTree = huffmanBranch(
50 huffmanLeaf(3'u16),
51 huffmanLeaf(4'u16))
52 let stream = newStringStream()
53 defer: stream.close()
54 let bitWriter = stream.bitWriter()
55 writeChain(chain, symbolTree.encoder(uint16), positionTree.encoder(uint16), bitWriter)
56 bitWriter.flush()
57 stream.setPosition(0)
58 let bitReader = stream.bitReader()
59 check bitReader.readBits(2, uint8) == 0b00'u8 # char 0
60 check bitReader.readBits(2, uint8) == 0b10'u8 # char 1
61 check bitReader.readBits(3, uint8) == 0b011'u8 # char 2
62 check bitReader.readBits(2, uint8) == 0b01'u8 # ref len 3
63 check bitReader.readBits(1, uint8) == 0b0'u8 # ref pos 3
64 check bitReader.readBits(2, uint8) == 0b01'u8 # ref len 3
65 check bitReader.readBits(1, uint8) == 0b1'u8 # ref pos 4
66 check bitReader.readBits(3, uint8) == 0b111'u8 # eof