16
16
// RUN: %target-clang %S/Inputs/ArrayBridge/ArrayBridge.m -c -o %t/ArrayBridgeObjC.o -g
17
17
// RUN: %target-build-swift %s -I %S/Inputs/ArrayBridge/ -Xlinker %t/ArrayBridgeObjC.o -o %t/ArrayBridge
18
18
19
- // RUN: %target-run %t/ArrayBridge > %t.txt
20
- // RUN: FileCheck %s < %t.txt
19
+ // RUN: %target-run %t/ArrayBridge
21
20
// REQUIRES: executable_test
22
21
// REQUIRES: objc_interop
23
22
@@ -58,15 +57,6 @@ class Base : NSObject, Fooable {
58
57
serialNumber = - serialNumber
59
58
}
60
59
61
- override var description : String {
62
- assert ( serialNumber > 0 , " dead Base! " )
63
- return " Base# \( serialNumber) ( \( value) ) "
64
- }
65
-
66
- func successor( ) -> Self {
67
- return self . dynamicType. init ( self . value + 1 )
68
- }
69
-
70
60
override func isEqual( _ other: AnyObject ? ) -> Bool {
71
61
return ( other as? Base ) ? . value == self . value
72
62
}
@@ -77,17 +67,13 @@ class Base : NSObject, Fooable {
77
67
78
68
class Subclass : Base , Barable {
79
69
func bar( ) { }
80
- override var description : String {
81
- assert ( serialNumber > 0 , " dead Base! " )
82
- return " Subclass# \( serialNumber) ( \( value) ) "
83
- }
84
70
}
85
71
86
72
var bridgeFromOperationCount = 0
87
73
var bridgeToOperationCount = 0
88
74
89
75
/// A value type that's bridged using _ObjectiveCBridgeable
90
- struct BridgeableValue : CustomStringConvertible , _ObjectiveCBridgeable {
76
+ struct BridgeableValue : _ObjectiveCBridgeable , Equatable {
91
77
func _bridgeToObjectiveC( ) -> Subclass {
92
78
bridgeToOperationCount += 1
93
79
return Subclass ( trak. value)
@@ -126,19 +112,10 @@ struct BridgeableValue : CustomStringConvertible, _ObjectiveCBridgeable {
126
112
return result!
127
113
}
128
114
129
- var description : String {
130
- assert ( trak. serialNumber > 0 , " dead Base! " )
131
- return " BridgeableValue# \( trak. serialNumber) ( \( trak. value) ) "
132
- }
133
-
134
115
init ( _ value: Int ) {
135
116
self . trak = Base ( value)
136
117
}
137
118
138
- func successor( ) -> BridgeableValue {
139
- return BridgeableValue ( trak. value + 1 )
140
- }
141
-
142
119
static func resetStats( ) {
143
120
bridgeFromOperationCount = 0
144
121
bridgeToOperationCount = 0
@@ -147,6 +124,10 @@ struct BridgeableValue : CustomStringConvertible, _ObjectiveCBridgeable {
147
124
var trak : Base
148
125
}
149
126
127
+ func == ( lhs: BridgeableValue , rhs: BridgeableValue ) -> Bool {
128
+ return lhs. trak. value == rhs. trak. value
129
+ }
130
+
150
131
// A class used to test various Objective-C thunks.
151
132
class Thunks : NSObject {
152
133
func createSubclass( _ value: Int ) -> AnyObject {
@@ -157,10 +138,10 @@ class Thunks : NSObject {
157
138
_ x: [ Subclass ] ,
158
139
expecting expected: NSArray
159
140
) {
160
- expectEqual ( x . count , expected . count )
161
- for i in 0 ..< x . count {
162
- expectTrue ( x [ i ] === expected [ i ] )
163
- }
141
+ expectEqualSequence (
142
+ expected . lazy . map { ObjectIdentifier ( $0 ) } ,
143
+ x . lazy . map { ObjectIdentifier ( $0 ) }
144
+ )
164
145
}
165
146
166
147
@objc func produceSubclassArray(
@@ -177,24 +158,30 @@ class Thunks : NSObject {
177
158
@objc func checkProducedSubclassArray(
178
159
_ produced: NSArray , expecting expected: NSArray
179
160
) {
180
- expectEqual ( produced . count , expected . count )
181
- for i in 0 ..< produced . count {
182
- expectTrue ( produced [ i ] === expected [ i ] )
183
- }
161
+ expectEqualSequence (
162
+ expected . lazy . map { ObjectIdentifier ( $0 ) } ,
163
+ produced. lazy . map { ObjectIdentifier ( $0 ) }
164
+ )
184
165
}
185
166
186
167
@objc func acceptBridgeableValueArray( _ raw: NSArray ) -> AnyObject {
187
168
let x = raw as! [ BridgeableValue ]
188
169
return Box ( x)
189
170
}
190
171
191
- @objc func produceBridgeableValueArray( _ numItems : Int ) -> NSArray {
172
+ @objc func produceBridgeableValueArray( ) -> NSArray {
192
173
var array : [ BridgeableValue ] = [ ]
193
- for i in 0 ..< numItems {
174
+ for i in 0 ..< 5 {
194
175
array. append ( BridgeableValue ( i) )
195
176
}
196
177
return array as NSArray
197
178
}
179
+
180
+ @objc func checkProducedBridgeableValueArray( _ produced: NSArray ) {
181
+ expectEqualSequence (
182
+ 0 ..< 5 ,
183
+ produced. lazy. map { ( $0 as! Subclass ) . value } )
184
+ }
198
185
}
199
186
200
187
@@ -209,9 +196,9 @@ tests.test("testBridgedVerbatim") {
209
196
//===--- Implicit conversion to/from NSArray ------------------------------===//
210
197
211
198
let basesConvertedToNSArray = bases as NSArray
212
- expectEqual (
213
- " Base#1 (100) " ,
214
- String ( basesConvertedToNSArray . object ( at : 0 ) as! Base ) )
199
+ let firstBase = basesConvertedToNSArray . object ( at : 0 ) as! Base
200
+ expectEqual ( 100 , firstBase . value )
201
+ expectEqual ( 1 , firstBase . serialNumber )
215
202
216
203
// Create an ordinary NSArray, not a native one
217
204
let nsArrayOfBase : NSArray = NSArray ( object: Base ( 42 ) )
@@ -295,175 +282,116 @@ tests.test("doTestSubclass") {
295
282
//===--- Explicitly Bridged -----------------------------------------------===//
296
283
// BridgeableValue conforms to _ObjectiveCBridgeable
297
284
//===----------------------------------------------------------------------===//
298
- func testExplicitlyBridged( ) {
299
- // CHECK-LABEL: testExplicitlyBridged()
300
- print ( " testExplicitlyBridged() " )
301
-
285
+ tests. test ( " testExplicitlyBridged " ) {
286
+
302
287
let bridgeableValues = [ BridgeableValue ( 42 ) , BridgeableValue ( 17 ) ]
303
288
304
289
let bridgeableValuesAsNSArray = bridgeableValues as NSArray
305
- // CHECK-NEXT: [Subclass#{{[0-9]+}}(42), Subclass#{{[0-9]+}}(17)]
306
- print ( " bridgeableValuesAsNSArray = \( bridgeableValuesAsNSArray as [ AnyObject ] ) ) " )
290
+ expectEqual ( 2 , bridgeableValuesAsNSArray. count)
291
+ expectEqual ( 42 , bridgeableValuesAsNSArray [ 0 ] . value)
292
+ expectEqual ( 17 , bridgeableValuesAsNSArray [ 1 ] . value)
307
293
308
294
// Make sure we can bridge back.
309
- let roundTripBridgeableValues
310
- = Swift . _forceBridgeFromObjectiveC ( bridgeableValuesAsNSArray,
311
- [ BridgeableValue ] . self)
312
- // CHECK-NOT: [BridgeableValue#[[id00]](42), BridgeableValue#[[id01]](17)]
313
- // CHECK-NEXT: [BridgeableValue#[[id10:[0-9]+]](42), BridgeableValue#[[id11:[0-9]+]](17)]
314
- print ( " roundTripBridgeableValues = \( roundTripBridgeableValues) ) " )
295
+ let roundTrippedValues = Swift . _forceBridgeFromObjectiveC (
296
+ bridgeableValuesAsNSArray, [ BridgeableValue ] . self)
297
+
298
+ // Expect equal values...
299
+ expectEqualSequence ( bridgeableValues, roundTrippedValues)
300
+ // ...with the same identity for some reason? FIXME: understand why.
301
+ expectFalse (
302
+ zip ( bridgeableValues, roundTrippedValues) . contains { $0. trak !== $1. trak } )
315
303
316
304
// Make a real Cocoa NSArray of these...
317
- let cocoaBridgeableValues = NSArray ( array: bridgeableValuesAsNSArray as [ AnyObject ] )
305
+ let cocoaBridgeableValues = NSArray (
306
+ array: bridgeableValuesAsNSArray as [ AnyObject ] )
318
307
319
308
// ...and bridge *that* back
320
- let bridgedBackSwifts
321
- = Swift . _forceBridgeFromObjectiveC ( cocoaBridgeableValues, [ BridgeableValue ] . self)
322
- // CHECK-NOT: [BridgeableValue#[[id00]](42), BridgeableValue#[[id01]](17)]
323
- // CHECK-NOT: [BridgeableValue#[[id10]](42), BridgeableValue#[[id11]](17)]
324
- // CHECK-NEXT: [BridgeableValue#{{[0-9]+}}(42), BridgeableValue#{{[0-9]+}}(17)]
325
- print ( " bridgedBackSwifts = \( bridgedBackSwifts) " )
309
+ let bridgedBackSwifts = Swift . _forceBridgeFromObjectiveC (
310
+ cocoaBridgeableValues, [ BridgeableValue ] . self)
326
311
327
- // all: verbatim, not, and doesn't bridge
328
- // implicit conversions to/from NSArray
329
- // [Base] -> [Subclass] and [Subclass] -> [Base] where Base can be AnyObject
330
- // defining @objc method taking [T] and returning [T]
312
+ // Expect equal, but distinctly created instances
313
+ expectEqualSequence ( bridgeableValues, bridgedBackSwifts)
314
+ expectFalse (
315
+ zip ( bridgedBackSwifts, roundTrippedValues) . contains { $0. trak === $1. trak } )
316
+ expectFalse (
317
+ zip ( bridgedBackSwifts, bridgeableValues) . contains { $0. trak === $1. trak } )
318
+
319
+ let expectedSubclasses = [ Subclass ( 42 ) , Subclass ( 17 ) ]
320
+ let expectedBases = expectedSubclasses. lazy. map { $0 as Base }
331
321
332
322
// Up-casts.
333
- let bridgeableValuesAsSubclasss : [ Subclass ] = bridgeableValues
334
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
335
- print ( bridgeableValuesAsSubclasss [ 0 ] )
336
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
337
- print ( bridgeableValuesAsSubclasss [ 1 ] )
323
+ let bridgeableValuesAsSubclasses : [ Subclass ] = bridgeableValues
324
+ expectEqualSequence ( expectedSubclasses, bridgeableValuesAsSubclasses)
338
325
339
326
let bridgeableValuesAsBases : [ Base ] = bridgeableValues
340
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
341
- print ( bridgeableValuesAsBases [ 0 ] )
342
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
343
- print ( bridgeableValuesAsBases [ 1 ] )
327
+ expectEqualSequence ( expectedBases, bridgeableValuesAsBases)
344
328
345
329
let bridgeableValuesAsAnyObjects : [ AnyObject ] = bridgeableValues
346
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
347
- print ( bridgeableValuesAsAnyObjects [ 0 ] )
348
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
349
- print ( bridgeableValuesAsAnyObjects [ 1 ] )
330
+ expectEqualSequence (
331
+ expectedBases,
332
+ bridgeableValuesAsAnyObjects. lazy. map { $0 as! Base } )
350
333
351
334
// Downcasts of non-verbatim bridged value types to objects.
352
335
do {
353
336
let downcasted = bridgeableValues as [ Subclass ]
354
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
355
- print ( downcasted [ 0 ] )
356
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
357
- print ( downcasted [ 1 ] )
337
+ expectEqualSequence ( expectedSubclasses, downcasted)
358
338
}
359
339
360
340
do {
361
341
let downcasted = bridgeableValues as [ Base ]
362
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
363
- print ( downcasted [ 0 ] )
364
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
365
- print ( downcasted [ 1 ] )
342
+ expectEqualSequence ( expectedBases, downcasted)
366
343
}
367
344
368
345
do {
369
346
let downcasted = bridgeableValues as [ AnyObject ]
370
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
371
- print ( downcasted [ 0 ] )
372
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
373
- print ( downcasted [ 1 ] )
347
+ expectEqualSequence ( expectedBases, downcasted. map { $0 as! Base } )
374
348
}
375
349
376
350
// Downcasts of up-casted arrays.
377
- if let downcasted = bridgeableValuesAsAnyObjects as? [ Subclass ] {
378
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
379
- print ( downcasted [ 0 ] )
380
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
381
- print ( downcasted [ 1 ] )
382
- } else {
383
- print ( " Could not downcast [AnyObject] to [Subclass]? " )
351
+ if let downcasted = expectNotEmpty (
352
+ bridgeableValuesAsAnyObjects as? [ Subclass ]
353
+ ) {
354
+ expectEqualSequence ( expectedSubclasses, downcasted)
384
355
}
385
356
386
357
if let downcasted = bridgeableValuesAsAnyObjects as? [ Base ] {
387
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
388
- print ( downcasted [ 0 ] )
389
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
390
- print ( downcasted [ 1 ] )
391
- } else {
392
- print ( " Could not downcast [AnyObject] to [Base]? " )
358
+ expectEqualSequence ( expectedBases, downcasted)
393
359
}
394
360
395
361
// Downcast of Cocoa array to an array of classes.
396
362
let wrappedCocoaBridgeableValues = cocoaBridgeableValues as [ AnyObject ]
397
363
if let downcasted = wrappedCocoaBridgeableValues as? [ Subclass ] {
398
- // CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
399
- print ( downcasted [ 0 ] )
400
- // CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
401
- print ( downcasted [ 1 ] )
402
- } else {
403
- print ( " Could not downcast [AnyObject] to [Subclass]? " )
364
+ expectEqualSequence ( expectedSubclasses, downcasted)
404
365
}
405
366
406
367
// Downcast of Cocoa array to an array of values.
407
368
if let downcasted = wrappedCocoaBridgeableValues as? [ BridgeableValue ] {
408
- // CHECK-NEXT: BridgeableValue#[[ID0:[0-9]+]](42)
409
- print ( downcasted [ 0 ] )
410
- // CHECK-NEXT: BridgeableValue#[[ID1:[0-9]+]](17)
411
- print ( downcasted [ 1 ] )
412
- } else {
413
- print ( " Could not downcast [AnyObject] to [BridgeableValue]? " )
369
+ expectEqualSequence ( bridgeableValues, downcasted)
414
370
}
415
371
416
372
// Downcast of Cocoa array to an array of strings (which should fail)
417
- // CHECK-NEXT: Could not downcast [AnyObject] to [String]
418
- if let _ = wrappedCocoaBridgeableValues as? [ String ] {
419
- print ( " Shouldn't be able to downcast to an array of strings " )
420
- } else {
421
- print ( " Could not downcast [AnyObject] to [String] " )
422
- }
373
+ expectEmpty ( wrappedCocoaBridgeableValues as? [ String ] )
423
374
424
375
// Downcast from an implicitly unwrapped optional array of AnyObjects.
425
376
var wrappedCocoaBridgeableValuesIUO : [ AnyObject ] ! = wrappedCocoaBridgeableValues
426
- if let downcasted = wrappedCocoaBridgeableValuesIUO as? [ BridgeableValue ] {
427
- // CHECK-NEXT: BridgeableValue#[[ID0:[0-9]+]](42)
428
- print ( downcasted [ 0 ] )
429
- // CHECK-NEXT: BridgeableValue#[[ID1:[0-9]+]](17)
430
- print ( downcasted [ 1 ] )
431
- } else {
432
- print ( " Could not downcast [AnyObject]! to [BridgeableValue] " )
377
+ if let downcasted = wrappedCocoaBridgeableValuesIUO as? [ BridgeableValue ] {
378
+ expectEqualSequence ( bridgeableValues, downcasted)
433
379
}
434
380
435
381
// Downcast from a nil implicitly unwrapped optional array of AnyObjects.
436
382
wrappedCocoaBridgeableValuesIUO = nil
437
- if let _ = wrappedCocoaBridgeableValuesIUO as? [ BridgeableValue ] {
438
- print ( " Cannot downcast from a nil array! " )
439
- } else {
440
- // CHECK-NEXT: Correctly rejected downcast of nil array
441
- print ( " Correctly rejected downcast of nil array " )
442
- }
383
+ expectEmpty ( wrappedCocoaBridgeableValuesIUO as? [ BridgeableValue ] )
443
384
444
385
// Downcast from an optional array of AnyObjects.
445
386
var wrappedCocoaBridgeableValuesOpt : [ AnyObject ] ? = wrappedCocoaBridgeableValues
446
- if let downcasted = wrappedCocoaBridgeableValuesOpt as? [ BridgeableValue ] {
447
- // CHECK-NEXT: BridgeableValue#[[ID0:[0-9]+]](42)
448
- print ( downcasted [ 0 ] )
449
- // CHECK-NEXT: BridgeableValue#[[ID1:[0-9]+]](17)
450
- print ( downcasted [ 1 ] )
451
- } else {
452
- print ( " Could not downcast [AnyObject]! to [BridgeableValue] " )
387
+ if let downcasted = wrappedCocoaBridgeableValuesOpt as? [ BridgeableValue ] {
388
+ expectEqualSequence ( bridgeableValues, downcasted)
453
389
}
454
390
455
391
// Downcast from a nil optional array of AnyObjects.
456
392
wrappedCocoaBridgeableValuesOpt = nil
457
- if let _ = wrappedCocoaBridgeableValuesOpt as? [ BridgeableValue ] {
458
- print ( " Cannot downcast from a nil array! " )
459
- } else {
460
- // CHECK-NEXT: Correctly rejected downcast of nil array
461
- print ( " Correctly rejected downcast of nil array " )
462
- }
463
- }
464
- testExplicitlyBridged ( )
465
-
466
- tests. test ( " testExplicitlyBridged " ) {
393
+ expectEmpty ( wrappedCocoaBridgeableValuesOpt as? [ BridgeableValue ] )
394
+
467
395
testBridgeableValue ( Thunks ( ) )
468
396
}
469
397
@@ -500,19 +428,18 @@ tests.test("testRoundTrip") {
500
428
501
429
struct X { }
502
430
503
- /*
504
- let x: NSArray = arrayAsID(bases)!
505
-
506
- print(x.objectAt(0) as Base)
507
- */
508
-
509
- func testMutableArray( ) {
431
+ tests. test ( " testMutableArray " ) {
510
432
let m = NSMutableArray ( array: [ " fu " , " bar " , " buzz " ] )
511
433
let a = m as NSArray as! [ NSString ]
512
- print ( a) // CHECK-NEXT: [fu, bar, buzz]
434
+
435
+ // Create distinct array storage with a copy of the elements in a
436
+ let aCopy = a. map { $0 }
437
+
438
+ // Modify the original mutable array
513
439
m. add ( " goop " )
514
- print ( a) // CHECK-NEXT: [fu, bar, buzz]
440
+
441
+ // Check that our [NSString] is unaffected
442
+ expectEqualSequence ( aCopy, a)
515
443
}
516
- testMutableArray ( )
517
444
518
445
runAllTests ( )
0 commit comments