aboutsummaryrefslogtreecommitdiff
path: root/tests/huffman
diff options
context:
space:
mode:
Diffstat (limited to 'tests/huffman')
-rw-r--r--tests/huffman/thuffmandecoder.nim43
-rw-r--r--tests/huffman/thuffmanencoder.nim38
-rw-r--r--tests/huffman/thuffmantree.nim75
-rw-r--r--tests/huffman/thuffmantreebuilder.nim30
4 files changed, 186 insertions, 0 deletions
diff --git a/tests/huffman/thuffmandecoder.nim b/tests/huffman/thuffmandecoder.nim
new file mode 100644
index 0000000..44fad64
--- /dev/null
+++ b/tests/huffman/thuffmandecoder.nim
@@ -0,0 +1,43 @@
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 huffman/huffmantree, huffman/huffmandecoder
20
21suite "huffmandecoder":
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/huffman/thuffmanencoder.nim b/tests/huffman/thuffmanencoder.nim
new file mode 100644
index 0000000..d08db03
--- /dev/null
+++ b/tests/huffman/thuffmanencoder.nim
@@ -0,0 +1,38 @@
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
18import huffman/huffmantree, huffman/huffmanencoder
19
20suite "huffmanencoder":
21 let tree = huffmanBranch(
22 huffmanLeaf(1'u),
23 huffmanBranch(
24 huffmanLeaf(2'u),
25 huffmanLeaf(3'u)))
26
27 test "buildCodebook":
28 let codebook = buildCodebook(tree, uint)
29 check codebook.len == 3
30 check codebook[1'u] == (1, 0b0'u)
31 check codebook[2'u] == (2, 0b01'u)
32 check codebook[3'u] == (2, 0b11'u)
33
34 test "encode":
35 let encoder = tree.encoder(uint)
36 check encoder.encode(1'u) == (1, 0b0'u)
37 check encoder.encode(2'u) == (2, 0b01'u)
38 check encoder.encode(3'u) == (2, 0b11'u)
diff --git a/tests/huffman/thuffmantree.nim b/tests/huffman/thuffmantree.nim
new file mode 100644
index 0000000..ac8fa59
--- /dev/null
+++ b/tests/huffman/thuffmantree.nim
@@ -0,0 +1,75 @@
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 huffman/huffmantree
20
21suite "huffmantree":
22 let tree = huffmanBranch(
23 huffmanLeaf(1'u),
24 huffmanBranch(
25 huffmanLeaf(2'u),
26 huffmanLeaf(3'u)))
27
28 test "equality":
29 check huffmanLeaf(12'u) == huffmanLeaf(12'u)
30 check huffmanLeaf(12'u) != huffmanLeaf(21'u)
31 check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u))
32 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u))
33 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u))
34 check tree == tree
35
36 test "maxValue":
37 check tree.maxValue() == 3
38
39 test "deserialise":
40 let stream = newStringStream()
41 defer: stream.close()
42 let bitWriter = stream.bitWriter()
43 bitWriter.writeBits(valueLengthFieldBitLength, 2'u8)
44 bitWriter.writeBool(false) # root
45 bitWriter.writeBool(true) # 1 leaf
46 bitWriter.writeBits(2, 1'u)
47 bitWriter.writeBool(false) # right branch
48 bitWriter.writeBool(true) # 2 leaf
49 bitWriter.writeBits(2, 2'u)
50 bitWriter.writeBool(true) # 3 leaf
51 bitWriter.writeBits(2, 3'u)
52 bitWriter.flush()
53
54 stream.setPosition(0)
55 let bitReader = stream.bitReader()
56 check huffmantree.deserialise(bitReader, uint) == tree
57
58 test "serialise":
59 let stream = newStringStream()
60 defer: stream.close()
61 let bitWriter = stream.bitWriter()
62 tree.serialise(bitWriter)
63 bitWriter.flush()
64
65 stream.setPosition(0)
66 let bitReader = stream.bitReader()
67 check bitReader.readBits(valueLengthFieldBitLength, uint8) == 2
68 check bitReader.readBool() == false # root
69 check bitReader.readBool() == true # 1 leaf
70 check bitReader.readBits(2, uint8) == 1
71 check bitReader.readBool() == false # right branch
72 check bitReader.readBool() == true # 2 leaf
73 check bitReader.readBits(2, uint8) == 2
74 check bitReader.readBool() == true # 3 leaf
75 check bitReader.readBits(2, uint8) == 3
diff --git a/tests/huffman/thuffmantreebuilder.nim b/tests/huffman/thuffmantreebuilder.nim
new file mode 100644
index 0000000..1045f1d
--- /dev/null
+++ b/tests/huffman/thuffmantreebuilder.nim
@@ -0,0 +1,30 @@
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, sequtils, tables
18import huffman/huffmantree, huffman/huffmantreebuilder
19
20suite "huffmantreebuilder":
21 let
22 stats = newCountTable(concat(repeat(1'u, 3), repeat(2'u, 1), repeat(3'u, 2)))
23 tree = huffmanBranch(
24 huffmanLeaf(1'u),
25 huffmanBranch(
26 huffmanLeaf(2'u),
27 huffmanLeaf(3'u)))
28
29 test "buildHuffmanTree":
30 check buildHuffmanTree(stats) == tree