@@ -846,4 +846,50 @@ mod tests {
846846 test ( & mut f, l) ;
847847 }
848848 }
849+
850+ #[ test]
851+ fn add_capacity ( ) {
852+ let layout = Layout :: from_size_align ( ALIGN_USIZE , ALIGN_USIZE ) . unwrap ( ) ;
853+
854+ let mut free_list = FreeList :: new ( 0 ) ;
855+ assert ! ( free_list. alloc( layout) . unwrap( ) . is_none( ) , "no capacity" ) ;
856+
857+ free_list. add_capacity ( ALIGN_USIZE ) ;
858+ assert ! (
859+ free_list. alloc( layout) . unwrap( ) . is_none( ) ,
860+ "still not enough capacity because we won't allocate the zero index"
861+ ) ;
862+
863+ free_list. add_capacity ( ALIGN_USIZE ) ;
864+ let a = free_list
865+ . alloc ( layout)
866+ . unwrap ( )
867+ . expect ( "now we have enough capacity for one" ) ;
868+ assert ! (
869+ free_list. alloc( layout) . unwrap( ) . is_none( ) ,
870+ "but not enough capacity for two"
871+ ) ;
872+
873+ free_list. add_capacity ( ALIGN_USIZE ) ;
874+ let b = free_list
875+ . alloc ( layout)
876+ . unwrap ( )
877+ . expect ( "now we have enough capacity for two" ) ;
878+
879+ free_list. dealloc ( a, layout) ;
880+ free_list. dealloc ( b, layout) ;
881+ assert_eq ! (
882+ free_list. free_block_index_to_len. len( ) ,
883+ 1 ,
884+ "`dealloc` should merge blocks from different `add_capacity` calls together"
885+ ) ;
886+
887+ free_list. add_capacity ( ALIGN_USIZE ) ;
888+ assert_eq ! (
889+ free_list. free_block_index_to_len. len( ) ,
890+ 1 ,
891+ "`add_capacity` should eagerly merge new capacity into the last block \
892+ in the free list, when possible"
893+ ) ;
894+ }
849895}
0 commit comments