1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 08:34:07 +02:00

writing wip and bool be read fixes

This commit is contained in:
Robin Appelman 2020-02-13 02:20:08 +01:00
commit 8979354c60
9 changed files with 477 additions and 55 deletions

View file

@ -25,7 +25,9 @@ fn read_u8_le() {
let buffer = BitReadBuffer::new(BYTES.to_vec(), LittleEndian);
assert_eq!(buffer.read_int::<u8>(0, 1).unwrap(), 0b1);
assert_eq!(buffer.read_bool(0).unwrap(), true);
assert_eq!(buffer.read_int::<u8>(1, 1).unwrap(), 0b0);
assert_eq!(buffer.read_bool(1).unwrap(), false);
assert_eq!(buffer.read_int::<u8>(2, 2).unwrap(), 0b01);
assert_eq!(buffer.read_int::<u8>(0, 3).unwrap(), 0b101);
assert_eq!(buffer.read_int::<u8>(7, 5).unwrap(), 0b1010_1);
@ -43,6 +45,9 @@ fn read_u8_be() {
assert_eq!(buffer.read_int::<u8>(0, 3).unwrap(), 0b101);
assert_eq!(buffer.read_int::<u8>(7, 5).unwrap(), 0b1011_0);
assert_eq!(buffer.read_int::<u8>(6, 5).unwrap(), 0b01_011);
assert_eq!(buffer.read_bool(0).unwrap(), true);
assert_eq!(buffer.read_bool(8).unwrap(), false);
}
#[test]

115
tests/write_tests.rs Normal file
View file

@ -0,0 +1,115 @@
use bitbuffer::{BigEndian, BitReadBuffer, BitReadStream, BitWriteStream, LittleEndian};
#[test]
fn test_write_bool_le() {
let mut stream = BitWriteStream::new(LittleEndian);
stream.write_bool(true).unwrap();
stream.write_bool(true).unwrap();
stream.write_bool(false).unwrap();
stream.write_bool(true).unwrap();
let data = stream.finish();
let mut read = BitReadStream::from(BitReadBuffer::new(data, LittleEndian));
assert_eq!(true, read.read_bool().unwrap());
assert_eq!(true, read.read_bool().unwrap());
assert_eq!(false, read.read_bool().unwrap());
assert_eq!(true, read.read_bool().unwrap());
// 0 padded
assert_eq!(false, read.read_bool().unwrap());
}
#[test]
fn test_write_bool_be() {
let mut stream = BitWriteStream::new(BigEndian);
stream.write_bool(true).unwrap();
stream.write_bool(true).unwrap();
stream.write_bool(false).unwrap();
stream.write_bool(true).unwrap();
let data = stream.finish();
let mut read = BitReadStream::from(BitReadBuffer::new(data, BigEndian));
assert_eq!(true, read.read_bool().unwrap());
assert_eq!(true, read.read_bool().unwrap());
assert_eq!(false, read.read_bool().unwrap());
assert_eq!(true, read.read_bool().unwrap());
// 0 padded
assert_eq!(false, read.read_bool().unwrap());
}
#[test]
fn test_write_bool_number_le() {
let mut stream = BitWriteStream::new(LittleEndian);
stream.write_bool(true).unwrap();
stream.write_int(3253u16, 16).unwrap();
stream.write_int(13253u64, 64).unwrap();
let data = stream.finish();
let mut read = BitReadStream::from(BitReadBuffer::new(data, LittleEndian));
assert_eq!(true, read.read_bool().unwrap());
assert_eq!(3253u16, read.read().unwrap());
assert_eq!(13253u64, read.read().unwrap());
// 0 padded
assert_eq!(false, read.read_bool().unwrap());
}
#[test]
fn test_write_bool_number_be() {
let mut stream = BitWriteStream::new(BigEndian);
stream.write_bool(true).unwrap();
stream.write_int(3253u16, 16).unwrap();
stream.write_int(13253u64, 64).unwrap();
let data = stream.finish();
let mut read = BitReadStream::from(BitReadBuffer::new(data, BigEndian));
assert_eq!(1u8, read.read_int(1).unwrap());
assert_eq!(3253u16, read.read().unwrap());
assert_eq!(13253u64, read.read().unwrap());
// 0 padded
assert_eq!(false, read.read_bool().unwrap());
}
#[test]
fn test_write_float_le() {
let mut stream = BitWriteStream::new(LittleEndian);
stream.write_bool(true).unwrap();
stream.write_float(3253.12f32).unwrap();
let data = stream.finish();
let mut read = BitReadStream::from(BitReadBuffer::new(data, LittleEndian));
assert_eq!(true, read.read_bool().unwrap());
assert_eq!(3253.12f32, read.read().unwrap());
// 0 padded
assert_eq!(false, read.read_bool().unwrap());
}
#[test]
fn test_write_float_be() {
let mut stream = BitWriteStream::new(BigEndian);
stream.write_bool(true).unwrap();
stream.write_float(3253.12f32).unwrap();
let data = stream.finish();
let mut read = BitReadStream::from(BitReadBuffer::new(data, BigEndian));
assert_eq!(1u8, read.read_int(1).unwrap());
assert_eq!(3253.12f32, read.read().unwrap());
// 0 padded
assert_eq!(false, read.read_bool().unwrap());
}