File tree 1 file changed +29
-1
lines changed
src/libstd/collections/hash
1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -72,7 +72,10 @@ impl DefaultResizePolicy {
72
72
//
73
73
// This doesn't have to be checked for overflow since allocation size
74
74
// in bytes will overflow earlier than multiplication by 10.
75
- cap * 10 / 11
75
+ //
76
+ // As per https://github.com/rust-lang/rust/pull/30991 this is updated
77
+ // to be: (cap * den + den - 1) / num
78
+ ( cap * 10 + 10 - 1 ) / 11
76
79
}
77
80
}
78
81
@@ -2418,4 +2421,29 @@ mod test_map {
2418
2421
assert_eq ! ( a[ & 2 ] , "two" ) ;
2419
2422
assert_eq ! ( a[ & 3 ] , "three" ) ;
2420
2423
}
2424
+
2425
+ #[ test]
2426
+ fn test_capacity_not_less_than_len ( ) {
2427
+ let mut a = HashMap :: new ( ) ;
2428
+ let mut item = 0 ;
2429
+
2430
+ for _ in 0 ..116 {
2431
+ a. insert ( item, 0 ) ;
2432
+ item += 1 ;
2433
+ }
2434
+
2435
+ assert ! ( a. capacity( ) > a. len( ) ) ;
2436
+
2437
+ let free = a. capacity ( ) - a. len ( ) ;
2438
+ for _ in 0 ..free {
2439
+ a. insert ( item, 0 ) ;
2440
+ item += 1 ;
2441
+ }
2442
+
2443
+ assert_eq ! ( a. len( ) , a. capacity( ) ) ;
2444
+
2445
+ // Insert at capacity should cause allocation.
2446
+ a. insert ( item, 0 ) ;
2447
+ assert ! ( a. capacity( ) > a. len( ) ) ;
2448
+ }
2421
2449
}
You can’t perform that action at this time.
0 commit comments