Improve tests (#158)

* Improve tests
This commit is contained in:
Nikolay Kim 2023-01-06 22:08:24 +06:00 committed by GitHub
parent 2f68a2a641
commit f47e830cfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 183 additions and 25 deletions

View file

@ -990,6 +990,25 @@ mod tests {
b.put_slice(b"123");
assert_eq!(&buf[..], b"1231111111");
let mut b: &mut [u8] = buf.as_mut();
let chunk = b.chunk_mut();
chunk.write_byte(0, b'9');
assert_eq!(&buf[..], b"9231111111");
let mut b = buf.as_mut();
let chunk = b.chunk_mut();
chunk.copy_from_slice(b"0000000000");
assert_eq!(&buf[..], b"0000000000");
let mut b = buf.as_mut();
assert_eq!(format!("{:?}", b.chunk_mut()), "UninitSlice[...]");
let b = buf.as_mut();
let mut bb = Box::new(b);
let chunk = bb.chunk_mut();
chunk.copy_from_slice(b"1111111111");
assert_eq!(&buf[..], b"1111111111");
let mut buf = BytesMut::new();
buf.put_u8(0x01);
assert_eq!(buf, b"\x01"[..]);

View file

@ -4078,12 +4078,39 @@ mod tests {
assert!(b.is_empty());
assert!(b.len() == 0);
let b = Bytes::from(&Bytes::from(LONG));
assert_eq!(b, LONG);
let b = Bytes::from(BytesMut::from(LONG));
assert_eq!(b, LONG);
let b = BytesMut::try_from(b).unwrap();
let mut b: Bytes = BytesMut::try_from(b).unwrap().freeze();
assert_eq!(b, LONG);
assert_eq!(Buf::remaining(&b), LONG.len());
assert_eq!(Buf::chunk(&b), LONG);
Buf::advance(&mut b, 10);
assert_eq!(Buf::chunk(&b), &LONG[10..]);
let mut b = BytesMut::try_from(LONG).unwrap();
assert_eq!(b, LONG);
assert_eq!(Buf::remaining(&b), LONG.len());
assert_eq!(BufMut::remaining_mut(&b), 25);
assert_eq!(Buf::chunk(&b), LONG);
Buf::advance(&mut b, 10);
assert_eq!(Buf::chunk(&b), &LONG[10..]);
let mut b = BytesMut::with_capacity(12);
BufMut::put_i8(&mut b, 1);
assert_eq!(b, b"\x01".as_ref());
BufMut::put_u8(&mut b, 2);
assert_eq!(b, b"\x01\x02".as_ref());
BufMut::put_slice(&mut b, b"12345");
assert_eq!(b, b"\x01\x0212345".as_ref());
BufMut::chunk_mut(&mut b).write_byte(0, b'1');
unsafe { BufMut::advance_mut(&mut b, 1) };
assert_eq!(b, b"\x01\x02123451".as_ref());
}
#[test]
fn bytes_vec() {
let bv = BytesVec::copy_from_slice(&LONG[..]);

View file

@ -86,3 +86,24 @@ macro_rules! serde_impl {
serde_impl!(Bytes, BytesVisitor, copy_from_slice);
serde_impl!(BytesMut, BytesMutVisitor, from);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize() {
let s: Bytes = serde_json::from_str(r#""nice bytes""#).unwrap();
assert_eq!(s, "nice bytes");
let s: BytesMut = serde_json::from_str(r#""nice bytes""#).unwrap();
assert_eq!(s, "nice bytes");
}
#[test]
fn test_deserialize() {
let s = serde_json::to_string(&Bytes::from_static(b"nice bytes")).unwrap();
assert_eq!(s, "[110,105,99,101,32,98,121,116,101,115]");
let s = serde_json::to_string(&BytesMut::copy_from_slice(b"nice bytes")).unwrap();
assert_eq!(s, "[110,105,99,101,32,98,121,116,101,115]");
}
}

View file

@ -371,7 +371,7 @@ mod utf8 {
#[cfg(test)]
mod test {
use std::borrow::ToOwned;
use std::borrow::{Borrow, Cow, ToOwned};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
@ -384,12 +384,34 @@ mod test {
assert_eq!(s, "test");
assert_eq!(s, *"test");
assert_eq!(s, "test".to_owned());
assert_eq!(s.as_str(), "test");
assert_eq!(s.as_slice(), b"test");
assert_eq!(s.as_bytes(), &Bytes::copy_from_slice(b"test"));
assert_eq!(Borrow::<str>::borrow(&s), "test");
assert_eq!(format!("{}", s), "test");
assert_eq!(format!("{:?}", s), "\"test\"");
assert_eq!(s.into_bytes(), Bytes::copy_from_slice(b"test"));
let b = s.into_bytes();
assert_eq!(b, Bytes::copy_from_slice(b"test"));
let s = unsafe { ByteString::from_bytes_unchecked(b) };
assert_eq!(s, "test");
assert_eq!(s.slice(0..2), "te");
let s = ByteString::from(Cow::Borrowed("test"));
assert_eq!(s, "test");
let mut s = ByteString::from(Cow::Owned("test".to_string()));
assert_eq!(s, "test");
s.clear();
assert_eq!(s, "");
assert!(ByteString::try_from(b"\xc3\x28".as_ref()).is_err());
assert!(ByteString::try_from(vec![b'\xc3']).is_err());
assert!(ByteString::try_from(Bytes::from_static(b"\xc3\x28")).is_err());
assert!(ByteString::try_from(&Bytes::from_static(b"\xc3\x28")).is_err());
assert!(ByteString::try_from(BytesMut::copy_from_slice(b"\xc3\x28")).is_err());
}
#[test]