aboutsummaryrefslogtreecommitdiff
path: root/tests/lzss/tlzssencoder.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lzss/tlzssencoder.nim')
-rw-r--r--tests/lzss/tlzssencoder.nim59
1 files changed, 59 insertions, 0 deletions
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)]