tuffy / bitstream-io

Library for reading/writing un-aligned values from/to streams in big-endian and little-endian formats
Apache License 2.0
52 stars 20 forks source link

Feature request: BitReader literal for testing. #7

Open vi opened 4 years ago

vi commented 4 years ago

It should be easy to test code that is consuming BitReader. For this it should be easy to specify specific sequence of bits in a test.

Prototype:

fn testbits(x:&str) -> BitReader<impl Read, impl Endianness> {
    let mut v = Vec::new();
    {
        let c = std::io::Cursor::new(&mut v);
        let mut w = bitstream_io::write::BitWriter::endian(c, bitstream_io::BE{});
        for b in x.as_bytes() {
            w.write_bit(b == &b'1');
        }
        while !w.byte_aligned() {
            w.write_bit(false);
        }
    }
    let c = std::io::Cursor::new(v);
    BitReader::endian(c, bitstream_io::BE{})
}

#[test]
fn test() {
    let mut r = testbits("110");
    assert_eq!(r.read_bit().unwrap(), true);
    assert_eq!(r.read_bit().unwrap(), true);
    assert_eq!(r.read_bit().unwrap(), false);
}