@@ -25,7 +25,10 @@ final class FlattenDistanceFromToTests {
2525 let tests = FlattenDistanceFromToTests ( )
2626 let suite = TestSuite ( " FlattenDistanceFromToTests " )
2727 suite. test ( " EachIndexPair " , tests. testEachIndexPair)
28- suite. test ( " MinMaxOutputs " , tests. testMinMaxOutputs)
28+ if #available( SwiftStdlib 6 . 0 , * ) {
29+ // The random access time complexity was fixed in Swift 6.0.
30+ suite. test ( " MinMaxRandomAccess " , tests. testMinMaxRandomAccess)
31+ }
2932 runAllTests ( )
3033 }
3134}
@@ -50,7 +53,7 @@ extension FlattenDistanceFromToTests {
5053 /// ─────────────────
5154 /// [][1][2,2][3,3,3]
5255 ///
53- func forEachLaneSizeCase(
56+ private func forEachLaneSizeCase(
5457 through limits: [ Int ] ,
5558 perform action: ( [ [ Int ] ] ) -> Void
5659 ) {
@@ -61,19 +64,18 @@ extension FlattenDistanceFromToTests {
6164
6265 if array [ index] . count < limits [ index] {
6366 array [ index] . append ( index)
64- continue
65- }
66-
67- while index < limits. endIndex, array [ index] . count == limits [ index] {
68- array. formIndex ( after: & index)
69- }
70-
71- if index < limits. endIndex {
72- array [ index] . append ( index)
67+ } else {
68+ while index < limits. endIndex, array [ index] . count == limits [ index] {
69+ array. formIndex ( after: & index)
70+ }
7371
74- while index > array. startIndex {
75- array. formIndex ( before: & index)
76- array [ index] . removeAll ( keepingCapacity: true )
72+ if index < limits. endIndex {
73+ array [ index] . append ( index)
74+
75+ while index > array. startIndex {
76+ array. formIndex ( before: & index)
77+ array [ index] . removeAll ( keepingCapacity: true )
78+ }
7779 }
7880 }
7981 }
@@ -88,10 +90,10 @@ extension FlattenDistanceFromToTests {
8890 /// offset: 2, index: 1,1
8991 /// offset: 3, index: 2
9092 ///
91- func forEachEnumeratedIndexIncludingEndIndex< T: Collection > (
93+ private func forEachEnumeratedIndexIncludingEndIndex< T> (
9294 in collection: T ,
9395 perform action: ( ( offset: Int , index: T . Index ) ) -> Void
94- ) {
96+ ) where T : Collection {
9597 var state = ( offset: 0 , index: collection. startIndex)
9698 while true {
9799 action ( state)
@@ -148,11 +150,15 @@ extension FlattenDistanceFromToTests {
148150
149151extension FlattenDistanceFromToTests {
150152
151- /// Checks some `Int.min` and `Int.max` distances.
153+ /// Checks some `Int.min` and `Int.max` distances with random access.
154+ ///
155+ /// It needs Swift 6.0+ because prior versions find the distance by
156+ /// iterating from one index to the other, which takes way too long.
152157 ///
153158 /// - Note: A distance of `Int.min` requires more than `Int.max` elements.
154159 ///
155- func testMinMaxOutputs( ) {
160+ @available ( SwiftStdlib 6 . 0 , * )
161+ func testMinMaxRandomAccess( ) {
156162 for s : FlattenSequence in [
157163
158164 [ - 1 ..< Int . max/ 1 ] ,
0 commit comments