@@ -285,6 +285,67 @@ fn test_vector_reserve() {
285285 let mut v = vector:: Vector :: < i32 > :: new ( ) ;
286286 v. reserve ( 10 ) ;
287287 expect_that ! ( v. capacity( ) , ge( 10 ) ) ;
288+ let current_capacity = v. capacity ( ) ;
289+ v. reserve ( 5 ) ; // 5 < 10, so it should do nothing.
290+ expect_eq ! ( v. capacity( ) , current_capacity) ;
291+ }
292+
293+ #[ gtest]
294+ fn test_vector_reserve_exact ( ) {
295+ let mut v = vector:: Vector :: < usize > :: new ( ) ;
296+ v. reserve_exact ( 10 ) ;
297+ expect_that ! ( v. capacity( ) , ge( 10 ) ) ;
298+ let current_capacity = v. capacity ( ) ;
299+ // Add elements to the current capacity.
300+ for i in v. len ( ) ..current_capacity {
301+ v. push ( i) ;
302+ }
303+ v. reserve_exact ( 5 ) ; // it ensures that the capacity for additional 5 elements.
304+ expect_that ! ( v. capacity( ) , ge( current_capacity + 5 ) ) ;
305+ }
306+
307+ #[ gtest]
308+ fn test_vector_try_reserve ( ) {
309+ let mut v = vector:: Vector :: < i32 > :: new ( ) ;
310+ v. try_reserve ( 100 ) . unwrap ( ) ;
311+ expect_that ! ( v. capacity( ) , ge( 100 ) ) ;
312+ }
313+
314+ #[ gtest]
315+ fn test_vector_try_reserve_exact ( ) {
316+ let mut v = vector:: Vector :: < usize > :: new ( ) ;
317+ v. try_reserve_exact ( 10 ) . unwrap ( ) ;
318+ expect_that ! ( v. capacity( ) , ge( 10 ) ) ;
319+ let current_capacity = v. capacity ( ) ;
320+ // Add elements to the current capacity.
321+ for i in v. len ( ) ..current_capacity {
322+ v. push ( i) ;
323+ }
324+ v. try_reserve_exact ( 5 ) . unwrap ( ) ; // it ensures that the capacity for additional 5 elements.
325+ expect_that ! ( v. capacity( ) , ge( current_capacity + 5 ) ) ;
326+ }
327+
328+ #[ gtest]
329+ fn test_shrink_to_fit ( ) {
330+ let mut v = vector:: Vector :: < i32 > :: new ( ) ;
331+ for i in 0 ..100 {
332+ v. push ( i) ;
333+ }
334+ v. clear ( ) ; // doesn't shrink the capacity.
335+ v. shrink_to_fit ( ) ;
336+ expect_eq ! ( v. capacity( ) , 0 ) ;
337+ }
338+
339+ #[ gtest]
340+ fn test_shrink_to ( ) {
341+ let mut v = vector:: Vector :: < i32 > :: new ( ) ;
342+ for i in 0 ..100 {
343+ v. push ( i) ;
344+ }
345+ v. clear ( ) ; // doesn't shrink the capacity.
346+ v. shrink_to ( 10 ) ;
347+ expect_that ! ( v. capacity( ) , le( 99 ) ) ;
348+ expect_that ! ( v. capacity( ) , ge( 10 ) ) ;
288349}
289350
290351#[ gtest]
0 commit comments