From 1baf305b1ff0099fd944655b49bf00a28996ddf2 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 31 Jan 2022 14:49:29 +0600 Subject: [PATCH] Update bytes tests --- ntex-bytes/src/buf/buf_impl.rs | 104 ++++++++++++++++++++++++++ ntex-bytes/src/buf/buf_mut.rs | 129 +++++++++++++++++++++++++++++++++ ntex-bytes/src/bytes.rs | 77 ++++++++++++++++---- ntex-bytes/src/hex.rs | 18 ++++- ntex-bytes/src/string.rs | 56 +++++++++----- ntex-bytes/tests/test_bytes.rs | 28 ++++++- 6 files changed, 377 insertions(+), 35 deletions(-) diff --git a/ntex-bytes/src/buf/buf_impl.rs b/ntex-bytes/src/buf/buf_impl.rs index 724ae24f..592d4e94 100644 --- a/ntex-bytes/src/buf/buf_impl.rs +++ b/ntex-bytes/src/buf/buf_impl.rs @@ -920,3 +920,107 @@ impl> Buf for std::io::Cursor { // The existence of this function makes the compiler catch if the Buf // trait is "object-safe" or not. fn _assert_trait_object(_b: &dyn Buf) {} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn buf_tests() { + let mut buf = &b"a"[..]; + assert!(buf.has_remaining()); + buf.get_u8(); + assert!(!buf.has_remaining()); + + let mut buf = &b"hello world"[..]; + let mut dst = [0; 5]; + buf.copy_to_slice(&mut dst); + assert_eq!(&b"hello"[..], &dst); + assert_eq!(6, buf.remaining()); + + let mut buf = &b"\x08 hello"[..]; + assert_eq!(8, buf.get_u8()); + + let mut buf = &b"\x08 hello"[..]; + assert_eq!(8, buf.get_i8()); + + let mut buf = &b"\x08\x09 hello"[..]; + assert_eq!(0x0809, buf.get_u16()); + + let mut buf = &b"\x09\x08 hello"[..]; + assert_eq!(0x0809, buf.get_u16_le()); + + let mut buf = &b"\x08\x09 hello"[..]; + assert_eq!(0x0809, buf.get_i16()); + + let mut buf = &b"\x09\x08 hello"[..]; + assert_eq!(0x0809, buf.get_i16_le()); + + let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; + assert_eq!(0x0809A0A1, buf.get_u32()); + + let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; + assert_eq!(0x0809A0A1, buf.get_u32_le()); + + let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; + assert_eq!(0x0809A0A1, buf.get_i32()); + + let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; + assert_eq!(0x0809A0A1, buf.get_i32_le()); + + let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; + assert_eq!(0x0102030405060708, buf.get_u64()); + + let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + assert_eq!(0x0102030405060708, buf.get_u64_le()); + + let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; + assert_eq!(0x0102030405060708, buf.get_i64()); + + let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + assert_eq!(0x0102030405060708, buf.get_i64_le()); + + let mut buf = + &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; + assert_eq!(0x01020304050607080910111213141516, buf.get_u128()); + + let mut buf = + &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le()); + + let mut buf = + &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; + assert_eq!(0x01020304050607080910111213141516, buf.get_i128()); + + let mut buf = + &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; + assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le()); + + let mut buf = &b"\x01\x02\x03 hello"[..]; + assert_eq!(0x010203, buf.get_uint(3)); + + let mut buf = &b"\x03\x02\x01 hello"[..]; + assert_eq!(0x010203, buf.get_uint_le(3)); + + let mut buf = &b"\x01\x02\x03 hello"[..]; + assert_eq!(0x010203, buf.get_int(3)); + + let mut buf = &b"\x03\x02\x01 hello"[..]; + assert_eq!(0x010203, buf.get_int_le(3)); + + let mut buf = &b"\x3F\x99\x99\x9A hello"[..]; + assert_eq!(1.2f32, buf.get_f32()); + + let mut buf = &b"\x9A\x99\x99\x3F hello"[..]; + assert_eq!(1.2f32, buf.get_f32_le()); + + let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..]; + assert_eq!(1.2f64, buf.get_f64()); + + let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..]; + assert_eq!(1.2f64, buf.get_f64_le()); + + let bytes = "hello world".to_bytes(); + assert_eq!(&bytes[..], &b"hello world"[..]); + } +} diff --git a/ntex-bytes/src/buf/buf_mut.rs b/ntex-bytes/src/buf/buf_mut.rs index 437d99f8..90bb4c99 100644 --- a/ntex-bytes/src/buf/buf_mut.rs +++ b/ntex-bytes/src/buf/buf_mut.rs @@ -968,3 +968,132 @@ impl BufMut for &mut [u8] { // The existence of this function makes the compiler catch if the BufMut // trait is "object-safe" or not. fn _assert_trait_object(_b: &dyn BufMut) {} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{BytesMut, BytesVec}; + + #[test] + fn buf_mut_tests() { + let mut buf = vec![]; + buf.put_u8(0x01); + assert_eq!(buf, b"\x01"); + + let mut buf = BytesMut::new(); + buf.put_u8(0x01); + assert_eq!(buf, b"\x01"[..]); + + let mut buf = BytesVec::new(); + buf.put_u8(0x01); + assert_eq!(buf, b"\x01"[..]); + + let mut buf = vec![]; + buf.put_i8(0x01); + assert_eq!(buf, b"\x01"); + + let mut buf = BytesMut::new(); + buf.put_i8(0x01); + assert_eq!(buf, b"\x01"[..]); + + let mut buf = BytesVec::new(); + buf.put_i8(0x01); + assert_eq!(buf, b"\x01"[..]); + + let mut buf = vec![]; + buf.put_i16(0x0809); + assert_eq!(buf, b"\x08\x09"); + + let mut buf = vec![]; + buf.put_i16_le(0x0809); + assert_eq!(buf, b"\x09\x08"); + + let mut buf = vec![]; + buf.put_u32(0x0809A0A1); + assert_eq!(buf, b"\x08\x09\xA0\xA1"); + + let mut buf = vec![]; + buf.put_i32(0x0809A0A1); + assert_eq!(buf, b"\x08\x09\xA0\xA1"); + + let mut buf = vec![]; + buf.put_i32_le(0x0809A0A1); + assert_eq!(buf, b"\xA1\xA0\x09\x08"); + + let mut buf = vec![]; + buf.put_u64(0x0102030405060708); + assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); + + let mut buf = vec![]; + buf.put_u64_le(0x0102030405060708); + assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + + let mut buf = vec![]; + buf.put_i64(0x0102030405060708); + assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); + + let mut buf = vec![]; + buf.put_i64_le(0x0102030405060708); + assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + + let mut buf = vec![]; + buf.put_u128(0x01020304050607080910111213141516); + assert_eq!( + buf, + b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16" + ); + + let mut buf = vec![]; + buf.put_u128_le(0x01020304050607080910111213141516); + assert_eq!( + buf, + b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01" + ); + + let mut buf = vec![]; + buf.put_i128(0x01020304050607080910111213141516); + assert_eq!( + buf, + b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16" + ); + + let mut buf = vec![]; + buf.put_i128_le(0x01020304050607080910111213141516); + assert_eq!( + buf, + b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01" + ); + + let mut buf = vec![]; + buf.put_uint(0x010203, 3); + assert_eq!(buf, b"\x01\x02\x03"); + + let mut buf = vec![]; + buf.put_uint_le(0x010203, 3); + assert_eq!(buf, b"\x03\x02\x01"); + + let mut buf = vec![]; + buf.put_int(0x010203, 3); + assert_eq!(buf, b"\x01\x02\x03"); + + let mut buf = vec![]; + buf.put_int_le(0x010203, 3); + assert_eq!(buf, b"\x03\x02\x01"); + + let mut buf = vec![]; + buf.put_f32(1.2f32); + assert_eq!(buf, b"\x3F\x99\x99\x9A"); + + let mut buf = vec![]; + buf.put_f32_le(1.2f32); + assert_eq!(buf, b"\x9A\x99\x99\x3F"); + + let mut buf = vec![]; + buf.put_f64(1.2f64); + assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33"); + + let mut buf = vec![]; + buf.put_f64_le(1.2f64); + assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F"); + } +} diff --git a/ntex-bytes/src/bytes.rs b/ntex-bytes/src/bytes.rs index 12a7bc0a..efb741ea 100644 --- a/ntex-bytes/src/bytes.rs +++ b/ntex-bytes/src/bytes.rs @@ -414,8 +414,6 @@ const INLINE_CAP: usize = 4 * 8 - 2; #[cfg(target_pointer_width = "32")] const INLINE_CAP: usize = 4 * 4 - 2; -const EMPTY: &[u8] = &[]; - /* * * ===== Bytes ===== @@ -746,18 +744,16 @@ impl Bytes { // trim down only if buffer is not inline or static and // buffer's unused space is greater than 64 bytes - if !(kind == KIND_INLINE || kind == KIND_STATIC) - && (self.inner.capacity() - self.inner.len() >= 64) - { - *self = if self.len() <= INLINE_CAP { - Bytes { + if !(kind == KIND_INLINE || kind == KIND_STATIC) { + if self.inner.len() <= INLINE_CAP { + *self = Bytes { inner: Inner::from_slice_inline(self), - } - } else { - Bytes { + }; + } else if self.inner.capacity() - self.inner.len() >= 64 { + *self = Bytes { inner: Inner::from_slice(self.len(), self, self.inner.pool()), } - }; + } } } @@ -774,7 +770,7 @@ impl Bytes { /// ``` #[inline] pub fn clear(&mut self) { - self.inner = Inner::from_static(EMPTY); + self.inner = Inner::empty_inline(); } /// Attempts to convert into a `BytesMut` handle. @@ -1143,12 +1139,19 @@ impl BytesMut { } /// Creates a new `BytesMut` from slice, by copying it. - pub fn copy_from_slice_in(src: &[u8], pool: T) -> Self + pub fn copy_from_slice>(src: T) -> Self { + Self::copy_from_slice_in(src, PoolId::DEFAULT) + } + + /// Creates a new `BytesMut` from slice, by copying it. + pub fn copy_from_slice_in(src: T, pool: U) -> Self where - PoolRef: From, + T: AsRef<[u8]>, + PoolRef: From, { + let s = src.as_ref(); BytesMut { - inner: Inner::from_slice(src.len(), src, pool.into()), + inner: Inner::from_slice(s.len(), s, pool.into()), } } @@ -2452,7 +2455,7 @@ impl Eq for BytesVec {} impl PartialEq for BytesVec { #[inline] fn eq(&self, other: &BytesVec) -> bool { - unsafe { ptr::eq(self.inner.as_ptr(), other.inner.as_ptr()) } + self.inner.as_ref() == other.inner.as_ref() } } @@ -4137,3 +4140,45 @@ fn abort() { let _a = Abort; panic!(); } + +#[cfg(test)] +mod tests { + use std::convert::TryFrom; + + use super::*; + + const LONG: &[u8] = + b"mary had a little lamb, little lamb, little lamb, little lamb, little lamb, little lamb \ + mary had a little lamb, little lamb, little lamb, little lamb, little lamb, little lamb \ + mary had a little lamb, little lamb, little lamb, little lamb, little lamb, little lamb"; + + #[test] + fn trimdown() { + let mut b = Bytes::from(LONG.to_vec()); + assert_eq!(b.inner.capacity(), 263); + unsafe { b.inner.set_len(68) }; + assert_eq!(b.len(), 68); + assert_eq!(b.inner.capacity(), 263); + b.trimdown(); + assert_eq!(b.inner.capacity(), 96); + + unsafe { b.inner.set_len(16) }; + b.trimdown(); + assert!(b.is_inline()); + } + + #[test] + fn bytes() { + let mut b = Bytes::from(LONG.to_vec()); + b.clear(); + assert!(b.is_inline()); + assert!(b.is_empty()); + assert!(b.len() == 0); + + let b = Bytes::from(BytesMut::from(LONG)); + assert_eq!(b, LONG); + + let b = BytesMut::try_from(b).unwrap(); + assert_eq!(b, LONG); + } +} diff --git a/ntex-bytes/src/hex.rs b/ntex-bytes/src/hex.rs index d3fdafbb..3a6fa5c4 100644 --- a/ntex-bytes/src/hex.rs +++ b/ntex-bytes/src/hex.rs @@ -1,4 +1,4 @@ -use crate::{Bytes, BytesMut}; +use crate::{Bytes, BytesMut, BytesVec}; use std::fmt::{Formatter, LowerHex, Result, UpperHex}; struct BytesRef<'a>(&'a [u8]); @@ -33,5 +33,21 @@ macro_rules! hex_impl { hex_impl!(LowerHex, Bytes); hex_impl!(LowerHex, BytesMut); +hex_impl!(LowerHex, BytesVec); hex_impl!(UpperHex, Bytes); hex_impl!(UpperHex, BytesMut); +hex_impl!(UpperHex, BytesVec); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hex() { + let b = Bytes::from_static(b"hello world"); + let f = format!("{:x}", b); + assert_eq!(f, "68656c6c6f20776f726c64"); + let f = format!("{:X}", b); + assert_eq!(f, "68656C6C6F20776F726C64"); + } +} diff --git a/ntex-bytes/src/string.rs b/ntex-bytes/src/string.rs index 3b7a3d1c..8e156a89 100644 --- a/ntex-bytes/src/string.rs +++ b/ntex-bytes/src/string.rs @@ -1,7 +1,7 @@ //! A UTF-8 encoded read-only string using Bytes as storage. use std::{borrow, convert::TryFrom, fmt, hash, ops, slice, str}; -use crate::{Bytes, BytesMut}; +use crate::{Bytes, BytesMut, BytesVec}; /// An immutable UTF-8 encoded string with [`Bytes`] as a storage. #[derive(Clone, Default, Eq, PartialOrd, Ord)] @@ -255,7 +255,20 @@ impl TryFrom for ByteString { type Error = (); #[inline] - fn try_from(value: crate::BytesMut) -> Result { + fn try_from(value: BytesMut) -> Result { + if utf8::is_valid(&value) { + Ok(ByteString(value.freeze())) + } else { + Err(()) + } + } +} + +impl TryFrom for ByteString { + type Error = (); + + #[inline] + fn try_from(value: BytesVec) -> Result { if utf8::is_valid(&value) { Ok(ByteString(value.freeze())) } else { @@ -326,11 +339,27 @@ mod test { use super::*; #[test] - fn test_partial_eq() { - let s: ByteString = ByteString::from_static("test"); + fn test_basics() { + let s = ByteString::from_static("test"); assert_eq!(s, "test"); assert_eq!(s, *"test"); assert_eq!(s, "test".to_owned()); + + assert_eq!(format!("{}", s), "test"); + assert_eq!(format!("{:?}", s), "\"test\""); + } + + #[test] + fn test_split() { + let mut s = ByteString::from_static("helloworld"); + let s1 = s.split_off(5); + assert_eq!(s, "hello"); + assert_eq!(s1, "world"); + + let mut s = ByteString::from_static("helloworld"); + let s1 = s.split_to(5); + assert_eq!(s, "world"); + assert_eq!(s1, "hello"); } #[test] @@ -369,28 +398,21 @@ mod test { } #[test] - fn test_try_from_slice() { + fn test_try_from() { + let _ = ByteString::try_from(&b"nice bytes"[..]).unwrap(); + let _ = ByteString::try_from(b"nice bytes".to_vec()).unwrap(); let _ = ByteString::try_from(Bytes::from_static(b"nice bytes")).unwrap(); + let _ = ByteString::try_from(BytesMut::from(&b"nice bytes"[..])).unwrap(); + let _ = + ByteString::try_from(BytesVec::copy_from_slice(&b"nice bytes"[..])).unwrap(); } - #[test] - fn test_try_from_bytes() { - let _ = ByteString::try_from(Bytes::from_static(b"nice bytes")).unwrap(); - } - - #[test] - fn test_try_from_bytes_mut() { - let _ = ByteString::try_from(crate::BytesMut::from(&b"nice bytes"[..])).unwrap(); - } - - #[cfg(feature = "serde")] #[test] fn test_serialize() { let s: ByteString = serde_json::from_str(r#""nice bytes""#).unwrap(); assert_eq!(s, "nice bytes"); } - #[cfg(feature = "serde")] #[test] fn test_deserialize() { let s = serde_json::to_string(&ByteString::from_static("nice bytes")).unwrap(); diff --git a/ntex-bytes/tests/test_bytes.rs b/ntex-bytes/tests/test_bytes.rs index b853a9ae..5c6fca83 100644 --- a/ntex-bytes/tests/test_bytes.rs +++ b/ntex-bytes/tests/test_bytes.rs @@ -440,7 +440,6 @@ fn split_off_to_at_gt_len() { #[test] fn fns_defined_for_bytes_mut() { let mut bytes = BytesMut::from(&b"hello world"[..]); - bytes.as_ptr(); bytes.as_mut_ptr(); @@ -448,6 +447,20 @@ fn fns_defined_for_bytes_mut() { let v: Vec = bytes.as_ref().iter().cloned().collect(); assert_eq!(&v[..], bytes); + let b2: BytesMut = v.iter().collect(); + assert_eq!(b2, bytes); + assert_eq!(&v[..], b2); + + bytes.truncate(5); + assert_eq!(bytes, b"hello"[..]); + + bytes.clear(); + assert!(bytes.is_empty()); + + bytes.resize(10, b'1'); + assert_eq!(bytes, b"1111111111"[..]); + + // BytesVec let mut bytes = BytesVec::copy_from_slice(&b"hello world"[..]); bytes.as_ptr(); bytes.as_mut_ptr(); @@ -455,6 +468,19 @@ fn fns_defined_for_bytes_mut() { // Iterator let v: Vec = bytes.as_ref().iter().cloned().collect(); assert_eq!(&v[..], bytes); + + let b2: BytesVec = v.iter().collect(); + assert_eq!(b2, bytes); + assert_eq!(&v[..], b2); + + bytes.truncate(5); + assert_eq!(bytes, b"hello"[..]); + + bytes.clear(); + assert!(bytes.is_empty()); + + bytes.resize(10, b'1'); + assert_eq!(bytes, b"1111111111"[..]); } #[test]