// 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.
letmut 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);
letmut 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,
]; letmut 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(); letmut 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,
]; letmut reader = BitReader::new(bytes);
assert_eq!(reader.read_u8(4).unwrap(), 0b1111); // Just some pattern that's definitely not in the bytes array letmut output = [0b1010_1101; 3];
reader.read_u8_slice(&mut output).unwrap();
assert_eq!(&output, &[0u8, 255u8, 0u8]);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.