Skip to content

Commit eb1f631

Browse files
authored
Rollup merge of rust-lang#62709 - nhynes:test-maplike-fromiter, r=cuviper
Test that maplike FromIter satisfies uniqueness This PR adds a simple assertion to the `HashMap` and `HashSet` tests to ensure that uniqueness is satisfied when `FromIter`ing. This is useful for people who want to test their custom type against the Map/Set interfaces since they'll copy the tests wholesale but possibly miss this bug (where _they_ = _me_).
2 parents 95b1fe5 + 503ceda commit eb1f631

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/libstd/collections/hash/map.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3138,13 +3138,15 @@ mod test_map {
31383138

31393139
#[test]
31403140
fn test_from_iter() {
3141-
let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
3141+
let xs = [(1, 1), (2, 2), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
31423142

31433143
let map: HashMap<_, _> = xs.iter().cloned().collect();
31443144

31453145
for &(k, v) in &xs {
31463146
assert_eq!(map.get(&k), Some(&v));
31473147
}
3148+
3149+
assert_eq!(map.iter().len(), xs.len() - 1);
31483150
}
31493151

31503152
#[test]

src/libstd/collections/hash/set.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1782,13 +1782,15 @@ mod test_set {
17821782

17831783
#[test]
17841784
fn test_from_iter() {
1785-
let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9];
1785+
let xs = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9];
17861786

17871787
let set: HashSet<_> = xs.iter().cloned().collect();
17881788

17891789
for x in &xs {
17901790
assert!(set.contains(x));
17911791
}
1792+
1793+
assert_eq!(set.iter().len(), xs.len() - 1);
17921794
}
17931795

17941796
#[test]

0 commit comments

Comments
 (0)