File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff 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]
681718fn advance_past_len ( ) {
You can’t perform that action at this time.
0 commit comments