aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/thuffmantree.nim31
-rw-r--r--tests/thuffmantreebuilder.nim29
2 files changed, 30 insertions, 30 deletions
diff --git a/tests/thuffmantree.nim b/tests/thuffmantree.nim
index 705ac17..bc6a505 100644
--- a/tests/thuffmantree.nim
+++ b/tests/thuffmantree.nim
@@ -18,39 +18,20 @@ import unittest, streams, sequtils, tables, heapqueue
18import bitreader, bitwriter, huffmantree 18import bitreader, bitwriter, huffmantree
19 19
20suite "huffmantree": 20suite "huffmantree":
21 let stats = newCountTable(concat(repeat(1'u, 3), repeat(2'u, 1), repeat(3'u, 2)))
22 let tree = huffmanBranch( 21 let tree = huffmanBranch(
23 huffmanLeaf(1'u), 22 huffmanLeaf(1'u),
24 huffmanBranch( 23 huffmanBranch(
25 huffmanLeaf(2'u), 24 huffmanLeaf(2'u),
26 huffmanLeaf(3'u))) 25 huffmanLeaf(3'u)))
27 26
28 test "equivalence":
29 check huffmanLeaf(12'u) ~= huffmanLeaf(12'u)
30 check huffmanLeaf(12'u) ~= huffmanLeaf(12'u, 2)
31 check huffmanLeaf(12'u) !~ huffmanLeaf(21'u)
32 check huffmanLeaf(12'u) !~ huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u))
33 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) ~= huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u))
34 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) !~ huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u))
35 check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) ~= huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 2))
36 check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) !~ huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(12'u, 2))
37
38 test "equality": 27 test "equality":
39 check huffmanLeaf(12'u) == huffmanLeaf(12'u) 28 check huffmanLeaf(12'u) == huffmanLeaf(12'u)
40 check huffmanLeaf(12'u) != huffmanLeaf(21'u) 29 check huffmanLeaf(12'u) != huffmanLeaf(21'u)
41 check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u)) 30 check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u))
42 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) 31 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u))
43 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u)) 32 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u))
44 check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) == huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1))
45 check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) != huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 2))
46 check tree == tree 33 check tree == tree
47 34
48 test "weight comparison":
49 check huffmanLeaf(12'u, 1) < huffmanLeaf(12'u, 2)
50 check huffmanLeaf(12'u, 2) > huffmanLeaf(12'u, 1)
51 check huffmanLeaf(12'u, 1) < huffmanLeaf(12'u, 1) == false
52 check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) < huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 2))
53
54 test "maxValue": 35 test "maxValue":
55 check tree.maxValue() == 3 36 check tree.maxValue() == 3
56 37
@@ -71,7 +52,7 @@ suite "huffmantree":
71 52
72 stream.setPosition(0) 53 stream.setPosition(0)
73 let bitReader = stream.bitReader() 54 let bitReader = stream.bitReader()
74 check huffmantree.deserialise(bitReader, uint) ~= tree 55 check huffmantree.deserialise(bitReader, uint) == tree
75 56
76 test "serialise": 57 test "serialise":
77 let stream = newStringStream() 58 let stream = newStringStream()
@@ -91,13 +72,3 @@ suite "huffmantree":
91 check bitReader.readBits(2, uint8) == 2 72 check bitReader.readBits(2, uint8) == 2
92 check bitReader.readBool() == true # 3 leaf 73 check bitReader.readBool() == true # 3 leaf
93 check bitReader.readBits(2, uint8) == 3 74 check bitReader.readBits(2, uint8) == 3
94
95 test "symbolQueue":
96 var symbolQueue = symbolQueue(stats)
97 check symbolQueue.len == 3
98 check symbolQueue.pop() == huffmanLeaf(2'u, 1)
99 check symbolQueue.pop() == huffmanLeaf(3'u, 2)
100 check symbolQueue.pop() == huffmanLeaf(1'u, 3)
101
102 test "buildHuffmanTree":
103 check buildHuffmanTree(stats) ~= tree
diff --git a/tests/thuffmantreebuilder.nim b/tests/thuffmantreebuilder.nim
new file mode 100644
index 0000000..f782b37
--- /dev/null
+++ b/tests/thuffmantreebuilder.nim
@@ -0,0 +1,29 @@
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, tables, sequtils
18import huffmantree, huffmantreebuilder
19
20suite "huffmantreebuilder":
21 let stats = newCountTable(concat(repeat(1'u, 3), repeat(2'u, 1), repeat(3'u, 2)))
22 let tree = huffmanBranch(
23 huffmanLeaf(1'u),
24 huffmanBranch(
25 huffmanLeaf(2'u),
26 huffmanLeaf(3'u)))
27
28 test "buildHuffmanTree":
29 check buildHuffmanTree(stats) == tree