Skip to content

Commit 3426dba

Browse files
authored
[test] checkCollection: Don’t pass decreasing indices to distance(from:to:) (apple#63)
Also, try running the conformance checkers on the minimal collection types to catch similar issues.
1 parent fa45de1 commit 3426dba

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/swift-collections-Package.xcscheme

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
<BuildActionEntry
3838
buildForTesting = "YES"
3939
buildForRunning = "YES"
40-
buildForProfiling = "NO"
41-
buildForArchiving = "NO"
40+
buildForProfiling = "YES"
41+
buildForArchiving = "YES"
4242
buildForAnalyzing = "YES">
4343
<BuildableReference
4444
BuildableIdentifier = "primary"
45-
BlueprintIdentifier = "DequeTests"
46-
BuildableName = "DequeTests"
47-
BlueprintName = "DequeTests"
45+
BlueprintIdentifier = "OrderedCollections"
46+
BuildableName = "OrderedCollections"
47+
BlueprintName = "OrderedCollections"
4848
ReferencedContainer = "container:">
4949
</BuildableReference>
5050
</BuildActionEntry>
@@ -56,9 +56,9 @@
5656
buildForAnalyzing = "YES">
5757
<BuildableReference
5858
BuildableIdentifier = "primary"
59-
BlueprintIdentifier = "CollectionsTestSupport"
60-
BuildableName = "CollectionsTestSupport"
61-
BlueprintName = "CollectionsTestSupport"
59+
BlueprintIdentifier = "swift-collections-benchmark"
60+
BuildableName = "swift-collections-benchmark"
61+
BlueprintName = "swift-collections-benchmark"
6262
ReferencedContainer = "container:">
6363
</BuildableReference>
6464
</BuildActionEntry>
@@ -70,9 +70,9 @@
7070
buildForAnalyzing = "YES">
7171
<BuildableReference
7272
BuildableIdentifier = "primary"
73-
BlueprintIdentifier = "swift-collections-benchmark"
74-
BuildableName = "swift-collections-benchmark"
75-
BlueprintName = "swift-collections-benchmark"
73+
BlueprintIdentifier = "CollectionsTestSupport"
74+
BuildableName = "CollectionsTestSupport"
75+
BlueprintName = "CollectionsTestSupport"
7676
ReferencedContainer = "container:">
7777
</BuildableReference>
7878
</BuildActionEntry>
@@ -106,15 +106,29 @@
106106
</BuildActionEntry>
107107
<BuildActionEntry
108108
buildForTesting = "YES"
109-
buildForRunning = "YES"
110-
buildForProfiling = "YES"
111-
buildForArchiving = "YES"
112-
buildForAnalyzing = "YES">
109+
buildForRunning = "NO"
110+
buildForProfiling = "NO"
111+
buildForArchiving = "NO"
112+
buildForAnalyzing = "NO">
113113
<BuildableReference
114114
BuildableIdentifier = "primary"
115-
BlueprintIdentifier = "OrderedCollections"
116-
BuildableName = "OrderedCollections"
117-
BlueprintName = "OrderedCollections"
115+
BlueprintIdentifier = "DequeTests"
116+
BuildableName = "DequeTests"
117+
BlueprintName = "DequeTests"
118+
ReferencedContainer = "container:">
119+
</BuildableReference>
120+
</BuildActionEntry>
121+
<BuildActionEntry
122+
buildForTesting = "YES"
123+
buildForRunning = "NO"
124+
buildForProfiling = "NO"
125+
buildForArchiving = "NO"
126+
buildForAnalyzing = "NO">
127+
<BuildableReference
128+
BuildableIdentifier = "primary"
129+
BlueprintIdentifier = "CollectionsTestSupportTests"
130+
BuildableName = "CollectionsTestSupportTests"
131+
BlueprintName = "CollectionsTestSupportTests"
118132
ReferencedContainer = "container:">
119133
</BuildableReference>
120134
</BuildActionEntry>
@@ -146,6 +160,16 @@
146160
ReferencedContainer = "container:">
147161
</BuildableReference>
148162
</TestableReference>
163+
<TestableReference
164+
skipped = "NO">
165+
<BuildableReference
166+
BuildableIdentifier = "primary"
167+
BlueprintIdentifier = "CollectionsTestSupportTests"
168+
BuildableName = "CollectionsTestSupportTests"
169+
BlueprintName = "CollectionsTestSupportTests"
170+
ReferencedContainer = "container:">
171+
</BuildableReference>
172+
</TestableReference>
149173
</Testables>
150174
</TestAction>
151175
<LaunchAction

Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ let package = Package(
8181
.when(platforms: [.macOS, .iOS, .watchOS, .tvOS])),
8282
]
8383
),
84+
.testTarget(
85+
name: "CollectionsTestSupportTests",
86+
dependencies: ["CollectionsTestSupport"],
87+
swiftSettings: settings),
8488

8589
// Benchmarking
8690
.target(

Sources/CollectionsTestSupport/ConformanceCheckers/CheckCollection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public func _checkCollection<C: Collection, Expected: Sequence>(
203203

204204
// Check `distance(from:to:)`
205205
withEvery("i", in: allIndices.indices) { i in
206-
withEvery("j", in: allIndices.indices) { j in
206+
withEvery("j", in: allIndices.indices[i...]) { j in
207207
let d = collection.distance(from: allIndices[i], to: allIndices[j])
208208
expectEqual(d, j - i)
209209
}

Sources/CollectionsTestSupport/MinimalTypes/MinimalSequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum UnderestimatedCountBehavior {
4343
/// A Sequence that implements the protocol contract in the most
4444
/// narrow way possible.
4545
///
46-
/// This sequence is consumed when its generator is advanced.
46+
/// This sequence is consumed when its iterator is advanced.
4747
public struct MinimalSequence<T>: Sequence, CustomDebugStringConvertible {
4848
public let timesMakeIteratorCalled = ResettableValue(0)
4949

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Collections 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 CollectionsTestSupport
14+
15+
final class DequeTests: CollectionTestCase {
16+
func testMinimalSequence() {
17+
withEvery(
18+
"behavior",
19+
in: [
20+
UnderestimatedCountBehavior.precise,
21+
UnderestimatedCountBehavior.half,
22+
UnderestimatedCountBehavior.value(0)
23+
]
24+
) { behavior in
25+
withEvery("isContiguous", in: [false, true]) { isContiguous in
26+
func make() -> MinimalSequence<Int> {
27+
MinimalSequence(
28+
elements: 0 ..< 50,
29+
underestimatedCount: behavior,
30+
isContiguous: isContiguous)
31+
}
32+
checkSequence(make, expectedContents: 0 ..< 50)
33+
}
34+
}
35+
}
36+
37+
func testMinimalCollection() {
38+
checkCollection(MinimalCollection(0 ..< 50), expectedContents: 0 ..< 50)
39+
}
40+
41+
func testMinimalBidirectionalCollection() {
42+
checkBidirectionalCollection(MinimalBidirectionalCollection(0 ..< 50), expectedContents: 0 ..< 50)
43+
}
44+
}

0 commit comments

Comments
 (0)