diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tstreamblock.nim | 66 |
1 files changed, 66 insertions, 0 deletions
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 | |||
17 | import unittest, streams | ||
18 | import bitreader, bitwriter, streamblock | ||
19 | |||
20 | suite "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() | ||