添加了测试文件并更新了NbtReader的实现
This commit is contained in:
parent
9e57447ab2
commit
e0520f713f
@ -3,6 +3,9 @@
|
|||||||
// Little,
|
// Little,
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
/// 用于读取 NBT 数据
|
/// 用于读取 NBT 数据
|
||||||
pub struct NbtReader<'data> {
|
pub struct NbtReader<'data> {
|
||||||
/// NBT 数据
|
/// NBT 数据
|
||||||
@ -81,14 +84,16 @@ impl NbtReader<'_> {
|
|||||||
}
|
}
|
||||||
pub fn read_int_array(&mut self, len: usize) -> &[i32] {
|
pub fn read_int_array(&mut self, len: usize) -> &[i32] {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
let value = std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i32, len);
|
||||||
self.cursor += len * 4;
|
self.cursor += len * 4;
|
||||||
std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i32, len)
|
value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn read_long_array(&mut self, len: usize) -> &[i64] {
|
pub fn read_long_array(&mut self, len: usize) -> &[i64] {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
let value = std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i64, len);
|
||||||
self.cursor += len * 8;
|
self.cursor += len * 8;
|
||||||
std::slice::from_raw_parts(self.data[self.cursor..].as_ptr() as *const i64, len)
|
value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38
shen-nbt5/src/tests.rs
Normal file
38
shen-nbt5/src/tests.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
use crate::{NbtReader, NbtValue};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_init() {
|
||||||
|
let data = vec![0x01, 0x02, 0x03, 0x04];
|
||||||
|
let reader = NbtReader::new(&data);
|
||||||
|
assert_eq!(reader.cursor, 0);
|
||||||
|
assert_eq!(reader.data, &data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read_i8() {
|
||||||
|
let data = vec![0x01, 0x02, 0x03, 0x04];
|
||||||
|
let mut reader = NbtReader::new(&data);
|
||||||
|
assert_eq!(reader.read_i8(), 0x01);
|
||||||
|
assert_eq!(reader.cursor, 1);
|
||||||
|
assert_eq!(reader.read_i8(), 0x02);
|
||||||
|
assert_eq!(reader.cursor, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read_array() {
|
||||||
|
let data = vec![0x01, 0x02, 0x03, 0x04];
|
||||||
|
let mut reader = NbtReader::new(&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.cursor, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn read_long_array() {
|
||||||
|
let data = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
|
||||||
|
let mut reader = NbtReader::new(&data);
|
||||||
|
assert_eq!(reader.read_long_array(1), &[i64::from_be_bytes([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])]);
|
||||||
|
assert_eq!(reader.cursor, 8);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user