Skip to content

[Stdlib] Add a test for the performance of CollectionType.first #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion test/1_stdlib/Collection.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// RUN: %target-run-simple-swift | FileCheck %s
// RUN: %target-run-simple-swift --stdlib-unittest-in-process | tee %t.txt
// RUN: FileCheck %s < %t.txt
// note: remove the --stdlib-unittest-in-process once all the FileCheck tests
// have been converted to StdlibUnittest
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--stdlib-unittest-in-process is done because otherwise it repeats all of the non-StdlibUnittest tests in the child process, which causes duplicate output.

// REQUIRES: executable_test

import StdlibUnittest

var CollectionTests = TestSuite("CollectionTests")

struct X : CollectionType {
typealias Element = String.CharacterView.Generator.Element
typealias Index = String.Index
Expand Down Expand Up @@ -202,5 +209,38 @@ func testIsEmptyFirstLast() {
}
testIsEmptyFirstLast()

/// A `CollectionType` that vends just the default implementations for
/// `CollectionType` methods.
struct CollectionOnly<T: CollectionType> : CollectionType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is Defaulted${traversal}Collection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

var base: T

var startIndex: T.Index {
return base.startIndex
}

var endIndex: T.Index {
return base.endIndex
}

func generate() -> T.Generator {
return base.generate()
}

subscript(position: T.Index) -> T.Generator.Element {
return base[position]
}
}

// CHECK: all done.
print("all done.")

CollectionTests.test("first/performance") {
// accessing `first` should not perform duplicate work on lazy collections
var log: [Int] = []
let col_ = (0..<10).lazy.filter({ log.append($0); return (2..<8).contains($0) })
let col = CollectionOnly(base: col_)
expectEqual(2, col.first)
expectEqual([0, 1, 2], log)
}

runAllTests()