aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-11-23 14:16:26 +0100
committerpacien2018-11-23 14:18:37 +0100
commit0a9579c37894d67d721710a13ecc8250cf702256 (patch)
tree16cc310161eda8636c876f28ffaef2fa2e9c024c
parent8c40f8aec1ebc7834dcb4a2db4f398fc3c4a2d18 (diff)
downloadgziplike-0a9579c37894d67d721710a13ecc8250cf702256.tar.gz
Add streamblock test and fix deserialisation
-rw-r--r--src/streamblock.nim4
-rw-r--r--tests/tstreamblock.nim66
2 files changed, 68 insertions, 2 deletions
diff --git a/src/streamblock.nim b/src/streamblock.nim
index 10639ed..49c3dcd 100644
--- a/src/streamblock.nim
+++ b/src/streamblock.nim
@@ -38,8 +38,8 @@ proc readSerialised*(bitReader: BitReader): StreamBlock =
38 result.last = bitReader.readBool() 38 result.last = bitReader.readBool()
39 result.kind = bitReader.readBits(2, uint8).BlockKind 39 result.kind = bitReader.readBits(2, uint8).BlockKind
40 case result.kind: 40 case result.kind:
41 of uncompressed: result.rawBlock = rawblock.readRaw(bitReader) 41 of uncompressed: result.rawBlock = rawblock.readSerialised(bitReader)
42 of lzss: result.lzssBlock = lzssblock.readRaw(bitReader) 42 of lzss: result.lzssBlock = lzssblock.readSerialised(bitReader)
43 else: raise newException(ValueError, "unhandled block type") 43 else: raise newException(ValueError, "unhandled block type")
44 44
45proc writeSerialisedTo*(streamBlock: StreamBlock, bitWriter: BitWriter) = 45proc writeSerialisedTo*(streamBlock: StreamBlock, bitWriter: BitWriter) =
diff --git a/tests/tstreamblock.nim b/tests/tstreamblock.nim
new file mode 100644
index 0000000..f1be559
--- /dev/null
+++ b/tests/tstreamblock.nim
@@ -0,0 +1,66 @@
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, streams
18import bitreader, bitwriter, streamblock
19
20suite "streamblock":
21 test "serialise":
22 let rawStream = newStringStream()
23 defer: rawStream.close()
24 rawStream.write(0xFEDC_BA98_7654_3210'u64)
25 rawStream.setPosition(0)
26 let rawBitReader = rawStream.bitReader()
27 let streamBlock = readRaw(rawBitReader, uncompressed)
28
29 let outputStream = newStringStream()
30 defer: outputStream.close()
31 let outputBitWriter = outputStream.bitWriter()
32 streamBlock.writeSerialisedTo(outputBitWriter)
33 outputBitWriter.flush()
34
35 outputStream.setPosition(0)
36 let produceReader = outputStream.bitReader()
37 check produceReader.readBool() == true # last block flag
38 check produceReader.readBits(2, uint8) == 0x00'u8 # block kind
39 check produceReader.readBits(16, uint16) == 64 # raw block length
40 check produceReader.readSeq(64, uint8) == (64, @[0x10'u8, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]) # raw block content
41 discard produceReader.readBits(8 - 2 - 1, uint8)
42 check produceReader.atEnd()
43
44 test "deserialise":
45 let serialisedStream = newStringStream()
46 defer: serialisedStream.close()
47 let serialisedBitWriter = serialisedStream.bitWriter()
48 serialisedBitWriter.writeBool(true)
49 serialisedBitWriter.writeBits(2, 0x00'u8)
50 serialisedBitWriter.writeBits(16, 64'u16)
51 serialisedBitWriter.writeBits(64, 0xFEDC_BA98_7654_3210'u64)
52 serialisedBitWriter.flush()
53
54 serialisedStream.setPosition(0)
55 let serialisedBitReader = serialisedStream.bitReader()
56 let streamBlock = readSerialised(serialisedBitReader)
57
58 let outputStream = newStringStream()
59 defer: outputStream.close()
60 let outputBitWriter = outputStream.bitWriter()
61 streamBlock.writeRawTo(outputBitWriter)
62 outputBitWriter.flush()
63
64 outputStream.setPosition(0)
65 check outputStream.readUint64 == 0xFEDC_BA98_7654_3210'u64
66 check outputStream.atEnd()