diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/thuffmandecoder.nim | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/thuffmandecoder.nim b/tests/thuffmandecoder.nim new file mode 100644 index 0000000..d4d81b4 --- /dev/null +++ b/tests/thuffmandecoder.nim | |||
@@ -0,0 +1,43 @@ | |||
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 bitreader, bitwriter | ||
19 | import huffmantree, huffmandecoder | ||
20 | |||
21 | suite "huffdecoder": | ||
22 | let tree = huffmanBranch( | ||
23 | huffmanLeaf(1'u), | ||
24 | huffmanBranch( | ||
25 | huffmanLeaf(2'u), | ||
26 | huffmanLeaf(3'u))) | ||
27 | |||
28 | test "decode": | ||
29 | let stream = newStringStream() | ||
30 | defer: stream.close() | ||
31 | let bitWriter = stream.bitWriter() | ||
32 | bitWriter.writeBool(true) # 2 | ||
33 | bitWriter.writeBool(false) | ||
34 | bitWriter.writeBool(false) # 1 | ||
35 | bitWriter.writeBool(true) # 3 | ||
36 | bitWriter.writeBool(true) | ||
37 | bitWriter.flush() | ||
38 | stream.setPosition(0) | ||
39 | let bitReader = stream.bitReader() | ||
40 | let decoder = tree.decoder() | ||
41 | check decoder.decode(bitReader) == 2'u | ||
42 | check decoder.decode(bitReader) == 1'u | ||
43 | check decoder.decode(bitReader) == 3'u | ||