summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bytes/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/bytes/tests
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/bytes/tests')
-rw-r--r--third_party/rust/bytes/tests/test_buf.rs120
-rw-r--r--third_party/rust/bytes/tests/test_buf_mut.rs178
-rw-r--r--third_party/rust/bytes/tests/test_bytes.rs1210
-rw-r--r--third_party/rust/bytes/tests/test_bytes_odd_alloc.rs97
-rw-r--r--third_party/rust/bytes/tests/test_bytes_vec_alloc.rs143
-rw-r--r--third_party/rust/bytes/tests/test_chain.rs177
-rw-r--r--third_party/rust/bytes/tests/test_debug.rs35
-rw-r--r--third_party/rust/bytes/tests/test_iter.rs21
-rw-r--r--third_party/rust/bytes/tests/test_reader.rs29
-rw-r--r--third_party/rust/bytes/tests/test_serde.rs20
-rw-r--r--third_party/rust/bytes/tests/test_take.rs32
11 files changed, 2062 insertions, 0 deletions
diff --git a/third_party/rust/bytes/tests/test_buf.rs b/third_party/rust/bytes/tests/test_buf.rs
new file mode 100644
index 0000000000..fbad003a40
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_buf.rs
@@ -0,0 +1,120 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::Buf;
+#[cfg(feature = "std")]
+use std::io::IoSlice;
+
+#[test]
+fn test_fresh_cursor_vec() {
+ let mut buf = &b"hello"[..];
+
+ assert_eq!(buf.remaining(), 5);
+ assert_eq!(buf.chunk(), b"hello");
+
+ buf.advance(2);
+
+ assert_eq!(buf.remaining(), 3);
+ assert_eq!(buf.chunk(), b"llo");
+
+ buf.advance(3);
+
+ assert_eq!(buf.remaining(), 0);
+ assert_eq!(buf.chunk(), b"");
+}
+
+#[test]
+fn test_get_u8() {
+ let mut buf = &b"\x21zomg"[..];
+ assert_eq!(0x21, buf.get_u8());
+}
+
+#[test]
+fn test_get_u16() {
+ let mut buf = &b"\x21\x54zomg"[..];
+ assert_eq!(0x2154, buf.get_u16());
+ let mut buf = &b"\x21\x54zomg"[..];
+ assert_eq!(0x5421, buf.get_u16_le());
+}
+
+#[test]
+#[should_panic]
+fn test_get_u16_buffer_underflow() {
+ let mut buf = &b"\x21"[..];
+ buf.get_u16();
+}
+
+#[cfg(feature = "std")]
+#[test]
+fn test_bufs_vec() {
+ let buf = &b"hello world"[..];
+
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+
+ let mut dst = [IoSlice::new(b1), IoSlice::new(b2)];
+
+ assert_eq!(1, buf.chunks_vectored(&mut dst[..]));
+}
+
+#[test]
+fn test_vec_deque() {
+ use std::collections::VecDeque;
+
+ let mut buffer: VecDeque<u8> = VecDeque::new();
+ buffer.extend(b"hello world");
+ assert_eq!(11, buffer.remaining());
+ assert_eq!(b"hello world", buffer.chunk());
+ buffer.advance(6);
+ assert_eq!(b"world", buffer.chunk());
+ buffer.extend(b" piece");
+ let mut out = [0; 11];
+ buffer.copy_to_slice(&mut out);
+ assert_eq!(b"world piece", &out[..]);
+}
+
+#[test]
+fn test_deref_buf_forwards() {
+ struct Special;
+
+ impl Buf for Special {
+ fn remaining(&self) -> usize {
+ unreachable!("remaining");
+ }
+
+ fn chunk(&self) -> &[u8] {
+ unreachable!("chunk");
+ }
+
+ fn advance(&mut self, _: usize) {
+ unreachable!("advance");
+ }
+
+ fn get_u8(&mut self) -> u8 {
+ // specialized!
+ b'x'
+ }
+ }
+
+ // these should all use the specialized method
+ assert_eq!(Special.get_u8(), b'x');
+ assert_eq!((&mut Special as &mut dyn Buf).get_u8(), b'x');
+ assert_eq!((Box::new(Special) as Box<dyn Buf>).get_u8(), b'x');
+ assert_eq!(Box::new(Special).get_u8(), b'x');
+}
+
+#[test]
+fn copy_to_bytes_less() {
+ let mut buf = &b"hello world"[..];
+
+ let bytes = buf.copy_to_bytes(5);
+ assert_eq!(bytes, &b"hello"[..]);
+ assert_eq!(buf, &b" world"[..])
+}
+
+#[test]
+#[should_panic]
+fn copy_to_bytes_overflow() {
+ let mut buf = &b"hello world"[..];
+
+ let _bytes = buf.copy_to_bytes(12);
+}
diff --git a/third_party/rust/bytes/tests/test_buf_mut.rs b/third_party/rust/bytes/tests/test_buf_mut.rs
new file mode 100644
index 0000000000..53f4e8611c
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_buf_mut.rs
@@ -0,0 +1,178 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::buf::UninitSlice;
+use bytes::{BufMut, BytesMut};
+use core::fmt::Write;
+use core::usize;
+
+#[test]
+fn test_vec_as_mut_buf() {
+ let mut buf = Vec::with_capacity(64);
+
+ assert_eq!(buf.remaining_mut(), isize::MAX as usize);
+
+ assert!(buf.chunk_mut().len() >= 64);
+
+ buf.put(&b"zomg"[..]);
+
+ assert_eq!(&buf, b"zomg");
+
+ assert_eq!(buf.remaining_mut(), isize::MAX as usize - 4);
+ assert_eq!(buf.capacity(), 64);
+
+ for _ in 0..16 {
+ buf.put(&b"zomg"[..]);
+ }
+
+ assert_eq!(buf.len(), 68);
+}
+
+#[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);
+ assert_eq!(b"\x21", &buf[..]);
+}
+
+#[test]
+fn test_put_u16() {
+ let mut buf = Vec::with_capacity(8);
+ buf.put_u16(8532);
+ assert_eq!(b"\x21\x54", &buf[..]);
+
+ buf.clear();
+ buf.put_u16_le(8532);
+ assert_eq!(b"\x54\x21", &buf[..]);
+}
+
+#[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
+ let mut buf = Vec::with_capacity(8);
+ unsafe {
+ buf.advance_mut(12);
+ }
+}
+
+#[test]
+fn test_clone() {
+ let mut buf = BytesMut::with_capacity(100);
+ buf.write_str("this is a test").unwrap();
+ let buf2 = buf.clone();
+
+ buf.write_str(" of our emergency broadcast system").unwrap();
+ assert!(buf != buf2);
+}
+
+#[test]
+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]
+fn test_deref_bufmut_forwards() {
+ struct Special;
+
+ unsafe impl BufMut for Special {
+ fn remaining_mut(&self) -> usize {
+ unreachable!("remaining_mut");
+ }
+
+ fn chunk_mut(&mut self) -> &mut UninitSlice {
+ unreachable!("chunk_mut");
+ }
+
+ unsafe fn advance_mut(&mut self, _: usize) {
+ unreachable!("advance");
+ }
+
+ fn put_u8(&mut self, _: u8) {
+ // specialized!
+ }
+ }
+
+ // these should all use the specialized method
+ Special.put_u8(b'x');
+ (&mut Special as &mut dyn BufMut).put_u8(b'x');
+ (Box::new(Special) as Box<dyn BufMut>).put_u8(b'x');
+ Box::new(Special).put_u8(b'x');
+}
+
+#[test]
+#[should_panic]
+fn write_byte_panics_if_out_of_bounds() {
+ let mut data = [b'b', b'a', b'r'];
+
+ let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
+ slice.write_byte(4, b'f');
+}
+
+#[test]
+#[should_panic]
+fn copy_from_slice_panics_if_different_length_1() {
+ let mut data = [b'b', b'a', b'r'];
+
+ let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
+ slice.copy_from_slice(b"a");
+}
+
+#[test]
+#[should_panic]
+fn copy_from_slice_panics_if_different_length_2() {
+ let mut data = [b'b', b'a', b'r'];
+
+ let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
+ slice.copy_from_slice(b"abcd");
+}
diff --git a/third_party/rust/bytes/tests/test_bytes.rs b/third_party/rust/bytes/tests/test_bytes.rs
new file mode 100644
index 0000000000..5ec60a5b0e
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_bytes.rs
@@ -0,0 +1,1210 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::{Buf, BufMut, Bytes, BytesMut};
+
+use std::usize;
+
+const LONG: &[u8] = b"mary had a little lamb, little lamb, little lamb";
+const SHORT: &[u8] = b"hello world";
+
+fn is_sync<T: Sync>() {}
+fn is_send<T: Send>() {}
+
+#[test]
+fn test_bounds() {
+ is_sync::<Bytes>();
+ is_sync::<BytesMut>();
+ is_send::<Bytes>();
+ is_send::<BytesMut>();
+}
+
+#[test]
+fn test_layout() {
+ use std::mem;
+
+ assert_eq!(
+ mem::size_of::<Bytes>(),
+ mem::size_of::<usize>() * 4,
+ "Bytes size should be 4 words",
+ );
+ assert_eq!(
+ mem::size_of::<BytesMut>(),
+ mem::size_of::<usize>() * 4,
+ "BytesMut should be 4 words",
+ );
+
+ assert_eq!(
+ mem::size_of::<Bytes>(),
+ mem::size_of::<Option<Bytes>>(),
+ "Bytes should be same size as Option<Bytes>",
+ );
+
+ assert_eq!(
+ mem::size_of::<BytesMut>(),
+ mem::size_of::<Option<BytesMut>>(),
+ "BytesMut should be same size as Option<BytesMut>",
+ );
+}
+
+#[test]
+fn from_slice() {
+ let a = Bytes::from(&b"abcdefgh"[..]);
+ assert_eq!(a, b"abcdefgh"[..]);
+ assert_eq!(a, &b"abcdefgh"[..]);
+ assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
+ assert_eq!(b"abcdefgh"[..], a);
+ assert_eq!(&b"abcdefgh"[..], a);
+ assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
+
+ let a = BytesMut::from(&b"abcdefgh"[..]);
+ assert_eq!(a, b"abcdefgh"[..]);
+ assert_eq!(a, &b"abcdefgh"[..]);
+ assert_eq!(a, Vec::from(&b"abcdefgh"[..]));
+ assert_eq!(b"abcdefgh"[..], a);
+ assert_eq!(&b"abcdefgh"[..], a);
+ assert_eq!(Vec::from(&b"abcdefgh"[..]), a);
+}
+
+#[test]
+fn fmt() {
+ let a = format!("{:?}", Bytes::from(&b"abcdefg"[..]));
+ let b = "b\"abcdefg\"";
+
+ assert_eq!(a, b);
+
+ let a = format!("{:?}", BytesMut::from(&b"abcdefg"[..]));
+ assert_eq!(a, b);
+}
+
+#[test]
+fn fmt_write() {
+ use std::fmt::Write;
+ use std::iter::FromIterator;
+ let s = String::from_iter((0..10).map(|_| "abcdefg"));
+
+ let mut a = BytesMut::with_capacity(64);
+ write!(a, "{}", &s[..64]).unwrap();
+ assert_eq!(a, s[..64].as_bytes());
+
+ let mut b = BytesMut::with_capacity(64);
+ write!(b, "{}", &s[..32]).unwrap();
+ write!(b, "{}", &s[32..64]).unwrap();
+ assert_eq!(b, s[..64].as_bytes());
+
+ let mut c = BytesMut::with_capacity(64);
+ write!(c, "{}", s).unwrap();
+ assert_eq!(c, s[..].as_bytes());
+}
+
+#[test]
+fn len() {
+ let a = Bytes::from(&b"abcdefg"[..]);
+ assert_eq!(a.len(), 7);
+
+ let a = BytesMut::from(&b"abcdefg"[..]);
+ assert_eq!(a.len(), 7);
+
+ let a = Bytes::from(&b""[..]);
+ assert!(a.is_empty());
+
+ let a = BytesMut::from(&b""[..]);
+ assert!(a.is_empty());
+}
+
+#[test]
+fn index() {
+ let a = Bytes::from(&b"hello world"[..]);
+ assert_eq!(a[0..5], *b"hello");
+}
+
+#[test]
+fn slice() {
+ let a = Bytes::from(&b"hello world"[..]);
+
+ let b = a.slice(3..5);
+ assert_eq!(b, b"lo"[..]);
+
+ let b = a.slice(0..0);
+ assert_eq!(b, b""[..]);
+
+ let b = a.slice(3..3);
+ assert_eq!(b, b""[..]);
+
+ let b = a.slice(a.len()..a.len());
+ assert_eq!(b, b""[..]);
+
+ let b = a.slice(..5);
+ assert_eq!(b, b"hello"[..]);
+
+ let b = a.slice(3..);
+ assert_eq!(b, b"lo world"[..]);
+}
+
+#[test]
+#[should_panic]
+fn slice_oob_1() {
+ let a = Bytes::from(&b"hello world"[..]);
+ a.slice(5..44);
+}
+
+#[test]
+#[should_panic]
+fn slice_oob_2() {
+ let a = Bytes::from(&b"hello world"[..]);
+ a.slice(44..49);
+}
+
+#[test]
+fn split_off() {
+ let mut hello = Bytes::from(&b"helloworld"[..]);
+ let world = hello.split_off(5);
+
+ assert_eq!(hello, &b"hello"[..]);
+ assert_eq!(world, &b"world"[..]);
+
+ let mut hello = BytesMut::from(&b"helloworld"[..]);
+ let world = hello.split_off(5);
+
+ assert_eq!(hello, &b"hello"[..]);
+ assert_eq!(world, &b"world"[..]);
+}
+
+#[test]
+#[should_panic]
+fn split_off_oob() {
+ let mut hello = Bytes::from(&b"helloworld"[..]);
+ let _ = hello.split_off(44);
+}
+
+#[test]
+fn split_off_uninitialized() {
+ let mut bytes = BytesMut::with_capacity(1024);
+ let other = bytes.split_off(128);
+
+ assert_eq!(bytes.len(), 0);
+ assert_eq!(bytes.capacity(), 128);
+
+ assert_eq!(other.len(), 0);
+ assert_eq!(other.capacity(), 896);
+}
+
+#[test]
+fn split_off_to_loop() {
+ let s = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ for i in 0..(s.len() + 1) {
+ {
+ let mut bytes = Bytes::from(&s[..]);
+ let off = bytes.split_off(i);
+ assert_eq!(i, bytes.len());
+ let mut sum = Vec::new();
+ sum.extend(bytes.iter());
+ sum.extend(off.iter());
+ assert_eq!(&s[..], &sum[..]);
+ }
+ {
+ let mut bytes = BytesMut::from(&s[..]);
+ let off = bytes.split_off(i);
+ assert_eq!(i, bytes.len());
+ let mut sum = Vec::new();
+ sum.extend(&bytes);
+ sum.extend(&off);
+ assert_eq!(&s[..], &sum[..]);
+ }
+ {
+ let mut bytes = Bytes::from(&s[..]);
+ let off = bytes.split_to(i);
+ assert_eq!(i, off.len());
+ let mut sum = Vec::new();
+ sum.extend(off.iter());
+ sum.extend(bytes.iter());
+ assert_eq!(&s[..], &sum[..]);
+ }
+ {
+ let mut bytes = BytesMut::from(&s[..]);
+ let off = bytes.split_to(i);
+ assert_eq!(i, off.len());
+ let mut sum = Vec::new();
+ sum.extend(&off);
+ sum.extend(&bytes);
+ assert_eq!(&s[..], &sum[..]);
+ }
+ }
+}
+
+#[test]
+fn split_to_1() {
+ // Static
+ let mut a = Bytes::from_static(SHORT);
+ let b = a.split_to(4);
+
+ assert_eq!(SHORT[4..], a);
+ assert_eq!(SHORT[..4], b);
+
+ // Allocated
+ let mut a = Bytes::copy_from_slice(LONG);
+ let b = a.split_to(4);
+
+ assert_eq!(LONG[4..], a);
+ assert_eq!(LONG[..4], b);
+
+ let mut a = Bytes::copy_from_slice(LONG);
+ let b = a.split_to(30);
+
+ assert_eq!(LONG[30..], a);
+ assert_eq!(LONG[..30], b);
+}
+
+#[test]
+fn split_to_2() {
+ let mut a = Bytes::from(LONG);
+ assert_eq!(LONG, a);
+
+ let b = a.split_to(1);
+
+ assert_eq!(LONG[1..], a);
+ drop(b);
+}
+
+#[test]
+#[should_panic]
+fn split_to_oob() {
+ let mut hello = Bytes::from(&b"helloworld"[..]);
+ let _ = hello.split_to(33);
+}
+
+#[test]
+#[should_panic]
+fn split_to_oob_mut() {
+ let mut hello = BytesMut::from(&b"helloworld"[..]);
+ let _ = hello.split_to(33);
+}
+
+#[test]
+#[should_panic]
+fn split_to_uninitialized() {
+ let mut bytes = BytesMut::with_capacity(1024);
+ let _other = bytes.split_to(128);
+}
+
+#[test]
+fn split_off_to_at_gt_len() {
+ fn make_bytes() -> Bytes {
+ let mut bytes = BytesMut::with_capacity(100);
+ bytes.put_slice(&[10, 20, 30, 40]);
+ bytes.freeze()
+ }
+
+ use std::panic;
+
+ let _ = make_bytes().split_to(4);
+ let _ = make_bytes().split_off(4);
+
+ assert!(panic::catch_unwind(move || {
+ let _ = make_bytes().split_to(5);
+ })
+ .is_err());
+
+ assert!(panic::catch_unwind(move || {
+ let _ = make_bytes().split_off(5);
+ })
+ .is_err());
+}
+
+#[test]
+fn truncate() {
+ let s = &b"helloworld"[..];
+ let mut hello = Bytes::from(s);
+ hello.truncate(15);
+ assert_eq!(hello, s);
+ hello.truncate(10);
+ assert_eq!(hello, s);
+ hello.truncate(5);
+ assert_eq!(hello, "hello");
+}
+
+#[test]
+fn freeze_clone_shared() {
+ let s = &b"abcdefgh"[..];
+ let b = BytesMut::from(s).split().freeze();
+ assert_eq!(b, s);
+ let c = b.clone();
+ assert_eq!(c, s);
+}
+
+#[test]
+fn freeze_clone_unique() {
+ let s = &b"abcdefgh"[..];
+ let b = BytesMut::from(s).freeze();
+ assert_eq!(b, s);
+ let c = b.clone();
+ assert_eq!(c, s);
+}
+
+#[test]
+fn freeze_after_advance() {
+ let s = &b"abcdefgh"[..];
+ let mut b = BytesMut::from(s);
+ b.advance(1);
+ assert_eq!(b, s[1..]);
+ let b = b.freeze();
+ // Verify fix for #352. Previously, freeze would ignore the start offset
+ // for BytesMuts in Vec mode.
+ assert_eq!(b, s[1..]);
+}
+
+#[test]
+fn freeze_after_advance_arc() {
+ let s = &b"abcdefgh"[..];
+ let mut b = BytesMut::from(s);
+ // Make b Arc
+ let _ = b.split_to(0);
+ b.advance(1);
+ assert_eq!(b, s[1..]);
+ let b = b.freeze();
+ assert_eq!(b, s[1..]);
+}
+
+#[test]
+fn freeze_after_split_to() {
+ let s = &b"abcdefgh"[..];
+ let mut b = BytesMut::from(s);
+ let _ = b.split_to(1);
+ assert_eq!(b, s[1..]);
+ let b = b.freeze();
+ assert_eq!(b, s[1..]);
+}
+
+#[test]
+fn freeze_after_truncate() {
+ let s = &b"abcdefgh"[..];
+ let mut b = BytesMut::from(s);
+ b.truncate(7);
+ assert_eq!(b, s[..7]);
+ let b = b.freeze();
+ assert_eq!(b, s[..7]);
+}
+
+#[test]
+fn freeze_after_truncate_arc() {
+ let s = &b"abcdefgh"[..];
+ let mut b = BytesMut::from(s);
+ // Make b Arc
+ let _ = b.split_to(0);
+ b.truncate(7);
+ assert_eq!(b, s[..7]);
+ let b = b.freeze();
+ assert_eq!(b, s[..7]);
+}
+
+#[test]
+fn freeze_after_split_off() {
+ let s = &b"abcdefgh"[..];
+ let mut b = BytesMut::from(s);
+ let _ = b.split_off(7);
+ assert_eq!(b, s[..7]);
+ let b = b.freeze();
+ assert_eq!(b, s[..7]);
+}
+
+#[test]
+fn fns_defined_for_bytes_mut() {
+ let mut bytes = BytesMut::from(&b"hello world"[..]);
+
+ let _ = bytes.as_ptr();
+ let _ = bytes.as_mut_ptr();
+
+ // Iterator
+ let v: Vec<u8> = bytes.as_ref().iter().cloned().collect();
+ assert_eq!(&v[..], bytes);
+}
+
+#[test]
+fn reserve_convert() {
+ // Vec -> Vec
+ let mut bytes = BytesMut::from(LONG);
+ bytes.reserve(64);
+ assert_eq!(bytes.capacity(), LONG.len() + 64);
+
+ // Arc -> Vec
+ let mut bytes = BytesMut::from(LONG);
+ let a = bytes.split_to(30);
+
+ bytes.reserve(128);
+ assert!(bytes.capacity() >= bytes.len() + 128);
+
+ drop(a);
+}
+
+#[test]
+fn reserve_growth() {
+ let mut bytes = BytesMut::with_capacity(64);
+ bytes.put("hello world".as_bytes());
+ let _ = bytes.split();
+
+ bytes.reserve(65);
+ assert_eq!(bytes.capacity(), 117);
+}
+
+#[test]
+fn reserve_allocates_at_least_original_capacity() {
+ let mut bytes = BytesMut::with_capacity(1024);
+
+ for i in 0..1020 {
+ bytes.put_u8(i as u8);
+ }
+
+ let _other = bytes.split();
+
+ bytes.reserve(16);
+ assert_eq!(bytes.capacity(), 1024);
+}
+
+#[test]
+#[cfg_attr(miri, ignore)] // Miri is too slow
+fn reserve_max_original_capacity_value() {
+ const SIZE: usize = 128 * 1024;
+
+ let mut bytes = BytesMut::with_capacity(SIZE);
+
+ for _ in 0..SIZE {
+ bytes.put_u8(0u8);
+ }
+
+ let _other = bytes.split();
+
+ bytes.reserve(16);
+ assert_eq!(bytes.capacity(), 64 * 1024);
+}
+
+#[test]
+fn reserve_vec_recycling() {
+ let mut bytes = BytesMut::with_capacity(16);
+ assert_eq!(bytes.capacity(), 16);
+ let addr = bytes.as_ptr() as usize;
+ bytes.put("0123456789012345".as_bytes());
+ assert_eq!(bytes.as_ptr() as usize, addr);
+ bytes.advance(10);
+ assert_eq!(bytes.capacity(), 6);
+ bytes.reserve(8);
+ assert_eq!(bytes.capacity(), 16);
+ assert_eq!(bytes.as_ptr() as usize, addr);
+}
+
+#[test]
+fn reserve_in_arc_unique_does_not_overallocate() {
+ let mut bytes = BytesMut::with_capacity(1000);
+ let _ = bytes.split();
+
+ // now bytes is Arc and refcount == 1
+
+ assert_eq!(1000, bytes.capacity());
+ bytes.reserve(2001);
+ assert_eq!(2001, bytes.capacity());
+}
+
+#[test]
+fn reserve_in_arc_unique_doubles() {
+ let mut bytes = BytesMut::with_capacity(1000);
+ let _ = bytes.split();
+
+ // now bytes is Arc and refcount == 1
+
+ assert_eq!(1000, bytes.capacity());
+ bytes.reserve(1001);
+ assert_eq!(2000, bytes.capacity());
+}
+
+#[test]
+fn reserve_in_arc_unique_does_not_overallocate_after_split() {
+ let mut bytes = BytesMut::from(LONG);
+ let orig_capacity = bytes.capacity();
+ drop(bytes.split_off(LONG.len() / 2));
+
+ // now bytes is Arc and refcount == 1
+
+ let new_capacity = bytes.capacity();
+ bytes.reserve(orig_capacity - new_capacity);
+ assert_eq!(bytes.capacity(), orig_capacity);
+}
+
+#[test]
+fn reserve_in_arc_unique_does_not_overallocate_after_multiple_splits() {
+ let mut bytes = BytesMut::from(LONG);
+ let orig_capacity = bytes.capacity();
+ for _ in 0..10 {
+ drop(bytes.split_off(LONG.len() / 2));
+
+ // now bytes is Arc and refcount == 1
+
+ let new_capacity = bytes.capacity();
+ bytes.reserve(orig_capacity - new_capacity);
+ }
+ assert_eq!(bytes.capacity(), orig_capacity);
+}
+
+#[test]
+fn reserve_in_arc_nonunique_does_not_overallocate() {
+ let mut bytes = BytesMut::with_capacity(1000);
+ let _copy = bytes.split();
+
+ // now bytes is Arc and refcount == 2
+
+ assert_eq!(1000, bytes.capacity());
+ bytes.reserve(2001);
+ assert_eq!(2001, bytes.capacity());
+}
+
+/// This function tests `BytesMut::reserve_inner`, where `BytesMut` holds
+/// a unique reference to the shared vector and decide to reuse it
+/// by reallocating the `Vec`.
+#[test]
+fn reserve_shared_reuse() {
+ let mut bytes = BytesMut::with_capacity(1000);
+ bytes.put_slice(b"Hello, World!");
+ drop(bytes.split());
+
+ bytes.put_slice(b"!123ex123,sadchELLO,_wORLD!");
+ // Use split_off so that v.capacity() - self.cap != off
+ drop(bytes.split_off(9));
+ assert_eq!(&*bytes, b"!123ex123");
+
+ bytes.reserve(2000);
+ assert_eq!(&*bytes, b"!123ex123");
+ assert_eq!(bytes.capacity(), 2009);
+}
+
+#[test]
+fn extend_mut() {
+ let mut bytes = BytesMut::with_capacity(0);
+ bytes.extend(LONG);
+ assert_eq!(*bytes, LONG[..]);
+}
+
+#[test]
+fn extend_from_slice_mut() {
+ for &i in &[3, 34] {
+ let mut bytes = BytesMut::new();
+ bytes.extend_from_slice(&LONG[..i]);
+ bytes.extend_from_slice(&LONG[i..]);
+ assert_eq!(LONG[..], *bytes);
+ }
+}
+
+#[test]
+fn extend_mut_from_bytes() {
+ let mut bytes = BytesMut::with_capacity(0);
+ bytes.extend([Bytes::from(LONG)]);
+ assert_eq!(*bytes, LONG[..]);
+}
+
+#[test]
+fn extend_mut_without_size_hint() {
+ let mut bytes = BytesMut::with_capacity(0);
+ let mut long_iter = LONG.iter();
+
+ // Use iter::from_fn since it doesn't know a size_hint
+ bytes.extend(std::iter::from_fn(|| long_iter.next()));
+ assert_eq!(*bytes, LONG[..]);
+}
+
+#[test]
+fn from_static() {
+ let mut a = Bytes::from_static(b"ab");
+ let b = a.split_off(1);
+
+ assert_eq!(a, b"a"[..]);
+ assert_eq!(b, b"b"[..]);
+}
+
+#[test]
+fn advance_static() {
+ let mut a = Bytes::from_static(b"hello world");
+ a.advance(6);
+ assert_eq!(a, &b"world"[..]);
+}
+
+#[test]
+fn advance_vec() {
+ let mut a = Bytes::from(b"hello world boooo yah world zomg wat wat".to_vec());
+ a.advance(16);
+ assert_eq!(a, b"o yah world zomg wat wat"[..]);
+
+ a.advance(4);
+ assert_eq!(a, b"h world zomg wat wat"[..]);
+
+ a.advance(6);
+ assert_eq!(a, b"d zomg wat wat"[..]);
+}
+
+#[test]
+fn advance_bytes_mut() {
+ let mut a = BytesMut::from("hello world boooo yah world zomg wat wat");
+ a.advance(16);
+ assert_eq!(a, b"o yah world zomg wat wat"[..]);
+
+ a.advance(4);
+ assert_eq!(a, b"h world zomg wat wat"[..]);
+
+ // Reserve some space.
+ a.reserve(1024);
+ assert_eq!(a, b"h world zomg wat wat"[..]);
+
+ a.advance(6);
+ assert_eq!(a, b"d zomg wat wat"[..]);
+}
+
+#[test]
+#[should_panic]
+fn advance_past_len() {
+ let mut a = BytesMut::from("hello world");
+ a.advance(20);
+}
+
+#[test]
+// Only run these tests on little endian systems. CI uses qemu for testing
+// big endian... and qemu doesn't really support threading all that well.
+#[cfg(any(miri, target_endian = "little"))]
+fn stress() {
+ // Tests promoting a buffer from a vec -> shared in a concurrent situation
+ use std::sync::{Arc, Barrier};
+ use std::thread;
+
+ const THREADS: usize = 8;
+ const ITERS: usize = if cfg!(miri) { 100 } else { 1_000 };
+
+ for i in 0..ITERS {
+ let data = [i as u8; 256];
+ let buf = Arc::new(Bytes::copy_from_slice(&data[..]));
+
+ let barrier = Arc::new(Barrier::new(THREADS));
+ let mut joins = Vec::with_capacity(THREADS);
+
+ for _ in 0..THREADS {
+ let c = barrier.clone();
+ let buf = buf.clone();
+
+ joins.push(thread::spawn(move || {
+ c.wait();
+ let buf: Bytes = (*buf).clone();
+ drop(buf);
+ }));
+ }
+
+ for th in joins {
+ th.join().unwrap();
+ }
+
+ assert_eq!(*buf, data[..]);
+ }
+}
+
+#[test]
+fn partial_eq_bytesmut() {
+ let bytes = Bytes::from(&b"The quick red fox"[..]);
+ let bytesmut = BytesMut::from(&b"The quick red fox"[..]);
+ assert!(bytes == bytesmut);
+ assert!(bytesmut == bytes);
+ let bytes2 = Bytes::from(&b"Jumped over the lazy brown dog"[..]);
+ assert!(bytes2 != bytesmut);
+ assert!(bytesmut != bytes2);
+}
+
+/*
+#[test]
+fn bytes_unsplit_basic() {
+ let buf = Bytes::from(&b"aaabbbcccddd"[..]);
+
+ let splitted = buf.split_off(6);
+ assert_eq!(b"aaabbb", &buf[..]);
+ assert_eq!(b"cccddd", &splitted[..]);
+
+ buf.unsplit(splitted);
+ assert_eq!(b"aaabbbcccddd", &buf[..]);
+}
+
+#[test]
+fn bytes_unsplit_empty_other() {
+ let buf = Bytes::from(&b"aaabbbcccddd"[..]);
+
+ // empty other
+ let other = Bytes::new();
+
+ buf.unsplit(other);
+ assert_eq!(b"aaabbbcccddd", &buf[..]);
+}
+
+#[test]
+fn bytes_unsplit_empty_self() {
+ // empty self
+ let mut buf = Bytes::new();
+
+ let mut other = Bytes::with_capacity(64);
+ other.extend_from_slice(b"aaabbbcccddd");
+
+ buf.unsplit(other);
+ assert_eq!(b"aaabbbcccddd", &buf[..]);
+}
+
+#[test]
+fn bytes_unsplit_arc_different() {
+ let mut buf = Bytes::with_capacity(64);
+ buf.extend_from_slice(b"aaaabbbbeeee");
+
+ buf.split_off(8); //arc
+
+ let mut buf2 = Bytes::with_capacity(64);
+ buf2.extend_from_slice(b"ccccddddeeee");
+
+ buf2.split_off(8); //arc
+
+ buf.unsplit(buf2);
+ assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
+}
+
+#[test]
+fn bytes_unsplit_arc_non_contiguous() {
+ let mut buf = Bytes::with_capacity(64);
+ buf.extend_from_slice(b"aaaabbbbeeeeccccdddd");
+
+ let mut buf2 = buf.split_off(8); //arc
+
+ let buf3 = buf2.split_off(4); //arc
+
+ buf.unsplit(buf3);
+ assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
+}
+
+#[test]
+fn bytes_unsplit_two_split_offs() {
+ let mut buf = Bytes::with_capacity(64);
+ buf.extend_from_slice(b"aaaabbbbccccdddd");
+
+ let mut buf2 = buf.split_off(8); //arc
+ let buf3 = buf2.split_off(4); //arc
+
+ buf2.unsplit(buf3);
+ buf.unsplit(buf2);
+ assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
+}
+
+#[test]
+fn bytes_unsplit_overlapping_references() {
+ let mut buf = Bytes::with_capacity(64);
+ buf.extend_from_slice(b"abcdefghijklmnopqrstuvwxyz");
+ let mut buf0010 = buf.slice(0..10);
+ let buf1020 = buf.slice(10..20);
+ let buf0515 = buf.slice(5..15);
+ buf0010.unsplit(buf1020);
+ assert_eq!(b"abcdefghijklmnopqrst", &buf0010[..]);
+ assert_eq!(b"fghijklmno", &buf0515[..]);
+}
+*/
+
+#[test]
+fn bytes_mut_unsplit_basic() {
+ let mut buf = BytesMut::with_capacity(64);
+ buf.extend_from_slice(b"aaabbbcccddd");
+
+ let splitted = buf.split_off(6);
+ assert_eq!(b"aaabbb", &buf[..]);
+ assert_eq!(b"cccddd", &splitted[..]);
+
+ buf.unsplit(splitted);
+ assert_eq!(b"aaabbbcccddd", &buf[..]);
+}
+
+#[test]
+fn bytes_mut_unsplit_empty_other() {
+ let mut buf = BytesMut::with_capacity(64);
+ buf.extend_from_slice(b"aaabbbcccddd");
+
+ // empty other
+ let other = BytesMut::new();
+
+ buf.unsplit(other);
+ assert_eq!(b"aaabbbcccddd", &buf[..]);
+}
+
+#[test]
+fn bytes_mut_unsplit_empty_self() {
+ // empty self
+ let mut buf = BytesMut::new();
+
+ let mut other = BytesMut::with_capacity(64);
+ other.extend_from_slice(b"aaabbbcccddd");
+
+ buf.unsplit(other);
+ assert_eq!(b"aaabbbcccddd", &buf[..]);
+}
+
+#[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");
+
+ let _ = buf.split_off(8); //arc
+
+ let mut buf2 = BytesMut::with_capacity(64);
+ buf2.extend_from_slice(b"ccccddddeeee");
+
+ let _ = buf2.split_off(8); //arc
+
+ buf.unsplit(buf2);
+ assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
+}
+
+#[test]
+fn bytes_mut_unsplit_arc_non_contiguous() {
+ let mut buf = BytesMut::with_capacity(64);
+ buf.extend_from_slice(b"aaaabbbbeeeeccccdddd");
+
+ let mut buf2 = buf.split_off(8); //arc
+
+ let buf3 = buf2.split_off(4); //arc
+
+ buf.unsplit(buf3);
+ assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
+}
+
+#[test]
+fn bytes_mut_unsplit_two_split_offs() {
+ let mut buf = BytesMut::with_capacity(64);
+ buf.extend_from_slice(b"aaaabbbbccccdddd");
+
+ let mut buf2 = buf.split_off(8); //arc
+ let buf3 = buf2.split_off(4); //arc
+
+ buf2.unsplit(buf3);
+ buf.unsplit(buf2);
+ assert_eq!(b"aaaabbbbccccdddd", &buf[..]);
+}
+
+#[test]
+fn from_iter_no_size_hint() {
+ use std::iter;
+
+ let mut expect = vec![];
+
+ let actual: Bytes = iter::repeat(b'x')
+ .scan(100, |cnt, item| {
+ if *cnt >= 1 {
+ *cnt -= 1;
+ expect.push(item);
+ Some(item)
+ } else {
+ None
+ }
+ })
+ .collect();
+
+ assert_eq!(&actual[..], &expect[..]);
+}
+
+fn test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8]) {
+ let slice = &(bytes.as_ref()[start..end]);
+ let sub = bytes.slice_ref(slice);
+ assert_eq!(&sub[..], expected);
+}
+
+#[test]
+fn slice_ref_works() {
+ let bytes = Bytes::from(&b"012345678"[..]);
+
+ test_slice_ref(&bytes, 0, 0, b"");
+ test_slice_ref(&bytes, 0, 3, b"012");
+ test_slice_ref(&bytes, 2, 6, b"2345");
+ test_slice_ref(&bytes, 7, 9, b"78");
+ test_slice_ref(&bytes, 9, 9, b"");
+}
+
+#[test]
+fn slice_ref_empty() {
+ let bytes = Bytes::from(&b""[..]);
+ let slice = &(bytes.as_ref()[0..0]);
+
+ let sub = bytes.slice_ref(slice);
+ assert_eq!(&sub[..], b"");
+}
+
+#[test]
+fn slice_ref_empty_subslice() {
+ let bytes = Bytes::from(&b"abcde"[..]);
+ let subbytes = bytes.slice(0..0);
+ let slice = &subbytes[..];
+ // The `slice` object is derived from the original `bytes` object
+ // so `slice_ref` should work.
+ assert_eq!(Bytes::new(), bytes.slice_ref(slice));
+}
+
+#[test]
+#[should_panic]
+fn slice_ref_catches_not_a_subset() {
+ let bytes = Bytes::from(&b"012345678"[..]);
+ let slice = &b"012345"[0..4];
+
+ bytes.slice_ref(slice);
+}
+
+#[test]
+fn slice_ref_not_an_empty_subset() {
+ let bytes = Bytes::from(&b"012345678"[..]);
+ let slice = &b""[0..0];
+
+ assert_eq!(Bytes::new(), bytes.slice_ref(slice));
+}
+
+#[test]
+fn empty_slice_ref_not_an_empty_subset() {
+ let bytes = Bytes::new();
+ let slice = &b"some other slice"[0..0];
+
+ assert_eq!(Bytes::new(), bytes.slice_ref(slice));
+}
+
+#[test]
+fn bytes_buf_mut_advance() {
+ let mut bytes = BytesMut::with_capacity(1024);
+
+ unsafe {
+ let ptr = bytes.chunk_mut().as_mut_ptr();
+ assert_eq!(1024, bytes.chunk_mut().len());
+
+ bytes.advance_mut(10);
+
+ let next = bytes.chunk_mut().as_mut_ptr();
+ assert_eq!(1024 - 10, bytes.chunk_mut().len());
+ assert_eq!(ptr.offset(10), next);
+
+ // advance to the end
+ bytes.advance_mut(1024 - 10);
+
+ // The buffer size is doubled
+ assert_eq!(1024, bytes.chunk_mut().len());
+ }
+}
+
+#[test]
+fn bytes_buf_mut_reuse_when_fully_consumed() {
+ use bytes::{Buf, BytesMut};
+ let mut buf = BytesMut::new();
+ buf.reserve(8192);
+ buf.extend_from_slice(&[0u8; 100][..]);
+
+ let p = &buf[0] as *const u8;
+ buf.advance(100);
+
+ buf.reserve(8192);
+ buf.extend_from_slice(b" ");
+
+ assert_eq!(&buf[0] as *const u8, p);
+}
+
+#[test]
+#[should_panic]
+fn bytes_reserve_overflow() {
+ let mut bytes = BytesMut::with_capacity(1024);
+ bytes.put_slice(b"hello world");
+
+ bytes.reserve(usize::MAX);
+}
+
+#[test]
+fn bytes_with_capacity_but_empty() {
+ // See https://github.com/tokio-rs/bytes/issues/340
+ 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());
+}
+
+#[test]
+fn bytes_into_vec() {
+ // Test kind == KIND_VEC
+ let content = b"helloworld";
+
+ let mut bytes = BytesMut::new();
+ bytes.put_slice(content);
+
+ let vec: Vec<u8> = bytes.into();
+ assert_eq!(&vec, content);
+
+ // Test kind == KIND_ARC, shared.is_unique() == True
+ let mut bytes = BytesMut::new();
+ bytes.put_slice(b"abcdewe23");
+ bytes.put_slice(content);
+
+ // Overwrite the bytes to make sure only one reference to the underlying
+ // Vec exists.
+ bytes = bytes.split_off(9);
+
+ let vec: Vec<u8> = bytes.into();
+ assert_eq!(&vec, content);
+
+ // Test kind == KIND_ARC, shared.is_unique() == False
+ let prefix = b"abcdewe23";
+
+ let mut bytes = BytesMut::new();
+ bytes.put_slice(prefix);
+ bytes.put_slice(content);
+
+ let vec: Vec<u8> = bytes.split_off(prefix.len()).into();
+ assert_eq!(&vec, content);
+
+ let vec: Vec<u8> = bytes.into();
+ assert_eq!(&vec, prefix);
+}
+
+#[test]
+fn test_bytes_into_vec() {
+ // Test STATIC_VTABLE.to_vec
+ let bs = b"1b23exfcz3r";
+ let vec: Vec<u8> = Bytes::from_static(bs).into();
+ assert_eq!(&*vec, bs);
+
+ // Test bytes_mut.SHARED_VTABLE.to_vec impl
+ eprintln!("1");
+ let mut bytes_mut: BytesMut = bs[..].into();
+
+ // Set kind to KIND_ARC so that after freeze, Bytes will use bytes_mut.SHARED_VTABLE
+ eprintln!("2");
+ drop(bytes_mut.split_off(bs.len()));
+
+ eprintln!("3");
+ let b1 = bytes_mut.freeze();
+ eprintln!("4");
+ let b2 = b1.clone();
+
+ eprintln!("{:#?}", (&*b1).as_ptr());
+
+ // shared.is_unique() = False
+ eprintln!("5");
+ assert_eq!(&*Vec::from(b2), bs);
+
+ // shared.is_unique() = True
+ eprintln!("6");
+ assert_eq!(&*Vec::from(b1), bs);
+
+ // Test bytes_mut.SHARED_VTABLE.to_vec impl where offset != 0
+ let mut bytes_mut1: BytesMut = bs[..].into();
+ let bytes_mut2 = bytes_mut1.split_off(9);
+
+ let b1 = bytes_mut1.freeze();
+ let b2 = bytes_mut2.freeze();
+
+ assert_eq!(Vec::from(b2), bs[9..]);
+ assert_eq!(Vec::from(b1), bs[..9]);
+}
+
+#[test]
+fn test_bytes_into_vec_promotable_even() {
+ let vec = vec![33u8; 1024];
+
+ // Test cases where kind == KIND_VEC
+ let b1 = Bytes::from(vec.clone());
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where kind == KIND_ARC, ref_cnt == 1
+ let b1 = Bytes::from(vec.clone());
+ drop(b1.clone());
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where kind == KIND_ARC, ref_cnt == 2
+ let b1 = Bytes::from(vec.clone());
+ let b2 = b1.clone();
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1
+ assert_eq!(Vec::from(b2), vec);
+
+ // Test cases where offset != 0
+ let mut b1 = Bytes::from(vec.clone());
+ let b2 = b1.split_off(20);
+
+ assert_eq!(Vec::from(b2), vec[20..]);
+ assert_eq!(Vec::from(b1), vec[..20]);
+}
+
+#[test]
+fn test_bytes_vec_conversion() {
+ let mut vec = Vec::with_capacity(10);
+ vec.extend(b"abcdefg");
+ let b = Bytes::from(vec);
+ let v = Vec::from(b);
+ assert_eq!(v.len(), 7);
+ assert_eq!(v.capacity(), 10);
+
+ let mut b = Bytes::from(v);
+ b.advance(1);
+ let v = Vec::from(b);
+ assert_eq!(v.len(), 6);
+ assert_eq!(v.capacity(), 10);
+ assert_eq!(v.as_slice(), b"bcdefg");
+}
+
+#[test]
+fn test_bytes_mut_conversion() {
+ let mut b1 = BytesMut::with_capacity(10);
+ b1.extend(b"abcdefg");
+ let b2 = Bytes::from(b1);
+ let v = Vec::from(b2);
+ assert_eq!(v.len(), 7);
+ assert_eq!(v.capacity(), 10);
+
+ let mut b = Bytes::from(v);
+ b.advance(1);
+ let v = Vec::from(b);
+ assert_eq!(v.len(), 6);
+ assert_eq!(v.capacity(), 10);
+ assert_eq!(v.as_slice(), b"bcdefg");
+}
+
+#[test]
+fn test_bytes_capacity_len() {
+ for cap in 0..100 {
+ for len in 0..=cap {
+ let mut v = Vec::with_capacity(cap);
+ v.resize(len, 0);
+ let _ = Bytes::from(v);
+ }
+ }
+}
diff --git a/third_party/rust/bytes/tests/test_bytes_odd_alloc.rs b/third_party/rust/bytes/tests/test_bytes_odd_alloc.rs
new file mode 100644
index 0000000000..27ed877362
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_bytes_odd_alloc.rs
@@ -0,0 +1,97 @@
+//! Test using `Bytes` with an allocator that hands out "odd" pointers for
+//! vectors (pointers where the LSB is set).
+
+#![cfg(not(miri))] // Miri does not support custom allocators (also, Miri is "odd" by default with 50% chance)
+
+use std::alloc::{GlobalAlloc, Layout, System};
+use std::ptr;
+
+use bytes::Bytes;
+
+#[global_allocator]
+static ODD: Odd = Odd;
+
+struct Odd;
+
+unsafe impl GlobalAlloc for Odd {
+ unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+ if layout.align() == 1 && layout.size() > 0 {
+ // Allocate slightly bigger so that we can offset the pointer by 1
+ let size = layout.size() + 1;
+ let new_layout = match Layout::from_size_align(size, 1) {
+ Ok(layout) => layout,
+ Err(_err) => return ptr::null_mut(),
+ };
+ let ptr = System.alloc(new_layout);
+ if !ptr.is_null() {
+ ptr.offset(1)
+ } else {
+ ptr
+ }
+ } else {
+ System.alloc(layout)
+ }
+ }
+
+ unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
+ if layout.align() == 1 && layout.size() > 0 {
+ let size = layout.size() + 1;
+ let new_layout = match Layout::from_size_align(size, 1) {
+ Ok(layout) => layout,
+ Err(_err) => std::process::abort(),
+ };
+ System.dealloc(ptr.offset(-1), new_layout);
+ } else {
+ System.dealloc(ptr, layout);
+ }
+ }
+}
+
+#[test]
+fn sanity_check_odd_allocator() {
+ let vec = vec![33u8; 1024];
+ let p = vec.as_ptr() as usize;
+ assert!(p & 0x1 == 0x1, "{:#b}", p);
+}
+
+#[test]
+fn test_bytes_from_vec_drop() {
+ let vec = vec![33u8; 1024];
+ let _b = Bytes::from(vec);
+}
+
+#[test]
+fn test_bytes_clone_drop() {
+ let vec = vec![33u8; 1024];
+ let b1 = Bytes::from(vec);
+ let _b2 = b1.clone();
+}
+
+#[test]
+fn test_bytes_into_vec() {
+ let vec = vec![33u8; 1024];
+
+ // Test cases where kind == KIND_VEC
+ let b1 = Bytes::from(vec.clone());
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where kind == KIND_ARC, ref_cnt == 1
+ let b1 = Bytes::from(vec.clone());
+ drop(b1.clone());
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where kind == KIND_ARC, ref_cnt == 2
+ let b1 = Bytes::from(vec.clone());
+ let b2 = b1.clone();
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1
+ assert_eq!(Vec::from(b2), vec);
+
+ // Test cases where offset != 0
+ let mut b1 = Bytes::from(vec.clone());
+ let b2 = b1.split_off(20);
+
+ assert_eq!(Vec::from(b2), vec[20..]);
+ assert_eq!(Vec::from(b1), vec[..20]);
+}
diff --git a/third_party/rust/bytes/tests/test_bytes_vec_alloc.rs b/third_party/rust/bytes/tests/test_bytes_vec_alloc.rs
new file mode 100644
index 0000000000..107e56e58c
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_bytes_vec_alloc.rs
@@ -0,0 +1,143 @@
+use std::alloc::{GlobalAlloc, Layout, System};
+use std::ptr::null_mut;
+use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
+
+use bytes::{Buf, Bytes};
+
+#[global_allocator]
+static LEDGER: Ledger = Ledger::new();
+
+const LEDGER_LENGTH: usize = 2048;
+
+struct Ledger {
+ alloc_table: [(AtomicPtr<u8>, AtomicUsize); LEDGER_LENGTH],
+}
+
+impl Ledger {
+ const fn new() -> Self {
+ const ELEM: (AtomicPtr<u8>, AtomicUsize) =
+ (AtomicPtr::new(null_mut()), AtomicUsize::new(0));
+ let alloc_table = [ELEM; LEDGER_LENGTH];
+
+ Self { alloc_table }
+ }
+
+ /// Iterate over our table until we find an open entry, then insert into said entry
+ fn insert(&self, ptr: *mut u8, size: usize) {
+ for (entry_ptr, entry_size) in self.alloc_table.iter() {
+ // SeqCst is good enough here, we don't care about perf, i just want to be correct!
+ if entry_ptr
+ .compare_exchange(null_mut(), ptr, Ordering::SeqCst, Ordering::SeqCst)
+ .is_ok()
+ {
+ entry_size.store(size, Ordering::SeqCst);
+ break;
+ }
+ }
+ }
+
+ fn remove(&self, ptr: *mut u8) -> usize {
+ for (entry_ptr, entry_size) in self.alloc_table.iter() {
+ // set the value to be something that will never try and be deallocated, so that we
+ // don't have any chance of a race condition
+ //
+ // dont worry, LEDGER_LENGTH is really long to compensate for us not reclaiming space
+ if entry_ptr
+ .compare_exchange(
+ ptr,
+ invalid_ptr(usize::MAX),
+ Ordering::SeqCst,
+ Ordering::SeqCst,
+ )
+ .is_ok()
+ {
+ return entry_size.load(Ordering::SeqCst);
+ }
+ }
+
+ panic!("Couldn't find a matching entry for {:x?}", ptr);
+ }
+}
+
+unsafe impl GlobalAlloc for Ledger {
+ unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+ let size = layout.size();
+ let ptr = System.alloc(layout);
+ self.insert(ptr, size);
+ ptr
+ }
+
+ unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
+ let orig_size = self.remove(ptr);
+
+ if orig_size != layout.size() {
+ panic!(
+ "bad dealloc: alloc size was {}, dealloc size is {}",
+ orig_size,
+ layout.size()
+ );
+ } else {
+ System.dealloc(ptr, layout);
+ }
+ }
+}
+
+#[test]
+fn test_bytes_advance() {
+ let mut bytes = Bytes::from(vec![10, 20, 30]);
+ bytes.advance(1);
+ drop(bytes);
+}
+
+#[test]
+fn test_bytes_truncate() {
+ let mut bytes = Bytes::from(vec![10, 20, 30]);
+ bytes.truncate(2);
+ drop(bytes);
+}
+
+#[test]
+fn test_bytes_truncate_and_advance() {
+ let mut bytes = Bytes::from(vec![10, 20, 30]);
+ bytes.truncate(2);
+ bytes.advance(1);
+ drop(bytes);
+}
+
+/// Returns a dangling pointer with the given address. This is used to store
+/// integer data in pointer fields.
+#[inline]
+fn invalid_ptr<T>(addr: usize) -> *mut T {
+ let ptr = std::ptr::null_mut::<u8>().wrapping_add(addr);
+ debug_assert_eq!(ptr as usize, addr);
+ ptr.cast::<T>()
+}
+
+#[test]
+fn test_bytes_into_vec() {
+ let vec = vec![33u8; 1024];
+
+ // Test cases where kind == KIND_VEC
+ let b1 = Bytes::from(vec.clone());
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where kind == KIND_ARC, ref_cnt == 1
+ let b1 = Bytes::from(vec.clone());
+ drop(b1.clone());
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where kind == KIND_ARC, ref_cnt == 2
+ let b1 = Bytes::from(vec.clone());
+ let b2 = b1.clone();
+ assert_eq!(Vec::from(b1), vec);
+
+ // Test cases where vtable = SHARED_VTABLE, kind == KIND_ARC, ref_cnt == 1
+ assert_eq!(Vec::from(b2), vec);
+
+ // Test cases where offset != 0
+ let mut b1 = Bytes::from(vec.clone());
+ let b2 = b1.split_off(20);
+
+ assert_eq!(Vec::from(b2), vec[20..]);
+ assert_eq!(Vec::from(b1), vec[..20]);
+}
diff --git a/third_party/rust/bytes/tests/test_chain.rs b/third_party/rust/bytes/tests/test_chain.rs
new file mode 100644
index 0000000000..cfda6b8dc7
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_chain.rs
@@ -0,0 +1,177 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::{Buf, BufMut, Bytes};
+#[cfg(feature = "std")]
+use std::io::IoSlice;
+
+#[test]
+fn collect_two_bufs() {
+ let a = Bytes::from(&b"hello"[..]);
+ let b = Bytes::from(&b"world"[..]);
+
+ let res = a.chain(b).copy_to_bytes(10);
+ assert_eq!(res, &b"helloworld"[..]);
+}
+
+#[test]
+fn writing_chained() {
+ let mut a = [0u8; 64];
+ let mut b = [0u8; 64];
+
+ {
+ let mut buf = (&mut a[..]).chain_mut(&mut b[..]);
+
+ for i in 0u8..128 {
+ buf.put_u8(i);
+ }
+ }
+
+ for i in 0..64 {
+ let expect = i as u8;
+ assert_eq!(expect, a[i]);
+ assert_eq!(expect + 64, b[i]);
+ }
+}
+
+#[test]
+fn iterating_two_bufs() {
+ let a = Bytes::from(&b"hello"[..]);
+ let b = Bytes::from(&b"world"[..]);
+
+ let res: Vec<u8> = a.chain(b).into_iter().collect();
+ assert_eq!(res, &b"helloworld"[..]);
+}
+
+#[cfg(feature = "std")]
+#[test]
+fn vectored_read() {
+ let a = Bytes::from(&b"hello"[..]);
+ let b = Bytes::from(&b"world"[..]);
+
+ let mut buf = a.chain(b);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(2, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"hello"[..]);
+ assert_eq!(iovecs[1][..], b"world"[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+
+ buf.advance(2);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(2, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"llo"[..]);
+ assert_eq!(iovecs[1][..], b"world"[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+
+ buf.advance(3);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(1, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"world"[..]);
+ assert_eq!(iovecs[1][..], b""[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+
+ buf.advance(3);
+
+ {
+ let b1: &[u8] = &mut [];
+ let b2: &[u8] = &mut [];
+ let b3: &[u8] = &mut [];
+ let b4: &[u8] = &mut [];
+ let mut iovecs = [
+ IoSlice::new(b1),
+ IoSlice::new(b2),
+ IoSlice::new(b3),
+ IoSlice::new(b4),
+ ];
+
+ assert_eq!(1, buf.chunks_vectored(&mut iovecs));
+ assert_eq!(iovecs[0][..], b"ld"[..]);
+ assert_eq!(iovecs[1][..], b""[..]);
+ assert_eq!(iovecs[2][..], b""[..]);
+ assert_eq!(iovecs[3][..], b""[..]);
+ }
+}
+
+#[test]
+fn chain_growing_buffer() {
+ let mut buff = [' ' as u8; 10];
+ let mut vec = b"wassup".to_vec();
+
+ let mut chained = (&mut buff[..]).chain_mut(&mut vec).chain_mut(Vec::new()); // Required for potential overflow because remaining_mut for Vec is isize::MAX - vec.len(), but for chain_mut is usize::MAX
+
+ chained.put_slice(b"hey there123123");
+
+ assert_eq!(&buff, b"hey there1");
+ assert_eq!(&vec, b"wassup23123");
+}
+
+#[test]
+fn chain_overflow_remaining_mut() {
+ let mut chained = Vec::<u8>::new().chain_mut(Vec::new()).chain_mut(Vec::new());
+
+ assert_eq!(chained.remaining_mut(), usize::MAX);
+ chained.put_slice(&[0; 256]);
+ assert_eq!(chained.remaining_mut(), usize::MAX);
+}
+
+#[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/third_party/rust/bytes/tests/test_debug.rs b/third_party/rust/bytes/tests/test_debug.rs
new file mode 100644
index 0000000000..08d2f254e8
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_debug.rs
@@ -0,0 +1,35 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::Bytes;
+
+#[test]
+fn fmt() {
+ let vec: Vec<_> = (0..0x100).map(|b| b as u8).collect();
+
+ let expected = "b\"\
+ \\0\\x01\\x02\\x03\\x04\\x05\\x06\\x07\
+ \\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\
+ \\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\
+ \\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f\
+ \x20!\\\"#$%&'()*+,-./0123456789:;<=>?\
+ @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_\
+ `abcdefghijklmnopqrstuvwxyz{|}~\\x7f\
+ \\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\
+ \\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\
+ \\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\
+ \\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\
+ \\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\
+ \\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\
+ \\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\
+ \\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\
+ \\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\
+ \\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\
+ \\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\
+ \\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\
+ \\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\
+ \\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\
+ \\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\
+ \\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff\"";
+
+ assert_eq!(expected, format!("{:?}", Bytes::from(vec)));
+}
diff --git a/third_party/rust/bytes/tests/test_iter.rs b/third_party/rust/bytes/tests/test_iter.rs
new file mode 100644
index 0000000000..a5bfddddf5
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_iter.rs
@@ -0,0 +1,21 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::Bytes;
+
+#[test]
+fn iter_len() {
+ let buf = Bytes::from_static(b"hello world");
+ let iter = buf.iter();
+
+ assert_eq!(iter.size_hint(), (11, Some(11)));
+ assert_eq!(iter.len(), 11);
+}
+
+#[test]
+fn empty_iter_len() {
+ let buf = Bytes::from_static(b"");
+ let iter = buf.iter();
+
+ assert_eq!(iter.size_hint(), (0, Some(0)));
+ assert_eq!(iter.len(), 0);
+}
diff --git a/third_party/rust/bytes/tests/test_reader.rs b/third_party/rust/bytes/tests/test_reader.rs
new file mode 100644
index 0000000000..897aff6455
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_reader.rs
@@ -0,0 +1,29 @@
+#![warn(rust_2018_idioms)]
+#![cfg(feature = "std")]
+
+use std::io::{BufRead, Read};
+
+use bytes::Buf;
+
+#[test]
+fn read() {
+ let buf1 = &b"hello "[..];
+ let buf2 = &b"world"[..];
+ let buf = Buf::chain(buf1, buf2); // Disambiguate with Read::chain
+ let mut buffer = Vec::new();
+ buf.reader().read_to_end(&mut buffer).unwrap();
+ assert_eq!(b"hello world", &buffer[..]);
+}
+
+#[test]
+fn buf_read() {
+ let buf1 = &b"hell"[..];
+ let buf2 = &b"o\nworld"[..];
+ let mut reader = Buf::chain(buf1, buf2).reader();
+ let mut line = String::new();
+ reader.read_line(&mut line).unwrap();
+ assert_eq!("hello\n", &line);
+ line.clear();
+ reader.read_line(&mut line).unwrap();
+ assert_eq!("world", &line);
+}
diff --git a/third_party/rust/bytes/tests/test_serde.rs b/third_party/rust/bytes/tests/test_serde.rs
new file mode 100644
index 0000000000..cf4aeffa78
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_serde.rs
@@ -0,0 +1,20 @@
+#![cfg(feature = "serde")]
+#![warn(rust_2018_idioms)]
+
+use serde_test::{assert_tokens, Token};
+
+#[test]
+fn test_ser_de_empty() {
+ let b = bytes::Bytes::new();
+ assert_tokens(&b, &[Token::Bytes(b"")]);
+ let b = bytes::BytesMut::with_capacity(0);
+ assert_tokens(&b, &[Token::Bytes(b"")]);
+}
+
+#[test]
+fn test_ser_de() {
+ let b = bytes::Bytes::from(&b"bytes"[..]);
+ assert_tokens(&b, &[Token::Bytes(b"bytes")]);
+ let b = bytes::BytesMut::from(&b"bytes"[..]);
+ assert_tokens(&b, &[Token::Bytes(b"bytes")]);
+}
diff --git a/third_party/rust/bytes/tests/test_take.rs b/third_party/rust/bytes/tests/test_take.rs
new file mode 100644
index 0000000000..51df91d142
--- /dev/null
+++ b/third_party/rust/bytes/tests/test_take.rs
@@ -0,0 +1,32 @@
+#![warn(rust_2018_idioms)]
+
+use bytes::buf::Buf;
+use bytes::Bytes;
+
+#[test]
+fn long_take() {
+ // Tests that get a take with a size greater than the buffer length will not
+ // overrun the buffer. Regression test for #138.
+ let buf = b"hello world".take(100);
+ 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);
+}