diff --git a/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift b/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift index 7ebec991..c5592e06 100644 --- a/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift +++ b/Tests/SortedCollectionsTests/SortedDictionary/SortedDictionary Tests.swift @@ -288,7 +288,26 @@ final class SortedDictionaryTests: CollectionTestCase { } } - func test_keys_Equatable() { + func test_Equatable_Hashable() { + let evens = stride(from: 0, to: 42, by: 2).map { ($0, $0 / 2) } + let keyChanged = evens.map { key, value in (key == 20 ? 21 : key, value) } + let valueChanged = evens.map { key, value in (key, key == 20 ? -1 : value) } + let classes: [[SortedDictionary]] = [ + [[:]], + [[0: 0]], + [[0: 1]], + [[1: 0]], + [ + SortedDictionary(sortedKeysWithValues: evens), + SortedDictionary(keysWithValues: evens), + ], + [SortedDictionary(sortedKeysWithValues: keyChanged)], + [SortedDictionary(sortedKeysWithValues: valueChanged)], + ] + checkHashable(equivalenceClasses: classes) + } + + func test_keys_Equatable_Hashable() { let left: SortedDictionary = [ 1: "one", 2: "two", @@ -309,14 +328,22 @@ final class SortedDictionaryTests: CollectionTestCase { expectEqual(left.keys, rightEqual.keys) expectNotEqual(left.keys, rightUnequal.keys) - checkEquatable(equivalenceClasses: [ + let evens = stride(from: 0, to: 42, by: 2).map { ($0, "\($0)") } + let keyChanged = evens.map { key, value in (key == 20 ? 21 : key, value) } + + checkHashable(equivalenceClasses: [ [SortedDictionary().keys, SortedDictionary().keys], [left.keys, rightEqual.keys], [rightUnequal.keys], + [ + SortedDictionary(sortedKeysWithValues: evens).keys, + SortedDictionary(keysWithValues: evens.map { key, _ in (key, "") }).keys, + ], + [SortedDictionary(sortedKeysWithValues: keyChanged).keys], ]) } - func test_values_Equatable() { + func test_values_Equatable_Hashable() { let left: SortedDictionary = [ 1: "one", 2: "two", @@ -337,14 +364,22 @@ final class SortedDictionaryTests: CollectionTestCase { expectEqual(left.values, rightEqual.values) expectNotEqual(left.values, rightUnequal.values) - checkEquatable(equivalenceClasses: [ + let evens = stride(from: 0, to: 42, by: 2).map { ($0, "\($0)") } + let valueChanged = evens.map { key, value in (key, key == 20 ? "" : value) } + + checkHashable(equivalenceClasses: [ [SortedDictionary().values, SortedDictionary().values], [left.values, rightEqual.values], [rightUnequal.values], + [ + SortedDictionary(sortedKeysWithValues: evens).values, + SortedDictionary(keysWithValues: evens.map { key, value in (key + 1, value) }).values, + ], + [SortedDictionary(sortedKeysWithValues: valueChanged).values], ]) } - func test_subSequence_Equatable() { + func test_subSequence_Equatable_Hashable() { let left: SortedDictionary = [ 1: "one", 2: "two", @@ -377,6 +412,28 @@ final class SortedDictionaryTests: CollectionTestCase { expectEqual(leftSlice, leftSlice) expectEqual(leftSlice, rightEqualSlice) expectNotEqual(leftSlice, rightUnequalSlice) + + let pairs = (0 ..< 40).map { ($0, "\($0)") } + let bulk = SortedDictionary(sortedKeysWithValues: pairs) + let inserted = SortedDictionary(keysWithValues: pairs) + let valueChanged = SortedDictionary( + sortedKeysWithValues: pairs.map { key, value in (key, key == 20 ? "" : value) }) + let keysShifted = SortedDictionary( + sortedKeysWithValues: pairs.map { key, value in (key + 1, value) }) + + let classes: [[SortedDictionary.SubSequence]] = [ + [left.prefix(0), SortedDictionary()[...]], + [leftSlice, rightEqualSlice], + [rightUnequalSlice], + [ + bulk.dropFirst(10).prefix(20), + inserted.dropFirst(10).prefix(20), + SortedDictionary(sortedKeysWithValues: pairs[10 ..< 30])[...], + ], + [valueChanged.dropFirst(10).prefix(20)], + [keysShifted.dropFirst(10).prefix(20)], + ] + checkHashable(equivalenceClasses: classes) } } diff --git a/Tests/SortedCollectionsTests/SortedSet/SortedSet Tests.swift b/Tests/SortedCollectionsTests/SortedSet/SortedSet Tests.swift index a2f3d21b..f239d595 100644 --- a/Tests/SortedCollectionsTests/SortedSet/SortedSet Tests.swift +++ b/Tests/SortedCollectionsTests/SortedSet/SortedSet Tests.swift @@ -68,6 +68,15 @@ class SortedSetTests: CollectionTestCase { } } + func test_BidirectionalCollection() { + withEvery("count", in: [0, 1, 5, 6, 17, 18, 29, 30]) { count in + checkBidirectionalCollection( + SortedSet(sortedElements: 0 ..< count), expectedContents: 0 ..< count) + checkBidirectionalCollection( + SortedSet(0 ..< count), expectedContents: 0 ..< count) + } + } + func test_firstIndexOf_lastIndexOf() { withEvery("count", in: 0 ..< 20) { count in let contents = Array(0 ..< count) @@ -257,6 +266,38 @@ class SortedSetTests: CollectionTestCase { } } + func test_Equatable_Hashable() { + let evens = stride(from: 0, to: 42, by: 2) + let classes: [[SortedSet]] = [ + [[]], + [[0]], + [[1]], + [SortedSet(sortedElements: 0 ..< 20), SortedSet(0 ..< 20)], + [SortedSet(sortedElements: 0 ..< 21), SortedSet(0 ..< 21)], + [SortedSet(sortedElements: 1 ..< 21), SortedSet(1 ..< 21)], + [SortedSet(evens)], + [SortedSet(evens.map { $0 == 20 ? 21 : $0 })], + [SortedSet(evens.map { $0 == 38 ? 39 : $0 })], + ] + checkHashable(equivalenceClasses: classes) + } + + func test_subSequence_Equatable_Hashable() { + let evens = stride(from: 0, to: 80, by: 2) + let classes: [[SortedSet.SubSequence]] = [ + [SortedSet()[0 ..< 10], SortedSet(0 ..< 40)[50 ..< 60]], + [ + SortedSet(sortedElements: 0 ..< 40)[10 ..< 30], + SortedSet(0 ..< 40)[10 ..< 30], + SortedSet(10 ..< 30)[...], + ], + [SortedSet(evens)[20 ..< 60]], + [SortedSet(evens.map { $0 == 40 ? 41 : $0 })[20 ..< 60]], + [SortedSet(evens.map { $0 == 56 ? 57 : $0 })[20 ..< 60]], + ] + checkHashable(equivalenceClasses: classes) + } + func test_Encodable() throws { let s1: SortedSet = [] let v1: MinimalEncoder.Value = .array([])