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/thuffman.nim | |
parent | b616e8f5773631945962d4b1256f8f2d575e0da1 (diff) | |
download | gziplike-925f8d7176c3d54d435896ec0d2eabda634bbcc8.tar.gz |
merge tests
Diffstat (limited to 'tests/thuffman.nim')
-rw-r--r-- | tests/thuffman.nim | 119 |
1 files changed, 0 insertions, 119 deletions
diff --git a/tests/thuffman.nim b/tests/thuffman.nim deleted file mode 100644 index db209f4..0000000 --- a/tests/thuffman.nim +++ /dev/null | |||
@@ -1,119 +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 | |||
17 | import unittest, streams, sequtils, tables, heapqueue | ||
18 | import bitio/bitreader, bitio/bitwriter | ||
19 | import huffman/huffmantree, huffman/huffmantreebuilder, huffman/huffmanencoder, huffman/huffmandecoder | ||
20 | |||
21 | let | ||
22 | stats = newCountTable(concat(repeat(1'u, 3), repeat(2'u, 1), repeat(3'u, 2))) | ||
23 | tree = huffmanBranch( | ||
24 | huffmanLeaf(1'u), | ||
25 | huffmanBranch( | ||
26 | huffmanLeaf(2'u), | ||
27 | huffmanLeaf(3'u))) | ||
28 | |||
29 | suite "huffmantree": | ||
30 | test "equality": | ||
31 | check huffmanLeaf(12'u) == huffmanLeaf(12'u) | ||
32 | check huffmanLeaf(12'u) != huffmanLeaf(21'u) | ||
33 | check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u)) | ||
34 | check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) | ||
35 | check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u)) | ||
36 | check tree == tree | ||
37 | |||
38 | test "maxValue": | ||
39 | check tree.maxValue() == 3 | ||
40 | |||
41 | test "deserialise": | ||
42 | let stream = newStringStream() | ||
43 | defer: stream.close() | ||
44 | let bitWriter = stream.bitWriter() | ||
45 | bitWriter.writeBits(valueLengthFieldBitLength, 2'u8) | ||
46 | bitWriter.writeBool(false) # root | ||
47 | bitWriter.writeBool(true) # 1 leaf | ||
48 | bitWriter.writeBits(2, 1'u) | ||
49 | bitWriter.writeBool(false) # right branch | ||
50 | bitWriter.writeBool(true) # 2 leaf | ||
51 | bitWriter.writeBits(2, 2'u) | ||
52 | bitWriter.writeBool(true) # 3 leaf | ||
53 | bitWriter.writeBits(2, 3'u) | ||
54 | bitWriter.flush() | ||
55 | |||
56 | stream.setPosition(0) | ||
57 | let bitReader = stream.bitReader() | ||
58 | check huffmantree.deserialise(bitReader, uint) == tree | ||
59 | |||
60 | test "serialise": | ||
61 | let stream = newStringStream() | ||
62 | defer: stream.close() | ||
63 | let bitWriter = stream.bitWriter() | ||
64 | tree.serialise(bitWriter) | ||
65 | bitWriter.flush() | ||
66 | |||
67 | stream.setPosition(0) | ||
68 | let bitReader = stream.bitReader() | ||
69 | check bitReader.readBits(valueLengthFieldBitLength, uint8) == 2 | ||
70 | check bitReader.readBool() == false # root | ||
71 | check bitReader.readBool() == true # 1 leaf | ||
72 | check bitReader.readBits(2, uint8) == 1 | ||
73 | check bitReader.readBool() == false # right branch | ||
74 | check bitReader.readBool() == true # 2 leaf | ||
75 | check bitReader.readBits(2, uint8) == 2 | ||
76 | check bitReader.readBool() == true # 3 leaf | ||
77 | check bitReader.readBits(2, uint8) == 3 | ||
78 | |||
79 | suite "huffmantreebuilder": | ||
80 | test "buildHuffmanTree": | ||
81 | check buildHuffmanTree(stats) == tree | ||
82 | |||
83 | suite "huffencoder": | ||
84 | let tree = huffmanBranch( | ||
85 | huffmanLeaf(1'u), | ||
86 | huffmanBranch( | ||
87 | huffmanLeaf(2'u), | ||
88 | huffmanLeaf(3'u))) | ||
89 | |||
90 | test "buildCodebook": | ||
91 | let codebook = buildCodebook(tree, uint) | ||
92 | check codebook.len == 3 | ||
93 | check codebook[1'u] == (1, 0b0'u) | ||
94 | check codebook[2'u] == (2, 0b01'u) | ||
95 | check codebook[3'u] == (2, 0b11'u) | ||
96 | |||
97 | test "encode": | ||
98 | let encoder = tree.encoder(uint) | ||
99 | check encoder.encode(1'u) == (1, 0b0'u) | ||
100 | check encoder.encode(2'u) == (2, 0b01'u) | ||
101 | check encoder.encode(3'u) == (2, 0b11'u) | ||
102 | |||
103 | suite "huffdecoder": | ||
104 | test "decode": | ||
105 | let stream = newStringStream() | ||
106 | defer: stream.close() | ||
107 | let bitWriter = stream.bitWriter() | ||
108 | bitWriter.writeBool(true) # 2 | ||
109 | bitWriter.writeBool(false) | ||
110 | bitWriter.writeBool(false) # 1 | ||
111 | bitWriter.writeBool(true) # 3 | ||
112 | bitWriter.writeBool(true) | ||
113 | bitWriter.flush() | ||
114 | stream.setPosition(0) | ||
115 | let bitReader = stream.bitReader() | ||
116 | let decoder = tree.decoder() | ||
117 | check decoder.decode(bitReader) == 2'u | ||
118 | check decoder.decode(bitReader) == 1'u | ||
119 | check decoder.decode(bitReader) == 3'u | ||