diff options
author | pacien | 2018-11-25 19:11:51 +0100 |
---|---|---|
committer | pacien | 2018-11-25 19:11:51 +0100 |
commit | 00cacbd664b1335bc8e7d9553448d1ad1f1744c7 (patch) | |
tree | 595b96a1d230aa5885d4bddbef6dd189fd21adaf | |
parent | 680c0a3c94f0bb84a2773bc9a95dc5399b6925fb (diff) | |
download | gziplike-00cacbd664b1335bc8e7d9553448d1ad1f1744c7.tar.gz |
Add text file identity test
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/main.nim | 6 | ||||
-rw-r--r-- | tests/tmain.nim | 32 |
3 files changed, 36 insertions, 3 deletions
@@ -1,6 +1,7 @@ | |||
1 | *.swp | 1 | *.swp |
2 | *~ | 2 | *~ |
3 | nimcache/ | 3 | nimcache/ |
4 | tmp/ | ||
4 | 5 | ||
5 | * | 6 | * |
6 | !*/ | 7 | !*/ |
diff --git a/src/main.nim b/src/main.nim index 8f0536c..4fc137f 100644 --- a/src/main.nim +++ b/src/main.nim | |||
@@ -17,20 +17,20 @@ | |||
17 | import os, streams, sugar | 17 | import os, streams, sugar |
18 | import bitreader, bitwriter, streamblock | 18 | import bitreader, bitwriter, streamblock |
19 | 19 | ||
20 | proc transform(operation: (BitReader, BitWriter) -> void, input, output: string) = | 20 | proc transform*(operation: (BitReader, BitWriter) -> void, input, output: string) = |
21 | let inputStream = openFileStream(input, fmRead) | 21 | let inputStream = openFileStream(input, fmRead) |
22 | defer: inputStream.close() | 22 | defer: inputStream.close() |
23 | let outputStream = openFileStream(output, fmWrite) | 23 | let outputStream = openFileStream(output, fmWrite) |
24 | defer: outputStream.close() | 24 | defer: outputStream.close() |
25 | operation(inputStream.bitReader(), outputStream.bitWriter()) | 25 | operation(inputStream.bitReader(), outputStream.bitWriter()) |
26 | 26 | ||
27 | proc compress(bitReader: BitReader, bitWriter: BitWriter) = | 27 | proc compress*(bitReader: BitReader, bitWriter: BitWriter) = |
28 | while not bitReader.atEnd(): | 28 | while not bitReader.atEnd(): |
29 | let streamBlock = streamblock.readRaw(bitReader) | 29 | let streamBlock = streamblock.readRaw(bitReader) |
30 | streamBlock.writeSerialisedTo(bitWriter) | 30 | streamBlock.writeSerialisedTo(bitWriter) |
31 | bitWriter.flush() | 31 | bitWriter.flush() |
32 | 32 | ||
33 | proc decompress(bitReader: BitReader, bitWriter: BitWriter) = | 33 | proc decompress*(bitReader: BitReader, bitWriter: BitWriter) = |
34 | var hasMore = true | 34 | var hasMore = true |
35 | while hasMore: | 35 | while hasMore: |
36 | let streamBlock = streamblock.readSerialised(bitReader) | 36 | let streamBlock = streamblock.readSerialised(bitReader) |
diff --git a/tests/tmain.nim b/tests/tmain.nim new file mode 100644 index 0000000..2deb443 --- /dev/null +++ b/tests/tmain.nim | |||
@@ -0,0 +1,32 @@ | |||
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, os, ospaths, osproc | ||
18 | import main | ||
19 | |||
20 | const tempDir = "tmp" | ||
21 | |||
22 | suite "main": | ||
23 | setup: createDir(tempDir) | ||
24 | teardown: removeDir(tempDir) | ||
25 | |||
26 | test "identity": | ||
27 | let input = "license.md" | ||
28 | let intermediate = tempDir / "compressed" | ||
29 | let final = tempDir / "decompressed" | ||
30 | compress.transform(input, intermediate) | ||
31 | decompress.transform(intermediate, final) | ||
32 | check startProcess("cmp", args=[input, final], options={poUsePath}).waitForExit() == 0 | ||