|
| 1 | +// RUN: %target-run-simple-swift |
| 2 | +// REQUIRES: executable_test |
| 3 | + |
| 4 | +import StdlibUnittest |
| 5 | + |
| 6 | +let Tests = TestSuite(#file) |
| 7 | + |
| 8 | +protocol CovariantSelf { |
| 9 | + var id: Int { get } |
| 10 | + |
| 11 | + func result() -> Self |
| 12 | + func resultArray() -> Array<Self> |
| 13 | + func resultDictionary() -> [String : Self] |
| 14 | + func param(_: (Self) -> Void) |
| 15 | + func paramVariadic(_: (Self...) -> Void) |
| 16 | + |
| 17 | + var propResult: Self { get } |
| 18 | + var propResultArray: Array<Self> { get } |
| 19 | + var propResultDictionary: [String : Self] { get } |
| 20 | + var propParam: ((Self) -> Void) -> Void { get } |
| 21 | + var propParamVariadic: ((Self...) -> Void) -> Void { get } |
| 22 | + |
| 23 | + subscript(result _: Void) -> Self { get } |
| 24 | + subscript(resultArray _: Void) -> Array<Self> { get } |
| 25 | + subscript(resultDictionary _: Void) -> [String : Self] { get } |
| 26 | + subscript(param _: (Self) -> Void) -> Void { get } |
| 27 | + subscript(paramVariadic _: (Self...) -> Void) -> Void { get } |
| 28 | +} |
| 29 | +struct CovariantSelfImpl: CovariantSelf { |
| 30 | + let id: Int |
| 31 | + |
| 32 | + func result() -> Self { self } |
| 33 | + func resultArray() -> Array<Self> { [self] } |
| 34 | + func resultDictionary() -> [String : Self] { [#file : self] } |
| 35 | + func param(_ arg: (Self) -> Void) { arg(self) } |
| 36 | + func paramVariadic(_ arg: (Self...) -> Void) { arg(self) } |
| 37 | + |
| 38 | + var propResult: Self { self } |
| 39 | + var propResultArray: Array<Self> { [self] } |
| 40 | + var propResultDictionary: [String : Self] { [#file : self] } |
| 41 | + var propParam: ((Self) -> Void) -> Void { { $0(self) } } |
| 42 | + var propParamVariadic: ((Self...) -> Void) -> Void { { $0(self) } } |
| 43 | + |
| 44 | + subscript(result _: Void) -> Self { self } |
| 45 | + subscript(resultArray _: Void) -> Array<Self> { [self] } |
| 46 | + subscript(resultDictionary _: Void) -> [String : Self] { [#file : self] } |
| 47 | + subscript(param arg: (Self) -> Void) -> Void { arg(self) } |
| 48 | + subscript(paramVariadic arg: (Self...) -> Void) -> Void { arg(self) } |
| 49 | +} |
| 50 | +Tests.test("Covariant Self erasure") { |
| 51 | + let id = Int.random(in: Int.min...Int.max) |
| 52 | + let p: any CovariantSelf = CovariantSelfImpl(id: id) |
| 53 | + |
| 54 | + expectTrue(p is CovariantSelfImpl) |
| 55 | + |
| 56 | + // Partial applications |
| 57 | + do { |
| 58 | + let result = p.result |
| 59 | + let resultArray = p.resultArray |
| 60 | + let resultDictionary = p.resultDictionary |
| 61 | + let param = p.param |
| 62 | + let paramVariadic = p.paramVariadic |
| 63 | + |
| 64 | + expectEqual(id, result().id) |
| 65 | + expectEqual(id, resultArray().first.unsafelyUnwrapped.id) |
| 66 | + expectEqual(id, resultDictionary()[#file].unsafelyUnwrapped.id) |
| 67 | + param { |
| 68 | + expectEqual(id, $0.id) |
| 69 | + } |
| 70 | + paramVariadic { |
| 71 | + expectEqual(id, $0.first.unsafelyUnwrapped.id) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Unbound method references |
| 76 | + do { |
| 77 | + let result = (any CovariantSelf).result |
| 78 | + let resultArray = (any CovariantSelf).resultArray |
| 79 | + let resultDictionary = (any CovariantSelf).resultDictionary |
| 80 | + let param = (any CovariantSelf).param |
| 81 | + let paramVariadic = (any CovariantSelf).paramVariadic |
| 82 | + |
| 83 | + expectEqual(id, result(p)().id) |
| 84 | + expectEqual(id, resultArray(p)().first.unsafelyUnwrapped.id) |
| 85 | + expectEqual(id, resultDictionary(p)()[#file].unsafelyUnwrapped.id) |
| 86 | + param(p)( |
| 87 | + { expectEqual(id, $0.id) } |
| 88 | + ) |
| 89 | + paramVariadic(p)( |
| 90 | + { expectEqual(id, $0.first.unsafelyUnwrapped.id) } |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + // Instance member calls |
| 95 | + expectEqual(id, p.id) |
| 96 | + |
| 97 | + expectEqual(id, p.result().id) |
| 98 | + expectEqual(id, p.resultArray().first.unsafelyUnwrapped.id) |
| 99 | + expectEqual(id, p.resultDictionary()[#file].unsafelyUnwrapped.id) |
| 100 | + p.param { |
| 101 | + expectEqual(id, $0.id) |
| 102 | + } |
| 103 | + p.paramVariadic { |
| 104 | + expectEqual(id, $0.first.unsafelyUnwrapped.id) |
| 105 | + } |
| 106 | + |
| 107 | + expectEqual(id, p.propResult.id) |
| 108 | + expectEqual(id, p.propResultArray.first.unsafelyUnwrapped.id) |
| 109 | + expectEqual(id, p.propResultDictionary[#file].unsafelyUnwrapped.id) |
| 110 | + p.propParam { |
| 111 | + expectEqual(id, $0.id) |
| 112 | + } |
| 113 | + p.propParamVariadic { |
| 114 | + expectEqual(id, $0.first.unsafelyUnwrapped.id) |
| 115 | + } |
| 116 | + |
| 117 | + expectEqual(id, p[result: ()].id) |
| 118 | + expectEqual(id, p[resultArray: ()].first.unsafelyUnwrapped.id) |
| 119 | + expectEqual(id, p[resultDictionary: ()][#file].unsafelyUnwrapped.id) |
| 120 | + p[ |
| 121 | + param: { expectEqual(id, $0.id) } |
| 122 | + ] |
| 123 | + p[ |
| 124 | + paramVariadic: { expectEqual(id, $0.first.unsafelyUnwrapped.id) } |
| 125 | + ] |
| 126 | +} |
| 127 | + |
| 128 | +protocol CovariantAssoc { |
| 129 | + associatedtype Assoc: CovariantAssoc |
| 130 | + |
| 131 | + var id: Int { get } |
| 132 | + |
| 133 | + func result() -> Assoc |
| 134 | + func resultArray() -> Array<Assoc> |
| 135 | + func resultDictionary() -> [String : Assoc] |
| 136 | + func param(_: (Assoc) -> Void) |
| 137 | + func paramVariadic(_: (Assoc...) -> Void) |
| 138 | + |
| 139 | + var propResult: Assoc { get } |
| 140 | + var propResultArray: Array<Assoc> { get } |
| 141 | + var propResultDictionary: [String : Assoc] { get } |
| 142 | + var propParam: ((Assoc) -> Void) -> Void { get } |
| 143 | + var propParamVariadic: ((Assoc...) -> Void) -> Void { get } |
| 144 | + |
| 145 | + subscript(result _: Void) -> Assoc { get } |
| 146 | + subscript(resultArray _: Void) -> Array<Assoc> { get } |
| 147 | + subscript(resultDictionary _: Void) -> [String : Assoc] { get } |
| 148 | + subscript(param _: (Assoc) -> Void) -> Void { get } |
| 149 | + subscript(paramVariadic _: (Assoc...) -> Void) -> Void { get } |
| 150 | +} |
| 151 | +struct CovariantAssocImpl: CovariantAssoc { |
| 152 | + typealias Assoc = Self |
| 153 | + |
| 154 | + let id: Int |
| 155 | + |
| 156 | + func result() -> Assoc { self } |
| 157 | + func resultArray() -> Array<Assoc> { [self] } |
| 158 | + func resultDictionary() -> [String : Assoc] { [#file : self] } |
| 159 | + func param(_ arg: (Assoc) -> Void) { arg(self) } |
| 160 | + func paramVariadic(_ arg: (Assoc...) -> Void) { arg(self) } |
| 161 | + |
| 162 | + var propResult: Assoc { self } |
| 163 | + var propResultArray: Array<Assoc> { [self] } |
| 164 | + var propResultDictionary: [String : Assoc] { [#file : self] } |
| 165 | + var propParam: ((Assoc) -> Void) -> Void { { $0(self) } } |
| 166 | + var propParamVariadic: ((Assoc...) -> Void) -> Void { { $0(self) } } |
| 167 | + |
| 168 | + subscript(result _: Void) -> Assoc { self } |
| 169 | + subscript(resultArray _: Void) -> Array<Assoc> { [self] } |
| 170 | + subscript(resultDictionary _: Void) -> [String : Assoc] { [#file : self] } |
| 171 | + subscript(param arg: (Assoc) -> Void) -> Void { arg(self) } |
| 172 | + subscript(paramVariadic arg: (Assoc...) -> Void) -> Void { arg(self) } |
| 173 | +} |
| 174 | +Tests.test("Covariant associated type erasure") { |
| 175 | + let id = Int.random(in: Int.min...Int.max) |
| 176 | + let p: any CovariantAssoc = CovariantAssocImpl(id: id) |
| 177 | + |
| 178 | + expectTrue(p is CovariantAssocImpl) |
| 179 | + expectTrue(p.result() is CovariantAssocImpl) |
| 180 | + |
| 181 | + // Partial applications |
| 182 | + do { |
| 183 | + let result = p.result |
| 184 | + let resultArray = p.resultArray |
| 185 | + let resultDictionary = p.resultDictionary |
| 186 | + let param = p.param |
| 187 | + let paramVariadic = p.paramVariadic |
| 188 | + |
| 189 | + expectEqual(id, result().id) |
| 190 | + expectEqual(id, resultArray().first.unsafelyUnwrapped.id) |
| 191 | + expectEqual(id, resultDictionary()[#file].unsafelyUnwrapped.id) |
| 192 | + param { |
| 193 | + expectEqual(id, $0.id) |
| 194 | + } |
| 195 | + paramVariadic { |
| 196 | + expectEqual(id, $0.first.unsafelyUnwrapped.id) |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + // Unbound method references |
| 201 | + do { |
| 202 | + let result = (any CovariantAssoc).result |
| 203 | + let resultArray = (any CovariantAssoc).resultArray |
| 204 | + let resultDictionary = (any CovariantAssoc).resultDictionary |
| 205 | + let param = (any CovariantAssoc).param |
| 206 | + let paramVariadic = (any CovariantAssoc).paramVariadic |
| 207 | + |
| 208 | + expectEqual(id, result(p)().id) |
| 209 | + expectEqual(id, resultArray(p)().first.unsafelyUnwrapped.id) |
| 210 | + expectEqual(id, resultDictionary(p)()[#file].unsafelyUnwrapped.id) |
| 211 | + param(p)( |
| 212 | + { expectEqual(id, $0.id) } |
| 213 | + ) |
| 214 | + paramVariadic(p)( |
| 215 | + { expectEqual(id, $0.first.unsafelyUnwrapped.id) } |
| 216 | + ) |
| 217 | + } |
| 218 | + |
| 219 | + // Instance member calls |
| 220 | + expectEqual(id, p.id) |
| 221 | + |
| 222 | + expectEqual(id, p.result().id) |
| 223 | + expectEqual(id, p.resultArray().first.unsafelyUnwrapped.id) |
| 224 | + expectEqual(id, p.resultDictionary()[#file].unsafelyUnwrapped.id) |
| 225 | + p.param { |
| 226 | + expectEqual(id, $0.id) |
| 227 | + } |
| 228 | + p.paramVariadic { |
| 229 | + expectEqual(id, $0.first.unsafelyUnwrapped.id) |
| 230 | + } |
| 231 | + |
| 232 | + expectEqual(id, p.propResult.id) |
| 233 | + expectEqual(id, p.propResultArray.first.unsafelyUnwrapped.id) |
| 234 | + expectEqual(id, p.propResultDictionary[#file].unsafelyUnwrapped.id) |
| 235 | + p.propParam { |
| 236 | + expectEqual(id, $0.id) |
| 237 | + } |
| 238 | + p.propParamVariadic { |
| 239 | + expectEqual(id, $0.first.unsafelyUnwrapped.id) |
| 240 | + } |
| 241 | + |
| 242 | + expectEqual(id, p[result: ()].id) |
| 243 | + expectEqual(id, p[resultArray: ()].first.unsafelyUnwrapped.id) |
| 244 | + expectEqual(id, p[resultDictionary: ()][#file].unsafelyUnwrapped.id) |
| 245 | + p[ |
| 246 | + param: { expectEqual(id, $0.id) } |
| 247 | + ] |
| 248 | + p[ |
| 249 | + paramVariadic: { expectEqual(id, $0.first.unsafelyUnwrapped.id) } |
| 250 | + ] |
| 251 | +} |
| 252 | + |
| 253 | +func assertExactType<T>(of _: T, is _: T.Type) {} |
| 254 | + |
| 255 | +protocol CollectionOfBinaryInteger: Collection where Element: BinaryInteger {} |
| 256 | + |
| 257 | +extension Array: CollectionOfBinaryInteger where Element: BinaryInteger {} |
| 258 | + |
| 259 | +Tests.test("Collection of BinaryInteger") { |
| 260 | + let c: any CollectionOfBinaryInteger = [1, 2, 3, 4] |
| 261 | + |
| 262 | + expectEqual(4, c.count) |
| 263 | + expectFalse(c.isEmpty) |
| 264 | + expectTrue(c.startIndex is Int) |
| 265 | + expectTrue(c.first.unsafelyUnwrapped is Int) |
| 266 | + expectEqual( |
| 267 | + [0, 1, 0, 2], |
| 268 | + c.map { (elt: BinaryInteger) in elt.trailingZeroBitCount } |
| 269 | + ) |
| 270 | +} |
| 271 | + |
| 272 | +protocol RandomAccessCollectionOfInt: RandomAccessCollection |
| 273 | +where Element == Int, Index == Int, SubSequence == Array<Element>.SubSequence {} |
| 274 | + |
| 275 | +extension Array: RandomAccessCollectionOfInt where Element == Int {} |
| 276 | + |
| 277 | +Tests.test("RandomAccessCollection of Int") { |
| 278 | + let c: any RandomAccessCollectionOfInt = [5, 8, 1, 9, 3, 8] |
| 279 | + |
| 280 | + expectEqual(6, c.count) |
| 281 | + expectEqual(0, c.startIndex) |
| 282 | + expectEqual(6, c.endIndex) |
| 283 | + expectEqual(1, c.index(after: c.startIndex)) |
| 284 | + expectEqual(5, c.index(before: c.endIndex)) |
| 285 | + expectEqual(2, c.index(c.startIndex, offsetBy: 2)) |
| 286 | + expectEqual(6, c.distance(from: c.startIndex, to: c.endIndex)) |
| 287 | + expectEqual(5, c[0]) |
| 288 | + expectEqual(5, c.first.unsafelyUnwrapped) |
| 289 | + expectEqual(9, c.first(where: { $0 > 8 }).unsafelyUnwrapped) |
| 290 | + expectEqual(8, c.last.unsafelyUnwrapped) |
| 291 | + expectEqual([5, 8, 1, 9, 3], c.dropLast()) |
| 292 | + expectEqual([8, 3, 9, 1, 8, 5], c.reversed()) |
| 293 | + expectEqual(9, c.max().unsafelyUnwrapped) |
| 294 | + expectEqual([1, 3, 5, 8, 8, 9], c.sorted()) |
| 295 | + expectEqual([9, 8, 8, 5, 3, 1], c.sorted(by: >)) |
| 296 | + expectEqual([8, 9, 8], c.filter { $0 > 7 }) |
| 297 | + expectEqual([4, 7, 0, 8, 2, 7], c.map { $0 - 1 }) |
| 298 | + expectEqual(34, c.reduce(0, +)) |
| 299 | + expectEqual(5, c.lastIndex(of: 8).unsafelyUnwrapped) |
| 300 | + expectEqual([5], c[c.startIndex..<c.firstIndex(of: 8).unsafelyUnwrapped]) |
| 301 | + expectEqual([[5, 8], [9, 3, 8]], c.split(separator: 1)) |
| 302 | + expectNotNil( |
| 303 | + c.withContiguousStorageIfAvailable { buffer in |
| 304 | + expectEqual(5, buffer[0]) |
| 305 | + } |
| 306 | + ) |
| 307 | + expectFalse(c.contains(0)) |
| 308 | + expectFalse(c.contains { $0 > 10 }) |
| 309 | + expectFalse(c.allSatisfy { $0 > 1 }) |
| 310 | + |
| 311 | + do { |
| 312 | + var index = c.startIndex |
| 313 | + c.formIndex(after: &index) |
| 314 | + expectEqual(1, index) |
| 315 | + c.formIndex(before: &index) |
| 316 | + expectEqual(0, index) |
| 317 | + } |
| 318 | + |
| 319 | + do { |
| 320 | + let slice = c.dropFirst(3) |
| 321 | + assertExactType(of: slice, is: ArraySlice<Int>.self) |
| 322 | + expectEqual([9, 3, 8], slice) |
| 323 | + } |
| 324 | + do { |
| 325 | + let slice = c.dropLast(3) |
| 326 | + assertExactType(of: slice, is: ArraySlice<Int>.self) |
| 327 | + expectEqual([5, 8, 1], slice) |
| 328 | + } |
| 329 | + do { |
| 330 | + let slice = c.drop { $0 < 9 } |
| 331 | + assertExactType(of: slice, is: ArraySlice<Int>.self) |
| 332 | + expectEqual([9, 3, 8], slice) |
| 333 | + } |
| 334 | + do { |
| 335 | + let slice = c.suffix(3) |
| 336 | + assertExactType(of: slice, is: ArraySlice<Int>.self) |
| 337 | + expectEqual([9, 3, 8], slice) |
| 338 | + } |
| 339 | + do { |
| 340 | + let slice = c.prefix(3) |
| 341 | + assertExactType(of: slice, is: ArraySlice<Int>.self) |
| 342 | + expectEqual([5, 8, 1], slice) |
| 343 | + } |
| 344 | + do { |
| 345 | + let slice = c.prefix { $0 < 9 } |
| 346 | + assertExactType(of: slice, is: ArraySlice<Int>.self) |
| 347 | + expectEqual([5, 8, 1], slice) |
| 348 | + } |
| 349 | +} |
| 350 | + |
| 351 | +protocol RangeReplaceableMutableIntCollection: |
| 352 | + RangeReplaceableCollection, MutableCollection, BidirectionalCollection |
| 353 | +where Element == Int, Index == Int, SubSequence == Array<Element>.SubSequence {} |
| 354 | + |
| 355 | +extension Array: RangeReplaceableMutableIntCollection where Element == Int {} |
| 356 | + |
| 357 | +Tests.test("RangeReplaceableCollection & MutableCollection of Int") { |
| 358 | + var c: any RangeReplaceableMutableIntCollection = [5, 8, 1] |
| 359 | + |
| 360 | + c.append(2) |
| 361 | + expectEqual([5, 8, 1, 2], c[...]) |
| 362 | + expectEqual(2, c.popLast().unsafelyUnwrapped) |
| 363 | + c.swapAt(c.startIndex, c.index(before: c.endIndex)) |
| 364 | + expectEqual([1, 8, 5], c[...]) |
| 365 | + c.removeFirst() |
| 366 | + c.removeLast() |
| 367 | + expectEqual([8], c[...]) |
| 368 | + c.append(contentsOf: 4...7) |
| 369 | + expectEqual([8, 4, 5, 6, 7], c[...]) |
| 370 | + expectEqual(4, c.remove(at: 1)) |
| 371 | + c.replaceSubrange(2..<4, with: 1...3) |
| 372 | + expectEqual([8, 5, 1, 2, 3], c[...]) |
| 373 | + c.removeSubrange(0..<2) |
| 374 | + expectEqual([1, 2, 3], c[...]) |
| 375 | +} |
| 376 | + |
| 377 | +runAllTests() |
0 commit comments