aboutsummaryrefslogtreecommitdiff
path: root/tests/bitio/tbitwriter.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/bitio/tbitwriter.nim')
-rw-r--r--tests/bitio/tbitwriter.nim85
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/bitio/tbitwriter.nim b/tests/bitio/tbitwriter.nim
new file mode 100644
index 0000000..70f5a1b
--- /dev/null
+++ b/tests/bitio/tbitwriter.nim
@@ -0,0 +1,85 @@
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/bitwriter
19
20suite "bitwriter":
21 test "flush":
22 let stream = newStringStream()
23 defer: stream.close()
24 let bitWriter = stream.bitWriter()
25
26 bitWriter.writeBool(true)
27 stream.setPosition(0)
28 expect IOError: discard stream.peekUint8()
29
30 bitWriter.flush()
31 stream.setPosition(0)
32 check stream.readUint8() == 0x01'u8
33 check stream.atEnd()
34
35 bitWriter.flush()
36 check stream.atEnd()
37
38 test "writeBool":
39 let stream = newStringStream()
40 defer: stream.close()
41
42 let bitWriter = stream.bitWriter()
43 let booleanValues = @[
44 true, true, true, true, true, false, false, true,
45 false, false, false, false, false, true, true, false,
46 true, true, false, true]
47 for b in booleanValues: bitWriter.writeBool(b)
48 bitWriter.flush()
49
50 stream.setPosition(0)
51 check stream.readUint8() == 0b1001_1111'u8
52 check stream.readUint8() == 0b0110_0000'u8
53 check stream.readUint8() == 0b0000_1011'u8
54 expect IOError: discard stream.readUint8()
55 check stream.atEnd()
56
57 test "writeBits":
58 let stream = newStringStream()
59 defer: stream.close()
60
61 let bitWriter = stream.bitWriter()
62 bitWriter.writeBits(4, 0xF00F'u16)
63 bitWriter.writeBits(16, 0xF00F'u16)
64 bitWriter.writeBits(16, 0xFFFF'u16)
65 bitWriter.flush()
66
67 stream.setPosition(0)
68 check stream.readUint16() == 0x00FF'u16
69 check stream.readUint16() == 0xFFFF'u16
70 check stream.readUint8() == 0x0F'u8
71 expect IOError: discard stream.readUint8()
72 check stream.atEnd()
73
74 test "writeSeq":
75 let stream = newStringStream()
76 defer: stream.close()
77
78 let bitWriter = stream.bitWriter()
79 bitWriter.writeSeq(32, @[0xF0F0'u16, 0xF0F0])
80 bitWriter.writeSeq(28, @[0xFF'u8, 0xF0, 0x00, 0xFF])
81 bitWriter.flush()
82
83 stream.setPosition(0)
84 check stream.readUint64() == 0x0F00_F0FF_F0F0_F0F0'u64
85 check stream.atEnd()