1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
// 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
// REQUIRES: executable_test
import StdlibUnittest
import StdlibCollectionUnittest
var CollectionTests = TestSuite("CollectionTests")
/// An *iterator* that adapts a *collection* `C` and any *sequence* of
/// its `Index` type to present the collection's elements in a
/// permuted order.
public struct PermutationGenerator<
C: Collection, Indices: Sequence
> : IteratorProtocol, Sequence where Indices.Element == C.Index {
var seq : C
var indices : Indices.Iterator
/// The type of element returned by `next()`.
public typealias Element = C.Element
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
///
/// - Precondition: No preceding call to `self.next()` has returned `nil`.
public mutating func next() -> Element? {
let result = indices.next()
return result != nil ? seq[result!] : .none
}
/// Construct an *iterator* over a permutation of `elements` given
/// by `indices`.
///
/// - Precondition: `elements[i]` is valid for every `i` in `indices`.
public init(elements: C, indices: Indices) {
self.seq = elements
self.indices = indices.makeIterator()
}
}
var foobar = MinimalCollection(elements: "foobar")
// CHECK: foobar
for a in foobar {
print(a, terminator: "")
}
print("")
// FIXME: separate r from the expression below pending
// <rdar://problem/15772601> Type checking failure
// CHECK: raboof
let i = foobar.indices
let r = i.lazy.reversed()
for a in PermutationGenerator(elements: foobar, indices: r) {
print(a, terminator: "")
}
print("")
func isPalindrome0<S: BidirectionalCollection>(_ seq: S) -> Bool
where S.Element : Equatable {
typealias Index = S.Index
let a = seq.indices
let i = seq.indices
let ir = i.lazy.reversed()
var b = ir.makeIterator()
for i in a {
if seq[i] != seq[b.next()!] {
return false
}
}
return true
}
// CHECK: false
print(isPalindrome0(MinimalBidirectionalCollection(elements: "GoHangaSalamiImaLasagneHoG")))
// CHECK: true
print(isPalindrome0(MinimalBidirectionalCollection(elements: "GoHangaSalamiimalaSagnaHoG")))
func isPalindrome1<
S : BidirectionalCollection
>(_ seq: S) -> Bool
where S.Element : Equatable {
let a = PermutationGenerator(elements: seq, indices: seq.indices)
var b = seq.lazy.reversed().makeIterator()
for nextChar in a {
if nextChar != b.next()! {
return false
}
}
return true
}
func isPalindrome1_5<S: BidirectionalCollection>(_ seq: S) -> Bool
where S.Element: Equatable {
var b = seq.lazy.reversed().makeIterator()
for nextChar in seq {
if nextChar != b.next()! {
return false
}
}
return true
}
// CHECK: false
print(isPalindrome1(MinimalBidirectionalCollection(elements: "MADAMINEDENIMWILLIAM")))
// CHECK: true
print(isPalindrome1(MinimalBidirectionalCollection(elements: "MadamInEdEnImadaM")))
// CHECK: false
print(isPalindrome1_5(MinimalBidirectionalCollection(elements: "FleetoMeRemoteelF")))
// CHECK: true
print(isPalindrome1_5(MinimalBidirectionalCollection(elements: "FleetoMeReMoteelF")))
// Finally, one that actually uses indexing to do half as much work.
// BidirectionalCollection traversal finally pays off!
func isPalindrome2<
S: BidirectionalCollection
>(_ seq: S) -> Bool
where
S.Element: Equatable {
var b = seq.startIndex, e = seq.endIndex
while (b != e) {
e = seq.index(before: e)
if (b == e) {
break
}
if seq[b] != seq[e] {
return false
}
b = seq.index(after: b)
}
return true
}
// Test even length
// CHECK: false
print(isPalindrome2(MinimalBidirectionalCollection(elements: "ZerimarRamireZ")))
// CHECK: true
print(isPalindrome2(MinimalBidirectionalCollection(elements: "ZerimaRRamireZ")))
// Test odd length
// CHECK: false
print(isPalindrome2(MinimalBidirectionalCollection(elements: "ZerimarORamireZ")))
// CHECK: true
print(isPalindrome2(MinimalBidirectionalCollection(elements: "Zerimar-O-ramireZ")))
func isPalindrome4<
S: BidirectionalCollection
>(_ seq: S) -> Bool
where
S.Element : Equatable {
typealias Index = S.Index
let a = PermutationGenerator(elements: seq, indices: seq.indices)
// FIXME: separate ri from the expression below pending
// <rdar://problem/15772601> Type checking failure
let i = seq.indices
let ri = i.lazy.reversed()
var b = PermutationGenerator(elements: seq, indices: ri)
for nextChar in a {
if nextChar != b.next()! {
return false
}
}
return true
}
// Can't put these literals into string interpolations pending
// <rdar://problem/16401145> hella-slow compilation
let array = [1, 2, 3, 4]
let dict = [0:0, 1:1, 2:2, 3:3, 4:4]
func testCount() {
// CHECK: testing count
print("testing count")
// CHECK-NEXT: random access: 4
print("random access: \(array.count)")
// CHECK-NEXT: bidirectional: 5
print("bidirectional: \(dict.count)")
}
testCount()
struct SequenceOnly<T : Sequence> : Sequence {
var base: T
func makeIterator() -> T.Iterator { return base.makeIterator() }
}
func testUnderestimatedCount() {
// CHECK: testing underestimatedCount
print("testing underestimatedCount")
// CHECK-NEXT: random access: 4
print("random access: \(array.underestimatedCount)")
// CHECK-NEXT: bidirectional: 5
print("bidirectional: \(dict.underestimatedCount)")
// CHECK-NEXT: Sequence only: 0
let s = SequenceOnly(base: array)
print("Sequence only: \(s.underestimatedCount)")
}
testUnderestimatedCount()
CollectionTests.test("isEmptyFirstLast") {
expectTrue((10..<10).isEmpty)
expectFalse((10...10).isEmpty)
expectEqual(10, (10..<100).first)
expectEqual(10, (10...100).first)
expectEqual(99, (10..<100).last)
expectEqual(100, (10...100).last)
}
/// A `Collection` that vends just the default implementations for
/// `CollectionType` methods.
struct CollectionOnly<T: Collection> : Collection {
var base: T
var startIndex: T.Index {
return base.startIndex
}
var endIndex: T.Index {
return base.endIndex
}
func makeIterator() -> T.Iterator {
return base.makeIterator()
}
subscript(position: T.Index) -> T.Element {
return base[position]
}
func index(after i: T.Index) -> T.Index { return base.index(after: i) }
}
// 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()
|