aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorpacien2018-11-30 17:08:01 +0100
committerpacien2018-11-30 17:08:01 +0100
commite88f60b63cb05f56a61060a953c726b7a78c0652 (patch)
treef7ee9b401ae92e7eb3fe8d9e352e20da3d2e9243 /tests
parent8af38da097b8358cb273baa37c748120461c718e (diff)
downloadgziplike-e88f60b63cb05f56a61060a953c726b7a78c0652.tar.gz
isolate huffman encoding module
Diffstat (limited to 'tests')
-rw-r--r--tests/thuffman.nim (renamed from tests/thuffmantree.nim)51
-rw-r--r--tests/thuffmandecoder.nim43
-rw-r--r--tests/thuffmanencoder.nim39
-rw-r--r--tests/thuffmantreebuilder.nim29
4 files changed, 48 insertions, 114 deletions
diff --git a/tests/thuffmantree.nim b/tests/thuffman.nim
index 467fac5..0294694 100644
--- a/tests/thuffmantree.nim
+++ b/tests/thuffman.nim
@@ -15,15 +15,18 @@
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, sequtils, tables, heapqueue 17import unittest, streams, sequtils, tables, heapqueue
18import bitio/bitreader, bitio/bitwriter, huffmantree 18import bitio/bitreader, bitio/bitwriter
19import huffman/huffmantree, huffman/huffmantreebuilder, huffman/huffmanencoder, huffman/huffmandecoder
19 20
20suite "huffmantree": 21let
21 let tree = huffmanBranch( 22 stats = newCountTable(concat(repeat(1'u, 3), repeat(2'u, 1), repeat(3'u, 2)))
23 tree = huffmanBranch(
22 huffmanLeaf(1'u), 24 huffmanLeaf(1'u),
23 huffmanBranch( 25 huffmanBranch(
24 huffmanLeaf(2'u), 26 huffmanLeaf(2'u),
25 huffmanLeaf(3'u))) 27 huffmanLeaf(3'u)))
26 28
29suite "huffmantree":
27 test "equality": 30 test "equality":
28 check huffmanLeaf(12'u) == huffmanLeaf(12'u) 31 check huffmanLeaf(12'u) == huffmanLeaf(12'u)
29 check huffmanLeaf(12'u) != huffmanLeaf(21'u) 32 check huffmanLeaf(12'u) != huffmanLeaf(21'u)
@@ -72,3 +75,45 @@ suite "huffmantree":
72 check bitReader.readBits(2, uint8) == 2 75 check bitReader.readBits(2, uint8) == 2
73 check bitReader.readBool() == true # 3 leaf 76 check bitReader.readBool() == true # 3 leaf
74 check bitReader.readBits(2, uint8) == 3 77 check bitReader.readBits(2, uint8) == 3
78
79suite "huffmantreebuilder":
80 test "buildHuffmanTree":
81 check buildHuffmanTree(stats) == tree
82
83suite "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] == 0b0
94 check codebook[2'u] == 0b01
95 check codebook[3'u] == 0b11
96
97 test "encode":
98 let encoder = tree.encoder(uint)
99 check encoder.encode(1'u) == 0b0
100 check encoder.encode(2'u) == 0b01
101 check encoder.encode(3'u) == 0b11
102
103suite "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
diff --git a/tests/thuffmandecoder.nim b/tests/thuffmandecoder.nim
deleted file mode 100644
index 9b44e9d..0000000
--- a/tests/thuffmandecoder.nim
+++ /dev/null
@@ -1,43 +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
17import unittest, streams
18import bitio/bitreader, bitio/bitwriter
19import huffmantree, huffmandecoder
20
21suite "huffdecoder":
22 let tree = huffmanBranch(
23 huffmanLeaf(1'u),
24 huffmanBranch(
25 huffmanLeaf(2'u),
26 huffmanLeaf(3'u)))
27
28 test "decode":
29 let stream = newStringStream()
30 defer: stream.close()
31 let bitWriter = stream.bitWriter()
32 bitWriter.writeBool(true) # 2
33 bitWriter.writeBool(false)
34 bitWriter.writeBool(false) # 1
35 bitWriter.writeBool(true) # 3
36 bitWriter.writeBool(true)
37 bitWriter.flush()
38 stream.setPosition(0)
39 let bitReader = stream.bitReader()
40 let decoder = tree.decoder()
41 check decoder.decode(bitReader) == 2'u
42 check decoder.decode(bitReader) == 1'u
43 check decoder.decode(bitReader) == 3'u
diff --git a/tests/thuffmanencoder.nim b/tests/thuffmanencoder.nim
deleted file mode 100644
index 9c46eed..0000000
--- a/tests/thuffmanencoder.nim
+++ /dev/null
@@ -1,39 +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
17import unittest, streams, tables
18import bitio/bitreader, bitio/bitwriter
19import huffmantree, huffmanencoder
20
21suite "huffencoder":
22 let tree = huffmanBranch(
23 huffmanLeaf(1'u),
24 huffmanBranch(
25 huffmanLeaf(2'u),
26 huffmanLeaf(3'u)))
27
28 test "buildCodebook":
29 let codebook = buildCodebook(tree, uint)
30 check codebook.len == 3
31 check codebook[1'u] == 0b0
32 check codebook[2'u] == 0b01
33 check codebook[3'u] == 0b11
34
35 test "encode":
36 let encoder = tree.encoder(uint)
37 check encoder.encode(1'u) == 0b0
38 check encoder.encode(2'u) == 0b01
39 check encoder.encode(3'u) == 0b11
diff --git a/tests/thuffmantreebuilder.nim b/tests/thuffmantreebuilder.nim
deleted file mode 100644
index f782b37..0000000
--- a/tests/thuffmantreebuilder.nim
+++ /dev/null
@@ -1,29 +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
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