|
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 |
2 | 5 | // REQUIRES: executable_test
|
3 | 6 |
|
| 7 | +import StdlibUnittest |
| 8 | + |
| 9 | +var CollectionTests = TestSuite("CollectionTests") |
| 10 | + |
4 | 11 | struct X : CollectionType {
|
5 | 12 | typealias Element = String.CharacterView.Generator.Element
|
6 | 13 | typealias Index = String.Index
|
@@ -202,5 +209,38 @@ func testIsEmptyFirstLast() {
|
202 | 209 | }
|
203 | 210 | testIsEmptyFirstLast()
|
204 | 211 |
|
| 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 | + |
205 | 234 | // CHECK: all done.
|
206 | 235 | 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