aboutsummaryrefslogtreecommitdiff
path: root/tests/lzsshuffman
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lzsshuffman')
-rw-r--r--tests/lzsshuffman/tlzsshuffmandecoder.nim65
-rw-r--r--tests/lzsshuffman/tlzsshuffmanencoder.nim66
-rw-r--r--tests/lzsshuffman/tlzsshuffmanstats.nim35
-rw-r--r--tests/lzsshuffman/tlzsshuffmansymbol.nim37
4 files changed, 203 insertions, 0 deletions
diff --git a/tests/lzsshuffman/tlzsshuffmandecoder.nim b/tests/lzsshuffman/tlzsshuffmandecoder.nim
new file mode 100644
index 0000000..b8cd589
--- /dev/null
+++ b/tests/lzsshuffman/tlzsshuffmandecoder.nim
@@ -0,0 +1,65 @@
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/bitwriter, bitio/bitreader
19import lzss/lzssnode
20import huffman/huffmantree, huffman/huffmandecoder
21import lzsshuffman/lzsshuffmansymbol, lzsshuffman/lzsshuffmandecoder
22
23suite "lzsshuffmandecoder":
24 test "readChain (empty)":
25 let symbolTree = huffmanLeaf(endSymbol.uint16)
26 let positionTree = huffmanLeaf(0'u16)
27 let stream = newStringStream()
28 defer: stream.close()
29 stream.write(0'u8) # eof
30 stream.setPosition(0)
31 let bitReader = stream.bitReader()
32 let result = readChain(bitReader, symbolTree.decoder(), positionTree.decoder(), 32_000)
33 check result.len == 0
34
35 test "readChain (minimal)":
36 let symbolTree = huffmanBranch(
37 huffmanBranch(
38 huffmanLeaf(0'u16),
39 huffmanLeaf(1'u16)),
40 huffmanBranch(
41 huffmanLeaf(257'u16),
42 huffmanBranch(
43 huffmanLeaf(2'u16),
44 huffmanLeaf(256'u16))))
45 let positionTree = huffmanBranch(
46 huffmanLeaf(3'u16),
47 huffmanLeaf(4'u16))
48 let stream = newStringStream()
49 defer: stream.close()
50 let bitWriter = stream.bitWriter()
51 bitWriter.writeBits(2, 0b00'u8)
52 bitWriter.writeBits(2, 0b10'u8)
53 bitWriter.writeBits(3, 0b011'u8)
54 bitWriter.writeBits(2, 0b01'u8)
55 bitWriter.writeBits(1, 0b0'u8)
56 bitWriter.writeBits(2, 0b01'u8)
57 bitWriter.writeBits(1, 0b1'u8)
58 bitWriter.writeBits(3, 0b111'u8)
59 bitWriter.flush()
60 stream.setPosition(0)
61 let bitReader = stream.bitReader()
62 let result = readChain(bitReader, symbolTree.decoder(), positionTree.decoder(), 32_000)
63 check result == [
64 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
65 lzssReference(3, 3), lzssReference(3, 4)]
diff --git a/tests/lzsshuffman/tlzsshuffmanencoder.nim b/tests/lzsshuffman/tlzsshuffmanencoder.nim
new file mode 100644
index 0000000..100b5f4
--- /dev/null
+++ b/tests/lzsshuffman/tlzsshuffmanencoder.nim
@@ -0,0 +1,66 @@
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/bitwriter, bitio/bitreader
19import lzss/lzssnode, lzss/lzsschain
20import huffman/huffmantree, huffman/huffmanencoder
21import lzsshuffman/lzsshuffmansymbol, lzsshuffman/lzsshuffmanencoder
22
23suite "lzsshuffmanencoder":
24 test "writeChain (empty)":
25 let chain = lzssChain(newSeq[LzssNode]())
26 let symbolTree = huffmanLeaf(endSymbol.uint16)
27 let positionTree = huffmanLeaf(0'u16)
28 let stream = newStringStream()
29 defer: stream.close()
30 let bitWriter = stream.bitWriter()
31 writeChain(chain, symbolTree.encoder(uint16), positionTree.encoder(uint16), bitWriter)
32 bitWriter.flush()
33 stream.setPosition(0)
34 check stream.atEnd()
35
36 test "writeChain (minimal)":
37 let chain = lzssChain([
38 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
39 lzssReference(3, 3), lzssReference(3, 4)])
40 let symbolTree = huffmanBranch(
41 huffmanBranch(
42 huffmanLeaf(0'u16),
43 huffmanLeaf(1'u16)),
44 huffmanBranch(
45 huffmanLeaf(257'u16),
46 huffmanBranch(
47 huffmanLeaf(2'u16),
48 huffmanLeaf(256'u16))))
49 let positionTree = huffmanBranch(
50 huffmanLeaf(3'u16),
51 huffmanLeaf(4'u16))
52 let stream = newStringStream()
53 defer: stream.close()
54 let bitWriter = stream.bitWriter()
55 writeChain(chain, symbolTree.encoder(uint16), positionTree.encoder(uint16), bitWriter)
56 bitWriter.flush()
57 stream.setPosition(0)
58 let bitReader = stream.bitReader()
59 check bitReader.readBits(2, uint8) == 0b00'u8 # char 0
60 check bitReader.readBits(2, uint8) == 0b10'u8 # char 1
61 check bitReader.readBits(3, uint8) == 0b011'u8 # char 2
62 check bitReader.readBits(2, uint8) == 0b01'u8 # ref len 3
63 check bitReader.readBits(1, uint8) == 0b0'u8 # ref pos 3
64 check bitReader.readBits(2, uint8) == 0b01'u8 # ref len 3
65 check bitReader.readBits(1, uint8) == 0b1'u8 # ref pos 4
66 check bitReader.readBits(3, uint8) == 0b111'u8 # eof
diff --git a/tests/lzsshuffman/tlzsshuffmanstats.nim b/tests/lzsshuffman/tlzsshuffmanstats.nim
new file mode 100644
index 0000000..55eaf96
--- /dev/null
+++ b/tests/lzsshuffman/tlzsshuffmanstats.nim
@@ -0,0 +1,35 @@
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 lzss/lzssnode, lzss/lzsschain
19import lzsshuffman/lzsshuffmansymbol, lzsshuffman/lzsshuffmanstats
20
21suite "lzsshuffmanstats":
22 test "aggretateStats":
23 let chain = lzssChain([
24 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
25 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
26 lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
27 lzssReference(3, 8), lzssCharacter(5),
28 lzssReference(3, 3), lzssCharacter(5)])
29 let (symbolTable, positionTable) = chain.aggregateStats()
30 check symbolTable == newCountTable(concat(
31 repeat(0'u16, 2), repeat(1'u16, 2), repeat(2'u16, 1), repeat(3'u16, 1), repeat(4'u16, 1), repeat(5'u16, 3),
32 repeat(endSymbol.uint16, 1),
33 repeat(257'u16, 2), repeat(258'u16, 1)))
34 check positionTable == newCountTable(concat(
35 repeat(3'u16, 1), repeat(6'u16, 1), repeat(8'u16, 1)))
diff --git a/tests/lzsshuffman/tlzsshuffmansymbol.nim b/tests/lzsshuffman/tlzsshuffmansymbol.nim
new file mode 100644
index 0000000..1e4653d
--- /dev/null
+++ b/tests/lzsshuffman/tlzsshuffmansymbol.nim
@@ -0,0 +1,37 @@
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
18import lzss/lzssnode
19import lzsshuffman/lzsshuffmansymbol
20
21suite "lzsshuffmansymbol":
22 test "isEndMarker":
23 check 'a'.Symbol.isEndMarker() == false
24 check endSymbol.isEndMarker()
25
26 test "isCharacter":
27 check 'a'.Symbol.isCharacter()
28 check endSymbol.isCharacter() == false
29 check 300.Symbol.isCharacter() == false
30
31 test "unpackLzssReference":
32 check unpackLzssReference(257.Symbol, 10) == lzssReference(3, 10)
33 check unpackLzssReference(300.Symbol, 10) == lzssReference(46, 10)
34
35 test "shiftLzssLength":
36 check shiftLzssLength(3) == 257'u16
37 check shiftLzssLength(10) == 264'u16