Skip to content

Commit a42bc53

Browse files
[CodeReview] Making the corrections
1 parent abe4890 commit a42bc53

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Sources/Algorithms/Compacted.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public struct CompactedCollection<Base: Collection, Element>: Collection
107107

108108
@inlinable
109109
public func index(after i: Index) -> Index {
110-
precondition(i < endIndex, "Index out of bounds")
110+
precondition(i != endIndex, "Index out of bounds")
111111

112112
let baseIdx = base.index(after: i.base)
113113
guard let idx = base[baseIdx...].firstIndex(where: { $0 != nil })
@@ -121,7 +121,7 @@ extension CompactedCollection: BidirectionalCollection
121121

122122
@inlinable
123123
public func index(before i: Index) -> Index {
124-
precondition(i > startIndex, "Index out of bounds")
124+
precondition(i != startIndex, "Index out of bounds")
125125

126126
guard let idx =
127127
base[startIndex.base..<i.base]
@@ -175,8 +175,6 @@ extension Collection {
175175
extension CompactedSequence: LazySequenceProtocol
176176
where Base: LazySequenceProtocol {}
177177

178-
extension CompactedCollection: RandomAccessCollection
179-
where Base: RandomAccessCollection {}
180178
extension CompactedCollection: LazySequenceProtocol
181179
where Base: LazySequenceProtocol {}
182180
extension CompactedCollection: LazyCollectionProtocol

Tests/SwiftAlgorithmsTests/CompactedTests.swift

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class CompactedTests: XCTestCase {
3838

3939
func testCollectionTraversals() {
4040
for array in self.tests {
41-
validateIndexTraversals(array)
41+
validateIndexTraversals(array.compacted())
4242
}
4343
}
4444

@@ -55,14 +55,27 @@ final class CompactedTests: XCTestCase {
5555
}
5656

5757
func testCollectionHashableConformances() {
58-
for array in self.tests {
59-
let seq = array.eraseToAnyHashableSequence()
60-
XCTAssertEqualHashValue(
61-
seq.compacted(), seq.compactMap({ $0 }).compacted()
62-
)
63-
XCTAssertEqualHashValue(
64-
array.compacted(), array.compactMap({ $0 }).compacted()
65-
)
58+
for array1 in self.tests {
59+
for array2 in self.tests {
60+
// For non-equal Collections and Sequences that produce the same
61+
// compacted, the compacted wrapper should produce the same hash.
62+
// e.g. [1, 2, 3, nil, nil, 4].compacted() should produce the
63+
// same hash as [1, nil, 2, nil, 3, 4].compacted()
64+
guard !array1.elementsEqual(array2) &&
65+
array1.compacted() == array2.compacted() else {
66+
continue
67+
}
68+
69+
let seq = array1.eraseToAnyHashableSequence()
70+
let seq2 = array2.eraseToAnyHashableSequence()
71+
72+
XCTAssertEqualHashValue(
73+
seq.compacted(), seq2.compacted()
74+
)
75+
XCTAssertEqualHashValue(
76+
array1.compacted(), array2.compacted()
77+
)
78+
}
6679
}
6780
}
6881
}

Tests/SwiftAlgorithmsTests/TestUtilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func hash<T: Hashable>(_ value: T) -> Int {
190190
return hasher.finalize()
191191
}
192192

193-
/// Asserts two hashable value produce the same hash value.
193+
/// Asserts that two hashable instances produce the same hash value.
194194
func XCTAssertEqualHashValue<T: Hashable, U: Hashable>(
195195
_ expression1: @autoclosure () throws -> T,
196196
_ expression2: @autoclosure () throws -> U,

0 commit comments

Comments
 (0)