aboutsummaryrefslogtreecommitdiff
path: root/tests/tlzss.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tlzss.nim')
-rw-r--r--tests/tlzss.nim120
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/tlzss.nim b/tests/tlzss.nim
new file mode 100644
index 0000000..7325d5b
--- /dev/null
+++ b/tests/tlzss.nim
@@ -0,0 +1,120 @@
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, sequtils, tables, lists
18import lzss/listpolyfill, lzss/matchtable, lzss/lzssnode, lzss/lzsschain, lzss/lzssencoder
19
20suite "listpolyfill":
21 test "append":
22 const data = [1, 2, 3, 4, 5, 6]
23 var L: SinglyLinkedList[int]
24 for d in items(data): listpolyfill.prepend(L, d)
25 for d in items(data): listpolyfill.append(L, d)
26 check $L == "[6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6]"
27 check 4 in L
28
29suite "matchtable":
30 test "matchList":
31 let matchTable = initMatchTable(seq[int], int)
32 check toSeq(matchTable.matchList(@[0, 1, 2]).items).len == 0
33
34 test "addMatch":
35 let matchTable = initMatchTable(seq[int], int)
36 matchTable.addMatch(@[0, 1, 2], 42)
37 matchTable.addMatch(@[2, 1, 0], 24)
38 check matchTable.len == 2
39 check toSeq(matchTable.matchList(@[0, 1, 2]).items) == @[42]
40 check toSeq(matchTable.matchList(@[2, 1, 0]).items) == @[24]
41 matchTable.addMatch(@[0, 1, 2], 1337)
42 check matchTable.len == 2
43 check toSeq(matchTable.matchList(@[0, 1, 2]).items) == @[1337, 42]
44 check toSeq(matchTable.matchList(@[2, 1, 0]).items) == @[24]
45
46suite "lzssnode":
47 test "equality":
48 check lzssCharacter(1) == lzssCharacter(1)
49 check lzssCharacter(0) != lzssCharacter(1)
50 check lzssReference(0, 1) == lzssReference(0, 1)
51 check lzssReference(1, 0) != lzssReference(0, 1)
52 check lzssCharacter(0) != lzssReference(0, 1)
53
54suite "lzsschain":
55 proc chain(): LzssChain =
56 let chainArray = [
57 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
58 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
59 lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
60 lzssReference(3, 8), lzssCharacter(5),
61 lzssReference(3, 3), lzssCharacter(5)]
62 var chain = lzssChain()
63 for node in chainArray: chain.append(node)
64 result = chain
65
66 test "decode":
67 check chain().decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
68
69 test "stats":
70 let stats = chain().stats()
71 check stats.characters == newCountTable(concat(
72 repeat(0'u8, 2), repeat(1'u8, 2), repeat(2'u8, 1), repeat(3'u8, 1), repeat(4'u8, 1), repeat(5'u8, 3)))
73 check stats.lengths == newCountTable(concat(
74 repeat(3, 2), repeat(4, 1)))
75 check stats.positions == newCountTable(concat(
76 repeat(3, 1), repeat(6, 1), repeat(8, 1)))
77
78suite "lzssencoder":
79 test "commonPrefixLength":
80 check commonPrefixLength([], [], 0, 10) == 0
81 check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 0, 10) == 2
82 check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 1, 10) == 2
83 check commonPrefixLength([1'u8, 2, 3], [1'u8, 2, 4], 1, 10) == 2
84 check commonPrefixLength([1'u8, 2, 3, 4], [1'u8, 2, 3, 4], 1, 3) == 3
85
86 test "longestPrefix":
87 let buffer = [
88 0'u8, 1, 2, 9,
89 0, 1, 2, 3,
90 0, 1, 2,
91 0, 1, 2, 3, 4]
92 var candidatePos = initSinglyLinkedList[int]()
93 listpolyfill.prepend(candidatePos, 0)
94 listpolyfill.prepend(candidatePos, 4)
95 listpolyfill.prepend(candidatePos, 8)
96 let result = longestPrefix(candidatePos, buffer.toOpenArray(0, 10), buffer.toOpenArray(11, buffer.len - 1))
97 check result.pos == 4
98 check result.length == 4
99
100 test "addGroups":
101 let matchTable = initMatchTable(seq[uint8], int)
102 let buffer = toSeq(0'u8..10'u8)
103 matchTable.addGroups(buffer, 0, 1)
104 check matchTable.len == 0
105 matchTable.addGroups(buffer, 2, 9)
106 check matchTable.len == 5
107 check toSeq(matchTable.matchList(@[1'u8, 2, 3]).items).len == 0
108 check toSeq(matchTable.matchList(@[7'u8, 8, 9]).items).len == 0
109 check toSeq(matchTable.matchList(@[2'u8, 3, 4]).items) == @[2]
110 check toSeq(matchTable.matchList(@[4'u8, 5, 6]).items) == @[4]
111 check toSeq(matchTable.matchList(@[6'u8, 7, 8]).items) == @[6]
112
113 test "lzssEncode":
114 let buffer = [0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
115 check toSeq(lzssEncode(buffer).items) == @[
116 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
117 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
118 lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
119 lzssReference(3, 8), lzssCharacter(5),
120 lzssReference(3, 3), lzssCharacter(5)]