Files
swift-mirror/test/1_stdlib/Lazy.swift.gyb
2015-05-07 00:30:28 +00:00

109 lines
3.5 KiB
Plaintext

//===--- Lazy.swift - Tests for LazySequence and LazyCollection -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %S/../../utils/gyb %s -o %t/Lazy.swift
// RUN: %S/../../utils/line-directive %t/Lazy.swift -- %target-build-swift %t/Lazy.swift -o %t/a.out_Debug
// RUN: %S/../../utils/line-directive %t/Lazy.swift -- %target-build-swift %t/Lazy.swift -o %t/a.out_Release -O
//
// RUN: %S/../../utils/line-directive %t/Lazy.swift -- %target-run %t/a.out_Debug
// RUN: %S/../../utils/line-directive %t/Lazy.swift -- %target-run %t/a.out_Release
import StdlibUnittest
var LazyTestSuite = TestSuite("Lazy")
LazyTestSuite.test("isEmpty") {
expectTrue(lazy(0..<0).isEmpty)
expectFalse(lazy(0...0).isEmpty)
}
LazyTestSuite.test("firstLast") {
expectOptionalEqual(7, lazy(7..<42).first)
expectOptionalEqual(41, lazy(7..<42).last)
expectEmpty(lazy(7..<7).first)
expectEmpty(lazy(7..<7).last)
}
//===----------------------------------------------------------------------===//
// LazySequence
//===----------------------------------------------------------------------===//
LazyTestSuite.test("LazySequence<Sequence>/underestimateCount") {
let s = MinimalSequence(
[ 0, 30, 10, 90 ]._prext_map { OpaqueValue($0) },
underestimatedCount: .Value(42))
var lazySeq = lazy(s)
expectType(LazySequence<MinimalSequence<OpaqueValue<Int>>>.self, &lazySeq)
expectEqual(42, lazySeq.underestimateCount())
}
%for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ]:
LazyTestSuite.test("LazySequence<${traversal}Collection>/underestimateCount") {
let s = Minimal${traversal}Collection(
[ 0, 30, 10, 90 ]._prext_map { OpaqueValue($0) },
underestimatedCount: .Value(42))
var lazySeq = lazy(s)
expectType(
Lazy${traversal}Collection<
Minimal${traversal}Collection<OpaqueValue<Int>>
>.self,
&lazySeq)
expectEqual(42, lazySeq.underestimateCount())
}
%end
//===----------------------------------------------------------------------===//
// MapSequenceView
//===----------------------------------------------------------------------===//
LazyTestSuite.test("MapSequenceView<Sequence>/underestimateCount") {
let s = MinimalSequence(
[ 0, 30, 10, 90 ]._prext_map { OpaqueValue($0) },
underestimatedCount: .Value(42))
var lazyMap = lazy(s).map { OpaqueValue(Int32($0.value)) }
expectType(
LazySequence<
MapSequenceView<
MinimalSequence<OpaqueValue<Int>>,
OpaqueValue<Int32>
>
>.self,
&lazyMap)
expectEqual(42, lazyMap.underestimateCount())
}
%for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ]:
LazyTestSuite.test("MapCollectionView<${traversal}Collection>/underestimateCount") {
let s = Minimal${traversal}Collection(
[ 0, 30, 10, 90 ]._prext_map { OpaqueValue($0) },
underestimatedCount: .Value(42))
var lazyMap = lazy(s).map { OpaqueValue(Int32($0.value)) }
expectType(
Lazy${traversal}Collection<
MapCollectionView<
Minimal${traversal}Collection<OpaqueValue<Int>>,
OpaqueValue<Int32>
>
>.self,
&lazyMap)
expectEqual(42, lazyMap.underestimateCount())
}
%end
runAllTests()