From 1524ab71168b7c214a531f796c94962776e9d88a Mon Sep 17 00:00:00 2001
From: pacien
Date: Thu, 29 Nov 2018 12:41:59 +0100
Subject: add generic huffman tree builder
---
tests/thuffmantree.nim | 33 +++++++++++++++++++++++++++++++--
tests/tlzsschain.nim | 18 +++++++++++++++---
2 files changed, 46 insertions(+), 5 deletions(-)
(limited to 'tests')
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 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
-import unittest, streams
+import unittest, streams, sequtils, tables, heapqueue
import bitreader, bitwriter, huffmantree
suite "huffmantree":
+ let stats = newCountTable(concat(repeat(1'u, 3), repeat(2'u, 1), repeat(3'u, 2)))
let tree = huffmanBranch(
huffmanLeaf(1'u),
huffmanBranch(
huffmanLeaf(2'u),
huffmanLeaf(3'u)))
+ test "equivalence":
+ check huffmanLeaf(12'u) ~= huffmanLeaf(12'u)
+ check huffmanLeaf(12'u) ~= huffmanLeaf(12'u, 2)
+ check huffmanLeaf(12'u) !~ huffmanLeaf(21'u)
+ check huffmanLeaf(12'u) !~ huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u))
+ check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) ~= huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u))
+ check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) !~ huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u))
+ check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) ~= huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 2))
+ check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) !~ huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(12'u, 2))
+
test "equality":
check huffmanLeaf(12'u) == huffmanLeaf(12'u)
check huffmanLeaf(12'u) != huffmanLeaf(21'u)
check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u))
check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u))
check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u))
+ check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) == huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1))
+ check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) != huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 2))
check tree == tree
+ test "weight comparison":
+ check huffmanLeaf(12'u, 1) < huffmanLeaf(12'u, 2)
+ check huffmanLeaf(12'u, 2) > huffmanLeaf(12'u, 1)
+ check huffmanLeaf(12'u, 1) < huffmanLeaf(12'u, 1) == false
+ check huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 1)) < huffmanBranch(huffmanLeaf(12'u, 1), huffmanLeaf(21'u, 2))
+
test "maxValue":
check tree.maxValue() == 3
@@ -52,7 +71,7 @@ suite "huffmantree":
stream.setPosition(0)
let bitReader = stream.bitReader()
- check huffmantree.deserialise(bitReader, uint) == tree
+ check huffmantree.deserialise(bitReader, uint) ~= tree
test "serialise":
let stream = newStringStream()
@@ -72,3 +91,13 @@ suite "huffmantree":
check bitReader.readBits(2, uint8) == 2
check bitReader.readBool() == true # 3 leaf
check bitReader.readBits(2, uint8) == 3
+
+ test "symbolQueue":
+ var symbolQueue = symbolQueue(stats)
+ check symbolQueue.len == 3
+ check symbolQueue.pop() == huffmanLeaf(2'u, 1)
+ check symbolQueue.pop() == huffmanLeaf(3'u, 2)
+ check symbolQueue.pop() == huffmanLeaf(1'u, 3)
+
+ test "buildHuffmanTree":
+ 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 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
-import unittest
+import unittest, sequtils, tables
import polyfill, lzssnode, lzsschain
suite "lzsschain":
- test "decode":
+ proc chain(): LzssChain =
let chainArray = [
lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
@@ -27,4 +27,16 @@ suite "lzsschain":
lzssReference(3, 3), lzssCharacter(5)]
var chain = lzssChain()
for node in chainArray: chain.append(node)
- check chain.decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
+ result = chain
+
+ test "decode":
+ check chain().decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
+
+ test "stats":
+ let stats = chain().stats()
+ check stats.characters == newCountTable(concat(
+ repeat(0'u8, 2), repeat(1'u8, 2), repeat(2'u8, 1), repeat(3'u8, 1), repeat(4'u8, 1), repeat(5'u8, 3)))
+ check stats.lengths == newCountTable(concat(
+ repeat(3, 2), repeat(4, 1)))
+ check stats.positions == newCountTable(concat(
+ repeat(3, 1), repeat(6, 1), repeat(8, 1)))
--
cgit v1.2.3