diff options
author | pacien | 2018-11-30 16:38:17 +0100 |
---|---|---|
committer | pacien | 2018-11-30 16:38:17 +0100 |
commit | b03508ea5e20370de26c6faf23bbbdd4e89ab1a9 (patch) | |
tree | 92539b9f20783f9c82dd5e74399b958a336e9971 /tests/thuffmantree.nim | |
parent | 4361a9e9c918bc4feff4d04e47dbe686b1d31a6c (diff) | |
download | gziplike-b03508ea5e20370de26c6faf23bbbdd4e89ab1a9.tar.gz |
isolate huffman tree construction
Diffstat (limited to 'tests/thuffmantree.nim')
-rw-r--r-- | tests/thuffmantree.nim | 31 |
1 files changed, 1 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 | |||
18 | import bitreader, bitwriter, huffmantree | 18 | import bitreader, bitwriter, huffmantree |
19 | 19 | ||
20 | suite "huffmantree": | 20 | suite "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 | ||