[test not passed]

This commit is contained in:
shenjack 2024-03-09 13:38:38 +08:00
parent 5cf3918e74
commit 98cf4c77ee
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 106 additions and 33 deletions

View File

@ -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 /// 长度溢出会导致 UB
@ -158,7 +192,7 @@ impl NbtReader<'_> {
/// # 安全性 /// # 安全性
/// ///
/// 长度溢出会导致 UB /// 长度溢出会导致 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 { let value = unsafe {
std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i8, len) 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] { pub fn read_i32_array_unchecked(&mut self, len: usize) -> &[i32] {
unsafe { unsafe {
println!("data: {:?}", self.data);
let value = let value =
std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i32, len); std::slice::from_raw_parts_mut(self.data[self.cursor..].as_ptr() as *mut i32, len);
for n in &mut *value { for n in &mut *value {
*n = n.to_be(); *n = n.to_be();
} }
self.cursor += len * 4; self.cursor += len * 4;
println!("data: {:?}", self.data);
value value
} }
} }

View File

@ -9,37 +9,78 @@ pub fn gen_datas(len: usize) -> Vec<u8> {
datas datas
} }
#[test] mod safe_test {
fn basic_init() { use super::*;
#[test]
fn basic_init() {
let mut data = vec![0x01, 0x02, 0x03, 0x04]; let mut data = vec![0x01, 0x02, 0x03, 0x04];
let reader = NbtReader::new(&mut data); let reader = NbtReader::new(&mut data);
assert_eq!(reader.cursor, 0); assert_eq!(reader.cursor, 0);
let same_data = vec![0x01, 0x02, 0x03, 0x04]; let same_data = vec![0x01, 0x02, 0x03, 0x04];
assert_eq!(reader.data, &same_data); assert_eq!(reader.data, &same_data);
} }
#[test] #[test]
fn read_x8() { fn read_x8() {
let mut data = vec![0x01, 0x02, 0x03, 0x04]; let mut data = vec![0x01, 0x02, 0x03, 0x04];
let mut reader = NbtReader::new(&mut data); let mut reader = NbtReader::new(&mut data);
assert_eq!(reader.read_i8(), 0x01); assert_eq!(reader.read_i8(), 0x01);
assert_eq!(reader.cursor, 1); assert_eq!(reader.cursor, 1);
assert_eq!(reader.read_u8(), 0x02); assert_eq!(reader.read_u8(), 0x02);
assert_eq!(reader.cursor, 2); assert_eq!(reader.cursor, 2);
} }
#[test] #[test]
fn read_data_safe() { fn read_x16() {
let mut data = vec![0x01, 0x02, 0x03, 0x04]; let mut data = vec![0x01, 0x02, 0x03, 0x04];
let mut reader = NbtReader::new(&mut data); let mut reader = NbtReader::new(&mut data);
assert_eq!(reader.read_u8(), 0x01); assert_eq!(reader.read_i16(), 0x0102);
assert_eq!(reader.cursor, 1);
assert_eq!(reader.read_i8(), 0x02);
assert_eq!(reader.cursor, 2); assert_eq!(reader.cursor, 2);
assert_eq!(reader.read_u8(), 0x03); assert_eq!(reader.read_u16(), 0x0304);
assert_eq!(reader.cursor, 3);
assert_eq!(reader.read_i8(), 0x04);
assert_eq!(reader.cursor, 4); 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);
}
}
mod unsafe_test {
use super::*;
} }
#[test] #[test]
@ -48,7 +89,7 @@ fn read_array() {
let mut reader = NbtReader::new(&mut data); let mut reader = NbtReader::new(&mut data);
assert_eq!(reader.read_u8_array(2), &[0x01, 0x02]); assert_eq!(reader.read_u8_array(2), &[0x01, 0x02]);
assert_eq!(reader.cursor, 2); 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); assert_eq!(reader.cursor, 4);
} }