Skip to content

Commit 05491db

Browse files
[tests] [Compacted] Adding tests for compacted()
1 parent d0d9ed5 commit 05491db

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Algorithms open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import XCTest
13+
import Algorithms
14+
15+
final class CompactedTests: XCTestCase {
16+
17+
let tests: [[Int?]] =
18+
[nil, nil, nil, 0, 1, 2]
19+
.uniquePermutations(ofCount: 0...)
20+
.map(Array.init)
21+
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())
29+
}
30+
}
31+
32+
func testCompactedBidirectionalCollection() {
33+
for array in self.tests {
34+
XCTAssertEqualSequences(array.compactMap({ $0 }).reversed(),
35+
array.compacted().reversed())
36+
}
37+
}
38+
39+
func testCollectionTraversals() {
40+
for array in self.tests {
41+
validateIndexTraversals(array)
42+
}
43+
}
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+
}
68+
}

Tests/SwiftAlgorithmsTests/TestUtilities.swift

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

49+
// An eraser helper to any hashable sequence.
50+
struct AnyHashableSequence<Base>
51+
where Base: Sequence, Base: Hashable {
52+
var base: Base
53+
}
54+
55+
extension AnyHashableSequence: Hashable {}
56+
extension AnyHashableSequence: Sequence {
57+
typealias Iterator = Base.Iterator
58+
59+
func makeIterator() -> Iterator {
60+
base.makeIterator()
61+
}
62+
}
63+
64+
extension Sequence where Self: Hashable {
65+
func eraseToAnyHashableSequence() -> AnyHashableSequence<Self> {
66+
AnyHashableSequence(base: self)
67+
}
68+
}
69+
4970
// An eraser helper to any mutable collection
5071
struct AnyMutableCollection<Base> where Base: MutableCollection {
5172
var base: Base
@@ -163,6 +184,25 @@ func XCTAssertEqualCollections<C1: Collection, C2: Collection>(
163184
}
164185
}
165186

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

0 commit comments

Comments
 (0)