1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-04 00:54: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

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());
}