Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: ensure BytesMut::advance reduces capacity #728

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,43 @@ fn advance_bytes_mut() {
assert_eq!(a, b"d zomg wat wat"[..]);
}

// Ensures BytesMut::advance reduces always capacity
//
// See https://github.com/tokio-rs/bytes/issues/725
#[test]
fn advance_bytes_mut_remaining_capacity() {
// reduce the search space under miri
let max_capacity = if cfg!(miri) { 16 } else { 256 };
for capacity in 0..=max_capacity {
for len in 0..=capacity {
for advance in 0..=len {
eprintln!("testing capacity={capacity}, len={len}, advance={advance}");
let mut buf = BytesMut::with_capacity(capacity);

buf.resize(len, 42);
assert_eq!(buf.len(), len, "resize should write `len` bytes");
assert_eq!(
buf.remaining(),
len,
"Buf::remaining() should equal BytesMut::len"
);

buf.advance(advance);
assert_eq!(
buf.remaining(),
len - advance,
"Buf::advance should reduce the remaining len"
);
assert_eq!(
buf.capacity(),
capacity - advance,
"Buf::advance should reduce the remaining capacity"
);
}
}
}
}

#[test]
#[should_panic]
fn advance_past_len() {
Expand Down