From 618dd911c5f2747721b7b719b2adfdf239d0abaf Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Tue, 16 Jan 2024 21:22:58 +0800 Subject: [PATCH] Refactor NBT test functions and add support for multiple NBT versions --- crates/nbt-test/src/main.rs | 90 +++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/crates/nbt-test/src/main.rs b/crates/nbt-test/src/main.rs index 956a3ee..3f1c471 100644 --- a/crates/nbt-test/src/main.rs +++ b/crates/nbt-test/src/main.rs @@ -1,22 +1,12 @@ fn main() { println!("Hello, nbt!"); // sleep 1s - println!("====== small test ======"); - small_read_test(); - println!("====== big test ======"); + println!("============ small test ============"); big_read_test(); - println!("====== cli test ======"); + println!("============ cli test ============"); cli_read_test(); } -fn small_read_test() { - let data: [u8; 0x21] = [ - 0x0A, 0x00, 0x0B, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x08, - 0x00, 0x04, 0x6E, 0x61, 0x6D, 0x65, 0x00, 0x09, 0x42, 0x61, 0x6E, 0x61, 0x6E, 0x72, 0x61, - 0x6D, 0x61, 0x00, - ]; - read_test(data.to_vec()); -} fn big_read_test() { let data: [u8; 0x608] = [ @@ -151,15 +141,8 @@ macro_rules! test_lib { }; } -fn read_test(in_data: Vec) { - let len = in_data.len(); - #[cfg(feature = "debug")] - println!("data: {:?}", data); - - let data = in_data.clone(); - // let data2 = in_data.clone(); - // let data3 = in_data.clone(); - // let data4 = in_data.clone(); +fn test_v1(data: Vec) { + let len = data.len(); std::thread::sleep(std::time::Duration::from_secs(1)); test_lib!( { @@ -167,25 +150,40 @@ fn read_test(in_data: Vec) { let _nbt_data = shen_nbt1::data::NbtItem::try_from(cursor).unwrap(); }, "nbt v1", len ); - +} +fn test_v2(data: Vec) { + let len = data.len(); + std::thread::sleep(std::time::Duration::from_secs(1)); test_lib!( { - let _nbt_data = shen_nbt2::Value::from_vec(data.as_slice().to_vec()); + let _nbt_data = shen_nbt2::Value::from_vec(data); }, "nbt v2", len ); +} +fn test_v3(data: Vec) { + let len = data.len(); + std::thread::sleep(std::time::Duration::from_secs(1)); test_lib!( { - let _nbt_data = shen_nbt3::Value::from_vec(data.as_slice().to_vec()); + let _nbt_data = shen_nbt3::Value::from_vec(data); }, "nbt v3", len ); +} +fn test_v4(data: Vec) { + let len = data.len(); + std::thread::sleep(std::time::Duration::from_secs(1)); test_lib!( { - let _nbt_data = shen_nbt4::Value::from_vec(data.as_slice().to_vec()); + let _nbt_data = shen_nbt4::Value::from_vec(data); }, "nbt v4", len ); +} +fn test_fastnbt(data: Vec) { + let len = data.len(); + std::thread::sleep(std::time::Duration::from_secs(1)); test_lib!( { let _nbt_data: fastnbt::Value = fastnbt::from_bytes(data.as_slice()).unwrap(); @@ -193,6 +191,29 @@ fn read_test(in_data: Vec) { ); } +fn read_test(in_data: Vec) { + let len = in_data.len(); + #[cfg(feature = "debug")] + println!("data: {:?}", data); + + std::thread::sleep(std::time::Duration::from_secs(1)); + + let data = in_data.clone(); + test_v1(data); + + let data = in_data.clone(); + test_v2(data); + + let data = in_data.clone(); + test_v3(data); + + let data = in_data.clone(); + test_v4(data); + + let data = in_data.clone(); + test_fastnbt(data); +} + fn cli_read_test() { let mut args = std::env::args(); @@ -204,35 +225,36 @@ fn cli_read_test() { test_lib!( { let cursor: shen_nbt1::data::Reader = std::io::Cursor::new(data.as_slice()); - let _nbt_data = shen_nbt1::data::NbtItem::try_from(cursor).unwrap(); + let nbt_data = shen_nbt1::data::NbtItem::try_from(cursor).unwrap(); + }, "nbt v1", len ); - let data = std::fs::read(&arg).unwrap(); + let data2 = std::fs::read(&arg).unwrap(); test_lib!( { - let _nbt_data = shen_nbt2::Value::from_vec(data); + let nbt_data = shen_nbt2::Value::from_vec(data2); }, "nbt v2", len ); - let data = std::fs::read(&arg).unwrap(); + let data3 = std::fs::read(&arg).unwrap(); test_lib!( { - let _nbt_data = shen_nbt3::Value::from_vec(data); + let nbt_data = shen_nbt3::Value::from_vec(data3); }, "nbt v3", len ); - let data = std::fs::read(&arg).unwrap(); + let data4 = std::fs::read(&arg).unwrap(); test_lib!( { - let _nbt_data = shen_nbt4::Value::from_vec(data); + let nbt_data = shen_nbt4::Value::from_vec(data4); }, "nbt v4", len ); - let data = std::fs::read(&arg).unwrap(); + let data5 = std::fs::read(&arg).unwrap(); test_lib!( { - let _nbt_data: fastnbt::Value = fastnbt::from_bytes(data.as_slice()).unwrap(); + let nbt_data: fastnbt::Value = fastnbt::from_bytes(data5.as_slice()).unwrap(); }, "fastnbt", len ); } else {