From 9a66cb97a659ed1f0e52ba3d9438de1d9b78d64a Mon Sep 17 00:00:00 2001 From: EightFactorial Date: Wed, 25 Jan 2023 09:51:27 -0800 Subject: Fix test and packets (#60) * Fix test and packets * Fix bug, fix a couple more packets * add tests and fix stuff * fix warnings Co-authored-by: Ubuntu --- azalea-buf/src/lib.rs | 15 +++++++++++++++ azalea-buf/src/write.rs | 7 ++++--- 2 files changed, 19 insertions(+), 3 deletions(-) (limited to 'azalea-buf/src') diff --git a/azalea-buf/src/lib.rs b/azalea-buf/src/lib.rs index 568b542e..73d76949 100755 --- a/azalea-buf/src/lib.rs +++ b/azalea-buf/src/lib.rs @@ -142,6 +142,21 @@ mod tests { assert_eq!(i32::var_read_from(&mut Cursor::new(&buf)).unwrap(), 7178); } + #[test] + fn test_write_varlong() { + let mut buf = Vec::new(); + 0u64.var_write_into(&mut buf).unwrap(); + assert_eq!(buf, vec![0]); + + let mut buf = Vec::new(); + 1u64.var_write_into(&mut buf).unwrap(); + assert_eq!(buf, vec![1]); + + let mut buf = Vec::new(); + 9223372036854775807u64.var_write_into(&mut buf).unwrap(); + assert_eq!(buf, vec![255, 255, 255, 255, 255, 255, 255, 255, 127]); + } + #[test] fn test_list() { let original_vec = vec!["a".to_string(), "bc".to_string(), "def".to_string()]; diff --git a/azalea-buf/src/write.rs b/azalea-buf/src/write.rs index e00ddce9..161f8645 100755 --- a/azalea-buf/src/write.rs +++ b/azalea-buf/src/write.rs @@ -132,15 +132,16 @@ impl McBufVarWritable for i64 { fn var_write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { let mut buffer = [0]; let mut value = *self; + if value == 0 { + buf.write_all(&buffer).unwrap(); + } while value != 0 { buffer[0] = (value & 0b0111_1111) as u8; value = (value >> 7) & (i64::max_value() >> 6); if value != 0 { buffer[0] |= 0b1000_0000; } - // this only writes a single byte, so write_all isn't necessary - // the let _ = is so clippy doesn't complain - let _ = buf.write(&buffer)?; + buf.write_all(&buffer)?; } Ok(()) } -- cgit v1.2.3