diff options
| author | bors <bors@rust-lang.org> | 2016-02-02 10:45:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-02-02 10:45:06 +0000 |
| commit | ddd1bf594fdaf2f4a9ac3f03ffdb726f5121050e (patch) | |
| tree | b3de7fbfe1d09b8554cbe7cccba7d23f4b9902c0 /src/libstd | |
| parent | 01d44ca74fa63e078d7b8c2e5f221df06d72fb46 (diff) | |
| parent | 8aae7f78ce16c3386652ab909c1d1318dc780ddd (diff) | |
| download | rust-ddd1bf594fdaf2f4a9ac3f03ffdb726f5121050e.tar.gz rust-ddd1bf594fdaf2f4a9ac3f03ffdb726f5121050e.zip | |
Auto merge of #30991 - rthomas:master, r=Gankro
Using the test @bluss suggested in #30983
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 173214eda44..7ce4aa07b50 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -72,7 +72,10 @@ impl DefaultResizePolicy { // // This doesn't have to be checked for overflow since allocation size // in bytes will overflow earlier than multiplication by 10. - cap * 10 / 11 + // + // As per https://github.com/rust-lang/rust/pull/30991 this is updated + // to be: (cap * den + den - 1) / num + (cap * 10 + 10 - 1) / 11 } } @@ -2418,4 +2421,29 @@ mod test_map { assert_eq!(a[&2], "two"); assert_eq!(a[&3], "three"); } + + #[test] + fn test_capacity_not_less_than_len() { + let mut a = HashMap::new(); + let mut item = 0; + + for _ in 0..116 { + a.insert(item, 0); + item += 1; + } + + assert!(a.capacity() > a.len()); + + let free = a.capacity() - a.len(); + for _ in 0..free { + a.insert(item, 0); + item += 1; + } + + assert_eq!(a.len(), a.capacity()); + + // Insert at capacity should cause allocation. + a.insert(item, 0); + assert!(a.capacity() > a.len()); + } } |
