From 1efcb6cabd097c8b51d86c9d59e92f6cdb1de6f6 Mon Sep 17 00:00:00 2001 From: Hashim1999164 <64767361+Hashim1999164@users.noreply.github.com> Date: Sun, 2 Aug 2026 22:52:46 +0500 Subject: [PATCH 1/2] Fix inverted equality in SortedDictionary Keys, Values, and SubSequence. Equality comparisons returned false for matching elements and true for mismatches. Correct the comparisons and add coverage for the affected views. --- .../SortedDictionary+Keys.swift | 2 +- .../SortedDictionary+SubSequence.swift | 2 +- .../SortedDictionary+Values.swift | 2 +- .../SortedDictionary Tests.swift | 91 +++++++++++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) diff --git a/Sources/SortedCollections/SortedDictionary/SortedDictionary+Keys.swift b/Sources/SortedCollections/SortedDictionary/SortedDictionary+Keys.swift index 71dd4e5d..00c0a081 100644 --- a/Sources/SortedCollections/SortedDictionary/SortedDictionary+Keys.swift +++ b/Sources/SortedCollections/SortedDictionary/SortedDictionary+Keys.swift @@ -239,7 +239,7 @@ extension SortedDictionary.Keys: Equatable { public static func ==(lhs: Self, rhs: Self) -> Bool { if lhs.count != rhs.count { return false } for (e1, e2) in zip(lhs, rhs) { - if e1 == e2 { + if e1 != e2 { return false } } diff --git a/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift b/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift index 56880112..e78b85b8 100644 --- a/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift +++ b/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift @@ -300,7 +300,7 @@ extension SortedDictionary.SubSequence: Equatable where Value: Equatable { public static func ==(lhs: Self, rhs: Self) -> Bool { if lhs.count != rhs.count { return false } for (e1, e2) in zip(lhs, rhs) { - if e1 == e2 { + if e1.key != e2.key || e1.value != e2.value { return false } } diff --git a/Sources/SortedCollections/SortedDictionary/SortedDictionary+Values.swift b/Sources/SortedCollections/SortedDictionary/SortedDictionary+Values.swift index e75f3e44..f99cdd8b 100644 --- a/Sources/SortedCollections/SortedDictionary/SortedDictionary+Values.swift +++ b/Sources/SortedCollections/SortedDictionary/SortedDictionary+Values.swift @@ -258,7 +258,7 @@ extension SortedDictionary.Values: Equatable where Value: Equatable { public static func ==(lhs: Self, rhs: Self) -> Bool { if lhs.count != rhs.count { return false } for (e1, e2) in zip(lhs, rhs) { - if e1 == e2 { + if e1 != e2 { return false } } diff --git a/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift b/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift index d12e62a0..fc25dddd 100644 --- a/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift +++ b/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift @@ -284,6 +284,97 @@ final class SortedDictionaryTests: CollectionTestCase { } } } + + func test_keys_Equatable() { + let left: SortedDictionary = [ + 1: "one", + 2: "two", + 3: "three", + ] + let rightEqual: SortedDictionary = [ + 3: "trois", + 1: "un", + 2: "deux", + ] + let rightUnequal: SortedDictionary = [ + 1: "one", + 2: "two", + 4: "four", + ] + + expectEqual(left.keys, left.keys) + expectEqual(left.keys, rightEqual.keys) + expectNotEqual(left.keys, rightUnequal.keys) + + checkEquatable(equivalenceClasses: [ + [SortedDictionary().keys, SortedDictionary().keys], + [left.keys, rightEqual.keys], + [rightUnequal.keys], + ]) + } + + func test_values_Equatable() { + let left: SortedDictionary = [ + 1: "one", + 2: "two", + 3: "three", + ] + let rightEqual: SortedDictionary = [ + 1: "one", + 2: "two", + 3: "three", + ] + let rightUnequal: SortedDictionary = [ + 1: "one", + 2: "two", + 3: "trois", + ] + + expectEqual(left.values, left.values) + expectEqual(left.values, rightEqual.values) + expectNotEqual(left.values, rightUnequal.values) + + checkEquatable(equivalenceClasses: [ + [SortedDictionary().values, SortedDictionary().values], + [left.values, rightEqual.values], + [rightUnequal.values], + ]) + } + + func test_subSequence_Equatable() { + let left: SortedDictionary = [ + 1: "one", + 2: "two", + 3: "three", + 4: "four", + ] + let rightEqual: SortedDictionary = [ + 1: "one", + 2: "two", + 3: "three", + 4: "four", + ] + let rightUnequal: SortedDictionary = [ + 1: "one", + 2: "two", + 3: "trois", + 4: "four", + ] + + let leftSlice = left[left.index(after: left.startIndex).. Date: Sun, 2 Aug 2026 22:52:46 +0500 Subject: [PATCH 2/2] Use tuple inequality in SortedDictionary.SubSequence equality. Match the Keys and Values comparison style after review feedback. --- .../SortedDictionary/SortedDictionary+SubSequence.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift b/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift index e78b85b8..720f14a7 100644 --- a/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift +++ b/Sources/SortedCollections/SortedDictionary/SortedDictionary+SubSequence.swift @@ -300,7 +300,7 @@ extension SortedDictionary.SubSequence: Equatable where Value: Equatable { public static func ==(lhs: Self, rhs: Self) -> Bool { if lhs.count != rhs.count { return false } for (e1, e2) in zip(lhs, rhs) { - if e1.key != e2.key || e1.value != e2.value { + if e1 != e2 { return false } }