diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/thuffmantree.nim | 74 | ||||
-rw-r--r-- | tests/tintegers.nim | 4 |
2 files changed, 78 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 | |||
17 | import unittest, streams | ||
18 | import bitreader, bitwriter, huffmantree | ||
19 | |||
20 | suite "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 | ||
diff --git a/tests/tintegers.nim b/tests/tintegers.nim index 956e4aa..851e926 100644 --- a/tests/tintegers.nim +++ b/tests/tintegers.nim | |||
@@ -27,6 +27,10 @@ suite "integers": | |||
27 | check truncateToUint8(0x00FA'u16) == 0xFA'u8 | 27 | check truncateToUint8(0x00FA'u16) == 0xFA'u8 |
28 | check truncateToUint8(0xFFFA'u16) == 0xFA'u8 | 28 | check truncateToUint8(0xFFFA'u16) == 0xFA'u8 |
29 | 29 | ||
30 | test "bitLength": | ||
31 | check bitLength(0b1_1111) == 5 | ||
32 | check bitLength(0b1000_0000) == 8 | ||
33 | |||
30 | test "leastSignificantBits": | 34 | test "leastSignificantBits": |
31 | check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8 | 35 | check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8 |
32 | check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8 | 36 | check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8 |