aboutsummaryrefslogtreecommitdiff
path: root/tests/tlzssencoder.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tlzssencoder.nim')
-rw-r--r--tests/tlzssencoder.nim62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/tlzssencoder.nim b/tests/tlzssencoder.nim
new file mode 100644
index 0000000..253d0ac
--- /dev/null
+++ b/tests/tlzssencoder.nim
@@ -0,0 +1,62 @@
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, lists, tables
18import matchtable, lzssnode, lzsschain, lzssencoder
19
20suite "lzssencoder":
21 test "commonPrefixLength":
22 check commonPrefixLength([], [], 0, 10) == 0
23 check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 0, 10) == 2
24 check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 1, 10) == 2
25 check commonPrefixLength([1'u8, 2, 3], [1'u8, 2, 4], 1, 10) == 2
26 check commonPrefixLength([1'u8, 2, 3, 4], [1'u8, 2, 3, 4], 1, 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 = initSinglyLinkedList[int]()
35 candidatePos.prepend(0)
36 candidatePos.prepend(4)
37 candidatePos.prepend(8)
38 let result = longestPrefix(candidatePos, buffer.toOpenArray(0, 10), buffer.toOpenArray(11, buffer.len - 1))
39 check result.pos == 4
40 check result.length == 4
41
42 test "addGroups":
43 let matchTable = initMatchTable(seq[uint8], int)
44 let buffer = toSeq(0'u8..10'u8)
45 matchTable.addGroups(buffer, 0, 1)
46 check matchTable.len == 0
47 matchTable.addGroups(buffer, 2, 9)
48 check matchTable.len == 5
49 check toSeq(matchTable.matchList(@[1'u8, 2, 3]).items).len == 0
50 check toSeq(matchTable.matchList(@[7'u8, 8, 9]).items).len == 0
51 check toSeq(matchTable.matchList(@[2'u8, 3, 4]).items) == @[2]
52 check toSeq(matchTable.matchList(@[4'u8, 5, 6]).items) == @[4]
53 check toSeq(matchTable.matchList(@[6'u8, 7, 8]).items) == @[6]
54
55 test "lzssEncode":
56 let buffer = [0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
57 check toSeq(lzssEncode(buffer).items) == @[
58 lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
59 lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
60 lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
61 lzssReference(3, 8), lzssCharacter(5),
62 lzssReference(3, 3), lzssCharacter(5)]