diff options
author | pacien | 2018-11-23 12:57:00 +0100 |
---|---|---|
committer | pacien | 2018-11-23 13:00:53 +0100 |
commit | d9768cd0315b0415d20818de9c897c6168c95b78 (patch) | |
tree | 41987740d02f689550436384dd7d2b755aa85869 /tests | |
parent | c87ea5c5d11bffbe84acf66215e6bf5a5e047a50 (diff) | |
download | gziplike-d9768cd0315b0415d20818de9c897c6168c95b78.tar.gz |
Split bitstream into bitreader and bitwriter
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tbitreader.nim | 61 | ||||
-rw-r--r-- | tests/tbitstream.nim | 131 | ||||
-rw-r--r-- | tests/tbitwriter.nim | 85 | ||||
-rw-r--r-- | tests/tintegers.nim | 6 | ||||
-rw-r--r-- | tests/trawblock.nim | 22 |
5 files changed, 162 insertions, 143 deletions
diff --git a/tests/tbitreader.nim b/tests/tbitreader.nim new file mode 100644 index 0000000..6b3be2b --- /dev/null +++ b/tests/tbitreader.nim | |||
@@ -0,0 +1,61 @@ | |||
1 | # "à-la-gzip" 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 integers, bitreader | ||
19 | |||
20 | suite "bitreader": | ||
21 | test "readBool": | ||
22 | let stream = newStringStream() | ||
23 | defer: stream.close() | ||
24 | stream.write(0b1001_1111'u8) | ||
25 | stream.write(0b0110_0000'u8) | ||
26 | stream.setPosition(0) | ||
27 | |||
28 | let bitReader = stream.bitReader() | ||
29 | check lc[bitReader.readBool() | (_ <- 0..<16), bool] == @[ | ||
30 | true, true, true, true, true, false, false, true, | ||
31 | false, false, false, false, false, true, true, false] | ||
32 | |||
33 | expect IOError: discard bitReader.readBool() | ||
34 | check bitReader.atEnd() | ||
35 | |||
36 | test "readBits": | ||
37 | let stream = newStringStream() | ||
38 | defer: stream.close() | ||
39 | stream.write(0xF00F'u16) | ||
40 | stream.write(0x0FFF'u16) | ||
41 | stream.setPosition(0) | ||
42 | |||
43 | let bitReader = stream.bitReader() | ||
44 | check bitReader.readBits(8, uint8) == 0x0F'u8 | ||
45 | check bitReader.readBits(16, uint16) == 0xFFF0'u16 | ||
46 | check bitReader.readBits(8, uint8) == 0x0F'u8 | ||
47 | |||
48 | expect RangeError: discard bitReader.readBits(9, uint8) | ||
49 | expect IOError: discard bitReader.readBits(16, uint16) | ||
50 | check bitReader.atEnd() | ||
51 | |||
52 | test "readSeq": | ||
53 | let stream = newStringStream() | ||
54 | defer: stream.close() | ||
55 | stream.write(0x0F00_F0FF_F0F0_F0F0'u64) | ||
56 | stream.setPosition(0) | ||
57 | |||
58 | let bitReader = stream.bitReader() | ||
59 | check bitReader.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0]) | ||
60 | check bitReader.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F]) | ||
61 | check bitReader.atEnd() | ||
diff --git a/tests/tbitstream.nim b/tests/tbitstream.nim deleted file mode 100644 index 2292049..0000000 --- a/tests/tbitstream.nim +++ /dev/null | |||
@@ -1,131 +0,0 @@ | |||
1 | # "à-la-gzip" 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 integers, bitstream | ||
19 | |||
20 | suite "bitstream": | ||
21 | test "chunks iterator": | ||
22 | check toSeq(chunks(70, uint32)) == @[(0, 32), (1, 32), (2, 6)] | ||
23 | check toSeq(chunks(32, uint16)) == @[(0, 16), (1, 16)] | ||
24 | |||
25 | test "flush": | ||
26 | let stream = newStringStream() | ||
27 | defer: stream.close() | ||
28 | let bitStream = stream.bitStream() | ||
29 | |||
30 | bitStream.writeBool(true) | ||
31 | stream.setPosition(0) | ||
32 | expect IOError: discard stream.peekUint8() | ||
33 | |||
34 | bitStream.flush() | ||
35 | stream.setPosition(0) | ||
36 | check stream.readUint8() == 0x01'u8 | ||
37 | check stream.atEnd() | ||
38 | |||
39 | bitStream.flush() | ||
40 | check stream.atEnd() | ||
41 | |||
42 | test "readBool": | ||
43 | let stream = newStringStream() | ||
44 | defer: stream.close() | ||
45 | stream.write(0b1001_1111'u8) | ||
46 | stream.write(0b0110_0000'u8) | ||
47 | stream.setPosition(0) | ||
48 | |||
49 | let bitStream = stream.bitStream() | ||
50 | check lc[bitStream.readBool() | (_ <- 0..<16), bool] == @[ | ||
51 | true, true, true, true, true, false, false, true, | ||
52 | false, false, false, false, false, true, true, false] | ||
53 | |||
54 | expect IOError: discard bitStream.readBool() | ||
55 | check bitStream.atEnd() | ||
56 | |||
57 | test "readBits": | ||
58 | let stream = newStringStream() | ||
59 | defer: stream.close() | ||
60 | stream.write(0xF00F'u16) | ||
61 | stream.write(0x0FFF'u16) | ||
62 | stream.setPosition(0) | ||
63 | |||
64 | let bitStream = stream.bitStream() | ||
65 | check bitStream.readBits(8, uint8) == 0x0F'u8 | ||
66 | check bitStream.readBits(16, uint16) == 0xFFF0'u16 | ||
67 | check bitStream.readBits(8, uint8) == 0x0F'u8 | ||
68 | |||
69 | expect RangeError: discard bitStream.readBits(9, uint8) | ||
70 | expect IOError: discard bitStream.readBits(16, uint16) | ||
71 | check bitStream.atEnd() | ||
72 | |||
73 | test "readSeq": | ||
74 | let stream = newStringStream() | ||
75 | defer: stream.close() | ||
76 | stream.write(0x0F00_F0FF_F0F0_F0F0'u64) | ||
77 | stream.setPosition(0) | ||
78 | |||
79 | let bitStream = stream.bitStream() | ||
80 | check bitStream.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0]) | ||
81 | check bitStream.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F]) | ||
82 | check bitStream.atEnd() | ||
83 | |||
84 | test "writeBool": | ||
85 | let stream = newStringStream() | ||
86 | defer: stream.close() | ||
87 | |||
88 | let bitStream = stream.bitStream() | ||
89 | let booleanValues = @[ | ||
90 | true, true, true, true, true, false, false, true, | ||
91 | false, false, false, false, false, true, true, false, | ||
92 | true, true, false, true] | ||
93 | for b in booleanValues: bitStream.writeBool(b) | ||
94 | bitStream.flush() | ||
95 | |||
96 | stream.setPosition(0) | ||
97 | check stream.readUint8() == 0b1001_1111'u8 | ||
98 | check stream.readUint8() == 0b0110_0000'u8 | ||
99 | check stream.readUint8() == 0b0000_1011'u8 | ||
100 | expect IOError: discard stream.readUint8() | ||
101 | check stream.atEnd() | ||
102 | |||
103 | test "writeBits": | ||
104 | let stream = newStringStream() | ||
105 | defer: stream.close() | ||
106 | |||
107 | let bitStream = stream.bitStream() | ||
108 | bitStream.writeBits(4, 0xF00F'u16) | ||
109 | bitStream.writeBits(16, 0xF00F'u16) | ||
110 | bitStream.writeBits(16, 0xFFFF'u16) | ||
111 | bitStream.flush() | ||
112 | |||
113 | stream.setPosition(0) | ||
114 | check stream.readUint16() == 0x00FF'u16 | ||
115 | check stream.readUint16() == 0xFFFF'u16 | ||
116 | check stream.readUint8() == 0x0F'u8 | ||
117 | expect IOError: discard stream.readUint8() | ||
118 | check stream.atEnd() | ||
119 | |||
120 | test "writeSeq": | ||
121 | let stream = newStringStream() | ||
122 | defer: stream.close() | ||
123 | |||
124 | let bitStream = stream.bitStream() | ||
125 | bitStream.writeSeq(32, @[0xF0F0'u16, 0xF0F0]) | ||
126 | bitStream.writeSeq(28, @[0xFF'u8, 0xF0, 0x00, 0xFF]) | ||
127 | bitStream.flush() | ||
128 | |||
129 | stream.setPosition(0) | ||
130 | check stream.readUint64() == 0x0F00_F0FF_F0F0_F0F0'u64 | ||
131 | check stream.atEnd() | ||
diff --git a/tests/tbitwriter.nim b/tests/tbitwriter.nim new file mode 100644 index 0000000..cc7fdc5 --- /dev/null +++ b/tests/tbitwriter.nim | |||
@@ -0,0 +1,85 @@ | |||
1 | # "à-la-gzip" 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 integers, bitwriter | ||
19 | |||
20 | suite "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, | ||