ruzstd/tests/
bit_reader.rs

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
#[test]
fn test_bitreader_reversed() {
    use crate::decoding::bit_reader_reverse::BitReaderReversed;

    let encoded: [u8; 16] = [
        0xC1, 0x41, 0x08, 0x00, 0x00, 0xEC, 0xC8, 0x96, 0x42, 0x79, 0xD4, 0xBC, 0xF7, 0x2C, 0xD5,
        0x48,
    ];
    //just the u128 in encoded
    let num_rev: u128 = 0x48_D5_2C_F7_BC_D4_79_42_96_C8_EC_00_00_08_41_C1;

    let mut br = BitReaderReversed::new(&encoded[..]);
    let mut accumulator = 0;
    let mut bits_read = 0;
    let mut x = 0;

    loop {
        x += 3;
        //semi random access pattern
        let mut num_bits = x % 16;
        if bits_read > 128 - num_bits {
            num_bits = 128 - bits_read;
        }

        let bits = br.get_bits(num_bits);
        bits_read += num_bits;
        accumulator |= u128::from(bits) << (128 - bits_read);
        if bits_read >= 128 {
            break;
        }
    }

    if accumulator != num_rev {
        panic!(
            "Bitreader failed somewhere. Accumulated bits: {:?}, Should be: {:?}",
            accumulator, num_rev
        );
    }
}

#[test]
fn test_bitreader_normal() {
    use crate::decoding::bit_reader::BitReader;

    let encoded: [u8; 16] = [
        0xC1, 0x41, 0x08, 0x00, 0x00, 0xEC, 0xC8, 0x96, 0x42, 0x79, 0xD4, 0xBC, 0xF7, 0x2C, 0xD5,
        0x48,
    ];
    //just the u128 in encoded
    let num: u128 = 0x48_D5_2C_F7_BC_D4_79_42_96_C8_EC_00_00_08_41_C1;

    let mut br = BitReader::new(&encoded[..]);
    let mut accumulator = 0;
    let mut bits_read = 0;
    let mut x = 0;

    loop {
        x += 3;
        //semi random access pattern
        let mut num_bits = x % 16;
        if bits_read > 128 - num_bits {
            num_bits = 128 - bits_read;
        }

        let bits = br.get_bits(num_bits).unwrap();
        accumulator |= u128::from(bits) << bits_read;
        bits_read += num_bits;
        if bits_read >= 128 {
            break;
        }
    }

    if accumulator != num {
        panic!(
            "Bitreader failed somewhere. Accumulated bits: {:?}, Should be: {:?}",
            accumulator, num
        );
    }
}