Skip to content

Commit 4fb1b6d

Browse files
committed
API Breaking: Equality tests on LinkedHashSet should be consistent
`LinkedHashSet`'s `PartialEq` / `Eq` implementation is now consistent with `LinkedHashMap`'s implementation, it is order dependent. Add some simple tests for this property.
1 parent 9b0ee5d commit 4fb1b6d

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/linked_hash_set.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,8 @@ where
304304
S: BuildHasher,
305305
{
306306
#[inline]
307-
fn eq(&self, other: &LinkedHashSet<T, S>) -> bool {
308-
if self.len() != other.len() {
309-
return false;
310-
}
311-
312-
self.iter().all(|key| other.contains(key))
307+
fn eq(&self, other: &Self) -> bool {
308+
self.len() == other.len() && self.iter().eq(other)
313309
}
314310
}
315311

tests/linked_hash_map.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,18 @@ fn test_retain() {
496496
drop(map);
497497
assert!(c.get() == 4);
498498
}
499+
500+
#[test]
501+
fn test_order_equality() {
502+
let xs = [1, 2, 3, 4, 5, 6];
503+
let mut map1: LinkedHashMap<String, i32> = xs.iter().map(|i| (i.to_string(), *i)).collect();
504+
let mut map2: LinkedHashMap<String, i32> = xs.iter().map(|i| (i.to_string(), *i)).collect();
505+
506+
assert_eq!(map1, map2);
507+
508+
map1.to_front("4");
509+
assert_ne!(map1, map2);
510+
511+
map2.to_front("4");
512+
assert_eq!(map1, map2);
513+
}

tests/linked_hash_set.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,18 @@ fn to_back_front_order() {
510510
set.to_front(&3);
511511
assert_eq!(set.front().copied(), Some(3));
512512
}
513+
514+
#[test]
515+
fn test_order_equality() {
516+
let xs = [1, 2, 3, 4, 5, 6];
517+
let mut set1: LinkedHashSet<i32> = xs.iter().copied().collect();
518+
let mut set2: LinkedHashSet<i32> = xs.iter().copied().collect();
519+
520+
assert_eq!(set1, set2);
521+
522+
set1.to_front(&4);
523+
assert_ne!(set1, set2);
524+
525+
set2.to_front(&4);
526+
assert_eq!(set1, set2);
527+
}

0 commit comments

Comments
 (0)