aboutsummaryrefslogtreecommitdiff
path: root/tests/test_bytes.rs
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2022-12-08 09:42:43 +0100
committerJeff Vander Stoep <jeffv@google.com>2022-12-08 09:48:48 +0100
commit61e66a44bc92c925ada24e2765572e5de1b827fe (patch)
treef076aa0469c4503eed1329b45d8a42af241ffb3b /tests/test_bytes.rs
parenta8cac4dbb79bd0c8317dc7e8442cd02578df10ca (diff)
downloadbytes-61e66a44bc92c925ada24e2765572e5de1b827fe.tar.gz
Upgrade bytes to 1.3.0
This project was upgraded with external_updater. Usage: tools/external_updater/updater.sh update rust/crates/bytes For more info, check https://cs.android.com/android/platform/superproject/+/master:tools/external_updater/README.md Test: TreeHugger Change-Id: Ifc6693cab6343ed5bcd70b718bb4b99c447783ea
Diffstat (limited to 'tests/test_bytes.rs')
-rw-r--r--tests/test_bytes.rs175
1 files changed, 168 insertions, 7 deletions
diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs
index 402017b..3481f8d 100644
--- a/tests/test_bytes.rs
+++ b/tests/test_bytes.rs
@@ -4,8 +4,8 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::usize;
-const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb";
-const SHORT: &'static [u8] = b"hello world";
+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>() {}
@@ -412,8 +412,8 @@ fn freeze_after_split_off() {
fn fns_defined_for_bytes_mut() {
let mut bytes = BytesMut::from(&b"hello world"[..]);
- bytes.as_ptr();
- bytes.as_mut_ptr();
+ let _ = bytes.as_ptr();
+ let _ = bytes.as_mut_ptr();
// Iterator
let v: Vec<u8> = bytes.as_ref().iter().cloned().collect();
@@ -444,7 +444,7 @@ fn reserve_growth() {
let _ = bytes.split();
bytes.reserve(65);
- assert_eq!(bytes.capacity(), 128);
+ assert_eq!(bytes.capacity(), 117);
}
#[test]
@@ -517,6 +517,34 @@ fn reserve_in_arc_unique_doubles() {
}
#[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();
@@ -528,6 +556,25 @@ fn reserve_in_arc_nonunique_does_not_overallocate() {
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);
@@ -546,6 +593,13 @@ fn extend_from_slice_mut() {
}
#[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();
@@ -875,7 +929,7 @@ fn from_iter_no_size_hint() {
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);
+ let sub = bytes.slice_ref(slice);
assert_eq!(&sub[..], expected);
}
@@ -895,7 +949,7 @@ fn slice_ref_empty() {
let bytes = Bytes::from(&b""[..]);
let slice = &(bytes.as_ref()[0..0]);
- let sub = bytes.slice_ref(&slice);
+ let sub = bytes.slice_ref(slice);
assert_eq!(&sub[..], b"");
}
@@ -1003,3 +1057,110 @@ fn box_slice_empty() {
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]);
+}