aboutsummaryrefslogtreecommitdiff
path: root/tests/lzss
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lzss')
-rw-r--r--tests/lzss/tlzsschain.nim29
-rw-r--r--tests/lzss/tlzssencoder.nim59
-rw-r--r--tests/lzss/tlzssnode.nim26
-rw-r--r--tests/lzss/tmatchring.nim35
-rw-r--r--tests/lzss/tmatchtable.nim29
5 files changed, 178 insertions, 0 deletions
diff --git a/tests/lzss/tlzsschain.nim b/tests/lzss/tlzsschain.nim
new file mode 100644
index 0000000..92bee0f
--- /dev/null
+++ b/tests/lzss/tlzsschain.nim
@@ -0,0 +1,29 @@
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, lzss/lzsschain
19
20suite "lzsschain":
21 test "decode":
22 let chain = lzssChain([
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 check chain.decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
29 \ No newline at end of file
diff --git a/tests/lzss/tlzssencoder.nim b/tests/lzss/tlzssencoder.nim
new file mode 100644
index 0000000..48477d7
--- /dev/null
+++ b/tests/lzss/tlzssencoder.nim
@@ -0,0 +1,59 @@
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
18import lzss/matchring, lzss/matchtable, lzss/lzssnode, lzss/lzssencoder
19
20suite "lzssencoder":
21 test "commonPrefixLength":
22 check commonPrefixLength([], [], 10) == 0
23 check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 10) == 2
24 check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 10) == 2
25 check commonPrefixLength([1'u8, 2, 3], [1'u8, 2, 4], 10) == 2
26 check commonPrefixLength([1'u8, 2, 3, 4], [1'u8, 2, 3, 4], 3) == 3
27
28 test "longestPrefix":
29 let buffer = [
30 0'u8, 1, 2, 9,
31 0, 1, 2, 3,
32 0, 1, 2,
33 0, 1, 2, 3, 4]
34 var candidatePos = [0, 4, 8]
35 var matchRing = initMatchRing()
36 for pos in candidatePos: matchRing.addMatch(pos)
37 let result = longestPrefix(matchRing, buffer.toOpenArray(0, 10), buffer.toOpenArray(11, buffer.len - 1))
38 check result.pos == 4
39 check result.length == 4
40
41 test "addGroups":
42 var matchTable = initMatchTable()
43 let buffer = toSeq(0'u8..10'u8)
44 matchTable.addGroups(buffer, 0, 1)
45 matchTable.addGroups(buffer, 2, 9)
46 check toSeq(matchTable.candidates([1'u8, 2, 3]).items).len == 0
47 check toSeq(matchTable.candidates([7'u8, 8, 9]).items).len == 0
48 check toSeq(matchTable.candidates([2'u8, 3, 4]).items) == [2]
49 check toSeq(matchTable.candidates([4'u8, 5, 6]).items) == [4]
50 check toSeq(matchTable.candidates([6'u8, 7, 8]).items) == [6]
51
52 test "lzssEncode":
53 let buffer = [0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
54 check lzssEncode(buffer) == [
55 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
56 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
57 lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
58 lzssReference(3, 8), lzssCharacter(5),
59 lzssReference(3, 3), lzssCharacter(5)]
diff --git a/tests/lzss/tlzssnode.nim b/tests/lzss/tlzssnode.nim
new file mode 100644
index 0000000..7675fc0
--- /dev/null
+++ b/tests/lzss/tlzssnode.nim
@@ -0,0 +1,26 @@
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
19
20suite "lzssnode":
21 test "equality":
22 check lzssCharacter(1) == lzssCharacter(1)
23 check lzssCharacter(0) != lzssCharacter(1)
24 check lzssReference(0, 1) == lzssReference(0, 1)
25 check lzssReference(1, 0) != lzssReference(0, 1)
26 check lzssCharacter(0) != lzssReference(0, 1)
diff --git a/tests/lzss/tmatchring.nim b/tests/lzss/tmatchring.nim
new file mode 100644
index 0000000..ccf3856
--- /dev/null
+++ b/tests/lzss/tmatchring.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, sequtils, algorithm
18import lzss/matchring
19
20suite "matchring":
21 test "items (empty)":
22 var ring = initMatchRing()
23 check toSeq(ring.items).len == 0
24
25 test "addMatch, items (partial)":
26 var ring = initMatchRing()
27 let items = [0, 1, 2]
28 for i in items: ring.addMatch(i)
29 check toSeq(ring.items) == items.reversed()
30
31 test "addMatch, items (rolling)":
32 var ring = initMatchRing()
33 let items = toSeq(0..13)
34 for i in items: ring.addMatch(i)
35 check toSeq(ring.items) == items[^matchLimit..<items.len].reversed()
diff --git a/tests/lzss/tmatchtable.nim b/tests/lzss/tmatchtable.nim
new file mode 100644
index 0000000..c55e917
--- /dev/null
+++ b/tests/lzss/tmatchtable.nim
@@ -0,0 +1,29 @@
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
18import lzss/matchring, lzss/matchtable
19
20suite "matchtable":
21 test "addMatch":
22 var matchTable = initMatchTable()
23 matchTable.addMatch([0'u8, 1, 2], 42)
24 matchTable.addMatch([2'u8, 1, 0], 24)
25 check toSeq(matchTable.candidates([0'u8, 1, 2]).items) == [42]
26 check toSeq(matchTable.candidates([2'u8, 1, 0]).items) == [24]
27 matchTable.addMatch([0'u8, 1, 2], 1337)
28 check toSeq(matchTable.candidates([0'u8, 1, 2]).items) == [1337, 42]
29 check toSeq(matchTable.candidates([2'u8, 1, 0]).items) == [24]