1 | #[test ] |
2 | fn test_bitreader_reversed() { |
3 | use crate::decoding::bit_reader_reverse::BitReaderReversed; |
4 | |
5 | let encoded: [u8; 16] = [ |
6 | 0xC1, 0x41, 0x08, 0x00, 0x00, 0xEC, 0xC8, 0x96, 0x42, 0x79, 0xD4, 0xBC, 0xF7, 0x2C, 0xD5, |
7 | 0x48, |
8 | ]; |
9 | //just the u128 in encoded |
10 | let num_rev: u128 = 0x48_D5_2C_F7_BC_D4_79_42_96_C8_EC_00_00_08_41_C1; |
11 | |
12 | let mut br = BitReaderReversed::new(&encoded[..]); |
13 | let mut accumulator = 0; |
14 | let mut bits_read = 0; |
15 | let mut x = 0; |
16 | |
17 | loop { |
18 | x += 3; |
19 | //semi random access pattern |
20 | let mut num_bits = x % 16; |
21 | if bits_read > 128 - num_bits { |
22 | num_bits = 128 - bits_read; |
23 | } |
24 | |
25 | let bits = br.get_bits(num_bits).unwrap(); |
26 | bits_read += num_bits; |
27 | accumulator |= u128::from(bits) << (128 - bits_read); |
28 | if bits_read >= 128 { |
29 | break; |
30 | } |
31 | } |
32 | |
33 | if accumulator != num_rev { |
34 | panic!( |
35 | "Bitreader failed somewhere. Accumulated bits: {:?}, Should be: {:?}" , |
36 | accumulator, num_rev |
37 | ); |
38 | } |
39 | } |
40 | |
41 | #[test ] |
42 | fn test_bitreader_normal() { |
43 | use crate::decoding::bit_reader::BitReader; |
44 | |
45 | let encoded: [u8; 16] = [ |
46 | 0xC1, 0x41, 0x08, 0x00, 0x00, 0xEC, 0xC8, 0x96, 0x42, 0x79, 0xD4, 0xBC, 0xF7, 0x2C, 0xD5, |
47 | 0x48, |
48 | ]; |
49 | //just the u128 in encoded |
50 | let num: u128 = 0x48_D5_2C_F7_BC_D4_79_42_96_C8_EC_00_00_08_41_C1; |
51 | |
52 | let mut br = BitReader::new(&encoded[..]); |
53 | let mut accumulator = 0; |
54 | let mut bits_read = 0; |
55 | let mut x = 0; |
56 | |
57 | loop { |
58 | x += 3; |
59 | //semi random access pattern |
60 | let mut num_bits = x % 16; |
61 | if bits_read > 128 - num_bits { |
62 | num_bits = 128 - bits_read; |
63 | } |
64 | |
65 | let bits = br.get_bits(num_bits).unwrap(); |
66 | accumulator |= u128::from(bits) << bits_read; |
67 | bits_read += num_bits; |
68 | if bits_read >= 128 { |
69 | break; |
70 | } |
71 | } |
72 | |
73 | if accumulator != num { |
74 | panic!( |
75 | "Bitreader failed somewhere. Accumulated bits: {:?}, Should be: {:?}" , |
76 | accumulator, num |
77 | ); |
78 | } |
79 | } |
80 | |