2024-03-06 23:39:15 +08:00
|
|
|
use crate::{NbtReader, NbtValue};
|
|
|
|
|
2024-03-08 06:13:05 +08:00
|
|
|
/// 生成测试数据
|
|
|
|
pub fn gen_datas(len: usize) -> Vec<u8> {
|
|
|
|
let mut datas = Vec::with_capacity(len);
|
|
|
|
for i in 0..len {
|
|
|
|
datas.push(i as u8);
|
|
|
|
}
|
|
|
|
datas
|
|
|
|
}
|
|
|
|
|
2024-03-06 23:39:15 +08:00
|
|
|
#[test]
|
|
|
|
fn basic_init() {
|
2024-03-07 22:34:51 +08:00
|
|
|
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
|
|
|
let reader = NbtReader::new(&mut data);
|
2024-03-06 23:39:15 +08:00
|
|
|
assert_eq!(reader.cursor, 0);
|
2024-03-07 22:34:51 +08:00
|
|
|
let same_data = vec![0x01, 0x02, 0x03, 0x04];
|
|
|
|
assert_eq!(reader.data, &same_data);
|
2024-03-06 23:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_i8() {
|
2024-03-07 22:34:51 +08:00
|
|
|
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
|
|
|
let mut reader = NbtReader::new(&mut data);
|
2024-03-06 23:39:15 +08:00
|
|
|
assert_eq!(reader.read_i8(), 0x01);
|
|
|
|
assert_eq!(reader.cursor, 1);
|
|
|
|
assert_eq!(reader.read_i8(), 0x02);
|
|
|
|
assert_eq!(reader.cursor, 2);
|
|
|
|
}
|
|
|
|
|
2024-03-08 01:00:11 +08:00
|
|
|
#[test]
|
2024-03-08 06:13:05 +08:00
|
|
|
fn read_one_bytes() {
|
|
|
|
let mut data = vec![0x01, 0x02];
|
|
|
|
let mut reader = NbtReader::new(&mut data);
|
|
|
|
assert_eq!(reader.read_u8(), 0x01);
|
|
|
|
assert_eq!(reader.cursor, 1);
|
|
|
|
assert_eq!(reader.read_u8(), 0x02);
|
|
|
|
assert_eq!(reader.cursor, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-03-08 20:54:21 +08:00
|
|
|
fn read_data_safe() {
|
|
|
|
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
|
|
|
let mut reader = NbtReader::new(&mut data);
|
2024-03-08 06:13:05 +08:00
|
|
|
assert_eq!(reader.read_u8(), 0x00);
|
|
|
|
assert_eq!(reader.cursor, 1);
|
2024-03-08 01:00:11 +08:00
|
|
|
assert_eq!(reader.read_i8(), 0x01);
|
2024-03-08 06:13:05 +08:00
|
|
|
assert_eq!(reader.cursor, 2);
|
2024-03-08 01:00:11 +08:00
|
|
|
assert_eq!(reader.read_u8(), 0x02);
|
2024-03-08 06:13:05 +08:00
|
|
|
assert_eq!(reader.cursor, 3);
|
2024-03-08 20:54:21 +08:00
|
|
|
|
2024-03-08 01:00:11 +08:00
|
|
|
}
|
|
|
|
|
2024-03-06 23:39:15 +08:00
|
|
|
#[test]
|
|
|
|
fn read_array() {
|
2024-03-07 22:34:51 +08:00
|
|
|
let mut data = vec![0x01, 0x02, 0x03, 0x04];
|
|
|
|
let mut reader = NbtReader::new(&mut data);
|
2024-03-06 23:39:15 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-03-07 22:34:51 +08:00
|
|
|
#[test]
|
|
|
|
fn read_int_array() {
|
|
|
|
let mut value = 1234567890_i32.to_be_bytes();
|
|
|
|
let mut reader = NbtReader::new(&mut value);
|
2024-03-09 13:21:25 +08:00
|
|
|
assert_eq!(reader.read_i32_array_unchecked(1), &[1234567890_i32]);
|
2024-03-07 22:34:51 +08:00
|
|
|
assert_eq!(reader.cursor, 4);
|
|
|
|
}
|
|
|
|
|
2024-03-06 23:39:15 +08:00
|
|
|
#[test]
|
|
|
|
fn read_long_array() {
|
2024-03-07 22:34:51 +08:00
|
|
|
let mut value = 1234567890_i64.to_be_bytes();
|
|
|
|
let mut reader = NbtReader::new(&mut value);
|
2024-03-09 13:21:25 +08:00
|
|
|
assert_eq!(reader.read_i64_array(1), &[1234567890_i64]);
|
2024-03-06 23:39:15 +08:00
|
|
|
assert_eq!(reader.cursor, 8);
|
|
|
|
}
|