Test semantics of buffer allocation of get_mut()

* if remaining bytes are smaller then 8 * 1024, allocate 8 * 1024
* otherwise allocate as much as needed to hold the remaining bytes
  without re-allocations.
This commit is contained in:
Sebastian Thiel
2017-02-12 09:47:03 +01:00
parent e11dd06ead
commit 65c5c5241b
+12 -2
View File
@@ -153,7 +153,7 @@ impl EasyBuf {
// If we couldn't get access above then we give ourself a new buffer
// here.
let mut v = Vec::with_capacity(cmp::min(INITIAL_CAPACITY, self.as_ref().len()));
let mut v = Vec::with_capacity(cmp::max(INITIAL_CAPACITY, self.as_ref().len()));
v.extend_from_slice(self.as_ref());
self.start = 0;
self.buf = Arc::new(v);
@@ -465,7 +465,7 @@ mod tests {
}
#[test]
fn easybuf_get_mut_sliced_allocating() {
fn easybuf_get_mut_sliced_allocating_at_least_initial_capacity() {
let vec: Vec<u8> = (0u8..10u8).collect();
let mut buf: EasyBuf = vec.into();
buf.split_off(9);
@@ -477,6 +477,16 @@ mod tests {
mem::drop(clone); // prevent unused warning
}
#[test]
fn easybuf_get_mut_sliced_allocating_required_capacity() {
let vec: Vec<u8> = (0..INITIAL_CAPACITY * 2).map(|_|0u8).collect();
let mut buf: EasyBuf = vec.into();
buf.drain_to(INITIAL_CAPACITY / 2);
let clone = buf.clone();
assert_eq!(buf.get_mut().buf.capacity(), INITIAL_CAPACITY + INITIAL_CAPACITY / 2);
mem::drop(clone)
}
#[test]
fn easybuf_into_vec_simple() {
let vec: Vec<u8> = (0u8..10u8).collect();