Skip to content

Commit 1f2c865

Browse files
committed
Merge pull request #860 from kballard/test-collectiontype-lazy-first-perf
[Stdlib] Add a test for the performance of CollectionType.first
2 parents b47adb0 + 8cdaf4a commit 1f2c865

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

test/1_stdlib/Collection.swift

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
// RUN: %target-run-simple-swift | FileCheck %s
1+
// RUN: %target-run-simple-swift --stdlib-unittest-in-process | tee %t.txt
2+
// RUN: FileCheck %s < %t.txt
3+
// note: remove the --stdlib-unittest-in-process once all the FileCheck tests
4+
// have been converted to StdlibUnittest
25
// REQUIRES: executable_test
36

7+
import StdlibUnittest
8+
9+
var CollectionTests = TestSuite("CollectionTests")
10+
411
struct X : CollectionType {
512
typealias Element = String.CharacterView.Generator.Element
613
typealias Index = String.Index
@@ -202,5 +209,38 @@ func testIsEmptyFirstLast() {
202209
}
203210
testIsEmptyFirstLast()
204211

212+
/// A `CollectionType` that vends just the default implementations for
213+
/// `CollectionType` methods.
214+
struct CollectionOnly<T: CollectionType> : CollectionType {
215+
var base: T
216+
217+
var startIndex: T.Index {
218+
return base.startIndex
219+
}
220+
221+
var endIndex: T.Index {
222+
return base.endIndex
223+
}
224+
225+
func generate() -> T.Generator {
226+
return base.generate()
227+
}
228+
229+
subscript(position: T.Index) -> T.Generator.Element {
230+
return base[position]
231+
}
232+
}
233+
205234
// CHECK: all done.
206235
print("all done.")
236+
237+
CollectionTests.test("first/performance") {
238+
// accessing `first` should not perform duplicate work on lazy collections
239+
var log: [Int] = []
240+
let col_ = (0..<10).lazy.filter({ log.append($0); return (2..<8).contains($0) })
241+
let col = CollectionOnly(base: col_)
242+
expectEqual(2, col.first)
243+
expectEqual([0, 1, 2], log)
244+
}
245+
246+
runAllTests()

0 commit comments

Comments
 (0)