diff options
Diffstat (limited to 'tests/tbitio.nim')
-rw-r--r-- | tests/tbitio.nim | 176 |
1 files changed, 0 insertions, 176 deletions
diff --git a/tests/tbitio.nim b/tests/tbitio.nim deleted file mode 100644 index 91b9cbf..0000000 --- a/tests/tbitio.nim +++ /dev/null | |||
@@ -1,176 +0,0 @@ | |||
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, sugar, sequtils | ||
18 | import bitio/integers, bitio/bitreader, bitio/bitwriter | ||
19 | |||
20 | suite "integers": | ||
21 | test "Round-up integer division": | ||
22 | check 42 /^ 2 == 21 | ||
23 | check 43 /^ 2 == 22 | ||
24 | |||
25 | test "truncateToUint8": | ||
26 | check truncateToUint8(0xFA'u8) == 0xFA'u8 | ||
27 | check truncateToUint8(0x00FA'u16) == 0xFA'u8 | ||
28 | check truncateToUint8(0xFFFA'u16) == 0xFA'u8 | ||
29 | |||
30 | test "bitLength": | ||
31 | check bitLength(0b1_1111) == 5 | ||
32 | check bitLength(0b1000_0000) == 8 | ||
33 | |||
34 | test "leastSignificantBits": | ||
35 | check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8 | ||
36 | check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8 | ||
37 | check leastSignificantBits(0xFF'u8, 10) == 0xFF'u8 | ||
38 | check leastSignificantBits(0xFFFF'u16, 16) == 0xFFFF'u16 | ||
39 | check leastSignificantBits(0xFFFF'u16, 8) == 0x00FF'u16 | ||
40 | |||
41 | test "chunks iterator": | ||
42 | check toSeq(chunks(70, uint32)) == @[(0, 32), (1, 32), (2, 6)] | ||
43 | check toSeq(chunks(32, uint16)) == @[(0, 16), (1, 16)] | ||
44 | |||
45 | suite "bitreader": | ||
46 | test "readBool": | ||
47 | let stream = newStringStream() | ||
48 | defer: stream.close() | ||
49 | stream.write(0b1001_1111'u8) | ||
50 | stream.write(0b0110_0000'u8) | ||
51 | stream.setPosition(0) | ||
52 | |||
53 | let bitReader = stream.bitReader() | ||
54 | check lc[bitReader.readBool() | (_ <- 0..<16), bool] == @[ | ||
55 | true, true, true, true, true, false, false, true, | ||
56 | false, false, false, false, false, true, true, false] | ||
57 | |||
58 | expect IOError: discard bitReader.readBool() | ||
59 | check bitReader.atEnd() | ||
60 | |||
61 | test "readBits": | ||
62 | let stream = newStringStream() | ||
63 | defer: stream.close() | ||
64 | stream.write(0xF00F'u16) | ||
65 | stream.write(0x0FFF'u16) | ||
66 | stream.setPosition(0) | ||
67 | |||
68 | let bitReader = stream.bitReader() | ||
69 | check bitReader.readBits(8, uint8) == 0x0F'u8 | ||
70 | check bitReader.readBits(16, uint16) == 0xFFF0'u16 | ||
71 | check bitReader.readBits(8, uint8) == 0x0F'u8 | ||
72 | |||
73 | expect RangeError: discard bitReader.readBits(9, uint8) | ||
74 | expect IOError: discard bitReader.readBits(16, uint16) | ||
75 | check bitReader.atEnd() | ||
76 | |||
77 | test "readBits (look-ahead overflow)": | ||
78 | let stream = newStringStream() | ||
79 | defer: stream.close() | ||
80 | stream.write(0xAB'u8) | ||
81 | stream.setPosition(0) | ||
82 | |||
83 | let bitReader = stream.bitReader() | ||
84 | check bitReader.readBits(4, uint16) == 0x000B'u16 | ||
85 | check bitReader.readBits(4, uint16) == 0x000A'u16 | ||
86 | check bitReader.atEnd() | ||
87 | |||
88 | test "readBits (from buffer composition)": | ||
89 | let stream = newStringStream() | ||
90 | defer: stream.close() | ||
91 | stream.write(0xABCD'u16) | ||
92 | stream.setPosition(0) | ||
93 | |||
94 | let bitReader = stream.bitReader() | ||
95 | check bitReader.readBits(4, uint16) == 0x000D'u16 | ||
96 | check bitReader.readBits(8, uint16) == 0x00BC'u16 | ||
97 | check bitReader.readBits(4, uint16) == 0x000A'u16 | ||
98 | check bitReader.atEnd() | ||
99 | |||
100 | test "readSeq": | ||
101 | let stream = newStringStream() | ||
102 | defer: stream.close() | ||
103 | stream.write(0x0F00_F0FF_F0F0_F0F0'u64) | ||
104 | stream.setPosition(0) | ||
105 | |||
106 | let bitReader = stream.bitReader() | ||
107 | check bitReader.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0]) | ||
108 | check bitReader.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F]) | ||
109 | check bitReader.atEnd() | ||
110 | |||
111 | suite "bitwriter": | ||
112 | test "flush": | ||
113 | let stream = newStringStream() | ||
114 | defer: stream.close() | ||
115 | let bitWriter = stream.bitWriter() | ||
116 | |||
117 | bitWriter.writeBool(true) | ||
118 | stream.setPosition(0) | ||
119 | expect IOError: discard stream.peekUint8() | ||
120 | |||
121 | bitWriter.flush() | ||
122 | stream.setPosition(0) | ||
123 | check stream.readUint8() == 0x01'u8 | ||
124 | check stream.atEnd() | ||
125 | |||
126 | bitWriter.flush() | ||
127 | check stream.atEnd() | ||
128 | |||
129 | test "writeBool": | ||
130 | let stream = newStringStream() | ||
131 | defer: stream.close() | ||
132 | |||
133 | let bitWriter = stream.bitWriter() | ||
134 | let booleanValues = @[ | ||
135 | true, true, true, true, true, false, false, true, | ||
136 | false, false, false, false, false, true, true, false, | ||
137 | true, true, false, true] | ||
138 | for b in booleanValues: bitWriter.writeBool(b) | ||
139 | bitWriter.flush() | ||
140 | |||
141 | stream.setPosition(0) | ||
142 | check stream.readUint8() == 0b1001_1111'u8 | ||
143 | check stream.readUint8() == 0b0110_0000'u8 | ||
144 | check stream.readUint8() == 0b0000_1011'u8 | ||
145 | expect IOError: discard stream.readUint8() | ||
146 | check stream.atEnd() | ||
147 | |||
148 | test "writeBits": | ||
149 | let stream = newStringStream() | ||
150 | defer: stream.close() | ||
151 | |||
152 | let bitWriter = stream.bitWriter() | ||
153 | bitWriter.writeBits(4, 0xF00F'u16) | ||
154 | bitWriter.writeBits(16, 0xF00F'u16) | ||
155 | bitWriter.writeBits(16, 0xFFFF'u16) | ||
156 | bitWriter.flush() | ||
157 | |||
158 | stream.setPosition(0) | ||
159 | check stream.readUint16() == 0x00FF'u16 | ||
160 | check stream.readUint16() == 0xFFFF'u16 | ||
161 | check stream.readUint8() == 0x0F'u8 | ||
162 | expect IOError: discard stream.readUint8() | ||
163 | check stream.atEnd() | ||
164 | |||
165 | test "writeSeq": | ||
166 | let stream = newStringStream() | ||
167 | defer: stream.close() | ||
168 | |||
169 | let bitWriter = stream.bitWriter() | ||
170 | bitWriter.writeSeq(32, @[0xF0F0'u16, 0xF0F0]) | ||
171 | bitWriter.writeSeq(28, @[0xFF'u8, 0xF0, 0x00, 0xFF]) | ||
172 | bitWriter.flush() | ||
173 | |||
174 | stream.setPosition(0) | ||
175 | check stream.readUint64() == 0x0F00_F0FF_F0F0_F0F0'u64 | ||
176 | check stream.atEnd() | ||