aboutsummaryrefslogtreecommitdiff
path: root/tests/blocks/tstreamblock.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/blocks/tstreamblock.nim')
-rw-r--r--tests/blocks/tstreamblock.nim68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/blocks/tstreamblock.nim b/tests/blocks/tstreamblock.nim
new file mode 100644
index 0000000..4d92d69
--- /dev/null
+++ b/tests/blocks/tstreamblock.nim
@@ -0,0 +1,68 @@
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 bitio/bitreader, bitio/bitwriter, blocks/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 check streamBlock.isLast()
29
30 let outputStream = newStringStream()
31 defer: outputStream.close()
32 let outputBitWriter = outputStream.bitWriter()
33 streamBlock.writeSerialisedTo(outputBitWriter)
34 outputBitWriter.flush()
35
36 outputStream.setPosition(0)
37 let produceReader = outputStream.bitReader()
38 check produceReader.readBool() == true # last block flag
39 check produceReader.readBits(2, uint8) == 0x00'u8 # block kind
40 check produceReader.readBits(16, uint16) == 64 # raw block length
41 check produceReader.readSeq(64, uint8) == (64, @[0x10'u8, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]) # raw block content
42 discard produceReader.readBits(8 - 2 - 1, uint8)
43 check produceReader.atEnd()
44
45 test "deserialise":
46 let serialisedStream = newStringStream()
47 defer: serialisedStream.close()
48 let serialisedBitWriter = serialisedStream.bitWriter()
49 serialisedBitWriter.writeBool(true)
50 serialisedBitWriter.writeBits(2, 0x00'u8)
51 serialisedBitWriter.writeBits(16, 64'u16)
52 serialisedBitWriter.writeBits(64, 0xFEDC_BA98_7654_3210'u64)
53 serialisedBitWriter.flush()
54
55 serialisedStream.setPosition(0)
56 let serialisedBitReader = serialisedStream.bitReader()
57 let streamBlock = streamblock.readSerialised(serialisedBitReader)
58
59 let outputStream = newStringStream()
60 defer: outputStream.close()
61 let outputBitWriter = outputStream.bitWriter()
62 check streamBlock.isLast()
63 streamBlock.writeRawTo(outputBitWriter)
64 outputBitWriter.flush()
65
66 outputStream.setPosition(0)
67 check outputStream.readUint64 == 0xFEDC_BA98_7654_3210'u64
68 check outputStream.atEnd()