Skip to content

Commit f7d73f7

Browse files
[tests] Adding equatable and hashble tests
1 parent 5d2a24a commit f7d73f7

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

Tests/SwiftAlgorithmsTests/CompactedTests.swift

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@ final class CompactedTests: XCTestCase {
1919
.uniquePermutations(ofCount: 0...)
2020
.map(Array.init)
2121

22-
func testCompactedSequence() {
23-
for array in self.tests {
24-
let seq = AnySequence(array)
25-
XCTAssertEqualSequences(seq.compactMap({ $0 }), seq.compacted())
26-
}
27-
}
28-
29-
func testCompactedCollection() {
30-
for array in self.tests {
31-
XCTAssertEqualSequences(array.compactMap({ $0 }), array.compacted())
22+
func testCompactedCompacted() {
23+
for collection in self.tests {
24+
let seq = AnySequence(collection)
25+
XCTAssertEqualSequences(
26+
seq.compactMap({ $0 }), seq.compacted())
27+
XCTAssertEqualSequences(
28+
collection.compactMap({ $0 }), collection.compacted())
3229
}
3330
}
34-
31+
3532
func testCompactedBidirectionalCollection() {
3633
for array in self.tests {
3734
XCTAssertEqualSequences(array.compactMap({ $0 }).reversed(),
@@ -44,4 +41,28 @@ final class CompactedTests: XCTestCase {
4441
validateIndexTraversals(array)
4542
}
4643
}
44+
45+
func testCollectionEquatableConformances() {
46+
for array in self.tests {
47+
XCTAssertEqual(
48+
array.eraseToAnyHashableSequence().compacted(),
49+
array.compactMap({ $0 }).eraseToAnyHashableSequence().compacted()
50+
)
51+
XCTAssertEqual(
52+
array.compacted(), array.compactMap({ $0 }).compacted()
53+
)
54+
}
55+
}
56+
57+
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+
)
66+
}
67+
}
4768
}

Tests/SwiftAlgorithmsTests/TestUtilities.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ struct SplitMix64: RandomNumberGenerator {
4646
}
4747
}
4848

49+
struct AnyHashableSequence<Base>
50+
where Base: Sequence, Base: Hashable {
51+
var base: Base
52+
}
53+
54+
extension AnyHashableSequence: Hashable {}
55+
extension AnyHashableSequence: Sequence {
56+
typealias Iterator = Base.Iterator
57+
58+
func makeIterator() -> Iterator {
59+
base.makeIterator()
60+
}
61+
}
62+
63+
extension Sequence where Self: Hashable {
64+
func eraseToAnyHashableSequence() -> AnyHashableSequence<Self> {
65+
AnyHashableSequence(base: self)
66+
}
67+
}
68+
4969
// An eraser helper to any mutable collection
5070
struct AnyMutableCollection<Base> where Base: MutableCollection {
5171
var base: Base
@@ -163,6 +183,25 @@ func XCTAssertEqualCollections<C1: Collection, C2: Collection>(
163183
}
164184
}
165185

186+
func hash<T: Hashable>(_ value: T) -> Int {
187+
var hasher = Hasher()
188+
value.hash(into: &hasher)
189+
return hasher.finalize()
190+
}
191+
192+
/// Asserts two hashable value produce the same hash value.
193+
func XCTAssertEqualHashValue<T: Hashable, U: Hashable>(
194+
_ expression1: @autoclosure () throws -> T,
195+
_ expression2: @autoclosure () throws -> U,
196+
_ message: @autoclosure () -> String = "",
197+
file: StaticString = #file, line: UInt = #line
198+
) {
199+
XCTAssertEqual(
200+
hash(try expression1()), hash(try expression2()),
201+
message(), file: file, line: line
202+
)
203+
}
204+
166205
/// Tests that all index traversal methods behave as expected.
167206
///
168207
/// Verifies the correctness of the implementations of `startIndex`, `endIndex`,

0 commit comments

Comments
 (0)