diff options
Diffstat (limited to 'tests')
22 files changed, 752 insertions, 457 deletions
diff --git a/tests/bitio/tbitreader.nim b/tests/bitio/tbitreader.nim new file mode 100644 index 0000000..3949b8b --- /dev/null +++ b/tests/bitio/tbitreader.nim | |||
@@ -0,0 +1,84 @@ | |||
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 | ||
18 | import bitio/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 "readBits (look-ahead overflow)": | ||
53 | let stream = newStringStream() | ||
54 | defer: stream.close() | ||
55 | stream.write(0xAB'u8) | ||
56 | stream.setPosition(0) | ||
57 | |||
58 | let bitReader = stream.bitReader() | ||
59 | check bitReader.readBits(4, uint16) == 0x000B'u16 | ||
60 | check bitReader.readBits(4, uint16) == 0x000A'u16 | ||
61 | check bitReader.atEnd() | ||
62 | |||
63 | test "readBits (from buffer composition)": | ||
64 | let stream = newStringStream() | ||
65 | defer: stream.close() | ||
66 | stream.write(0xABCD'u16) | ||
67 | stream.setPosition(0) | ||
68 | |||
69 | let bitReader = stream.bitReader() | ||
70 | check bitReader.readBits(4, uint16) == 0x000D'u16 | ||
71 | check bitReader.readBits(8, uint16) == 0x00BC'u16 | ||
72 | check bitReader.readBits(4, uint16) == 0x000A'u16 | ||
73 | check bitReader.atEnd() | ||
74 | |||
75 | test "readSeq": | ||
76 | let stream = newStringStream() | ||
77 | defer: stream.close() | ||
78 | stream.write(0x0F00_F0FF_F0F0_F0F0'u64) | ||
79 | stream.setPosition(0) | ||
80 | |||
81 | let bitReader = stream.bitReader() | ||
82 | check bitReader.readSeq(32, uint16) == (32, @[0xF0F0'u16, 0xF0F0]) | ||
83 | check bitReader.readSeq(40, uint8) == (32, @[0xFF'u8, 0xF0, 0x00, 0x0F]) | ||
84 | check bitReader.atEnd() | ||
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 | |||
17 | import unittest, streams | ||
18 | import bitio/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, | ||
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() | ||
diff --git a/tests/bitio/tintegers.nim b/tests/bitio/tintegers.nim new file mode 100644 index 0000000..e17a2ac --- /dev/null +++ b/tests/bitio/tintegers.nim | |||
@@ -0,0 +1,43 @@ | |||
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, sequtils | ||
18 | import bitio/integers | ||
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 | ||