ireko/src/property/int_property.rs

82 lines
2 KiB
Rust
Raw Normal View History

2025-02-19 19:34:13 -05:00
use binrw::binrw;
2025-03-02 17:00:33 -05:00
use super::PropertyBase;
2025-02-19 19:34:13 -05:00
#[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,
}
2025-03-02 17:00:33 -05:00
impl PropertyBase for IntProperty {
fn type_name() -> &'static str {
2025-03-02 14:13:41 -05:00
"IntProperty"
}
fn size_in_bytes(&self) -> u32 {
4 + 5 + 4
}
}
2025-02-19 19:34:13 -05:00
#[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
}