From 98cf4c77eea0b32b80b68909024619846397b8c2 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 9 Mar 2024 13:38:38 +0800 Subject: [PATCH] [test not passed] --- shen-nbt5/src/lib.rs | 38 ++++++++++++++-- shen-nbt5/src/tests.rs | 101 +++++++++++++++++++++++++++++------------ 2 files changed, 106 insertions(+), 33 deletions(-) diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index bd04592..95e4f49 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -124,6 +124,40 @@ impl NbtReader<'_> { /// /// 转换大小端 /// + /// 会在超出长度时 panic + pub fn read_f32(&mut self) -> f32 { + let value = f32::from_be_bytes([ + self.data[self.cursor], + self.data[self.cursor + 1], + self.data[self.cursor + 2], + self.data[self.cursor + 3], + ]); + self.cursor += 4; + value + } + /// 读取一个 f64 类型的数据 + /// + /// 转换大小端 + /// + /// 会在超出长度时 panic + pub fn read_f64(&mut self) -> f64 { + let value = f64::from_be_bytes([ + self.data[self.cursor], + self.data[self.cursor + 1], + self.data[self.cursor + 2], + self.data[self.cursor + 3], + self.data[self.cursor + 4], + self.data[self.cursor + 5], + self.data[self.cursor + 6], + self.data[self.cursor + 7], + ]); + self.cursor += 8; + value + } + /// 读取一个 f32 类型的数据 + /// + /// 转换大小端 + /// /// # 安全性 /// 允许未对齐的地址 /// 长度溢出会导致 UB @@ -158,7 +192,7 @@ impl NbtReader<'_> { /// # 安全性 /// /// 长度溢出会导致 UB - pub fn read_i8_array(&mut self, len: usize) -> &[i8] { + pub fn read_i8_array_unchecked(&mut self, len: usize) -> &[i8] { let value = unsafe { std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i8, len) }; @@ -177,14 +211,12 @@ impl NbtReader<'_> { } pub fn read_i32_array_unchecked(&mut self, len: usize) -> &[i32] { unsafe { - println!("data: {:?}", self.data); let value = std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i32, len); for n in &mut *value { *n = n.to_be(); } self.cursor += len * 4; - println!("data: {:?}", self.data); value } } diff --git a/shen-nbt5/src/tests.rs b/shen-nbt5/src/tests.rs index 8289f72..b011d76 100644 --- a/shen-nbt5/src/tests.rs +++ b/shen-nbt5/src/tests.rs @@ -9,37 +9,78 @@ pub fn gen_datas(len: usize) -> Vec { datas } -#[test] -fn basic_init() { - let mut data = vec![0x01, 0x02, 0x03, 0x04]; - let reader = NbtReader::new(&mut data); - assert_eq!(reader.cursor, 0); - let same_data = vec![0x01, 0x02, 0x03, 0x04]; - assert_eq!(reader.data, &same_data); +mod safe_test { + use super::*; + + #[test] + fn basic_init() { + let mut data = vec![0x01, 0x02, 0x03, 0x04]; + let reader = NbtReader::new(&mut data); + assert_eq!(reader.cursor, 0); + let same_data = vec![0x01, 0x02, 0x03, 0x04]; + assert_eq!(reader.data, &same_data); + } + + #[test] + fn read_x8() { + let mut data = vec![0x01, 0x02, 0x03, 0x04]; + let mut reader = NbtReader::new(&mut data); + assert_eq!(reader.read_i8(), 0x01); + assert_eq!(reader.cursor, 1); + assert_eq!(reader.read_u8(), 0x02); + assert_eq!(reader.cursor, 2); + } + + #[test] + fn read_x16() { + let mut data = vec![0x01, 0x02, 0x03, 0x04]; + let mut reader = NbtReader::new(&mut data); + assert_eq!(reader.read_i16(), 0x0102); + assert_eq!(reader.cursor, 2); + assert_eq!(reader.read_u16(), 0x0304); + assert_eq!(reader.cursor, 4); + } + + #[test] + fn read_x32() { + let mut data = vec![0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04]; + let mut reader = NbtReader::new(&mut data); + assert_eq!(reader.read_i32(), 0x01020304); + assert_eq!(reader.cursor, 4); + assert_eq!(reader.read_u32(), 0x01020304); + assert_eq!(reader.cursor, 8); + } + + #[test] + fn read_x64() { + let mut data = vec![ + 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, + 0x03, 0x04, + ]; + let mut reader = NbtReader::new(&mut data); + assert_eq!(reader.read_i64(), 0x0102030401020304); + assert_eq!(reader.cursor, 8); + assert_eq!(reader.read_u64(), 0x0102030401020304); + assert_eq!(reader.cursor, 16); + } + + #[test] + fn read_fxx() { + let mut data = vec![ + 0x40, 0x49, 0x0f, 0xdb, 0x40, 0x49, 0x0f, 0xdb, 0x40, 0x49, 0x0f, 0xdb, 0x40, 0x49, + 0x0f, 0xdb, + ]; + let mut reader = NbtReader::new(&mut data); + println!("{}", f32::from_be_bytes([0x40, 0x49, 0x0f, 0xdb])); + assert_eq!(reader.read_f32(), 3.1415927); + assert_eq!(reader.cursor, 4); + assert_eq!(reader.read_f64(), 3.14159265); + assert_eq!(reader.cursor, 12); + } } -#[test] -fn read_x8() { - let mut data = vec![0x01, 0x02, 0x03, 0x04]; - let mut reader = NbtReader::new(&mut data); - assert_eq!(reader.read_i8(), 0x01); - assert_eq!(reader.cursor, 1); - assert_eq!(reader.read_u8(), 0x02); - assert_eq!(reader.cursor, 2); -} - -#[test] -fn read_data_safe() { - let mut data = vec![0x01, 0x02, 0x03, 0x04]; - let mut reader = NbtReader::new(&mut data); - assert_eq!(reader.read_u8(), 0x01); - assert_eq!(reader.cursor, 1); - assert_eq!(reader.read_i8(), 0x02); - assert_eq!(reader.cursor, 2); - assert_eq!(reader.read_u8(), 0x03); - assert_eq!(reader.cursor, 3); - assert_eq!(reader.read_i8(), 0x04); - assert_eq!(reader.cursor, 4); +mod unsafe_test { + use super::*; } #[test] @@ -48,7 +89,7 @@ fn read_array() { let mut reader = NbtReader::new(&mut data); assert_eq!(reader.read_u8_array(2), &[0x01, 0x02]); assert_eq!(reader.cursor, 2); - assert_eq!(reader.read_i8_array(2), &[0x03, 0x04]); + assert_eq!(reader.read_i8_array_unchecked(2), &[0x03, 0x04]); assert_eq!(reader.cursor, 4); }