1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// Copyright 2015 Ilkka Rauta
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use super::*;
#[test]
fn read_buffer() {
let bytes = &[
0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(1).unwrap(), 0b1);
assert_eq!(reader.peek_u8(3).unwrap(), 0b011);
assert_eq!(reader.read_u8(1).unwrap(), 0b0);
assert_eq!(reader.read_u8(2).unwrap(), 0b11);
assert!(!reader.is_aligned(1));
assert!(!reader.is_aligned(2));
assert!(!reader.is_aligned(4));
assert_eq!(reader.position(), 4);
assert_eq!(reader.remaining(), 60);
assert_eq!(reader.read_u8(4).unwrap(), 0b0101);
assert!(reader.is_aligned(1));
assert!(!reader.is_aligned(2));
assert!(!reader.is_aligned(4));
assert_eq!(reader.align(1), Ok(())); // shouldn't do anything if already aligned
assert_eq!(reader.peek_u64(16).unwrap(), 0b110_1010_1010_1100);
assert_eq!(reader.read_u8(3).unwrap(), 0b11);
assert_eq!(reader.peek_u16(13).unwrap(), 0b1010_1010_1100);
assert_eq!(reader.peek_u32(13).unwrap(), 0b1010_1010_1100);
assert_eq!(reader.peek_u64(13).unwrap(), 0b1010_1010_1100);
assert_eq!(reader.peek_u16(10).unwrap(), 0b01_0101_0101);
assert_eq!(reader.peek_u8(8).unwrap(), 0b0101_0101);
assert_eq!(reader.read_u16(10).unwrap(), 0b01_0101_0101);
assert_eq!(reader.read_u8(3).unwrap(), 0b100);
assert_eq!(reader.position(), 24);
assert_eq!(reader.remaining(), 40);
assert!(reader.is_aligned(1));
assert_eq!(reader.read_u32(32).unwrap(), 0b1001_1001_1001_1001_1001_1001_1001_1001);
assert_eq!(reader.peek_bool().unwrap(), true);
assert_eq!(reader.read_u8(4).unwrap(), 0b1110);
assert_eq!(reader.peek_bool().unwrap(), false);
assert_eq!(reader.read_u8(3).unwrap(), 0b011);
assert_eq!(reader.peek_bool().unwrap(), true);
assert_eq!(reader.read_bool().unwrap(), true);
// Could also be 8 at this point!
assert!(reader.is_aligned(4));
// shouldn't do anything if already aligned
assert_eq!(reader.align(1), Ok(()));
assert_eq!(reader.align(2), Ok(()));
assert_eq!(reader.align(4), Ok(()));
assert_eq!(reader.align(8), Ok(()));
// Start over to test align()
let mut reader = BitReader::new(bytes);
// shouldn't do anything if already aligned
assert_eq!(reader.align(1), Ok(()));
assert_eq!(reader.align(2), Ok(()));
assert_eq!(reader.align(4), Ok(()));
assert_eq!(reader.align(8), Ok(()));
assert_eq!(reader.position(), 0);
assert_eq!(reader.read_u8(1).unwrap(), 0b1);
assert_eq!(reader.align(1), Ok(()));
assert_eq!(reader.position(), 8);
assert!(reader.is_aligned(1));
assert!(!reader.is_aligned(2));
assert!(!reader.is_aligned(4));
assert_eq!(reader.align(2), Ok(()));
assert_eq!(reader.position(), 16);
assert!(reader.is_aligned(1));
assert!(reader.is_aligned(2));
assert!(!reader.is_aligned(4));
assert_eq!(reader.read_u8(7).unwrap(), 0b0101_0110);
assert_eq!(reader.align(4), Ok(()));
assert_eq!(reader.position(), 32);
assert!(reader.is_aligned(1));
assert!(reader.is_aligned(2));
assert!(reader.is_aligned(4));
let mut reader = BitReader::new(bytes);
assert_eq!(reader.position(), 0);
assert_eq!(reader.skip(1), Ok(()));
assert_eq!(reader.align(4), Ok(()));
assert_eq!(reader.position(), 32);
assert_eq!(reader.skip(7), Ok(()));
assert_eq!(reader.align(1), Ok(()));
assert_eq!(reader.position(), 40);
assert_eq!(reader.align(2), Ok(()));
assert_eq!(reader.position(), 48);
assert_eq!(reader.skip(5), Ok(()));
assert_eq!(reader.align(2), Ok(()));
assert_eq!(reader.position(), 64);
let mut reader = BitReader::new(bytes);
assert_eq!(reader.skip(1), Ok(()));
assert_eq!(reader.align(3), Ok(()));
assert_eq!(reader.position(), 24);
assert!(!reader.align(128).is_ok());
}
#[test]
fn try_all_sizes() {
let bytes = &[
0x4a, 0x1e, 0x39, 0xbb, 0xd0, 0x07, 0xca, 0x9a,
0xa6, 0xba, 0x25, 0x52, 0x6f, 0x0a, 0x6a, 0xba,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u64(64).unwrap(), 0x4a1e39bbd007ca9a);
assert_eq!(reader.read_u64(64).unwrap(), 0xa6ba25526f0a6aba);
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u32(32).unwrap(), 0x4a1e39bb);
assert_eq!(reader.read_u32(32).unwrap(), 0xd007ca9a);
assert_eq!(reader.read_u32(32).unwrap(), 0xa6ba2552);
assert_eq!(reader.read_u32(32).unwrap(), 0x6f0a6aba);
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u16(16).unwrap(), 0x4a1e);
assert_eq!(reader.read_u16(16).unwrap(), 0x39bb);
assert_eq!(reader.read_u16(16).unwrap(), 0xd007);
assert_eq!(reader.read_u16(16).unwrap(), 0xca9a);
assert_eq!(reader.read_u16(16).unwrap(), 0xa6ba);
assert_eq!(reader.read_u16(16).unwrap(), 0x2552);
assert_eq!(reader.read_u16(16).unwrap(), 0x6f0a);
assert_eq!(reader.read_u16(16).unwrap(), 0x6aba);
let mut reader = BitReader::new(&bytes[..]);
for byte in bytes {
assert_eq!(reader.read_u8(8).unwrap(), *byte);
}
}
#[test]
fn skipping_and_zero_reads() {
let bytes = &[
0b1011_0101, 0b1110_1010, 0b1010_1100, 0b0011_0101,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(4).unwrap(), 0b1011);
// Reading zero bits should be a no-op
assert_eq!(reader.read_u8(0).unwrap(), 0b0);
assert_eq!(reader.read_i8(0).unwrap(), 0b0);
assert_eq!(reader.read_u8(4).unwrap(), 0b0101);
reader.skip(3).unwrap(); // 0b111
assert_eq!(reader.read_u16(10).unwrap(), 0b0101010101);
assert_eq!(reader.read_u8(3).unwrap(), 0b100);
reader.skip(4).unwrap(); // 0b0011
assert_eq!(reader.read_u32(2).unwrap(), 0b01);
assert_eq!(reader.read_bool().unwrap(), false);
assert_eq!(reader.read_bool().unwrap(), true);
}
#[test]
fn errors() {
let bytes = &[
0b1011_0101, 0b1110_1010, 0b1010_1100,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(4).unwrap(), 0b1011);
assert_eq!(reader.read_u8(9).unwrap_err(), BitReaderError::TooManyBitsForType {
position: 4,
requested: 9,
allowed: 8
});
// If an error happens, it should be possible to resume as if nothing had happened
assert_eq!(reader.read_u8(4).unwrap(), 0b0101);
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(4).unwrap(), 0b1011);
// Same with this error
assert_eq!(reader.read_u32(21).unwrap_err(), BitReaderError::NotEnoughData {
position: 4,
length: (bytes.len() * 8) as u64,
requested: 21
});
assert_eq!(reader.read_u8(4).unwrap(), 0b0101);
}
#[test]
fn signed_values() {
let from = -2048;
let to = 2048;
for x in from..to {
let bytes = &[
(x >> 8) as u8,
x as u8,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(4).unwrap(), if x < 0 { 0b1111 } else { 0 });
assert_eq!(reader.read_i16(12).unwrap(), x);
}
}
#[test]
fn boolean_values() {
let bytes: Vec<u8> = (0..16).collect();
let mut reader = BitReader::new(&bytes);
for v in &bytes {
assert_eq!(reader.read_bool().unwrap(), false);
reader.skip(3).unwrap();
assert_eq!(reader.read_bool().unwrap(), v & 0x08 == 8);
assert_eq!(reader.read_bool().unwrap(), v & 0x04 == 4);
assert_eq!(reader.read_bool().unwrap(), v & 0x02 == 2);
assert_eq!(reader.read_bool().unwrap(), v & 0x01 == 1);
}
}
#[test]
fn read_slice() {
let bytes = &[
0b1111_0000, 0b0000_1111, 0b1111_0000,
0b0000_1000, 0b0000_0100, 0b0000_0011,
0b1111_1100, 0b0000_0011, 0b1101_1000,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(4).unwrap(), 0b1111);
// Just some pattern that's definitely not in the bytes array
let mut output = [0b1010_1101; 3];
reader.read_u8_slice(&mut output).unwrap();
assert_eq!(&output, &[0u8, 255u8, 0u8]);
assert_eq!(reader.read_u8(1).unwrap(), 1);
reader.read_u8_slice(&mut output[1..2]).unwrap();
assert_eq!(&output, &[0u8, 0u8, 0u8]);
assert_eq!(reader.read_u8(1).unwrap(), 1);
output = [0b1010_1101; 3];
reader.read_u8_slice(&mut output).unwrap();
assert_eq!(&output, &[0u8, 255u8, 0u8]);
reader.read_u8_slice(&mut output[0..1]).unwrap();
assert_eq!(output[0], 0b1111_0110);
assert_eq!(reader.read_u8(2).unwrap(), 0);
}
#[test]
fn read_slice_too_much() {
let bytes = &[
0b1111_1111, 0b1111_1111, 0b1111_1111, 0b1111_1111,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(1).unwrap(), 1);
let mut output = [0u8; 4];
let should_be_error = reader.read_u8_slice(&mut output);
assert_eq!(should_be_error.unwrap_err(), BitReaderError::NotEnoughData {
position: 1,
length: (bytes.len() * 8) as u64,
requested: (&output.len() * 8) as u64
});
assert_eq!(&output, &[0u8; 4]);
}
#[test]
fn relative_reader() {
let bytes = &[
0b0001_0010, 0b0011_0100,
];
let mut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(4).unwrap(), 0b0001);
let mut relative_reader = reader.relative_reader();
assert_eq!(reader.read_u8(4).unwrap(), 0b0010);
assert_eq!(reader.read_u8(4).unwrap(), 0b0011);
assert_eq!(reader.read_u8(4).unwrap(), 0b0100);
assert_eq!(reader.read_u8(1).unwrap_err(), BitReaderError::NotEnoughData {
position: 16,
length: 16,
requested: 1
});
assert_eq!(relative_reader.read_u8(4).unwrap(), 0b0010);
assert_eq!(relative_reader.read_u8(4).unwrap(), 0b0011);
assert_eq!(relative_reader.read_u8(4).unwrap(), 0b0100);
assert_eq!(relative_reader.read_u8(1).unwrap_err(), BitReaderError::NotEnoughData {
position: 12,
length: 12,
requested: 1
});
}
|