aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorpacien2018-11-23 12:31:11 +0100
committerpacien2018-11-23 12:31:11 +0100
commit52c08b0282aacb44c2e75cefeae5626a7aa1b21a (patch)
treedcf4df132277ecf4b12ade0464cf3175f9665dd2 /tests
parent18258716b4193279e806cacbf766931d9ae3ffba (diff)
downloadgziplike-52c08b0282aacb44c2e75cefeae5626a7aa1b21a.tar.gz
Implement bitstream
Diffstat (limited to 'tests')
-rw-r--r--tests/tbitstream.nim131
-rw-r--r--tests/tintegers.nim28
2 files changed, 159 insertions, 0 deletions
diff --git a/tests/tbitstream.nim b/tests/tbitstream.nim
new file mode 100644
index 0000000..2292049
--- /dev/null
+++ b/tests/tbitstream.nim
@@ -0,0 +1,131 @@
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
17import unittest, streams, sugar, sequtils
18import integers, bitstream
19
20suite "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/tintegers.nim b/tests/tintegers.nim
new file mode 100644
index 0000000..720677e
--- /dev/null
+++ b/tests/tintegers.nim
@@ -0,0 +1,28 @@
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
17import unittest
18import integers
19
20suite "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