diff options
author | Joel Galenson <jgalenson@google.com> | 2021-09-22 10:51:41 -0700 |
---|---|---|
committer | Joel Galenson <jgalenson@google.com> | 2021-09-22 10:51:41 -0700 |
commit | 12f62b94d6ccdca90dcd0edfbff82031fec3912f (patch) | |
tree | 8e48eb122f9388d510dacf7066a11e16c9b411d3 /tests | |
parent | 199730d90816980d7cac4cb1dabcce07bc548a38 (diff) | |
download | bytes-12f62b94d6ccdca90dcd0edfbff82031fec3912f.tar.gz |
Upgrade rust/crates/bytes to 1.1.0
Test: make
Change-Id: I62c795247b1a9f37ec66c3ef6b1add6b78530482
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_buf_mut.rs | 53 | ||||
-rw-r--r-- | tests/test_bytes.rs | 41 | ||||
-rw-r--r-- | tests/test_chain.rs | 21 | ||||
-rw-r--r-- | tests/test_take.rs | 20 |
4 files changed, 133 insertions, 2 deletions
diff --git a/tests/test_buf_mut.rs b/tests/test_buf_mut.rs index 8d270e3..53f4e86 100644 --- a/tests/test_buf_mut.rs +++ b/tests/test_buf_mut.rs @@ -9,7 +9,7 @@ use core::usize; fn test_vec_as_mut_buf() { let mut buf = Vec::with_capacity(64); - assert_eq!(buf.remaining_mut(), usize::MAX); + assert_eq!(buf.remaining_mut(), isize::MAX as usize); assert!(buf.chunk_mut().len() >= 64); @@ -17,7 +17,7 @@ fn test_vec_as_mut_buf() { assert_eq!(&buf, b"zomg"); - assert_eq!(buf.remaining_mut(), usize::MAX - 4); + assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4); assert_eq!(buf.capacity(), 64); for _ in 0..16 { @@ -28,6 +28,14 @@ fn test_vec_as_mut_buf() { } #[test] +fn test_vec_put_bytes() { + let mut buf = Vec::new(); + buf.push(17); + buf.put_bytes(19, 2); + assert_eq!([17, 19, 19], &buf[..]); +} + +#[test] fn test_put_u8() { let mut buf = Vec::with_capacity(8); buf.put_u8(33); @@ -46,6 +54,34 @@ fn test_put_u16() { } #[test] +fn test_put_int() { + let mut buf = Vec::with_capacity(8); + buf.put_int(0x1020304050607080, 3); + assert_eq!(b"\x60\x70\x80", &buf[..]); +} + +#[test] +#[should_panic] +fn test_put_int_nbytes_overflow() { + let mut buf = Vec::with_capacity(8); + buf.put_int(0x1020304050607080, 9); +} + +#[test] +fn test_put_int_le() { + let mut buf = Vec::with_capacity(8); + buf.put_int_le(0x1020304050607080, 3); + assert_eq!(b"\x80\x70\x60", &buf[..]); +} + +#[test] +#[should_panic] +fn test_put_int_le_nbytes_overflow() { + let mut buf = Vec::with_capacity(8); + buf.put_int_le(0x1020304050607080, 9); +} + +#[test] #[should_panic(expected = "cannot advance")] fn test_vec_advance_mut() { // Verify fix for #354 @@ -70,6 +106,19 @@ fn test_mut_slice() { let mut v = vec![0, 0, 0, 0]; let mut s = &mut v[..]; s.put_u32(42); + + assert_eq!(s.len(), 0); + assert_eq!(&v, &[0, 0, 0, 42]); +} + +#[test] +fn test_slice_put_bytes() { + let mut v = [0, 0, 0, 0]; + let mut s = &mut v[..]; + s.put_u8(17); + s.put_bytes(19, 2); + assert_eq!(1, s.remaining_mut()); + assert_eq!(&[17, 19, 19, 0], &v[..]); } #[test] diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index b9e6ce4..f0cae99 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -785,6 +785,31 @@ fn bytes_mut_unsplit_empty_self() { } #[test] +fn bytes_mut_unsplit_other_keeps_capacity() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aabb"); + + // non empty other created "from" buf + let mut other = buf.split_off(buf.len()); + other.extend_from_slice(b"ccddee"); + buf.unsplit(other); + + assert_eq!(buf.capacity(), 64); +} + +#[test] +fn bytes_mut_unsplit_empty_other_keeps_capacity() { + let mut buf = BytesMut::with_capacity(64); + buf.extend_from_slice(b"aabbccddee"); + + // empty other created "from" buf + let other = buf.split_off(buf.len()); + buf.unsplit(other); + + assert_eq!(buf.capacity(), 64); +} + +#[test] fn bytes_mut_unsplit_arc_different() { let mut buf = BytesMut::with_capacity(64); buf.extend_from_slice(b"aaaabbbbeeee"); @@ -961,3 +986,19 @@ fn bytes_with_capacity_but_empty() { let vec = Vec::with_capacity(1); let _ = Bytes::from(vec); } + +#[test] +fn bytes_put_bytes() { + let mut bytes = BytesMut::new(); + bytes.put_u8(17); + bytes.put_bytes(19, 2); + assert_eq!([17, 19, 19], bytes.as_ref()); +} + +#[test] +fn box_slice_empty() { + // See https://github.com/tokio-rs/bytes/issues/340 + let empty: Box<[u8]> = Default::default(); + let b = Bytes::from(empty); + assert!(b.is_empty()); +} diff --git a/tests/test_chain.rs b/tests/test_chain.rs index 500ccd4..affaf7a 100644 --- a/tests/test_chain.rs +++ b/tests/test_chain.rs @@ -132,3 +132,24 @@ fn vectored_read() { assert_eq!(iovecs[3][..], b""[..]); } } + +#[test] +fn chain_get_bytes() { + let mut ab = Bytes::copy_from_slice(b"ab"); + let mut cd = Bytes::copy_from_slice(b"cd"); + let ab_ptr = ab.as_ptr(); + let cd_ptr = cd.as_ptr(); + let mut chain = (&mut ab).chain(&mut cd); + let a = chain.copy_to_bytes(1); + let bc = chain.copy_to_bytes(2); + let d = chain.copy_to_bytes(1); + + assert_eq!(Bytes::copy_from_slice(b"a"), a); + assert_eq!(Bytes::copy_from_slice(b"bc"), bc); + assert_eq!(Bytes::copy_from_slice(b"d"), d); + + // assert `get_bytes` did not allocate + assert_eq!(ab_ptr, a.as_ptr()); + // assert `get_bytes` did not allocate + assert_eq!(cd_ptr.wrapping_offset(1), d.as_ptr()); +} diff --git a/tests/test_take.rs b/tests/test_take.rs index a23a29e..51df91d 100644 --- a/tests/test_take.rs +++ b/tests/test_take.rs @@ -1,6 +1,7 @@ #![warn(rust_2018_idioms)] use bytes::buf::Buf; +use bytes::Bytes; #[test] fn long_take() { @@ -10,3 +11,22 @@ fn long_take() { assert_eq!(11, buf.remaining()); assert_eq!(b"hello world", buf.chunk()); } + +#[test] +fn take_copy_to_bytes() { + let mut abcd = Bytes::copy_from_slice(b"abcd"); + let abcd_ptr = abcd.as_ptr(); + let mut take = (&mut abcd).take(2); + let a = take.copy_to_bytes(1); + assert_eq!(Bytes::copy_from_slice(b"a"), a); + // assert `to_bytes` did not allocate + assert_eq!(abcd_ptr, a.as_ptr()); + assert_eq!(Bytes::copy_from_slice(b"bcd"), abcd); +} + +#[test] +#[should_panic] +fn take_copy_to_bytes_panics() { + let abcd = Bytes::copy_from_slice(b"abcd"); + abcd.take(2).copy_to_bytes(3); +} |