diff options
author | pacien | 2018-11-30 18:44:20 +0100 |
---|---|---|
committer | pacien | 2018-11-30 18:44:20 +0100 |
commit | 5bbe75659aef55542268cbf35c66342cb22ce865 (patch) | |
tree | 53c1e79c4195be546ba5762d61eb995a4f9e0530 /tests | |
parent | e88f60b63cb05f56a61060a953c726b7a78c0652 (diff) | |
download | gziplike-5bbe75659aef55542268cbf35c66342cb22ce865.tar.gz |
isolate lzss chain module
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tlzss.nim | 120 | ||||
-rw-r--r-- | tests/tlzsschain.nim | 42 | ||||
-rw-r--r-- | tests/tlzssencoder.nim | 62 | ||||
-rw-r--r-- | tests/tlzssnode.nim | 26 | ||||
-rw-r--r-- | tests/tmatchtable.nim | 35 | ||||
-rw-r--r-- | tests/tpolyfill.nim | 27 |
6 files changed, 120 insertions, 192 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 | |||
17 | import unittest, sequtils, tables, lists | ||
18 | import lzss/listpolyfill, lzss/matchtable, lzss/lzssnode, lzss/lzsschain, lzss/lzssencoder | ||
19 | |||
20 | suite "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 | |||
29 | suite "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 | |||
46 | suite "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 | |||
54 | suite "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 | |||
78 | suite "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 | |||
17 | import unittest, sequtils, tables | ||
18 | import polyfill, lzssnode, lzsschain | ||
19 | |||
20 | suite "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 @@ | |||
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 | |||
17 | import unittest, sequtils, lists, tables | ||
18 | import matchtable, lzssnode, lzsschain, lzssencoder | ||
19 | |||
20 | suite "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)] | ||
diff --git a/tests/tlzssnode.nim b/tests/tlzssnode.nim deleted file mode 100644 index cb584ab..0000000 --- a/tests/tlzssnode.nim +++ /dev/null | |||
@@ -1,26 +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 | ||