@@ -848,6 +848,91 @@ mod tests {
848848 assert_eq ! ( map. buffer. capacity( ) , INITIAL_BUFFER_CAPACITY ) ;
849849 }
850850
851+ #[ test]
852+ fn push_value_bytes_ignores_an_empty_value ( ) {
853+ let mut buffer = Vec :: new ( ) ;
854+ push_value_bytes ( & mut buffer, b"" ) ;
855+ assert_eq ! ( buffer. capacity( ) , 0 ) ;
856+ assert ! ( buffer. is_empty( ) ) ;
857+
858+ push_value_bytes ( & mut buffer, b"abcd" ) ;
859+ let capacity = buffer. capacity ( ) ;
860+ push_value_bytes ( & mut buffer, b"" ) ;
861+ assert_eq ! ( buffer, b"abcd" ) ;
862+ assert_eq ! ( buffer. capacity( ) , capacity) ;
863+ }
864+
865+ #[ test]
866+ fn push_value_bytes_grows_only_past_a_power_of_two ( ) {
867+ let mut buffer = Vec :: new ( ) ;
868+
869+ // A first append that is not itself a power of two still lands on the
870+ // ladder: a plain `Vec` would have stopped at a capacity of 9.
871+ push_value_bytes ( & mut buffer, & [ 1u8 ; 9 ] ) ;
872+ assert_eq ! ( buffer. len( ) , 9 ) ;
873+ assert_eq ! ( buffer. capacity( ) , 16 ) ;
874+
875+ // Filling exactly to the capacity does not grow it.
876+ push_value_bytes ( & mut buffer, & [ 2u8 ; 7 ] ) ;
877+ assert_eq ! ( buffer. len( ) , 16 ) ;
878+ assert_eq ! ( buffer. capacity( ) , 16 ) ;
879+
880+ // The next byte moves up one rung.
881+ push_value_bytes ( & mut buffer, & [ 3u8 ; 1 ] ) ;
882+ assert_eq ! ( buffer. len( ) , 17 ) ;
883+ assert_eq ! ( buffer. capacity( ) , 32 ) ;
884+
885+ // So does an append that overshoots several rungs at once.
886+ push_value_bytes ( & mut buffer, & [ 4u8 ; 100 ] ) ;
887+ assert_eq ! ( buffer. len( ) , 117 ) ;
888+ assert_eq ! ( buffer. capacity( ) , 128 ) ;
889+
890+ let mut expected = vec ! [ 1u8 ; 9 ] ;
891+ expected. extend_from_slice ( & [ 2u8 ; 7 ] ) ;
892+ expected. extend_from_slice ( & [ 3u8 ; 1 ] ) ;
893+ expected. extend_from_slice ( & [ 4u8 ; 100 ] ) ;
894+ assert_eq ! ( buffer, expected) ;
895+ }
896+
897+ #[ test]
898+ fn push_value_bytes_does_not_grow_a_buffer_with_room ( ) {
899+ let mut buffer = Vec :: with_capacity ( INITIAL_BUFFER_CAPACITY ) ;
900+ let start = buffer. as_ptr ( ) ;
901+
902+ let value = vec ! [ 7u8 ; 512 ] ;
903+ for _ in 0 ..16 {
904+ push_value_bytes ( & mut buffer, & value) ;
905+ }
906+
907+ assert_eq ! ( buffer. len( ) , INITIAL_BUFFER_CAPACITY ) ;
908+ assert_eq ! ( buffer. capacity( ) , INITIAL_BUFFER_CAPACITY ) ;
909+ assert ! (
910+ std:: ptr:: eq( buffer. as_ptr( ) , start) ,
911+ "buffer was reallocated"
912+ ) ;
913+ assert ! ( buffer. iter( ) . all( |& byte| byte == 7 ) ) ;
914+ }
915+
916+ #[ test]
917+ fn push_value_bytes_rounds_a_single_oversized_append_up_to_a_power_of_two ( ) {
918+ let value = vec ! [ 9u8 ; INITIAL_BUFFER_CAPACITY + 1 ] ;
919+ let rounded = ( INITIAL_BUFFER_CAPACITY + 1 ) . next_power_of_two ( ) ;
920+
921+ // A value larger than the pre-allocation lands on the same rung
922+ // whether the buffer started empty or pre-allocated, which is what
923+ // keeps a lazily allocated map from outgrowing a pre-allocated one.
924+ let mut lazy = Vec :: new ( ) ;
925+ push_value_bytes ( & mut lazy, & value) ;
926+ assert_eq ! ( lazy. len( ) , value. len( ) ) ;
927+ assert_eq ! ( lazy. capacity( ) , rounded) ;
928+ assert_eq ! ( lazy, value) ;
929+
930+ let mut pre_allocated = Vec :: with_capacity ( INITIAL_BUFFER_CAPACITY ) ;
931+ push_value_bytes ( & mut pre_allocated, & value) ;
932+ assert_eq ! ( pre_allocated. capacity( ) , rounded) ;
933+ assert_eq ! ( pre_allocated, value) ;
934+ }
935+
851936 #[ test]
852937 fn lazy_and_pre_allocated_buffers_grow_on_the_same_ladder ( ) {
853938 // Value lengths chosen so the buffer requirement lands between powers
0 commit comments