From 56181da142f5e95a067feea5c4558fef2a2d49a7 Mon Sep 17 00:00:00 2001 From: Joey Sacchini Date: Tue, 29 Sep 2020 17:05:58 -0400 Subject: implement automated testing of all data-types --- src/types.rs | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) (limited to 'src/types.rs') diff --git a/src/types.rs b/src/types.rs index f62ede9..9d8f595 100644 --- a/src/types.rs +++ b/src/types.rs @@ -4,6 +4,9 @@ use crate::*; use crate::utils::*; use crate::uuid::UUID4; +#[cfg(test)] +use crate::protocol::TestRandom; + // bool impl Serialize for bool { fn mc_serialize(&self, to: &mut S) -> SerializeResult { @@ -23,6 +26,13 @@ impl Deserialize for bool { } } +#[cfg(test)] +impl TestRandom for bool { + fn test_gen_random() -> Self { + rand::random() + } +} + // u8 impl Serialize for u8 { fn mc_serialize(&self, to: &mut S) -> SerializeResult { @@ -36,6 +46,13 @@ impl Deserialize for u8 { } } +#[cfg(test)] +impl TestRandom for u8 { + fn test_gen_random() -> Self { + rand::random() + } +} + // i8 impl Serialize for i8 { fn mc_serialize(&self, to: &mut S) -> SerializeResult { @@ -49,6 +66,13 @@ impl Deserialize for i8 { } } +#[cfg(test)] +impl TestRandom for i8 { + fn test_gen_random() -> Self { + rand::random() + } +} + // u16 impl Serialize for u16 { fn mc_serialize(&self, to: &mut S) -> SerializeResult { @@ -63,6 +87,13 @@ impl Deserialize for u16 { } } +#[cfg(test)] +impl TestRandom for u16 { + fn test_gen_random() -> Self { + rand::random() + } +} + // i16 impl Serialize for i16 { fn mc_serialize(&self, to: &mut S) -> SerializeResult { @@ -76,6 +107,13 @@ impl Deserialize for i16 { } } +#[cfg(test)] +impl TestRandom for i16 { + fn test_gen_random() -> Self { + rand::random() + } +} + // int impl Serialize for i32 { fn mc_serialize(&self, to: &mut S) -> SerializeResult { @@ -90,6 +128,13 @@ impl Deserialize for i32 { } } +#[cfg(test)] +impl TestRandom for i32 { + fn test_gen_random() -> Self { + rand::random() + } +} + // long impl Serialize for i64 { fn mc_serialize(&self, to: &mut S) -> SerializeResult { @@ -104,6 +149,13 @@ impl Deserialize for i64 { } } +#[cfg(test)] +impl TestRandom for i64 { + fn test_gen_random() -> Self { + rand::random() + } +} + // float impl Serialize for f32 { @@ -120,6 +172,13 @@ impl Deserialize for f32 { } } +#[cfg(test)] +impl TestRandom for f32 { + fn test_gen_random() -> Self { + rand::random() + } +} + // double impl Serialize for f64 { //noinspection ALL @@ -135,6 +194,13 @@ impl Deserialize for f64 { } } +#[cfg(test)] +impl TestRandom for f64 { + fn test_gen_random() -> Self { + rand::random() + } +} + // VAR INT AND VAR LONG const VAR_INT_BYTES: usize = 5; const VAR_LONG_BYTES: usize = 10; @@ -188,6 +254,14 @@ impl std::fmt::Display for VarInt { } } +#[cfg(test)] +impl TestRandom for VarInt { + fn test_gen_random() -> Self { + let out: i32 = rand::random(); + Self(out) + } +} + #[derive(Copy, Clone, PartialOrd, PartialEq, Debug, Default, Hash, Ord, Eq)] pub struct VarLong(pub i64); @@ -204,6 +278,14 @@ impl Deserialize for VarLong { } } +#[cfg(test)] +impl TestRandom for VarLong { + fn test_gen_random() -> Self { + let out: i64 = rand::random(); + Self(out) + } +} + fn serialize_var_num(data: u64, out: &mut [u8]) -> &[u8] { let mut v: u64 = data; let mut byte_idx = 0; @@ -274,6 +356,28 @@ impl Deserialize for String { } } +#[cfg(test)] +impl TestRandom for String { + fn test_gen_random() -> Self { + let raw_len: u8 = rand::random(); + let len = raw_len as usize; + let mut out = String::with_capacity(len); + for _ in 0..len { + let c_idx: u8 = rand::random::() % 36; + + let c = if c_idx <= 10 { + (48 + c_idx) as char + } else { + ((c_idx - 10) + 65) as char + }; + + out.push(c) + } + + out + } +} + // position #[derive(Clone, Copy, PartialEq, Hash, Debug)] pub struct IntPosition { @@ -335,6 +439,20 @@ impl Deserialize for IntPosition { } } +#[cfg(test)] +impl TestRandom for IntPosition { + fn test_gen_random() -> Self { + let x: i32 = ((rand::random::() % (1 << 26)) as i32) - (1 << 25); + let z: i32 = ((rand::random::() % (1 << 26)) as i32) - (1 << 25); + let y: i16 = ((rand::random::() % (1 << 12)) as i16) - (1 << 11); + Self{ + x, + y, + z + } + } +} + // angle #[derive(Copy, Clone, PartialEq, Hash, Debug)] pub struct Angle { @@ -355,6 +473,15 @@ impl Deserialize for Angle { } } +#[cfg(test)] +impl TestRandom for Angle { + fn test_gen_random() -> Self { + Self{ + value: rand::random() + } + } +} + // UUID impl Serialize for UUID4 { @@ -388,6 +515,13 @@ impl Deserialize for UUID4 { } } +#[cfg(test)] +impl TestRandom for UUID4 { + fn test_gen_random() -> Self { + UUID4::random() + } +} + // NBT #[derive(Clone, PartialEq, Debug)] @@ -420,6 +554,13 @@ impl Into for NamedNbtTag { } } +#[cfg(test)] +impl TestRandom for NamedNbtTag { + fn test_gen_random() -> Self { + Self { root: nbt::NamedTag::test_gen_random() } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct FixedInt { raw: i32 @@ -449,6 +590,13 @@ impl FixedInt { } } +#[cfg(test)] +impl TestRandom for FixedInt { + fn test_gen_random() -> Self { + FixedInt::new(f64::test_gen_random(), 16) + } +} + // chat #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] pub struct Chat { @@ -704,6 +852,19 @@ impl Chat { to.push(formatter) } } + + pub fn from_text(text: &str) -> Chat { + Chat{ + text: text.to_owned(), + bold: None, + italic: None, + underlined: None, + strikethrough: None, + obfuscated: None, + color: None, + extra: None, + } + } } impl Serialize for Chat { @@ -724,6 +885,14 @@ impl Deserialize for Chat { } } +#[cfg(test)] +impl TestRandom for Chat { + fn test_gen_random() -> Self { + let str = String::test_gen_random(); + Chat::from_text(str.as_str()) + } +} + #[derive(Default)] pub struct BytesSerializer { data: Vec @@ -774,6 +943,18 @@ impl Deserialize for Option where T: Deserialize { } } +#[cfg(test)] +impl TestRandom for Option where T: TestRandom { + fn test_gen_random() -> Self { + let is_present: bool = rand::random(); + if is_present { + Some(T::test_gen_random()) + } else { + None + } + } +} + // SLOT #[derive(Debug, PartialEq, Clone)] pub struct Slot { @@ -812,6 +993,21 @@ impl Deserialize for Slot { } } +#[cfg(test)] +impl TestRandom for Slot { + fn test_gen_random() -> Self { + let item_id = VarInt::test_gen_random(); + let item_count = i8::test_gen_random() % 65; + let nbt = >::test_gen_random(); + + Self{ + item_id, + item_count, + nbt + } + } +} + #[cfg(test)] mod tests { use super::*; -- cgit