aboutsummaryrefslogtreecommitdiff
path: root/tests/thuffmantree.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/thuffmantree.nim')
-rw-r--r--tests/thuffmantree.nim74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/thuffmantree.nim b/tests/thuffmantree.nim
new file mode 100644
index 0000000..ec40bdb
--- /dev/null
+++ b/tests/thuffmantree.nim
@@ -0,0 +1,74 @@
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 bitreader, bitwriter, huffmantree
19
20suite "huffmantree":
21 let tree = huffmanBranch(
22 huffmanLeaf(1'u),
23 huffmanBranch(
24 huffmanLeaf(2'u),
25 huffmanLeaf(3'u)))
26
27 test "equality":
28 check huffmanLeaf(12'u) == huffmanLeaf(12'u)
29 check huffmanLeaf(12'u) != huffmanLeaf(21'u)
30 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))
32 check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u))
33 check tree == tree
34
35 test "maxValue":
36 check tree.maxValue() == 3
37
38 test "deserialise":
39 let stream = newStringStream()
40 defer: stream.close()
41 let bitWriter = stream.bitWriter()
42 bitWriter.writeBits(valueLengthFieldBitLength, 2'u8)
43 bitWriter.writeBool(false) # root
44 bitWriter.writeBool(true) # 1 leaf
45 bitWriter.writeBits(2, 1'u)
46 bitWriter.writeBool(false) # right branch
47 bitWriter.writeBool(true) # 2 leaf
48 bitWriter.writeBits(2, 2'u)
49 bitWriter.writeBool(true) # 3 leaf
50 bitWriter.writeBits(2, 3'u)
51 bitWriter.flush()
52
53 stream.setPosition(0)
54 let bitReader = stream.bitReader()
55 check huffmantree.deserialise(bitReader, uint) == tree
56
57 test "serialise":
58 let stream = newStringStream()
59 defer: stream.close()
60 let bitWriter = stream.bitWriter()
61 tree.serialise(bitWriter)
62 bitWriter.flush()
63
64 stream.setPosition(0)
65 let bitReader = stream.bitReader()
66 check bitReader.readBits(valueLengthFieldBitLength, uint8) == 2
67 check bitReader.readBool() == false # root
68 check bitReader.readBool() == true # 1 leaf
69 check bitReader.readBits(2, uint8) == 1
70 check bitReader.readBool() == false # right branch
71 check bitReader.readBool() == true # 2 leaf
72 check bitReader.readBits(2, uint8) == 2
73 check bitReader.readBool() == true # 3 leaf
74 check bitReader.readBits(2, uint8) == 3