From 3d44208aaaeca516eb08a90c98635543cae2bd4d Mon Sep 17 00:00:00 2001
From: pacien
Date: Tue, 27 Nov 2018 20:26:35 +0100
Subject: implement lzss encoding
---
tests/tlzsschain.nim | 30 ++++++++++++++++++++++++
tests/tlzssencoder.nim | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++
tests/tlzssnode.nim | 26 +++++++++++++++++++++
tests/tmatchtable.nim | 35 ++++++++++++++++++++++++++++
tests/tpolyfill.nim | 27 ++++++++++++++++++++++
5 files changed, 180 insertions(+)
create mode 100644 tests/tlzsschain.nim
create mode 100644 tests/tlzssencoder.nim
create mode 100644 tests/tlzssnode.nim
create mode 100644 tests/tmatchtable.nim
create mode 100644 tests/tpolyfill.nim
(limited to 'tests')
diff --git a/tests/tlzsschain.nim b/tests/tlzsschain.nim
new file mode 100644
index 0000000..241a0f1
--- /dev/null
+++ b/tests/tlzsschain.nim
@@ -0,0 +1,30 @@
+# gzip-like LZSS compressor
+# Copyright (C) 2018 Pacien TRAN-GIRARD
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+
+import unittest
+import polyfill, lzssnode, lzsschain
+
+suite "lzsschain":
+ test "decode":
+ let chainArray = [
+ lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
+ lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
+ lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
+ lzssReference(3, 8), lzssCharacter(5),
+ lzssReference(3, 3), lzssCharacter(5)]
+ var chain = lzssChain()
+ for node in chainArray: chain.append(node)
+ check chain.decode() == @[0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
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 @@
+# gzip-like LZSS compressor
+# Copyright (C) 2018 Pacien TRAN-GIRARD
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+
+import unittest, sequtils, lists, tables
+import matchtable, lzssnode, lzsschain, lzssencoder
+
+suite "lzssencoder":
+ test "commonPrefixLength":
+ check commonPrefixLength([], [], 0, 10) == 0
+ check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 0, 10) == 2
+ check commonPrefixLength([1'u8, 2], [1'u8, 2, 3], 1, 10) == 2
+ check commonPrefixLength([1'u8, 2, 3], [1'u8, 2, 4], 1, 10) == 2
+ check commonPrefixLength([1'u8, 2, 3, 4], [1'u8, 2, 3, 4], 1, 3) == 3
+
+ test "longestPrefix":
+ let buffer = [
+ 0'u8, 1, 2, 9,
+ 0, 1, 2, 3,
+ 0, 1, 2,
+ 0, 1, 2, 3, 4]
+ var candidatePos = initSinglyLinkedList[int]()
+ candidatePos.prepend(0)
+ candidatePos.prepend(4)
+ candidatePos.prepend(8)
+ let result = longestPrefix(candidatePos, buffer.toOpenArray(0, 10), buffer.toOpenArray(11, buffer.len - 1))
+ check result.pos == 4
+ check result.length == 4
+
+ test "addGroups":
+ let matchTable = initMatchTable(seq[uint8], int)
+ let buffer = toSeq(0'u8..10'u8)
+ matchTable.addGroups(buffer, 0, 1)
+ check matchTable.len == 0
+ matchTable.addGroups(buffer, 2, 9)
+ check matchTable.len == 5
+ check toSeq(matchTable.matchList(@[1'u8, 2, 3]).items).len == 0
+ check toSeq(matchTable.matchList(@[7'u8, 8, 9]).items).len == 0
+ check toSeq(matchTable.matchList(@[2'u8, 3, 4]).items) == @[2]
+ check toSeq(matchTable.matchList(@[4'u8, 5, 6]).items) == @[4]
+ check toSeq(matchTable.matchList(@[6'u8, 7, 8]).items) == @[6]
+
+ test "lzssEncode":
+ let buffer = [0'u8, 1, 2, 3, 4, 5, 0, 1, 2, 3, 0, 1, 4, 5, 0, 5, 5, 0, 5, 5]
+ check toSeq(lzssEncode(buffer).items) == @[
+ lzssCharacter(0), lzssCharacter(1), lzssCharacter(2),
+ lzssCharacter(3), lzssCharacter(4), lzssCharacter(5),
+ lzssReference(4, 6), lzssCharacter(0), lzssCharacter(1),
+ lzssReference(3, 8), lzssCharacter(5),
+ lzssReference(3, 3), lzssCharacter(5)]
diff --git a/tests/tlzssnode.nim b/tests/tlzssnode.nim
new file mode 100644
index 0000000..cb584ab
--- /dev/null
+++ b/tests/tlzssnode.nim
@@ -0,0 +1,26 @@
+# gzip-like LZSS compressor
+# Copyright (C) 2018 Pacien TRAN-GIRARD
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+
+import unittest
+import lzssnode
+
+suite "lzssnode":
+ test "equality":
+ check lzssCharacter(1) == lzssCharacter(1)
+ check lzssCharacter(0) != lzssCharacter(1)
+ check lzssReference(0, 1) == lzssReference(0, 1)
+ check lzssReference(1, 0) != lzssReference(0, 1)
+ check lzssCharacter(0) != lzssReference(0, 1)
diff --git a/tests/tmatchtable.nim b/tests/tmatchtable.nim
new file mode 100644
index 0000000..4b21f1d
--- /dev/null
+++ b/tests/tmatchtable.nim
@@ -0,0 +1,35 @@
+# gzip-like LZSS compressor
+# Copyright (C) 2018 Pacien TRAN-GIRARD
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+
+import unittest, lists, sequtils, tables
+import matchtable
+
+suite "matchtable":
+ test "matchList":
+ let matchTable = initMatchTable(seq[int], int)
+ check toSeq(matchTable.matchList(@[0, 1, 2]).items).len == 0
+
+ test "addMatch":
+ let matchTable = initMatchTable(seq[int], int)
+ matchTable.addMatch(@[0, 1, 2], 42)
+ matchTable.addMatch(@[2, 1, 0], 24)
+ check matchTable.len == 2
+ check toSeq(matchTable.matchList(@[0, 1, 2]).items) == @[42]
+ check toSeq(matchTable.matchList(@[2, 1, 0]).items) == @[24]
+ matchTable.addMatch(@[0, 1, 2], 1337)
+ check matchTable.len == 2
+ check toSeq(matchTable.matchList(@[0, 1, 2]).items) == @[1337, 42]
+ check toSeq(matchTable.matchList(@[2, 1, 0]).items) == @[24]
diff --git a/tests/tpolyfill.nim b/tests/tpolyfill.nim
new file mode 100644
index 0000000..b48eb77
--- /dev/null
+++ b/tests/tpolyfill.nim
@@ -0,0 +1,27 @@
+# gzip-like LZSS compressor
+# Copyright (C) 2018 Pacien TRAN-GIRARD
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+
+import unittest, sugar, lists, tables
+import polyfill
+
+suite "polyfill":
+ test "SinglyLinkedList append":
+ const data = [1, 2, 3, 4, 5, 6]
+ var L: SinglyLinkedList[int]
+ for d in items(data): polyfill.prepend(L, d)
+ for d in items(data): polyfill.append(L, d)
+ check $L == "[6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6]"
+ check 4 in L
--
cgit v1.2.3