mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-29 04:44:21 +00:00
Remove collection conformances to Equatable and Hashable (#124)
This commit is contained in:
1 parent
bcdbac800a
commit
abc5559ef6
14 files changed
+2
-209
No files matched your search
@@ -145,16 +145,4 @@ final class ChunkedTests: XCTestCase {
|
||||
validateIndexTraversals(chunks)
|
||||
}
|
||||
}
|
||||
|
||||
func testChunksOfCountHash() {
|
||||
let collection1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
let collection2 = [1, 2, 3, 4, 5]
|
||||
|
||||
XCTAssertEqualHashValue(
|
||||
collection1.chunks(ofCount: 1), collection1.chunks(ofCount: 1))
|
||||
XCTAssertNotEqualHashValue(
|
||||
collection1.chunks(ofCount: 1), collection2.chunks(ofCount: 1))
|
||||
XCTAssertNotEqualHashValue(
|
||||
collection1.chunks(ofCount: 2), collection2.chunks(ofCount: 4))
|
||||
}
|
||||
}
|
||||
@@ -41,41 +41,4 @@ final class CompactedTests: XCTestCase {
|
||||
validateIndexTraversals(array.compacted())
|
||||
}
|
||||
}
|
||||
|
||||
func testCollectionEquatableConformances() {
|
||||
for array in self.tests {
|
||||
XCTAssertEqual(
|
||||
array.eraseToAnyHashableSequence().compacted(),
|
||||
array.compactMap({ $0 }).eraseToAnyHashableSequence().compacted()
|
||||
)
|
||||
XCTAssertEqual(
|
||||
array.compacted(), array.compactMap({ $0 }).compacted()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func testCollectionHashableConformances() {
|
||||
for array1 in self.tests {
|
||||
for array2 in self.tests {
|
||||
// For non-equal Collections and Sequences that produce the same
|
||||
// compacted, the compacted wrapper should produce the same hash.
|
||||
// e.g. [1, 2, 3, nil, nil, 4].compacted() should produce the
|
||||
// same hash as [1, nil, 2, nil, 3, 4].compacted()
|
||||
guard !array1.elementsEqual(array2) &&
|
||||
array1.compacted() == array2.compacted() else {
|
||||
continue
|
||||
}
|
||||
|
||||
let seq = array1.eraseToAnyHashableSequence()
|
||||
let seq2 = array2.eraseToAnyHashableSequence()
|
||||
|
||||
XCTAssertEqualHashValue(
|
||||
seq.compacted(), seq2.compacted()
|
||||
)
|
||||
XCTAssertEqualHashValue(
|
||||
array1.compacted(), array2.compacted()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,19 +64,6 @@ final class StridingTests: XCTestCase {
|
||||
// a.index(after: a.endIndex) // Precondition failed: Advancing past end index
|
||||
}
|
||||
|
||||
func testStrideCompositionEquivalence() {
|
||||
let a = (0...10)
|
||||
XCTAssertEqualSequences(a.striding(by: 6), a.striding(by: 2).striding(by: 3))
|
||||
XCTAssertTrue(a.striding(by: 6) == a.striding(by: 2).striding(by: 3))
|
||||
XCTAssert(type(of: a.striding(by: 2).striding(by: 3)) == StrideCollection<ClosedRange<Int>>.self)
|
||||
}
|
||||
|
||||
func testEquality() {
|
||||
let a = [1, 2, 3, 4, 5].striding(by: 2)
|
||||
let b = [1, 0, 3, 0, 5].striding(by: 2)
|
||||
XCTAssertEqual(a, b)
|
||||
}
|
||||
|
||||
func testStrideLast() {
|
||||
XCTAssertEqual((1...10).striding(by: 2).last, 9) // 1, 3, 5, 7, 9
|
||||
XCTAssertEqual((1...10).striding(by: 3).last, 10) // 1, 4, 7, 10
|
||||
|
||||
@@ -46,27 +46,6 @@ struct SplitMix64: RandomNumberGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
// An eraser helper to any hashable sequence.
|
||||
struct AnyHashableSequence<Base>
|
||||
where Base: Sequence, Base: Hashable {
|
||||
var base: Base
|
||||
}
|
||||
|
||||
extension AnyHashableSequence: Hashable {}
|
||||
extension AnyHashableSequence: Sequence {
|
||||
typealias Iterator = Base.Iterator
|
||||
|
||||
func makeIterator() -> Iterator {
|
||||
base.makeIterator()
|
||||
}
|
||||
}
|
||||
|
||||
extension Sequence where Self: Hashable {
|
||||
func eraseToAnyHashableSequence() -> AnyHashableSequence<Self> {
|
||||
AnyHashableSequence(base: self)
|
||||
}
|
||||
}
|
||||
|
||||
// An eraser helper to any mutable collection
|
||||
struct AnyMutableCollection<Base> where Base: MutableCollection {
|
||||
var base: Base
|
||||
@@ -184,38 +163,6 @@ func XCTAssertEqualCollections<C1: Collection, C2: Collection>(
|
||||
}
|
||||
}
|
||||
|
||||
func hash<T: Hashable>(_ value: T) -> Int {
|
||||
var hasher = Hasher()
|
||||
value.hash(into: &hasher)
|
||||
return hasher.finalize()
|
||||
}
|
||||
|
||||
/// Asserts that two hashable instances produce the same hash value.
|
||||
func XCTAssertEqualHashValue<T: Hashable, U: Hashable>(
|
||||
_ expression1: @autoclosure () throws -> T,
|
||||
_ expression2: @autoclosure () throws -> U,
|
||||
_ message: @autoclosure () -> String = "",
|
||||
file: StaticString = #file, line: UInt = #line
|
||||
) {
|
||||
XCTAssertEqual(
|
||||
hash(try expression1()), hash(try expression2()),
|
||||
message(), file: file, line: line
|
||||
)
|
||||
}
|
||||
|
||||
/// Asserts that two hashable instances don't produce the same hash value.
|
||||
func XCTAssertNotEqualHashValue<T: Hashable, U: Hashable>(
|
||||
_ expression1: @autoclosure () throws -> T,
|
||||
_ expression2: @autoclosure () throws -> U,
|
||||
_ message: @autoclosure () -> String = "",
|
||||
file: StaticString = #file, line: UInt = #line
|
||||
) {
|
||||
XCTAssertNotEqual(
|
||||
hash(try expression1()), hash(try expression2()),
|
||||
message(), file: file, line: line
|
||||
)
|
||||
}
|
||||
|
||||
/// Tests that all index traversal methods behave as expected.
|
||||
///
|
||||
/// Verifies the correctness of the implementations of `startIndex`, `endIndex`,
|
||||
|
||||
Reference in new issue
Block a user