Skip to content

Commit ed7d5ff

Browse files
authored
test: ensure BytesMut::advance reduces capacity (#728)
1 parent dc4fb3e commit ed7d5ff

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/test_bytes.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,43 @@ fn advance_bytes_mut() {
676676
assert_eq!(a, b"d zomg wat wat"[..]);
677677
}
678678

679+
// Ensures BytesMut::advance reduces always capacity
680+
//
681+
// See https://github.com/tokio-rs/bytes/issues/725
682+
#[test]
683+
fn advance_bytes_mut_remaining_capacity() {
684+
// reduce the search space under miri
685+
let max_capacity = if cfg!(miri) { 16 } else { 256 };
686+
for capacity in 0..=max_capacity {
687+
for len in 0..=capacity {
688+
for advance in 0..=len {
689+
eprintln!("testing capacity={capacity}, len={len}, advance={advance}");
690+
let mut buf = BytesMut::with_capacity(capacity);
691+
692+
buf.resize(len, 42);
693+
assert_eq!(buf.len(), len, "resize should write `len` bytes");
694+
assert_eq!(
695+
buf.remaining(),
696+
len,
697+
"Buf::remaining() should equal BytesMut::len"
698+
);
699+
700+
buf.advance(advance);
701+
assert_eq!(
702+
buf.remaining(),
703+
len - advance,
704+
"Buf::advance should reduce the remaining len"
705+
);
706+
assert_eq!(
707+
buf.capacity(),
708+
capacity - advance,
709+
"Buf::advance should reduce the remaining capacity"
710+
);
711+
}
712+
}
713+
}
714+
}
715+
679716
#[test]
680717
#[should_panic]
681718
fn advance_past_len() {

0 commit comments

Comments
 (0)