ireko/src/int_property.rs

70 lines
1.8 KiB
Rust
Raw Normal View History

2025-02-19 19:34:13 -05:00
use binrw::binrw;
#[binrw]
#[derive(Debug)]
pub struct IntProperty {
2025-02-23 16:38:57 -05:00
#[bw(calc = 4)]
pub size_in_bytes: u32,
#[brw(pad_before = 5)]
2025-02-19 19:34:13 -05:00
pub value: u32,
}
#[cfg(test)]
mod tests {
use super::*;
2025-02-23 16:38:57 -05:00
use binrw::{BinRead, BinWrite};
2025-02-19 19:34:13 -05:00
use std::io::Cursor;
#[test]
2025-02-23 16:38:57 -05:00
fn read_zero() {
2025-02-19 19:34:13 -05:00
let data = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let mut cursor = Cursor::new(data);
let decoded = IntProperty::read_le(&mut cursor).unwrap();
assert_eq!(decoded.value, 0);
}
2025-02-23 16:38:57 -05:00
#[test]
fn write_zero() {
let expected_data: [u8; 13] = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let property = IntProperty { value: 0 };
2025-02-23 16:38:57 -05:00
let mut buffer: Vec<u8> = Vec::new();
{
let mut cursor = Cursor::new(&mut buffer);
property.write_le(&mut cursor).unwrap();
}
assert_eq!(expected_data, &buffer[..]);
}
2025-02-19 19:34:13 -05:00
#[test]
fn integer() {
let data = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
];
let mut cursor = Cursor::new(data);
let decoded = IntProperty::read_le(&mut cursor).unwrap();
assert_eq!(decoded.value, 4);
}
2025-02-23 16:38:57 -05:00
#[test]
fn write_integer() {
let expected_data: [u8; 13] = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
];
let property = IntProperty { value: 4 };
2025-02-23 16:38:57 -05:00
let mut buffer: Vec<u8> = Vec::new();
{
let mut cursor = Cursor::new(&mut buffer);
property.write_le(&mut cursor).unwrap();
}
assert_eq!(expected_data, &buffer[..]);
}
2025-02-19 19:34:13 -05:00
}