mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 08:34:07 +02:00
add test for reading derived Read
this requires moving the tests into their own folder, so they get tested loaded as a separate crate
This commit is contained in:
parent
b45ca3856e
commit
a49835d003
2 changed files with 57 additions and 3 deletions
|
|
@ -60,8 +60,6 @@ mod endianness;
|
|||
mod is_signed;
|
||||
mod read;
|
||||
mod stream;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Errors that can be returned when trying to read from a buffer
|
||||
#[derive(Debug)]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use super::*;
|
||||
use bitstream_reader::{BigEndian, BitBuffer, BitStream, LittleEndian, Read};
|
||||
|
||||
// for bench on nightly
|
||||
//use std::fs;
|
||||
//use test::Bencher;
|
||||
|
|
@ -271,6 +272,61 @@ fn read_sized_trait() {
|
|||
assert_eq!(vec![0b1011_0101, 0b0110_1010, 0b1010_1100], vec);
|
||||
}
|
||||
|
||||
#[derive(Read, PartialEq, Debug)]
|
||||
struct TestStruct {
|
||||
foo: u8,
|
||||
str: String,
|
||||
#[size = 2]
|
||||
truncated: String,
|
||||
bar: u16,
|
||||
float: f32,
|
||||
#[size = 3]
|
||||
asd: u8,
|
||||
#[size_bits = 2]
|
||||
dynamic: u8,
|
||||
#[size = "asd"]
|
||||
previous_field: u8,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_struct() {
|
||||
let float: [u8; 4] = 12.5f32.to_bits().to_le_bytes();
|
||||
let bytes = vec![
|
||||
12,
|
||||
'h' as u8,
|
||||
'e' as u8,
|
||||
'l' as u8,
|
||||
'l' as u8,
|
||||
'o' as u8,
|
||||
0,
|
||||
'f' as u8,
|
||||
'o' as u8,
|
||||
'o' as u8,
|
||||
0,
|
||||
float[0],
|
||||
float[1],
|
||||
float[2],
|
||||
float[3],
|
||||
0b0101_0101,
|
||||
0b1010_1010,
|
||||
];
|
||||
let buffer = BitBuffer::new(bytes, LittleEndian);
|
||||
let mut stream = BitStream::from(buffer);
|
||||
assert_eq!(
|
||||
TestStruct {
|
||||
foo: 12,
|
||||
str: "hello".to_owned(),
|
||||
truncated: "fo".to_owned(),
|
||||
bar: 'o' as u16,
|
||||
float: 12.5,
|
||||
asd: 0b101,
|
||||
dynamic: 0b10,
|
||||
previous_field: 0b1010_0,
|
||||
},
|
||||
stream.read().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
//0b1011_0101,
|
||||
// 0b0110_1010,
|
||||
// 0b1010_1100,
|
||||
Loading…
Add table
Add a link
Reference in a new issue