Skip to content

Commit ddd1bf5

Browse files
committed
Auto merge of #30991 - rthomas:master, r=Gankro
Using the test @bluss suggested in #30983
2 parents 01d44ca + 8aae7f7 commit ddd1bf5

File tree

1 file changed

+29
-1
lines changed
  • src/libstd/collections/hash

1 file changed

+29
-1
lines changed

src/libstd/collections/hash/map.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ impl DefaultResizePolicy {
7272
//
7373
// This doesn't have to be checked for overflow since allocation size
7474
// 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
7679
}
7780
}
7881

@@ -2418,4 +2421,29 @@ mod test_map {
24182421
assert_eq!(a[&2], "two");
24192422
assert_eq!(a[&3], "three");
24202423
}
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+
}
24212449
}

0 commit comments

Comments
 (0)