aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lzss/listpolyfill.nim (renamed from src/polyfill.nim)2
-rw-r--r--src/lzss/lzsschain.nim (renamed from src/lzsschain.nim)3
-rw-r--r--src/lzss/lzssencoder.nim (renamed from src/lzssencoder.nim)2
-rw-r--r--src/lzss/lzssnode.nim (renamed from src/lzssnode.nim)0
-rw-r--r--src/lzss/matchtable.nim (renamed from src/matchtable.nim)4
-rw-r--r--tests/tlzss.nim120
-rw-r--r--tests/tlzsschain.nim42
-rw-r--r--tests/tlzssencoder.nim62
-rw-r--r--tests/tlzssnode.nim26
-rw-r--r--tests/tmatchtable.nim35
-rw-r--r--tests/tpolyfill.nim27
11 files changed, 126 insertions, 197 deletions
diff --git a/src/polyfill.nim b/src/lzss/listpolyfill.nim
index b252953..00b30ee 100644
--- a/src/polyfill.nim
+++ b/src/lzss/listpolyfill.nim
@@ -26,7 +26,7 @@ proc prepend*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) =
26 26
27proc prepend*[T](L: var SinglyLinkedList[T], value: T) = 27proc prepend*[T](L: var SinglyLinkedList[T], value: T) =
28 ## prepends a node to `L`. Efficiency: O(1). 28 ## prepends a node to `L`. Efficiency: O(1).
29 polyfill.prepend(L, newSinglyLinkedNode(value)) 29 listpolyfill.prepend(L, newSinglyLinkedNode(value))
30 30
31proc append*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) = 31proc append*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) =
32 ## appends a node `n` to `L`. Efficiency: O(1). 32 ## appends a node `n` to `L`. Efficiency: O(1).
diff --git a/src/lzsschain.nim b/src/lzss/lzsschain.nim
index 44200f2..2ecff9e 100644
--- a/src/lzsschain.nim
+++ b/src/lzss/lzsschain.nim
@@ -15,7 +15,8 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import lists, tables, sugar 17import lists, tables, sugar
18import polyfill, integers, lzssnode, huffman/huffmantree 18import ../integers, ../huffman/huffmantree
19import listpolyfill, lzssnode
19 20
20const maxChainByteLength = 32_000 * wordBitLength 21const maxChainByteLength = 32_000 * wordBitLength
21 22
diff --git a/src/lzssencoder.nim b/src/lzss/lzssencoder.nim
index 05f3a16..8b750fb 100644
--- a/src/lzssencoder.nim
+++ b/src/lzss/lzssencoder.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import lists 17import lists
18import polyfill, matchtable, lzssnode, lzsschain 18import listpolyfill, matchtable, lzssnode, lzsschain
19 19
20const matchGroupLength = 3 20const matchGroupLength = 3
21const maxRefByteLength = high(uint8).int + matchGroupLength 21const maxRefByteLength = high(uint8).int + matchGroupLength
diff --git a/src/lzssnode.nim b/src/lzss/lzssnode.nim
index de5958d..de5958d 100644
--- a/src/lzssnode.nim
+++ b/src/lzss/lzssnode.nim
diff --git a/src/matchtable.nim b/src/lzss/matchtable.nim
index 5be652c..b17ce68 100644
--- a/src/matchtable.nim
+++ b/src/lzss/matchtable.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import tables, lists 17import tables, lists
18import polyfill 18import listpolyfill
19 19
20type MatchTable*[K, V] = 20type MatchTable*[K, V] =
21 TableRef[K, SinglyLinkedList[V]] 21 TableRef[K, SinglyLinkedList[V]]
@@ -28,5 +28,5 @@ proc matchList*[K, V](matchTable: MatchTable[K, V], pattern: K): SinglyLinkedLis
28 28
29proc addMatch*[K, V](matchTable: MatchTable[K, V], pattern: K, value: V) = 29proc addMatch*[K, V](matchTable: MatchTable[K, V], pattern: K, value: V) =
30 var matchList = matchTable.matchList(pattern) 30 var matchList = matchTable.matchList(pattern)
31 polyfill.prepend(matchList, value) 31 listpolyfill.prepend(matchList, value)
32 matchTable[pattern] = matchList 32 matchTable[pattern] = matchList
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)]
diff --git a/tests/tlzsschain.nim b/tests/tlzsschain.nim
deleted file mode 100644
index a8c2012..0000000
--- a/tests/tlzsschain.nim
+++ /dev/null
@@ -1,42 +0,0 @@
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
18import polyfill, lzssnode, lzsschain
19
20suite "lzsschain":
21 proc chain(): LzssChain =
22 let chainArray = [
23 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
24 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
25 lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
26 lzssReference(3, 8), lzssCharacter(5),
27 lzssReference(3, 3), lzssCharacter(5)]
28 var chain = lzssChain()
29 for node in chainArray: chain.append(node)
30 result = chain
31
32 test "decode":
33 check chain().decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
34
35 test "stats":
36 let stats = chain().stats()
37 check stats.characters == newCountTable(concat(
38 repeat(0'u8, 2), repeat(1'u8, 2), repeat(2'u8, 1), repeat(3'u8, 1), repeat(4'u8, 1), repeat(5'u8, 3)))
39 check stats.lengths == newCountTable(concat(
40 repeat(3, 2), repeat(4, 1)))
41 check stats.positions == newCountTable(concat(
42 repeat(3, 1), repeat(6, 1), repeat(8, 1)))
diff --git a/tests/tlzssencoder.nim b/tests/tlzssencoder.nim
deleted file mode 100644
index 253d0ac..0000000
--- a/tests/tlzssencoder.nim
+++ /dev/null
@@ -1,62 +0,0 @@