aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorpacien2018-11-29 12:41:59 +0100
committerpacien2018-11-29 12:42:25 +0100
commit1524ab71168b7c214a531f796c94962776e9d88a (patch)
tree9d4e7342ea0a0d8a534e474f40d6eabd0c108b91 /tests
parentd661132528d5c27148a0b55d52709ce97124000a (diff)
downloadgziplike-1524ab71168b7c214a531f796c94962776e9d88a.tar.gz
add generic huffman tree builder
Diffstat (limited to 'tests')
-rw-r--r--tests/thuffmantree.nim33
-rw-r--r--tests/tlzsschain.nim18
2 files changed, 46 insertions, 5 deletions
diff --git a/tests/thuffmantree.nim b/tests/thuffmantree.nim
index ec40bdb..705ac17 100644
--- a/tests/thuffmantree.nim
+++ b/tests/thuffmantree.nim
@@ -14,24 +14,43 @@
14# You should have received a copy of the GNU Affero General Public License 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/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import unittest, streams 17import 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)))
21 let tree = huffmanBranch( 22 let tree = huffmanBranch(
22 huffmanLeaf(1'u), 23 huffmanLeaf(1'u),
23 huffmanBranch( 24 huffmanBranch(
24 huffmanLeaf(2'u), 25 huffmanLeaf(2'u),
25 huffmanLeaf(3'u))) 26 huffmanLeaf(3'u)))
26 27
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
27 test "equality": 38 test "equality":
28 check huffmanLeaf(12'u) == huffmanLeaf(12'u) 39 check huffmanLeaf(12'u) == huffmanLeaf(12'u)
29 check huffmanLeaf(12'u) != huffmanLeaf(21'u) 40 check huffmanLeaf(12'u) != huffmanLeaf(21'u)
30 check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u)) 41 check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u))
31 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) 42 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u))
32 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u)) 43 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))
33 check tree == tree 46 check tree == tree
34 47
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
35 test "maxValue": 54 test "maxValue":
36 check tree.maxValue() == 3 55 check tree.maxValue() == 3
37 56
@@ -52,7 +71,7 @@ suite "huffmantree":
52 71
53 stream.setPosition(0) 72 stream.setPosition(0)
54 let bitReader = stream.bitReader() 73 let bitReader = stream.bitReader()
55 check huffmantree.deserialise(bitReader, uint) == tree 74 check huffmantree.deserialise(bitReader, uint) ~= tree
56 75
57 test "serialise": 76 test "serialise":
58 let stream = newStringStream() 77 let stream = newStringStream()
@@ -72,3 +91,13 @@ suite "huffmantree":
72 check bitReader.readBits(2, uint8) == 2 91 check bitReader.readBits(2, uint8) == 2
73 check bitReader.readBool() == true # 3 leaf 92 check bitReader.readBool() == true # 3 leaf
74 check bitReader.readBits(2, uint8) == 3 93 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/tlzsschain.nim b/tests/tlzsschain.nim
index 241a0f1..a8c2012 100644
--- a/tests/tlzsschain.nim
+++ b/tests/tlzsschain.nim
@@ -14,11 +14,11 @@
14# You should have received a copy of the GNU Affero General Public License 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/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import unittest 17import unittest, sequtils, tables
18import polyfill, lzssnode, lzsschain 18import polyfill, lzssnode, lzsschain
19 19
20suite "lzsschain": 20suite "lzsschain":
21 test "decode": 21 proc chain(): LzssChain =
22 let chainArray = [ 22 let chainArray = [
23 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2), 23 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
24 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5), 24 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
@@ -27,4 +27,16 @@ suite "lzsschain":
27 lzssReference(3, 3), lzssCharacter(5)] 27 lzssReference(3, 3), lzssCharacter(5)]
28 var chain = lzssChain() 28 var chain = lzssChain()
29 for node in chainArray: chain.append(node) 29 for node in chainArray: chain.append(node)
30 check chain.decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5] 30 result = chain
31
32 test "decode":
33 check chain().decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
34
35 test "stats":
36 let stats = chain().stats()
37 check stats.characters == newCountTable(concat(
38 repeat(0'u8, 2), repeat(1'u8, 2), repeat(2'u8, 1), repeat(3'u8, 1), repeat(4'u8, 1), repeat(5'u8, 3)))
39 check stats.lengths == newCountTable(concat(
40 repeat(3, 2), repeat(4, 1)))
41 check stats.positions == newCountTable(concat(
42 repeat(3, 1), repeat(6, 1), repeat(8, 1)))