@@ -3469,6 +3469,150 @@ DictionaryTestSuite.test("BridgedFromObjC.Nonverbatim.StringEqualityMismatch") {
3469
3469
expectTrue ( v == 42 || v == 23 )
3470
3470
}
3471
3471
3472
+ DictionaryTestSuite . test ( " BridgedFromObjC.Verbatim.OptionalDowncastFailure " ) {
3473
+ let nsd = NSDictionary (
3474
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3475
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3476
+ expectNotNil ( nsd as? [ NSString : NSNumber ] )
3477
+ expectNotNil ( nsd as? [ NSString : NSObject ] )
3478
+ expectNotNil ( nsd as? [ NSObject : NSNumber ] )
3479
+ expectNil ( nsd as? [ NSNumber : NSObject ] )
3480
+ expectNil ( nsd as? [ NSObject : NSString ] )
3481
+ }
3482
+
3483
+ DictionaryTestSuite
3484
+ . test ( " BridgedFromObjC.Verbatim.ForcedDowncastFailure.Keys " ) {
3485
+ let nsd = NSDictionary (
3486
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3487
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3488
+ let bridged = nsd as! [ NSNumber : NSObject ]
3489
+ expectCrashLater ( )
3490
+ // The forced downcast succeeds unconditionally; the cast is instead verified
3491
+ // when we access individual elements.
3492
+ _ = bridged. first
3493
+ }
3494
+
3495
+ DictionaryTestSuite
3496
+ . test ( " BridgedFromObjC.Verbatim.ForcedDowncastFailure.Values " ) {
3497
+ let nsd = NSDictionary (
3498
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3499
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3500
+
3501
+ let bridged = nsd as! [ NSObject : NSString ]
3502
+ expectCrashLater ( )
3503
+ // The forced downcast succeeds unconditionally; the cast is instead verified
3504
+ // when we access individual elements.
3505
+ _ = bridged. first
3506
+ }
3507
+
3508
+ DictionaryTestSuite
3509
+ . test ( " BridgedFromObjC.Verbatim.ForcedDowncastFailure.Both " ) {
3510
+ let nsd = NSDictionary (
3511
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3512
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3513
+ let bridged = nsd as! [ NSNumber : NSString ]
3514
+ expectCrashLater ( )
3515
+ // The forced downcast succeeds unconditionally; the cast is instead verified
3516
+ // when we access individual elements.
3517
+ _ = bridged. first
3518
+ }
3519
+
3520
+ DictionaryTestSuite
3521
+ . test ( " BridgedFromObjC.Verbatim.ForcedDowncastFailure.Partial " )
3522
+ . code {
3523
+ let nsd = NSMutableDictionary (
3524
+ objects: ( 0 ..< 10 ) . map { " \( $0) " as NSString } ,
3525
+ forKeys: ( 0 ..< 10 ) . map { $0 as NSNumber } )
3526
+ nsd. setObject ( " cuckoo " as NSString , forKey: " cuckoo " as NSString )
3527
+ let bridged = nsd as! [ NSNumber : NSString ]
3528
+ // The forced downcast succeeds unconditionally; the cast is instead verified
3529
+ // when we access individual elements.
3530
+ for i in 0 ..< 10 {
3531
+ expectEqual ( " \( i) " as NSString , bridged [ i as NSNumber ] )
3532
+ }
3533
+ // The item with the unexpected key is only accessible when we iterate over
3534
+ // the elements. There should be a trap when we get to it.
3535
+ expectCrashLater ( )
3536
+ for (key, value) in bridged {
3537
+ _ = key
3538
+ _ = value
3539
+ }
3540
+ }
3541
+
3542
+ DictionaryTestSuite . test ( " BridgedFromObjC.Verbatim.DowncastFailure.LeakTest " ) {
3543
+ let nsd = NSMutableDictionary (
3544
+ objects: ( 0 ..< 100 ) . map { TestObjCEquatableValueTy ( $0) } ,
3545
+ forKeys: ( 0 ..< 100 ) . map { TestObjCKeyTy ( $0) } )
3546
+ expectNotNil ( nsd as? [ TestObjCKeyTy : TestObjCEquatableValueTy ] )
3547
+ expectNotNil ( nsd as? [ TestObjCKeyTy : NSObject ] )
3548
+ expectNotNil ( nsd as? [ NSObject : TestObjCEquatableValueTy ] )
3549
+
3550
+ // Inserting a single key-value pair of a different type should make all these
3551
+ // downcasts fail.
3552
+ nsd. setObject ( " cuckoo " as NSString , forKey: " cuckoo " as NSString )
3553
+ expectNil ( nsd as? [ TestObjCKeyTy : TestObjCEquatableValueTy ] )
3554
+ expectNil ( nsd as? [ TestObjCKeyTy : NSObject ] )
3555
+ expectNil ( nsd as? [ NSObject : TestObjCEquatableValueTy ] )
3556
+ // No crash test here -- we're relying on the leak tests in tearDown.
3557
+ }
3558
+
3559
+ DictionaryTestSuite
3560
+ . test ( " BridgedFromObjC.Nonverbatim.OptionalDowncastFailure " ) {
3561
+ let nsd = NSDictionary (
3562
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3563
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3564
+ expectNotNil ( nsd as? [ String : Int ] )
3565
+ expectNotNil ( nsd as? [ String : NSObject ] )
3566
+ expectNotNil ( nsd as? [ NSObject : Int ] )
3567
+ expectNil ( nsd as? [ Int : NSObject ] )
3568
+ expectNil ( nsd as? [ NSObject : String ] )
3569
+ }
3570
+
3571
+ DictionaryTestSuite
3572
+ . test ( " BridgedFromObjC.Nonverbatim.ForcedDowncastFailure.Keys " ) {
3573
+ let nsd = NSDictionary (
3574
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3575
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3576
+ expectCrashLater ( )
3577
+ _ = nsd as! [ Int : NSObject ]
3578
+ }
3579
+
3580
+ DictionaryTestSuite
3581
+ . test ( " BridgedFromObjC.Nonverbatim.ForcedDowncastFailure.Values " ) {
3582
+ let nsd = NSDictionary (
3583
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3584
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3585
+ expectCrashLater ( )
3586
+ _ = nsd as! [ NSObject : String ]
3587
+ }
3588
+
3589
+ DictionaryTestSuite
3590
+ . test ( " BridgedFromObjC.Nonverbatim.ForcedDowncastFailure.Both " ) {
3591
+ let nsd = NSDictionary (
3592
+ objects: [ 1 as NSNumber , 2 as NSNumber , 3 as NSNumber ] ,
3593
+ forKeys: [ " One " as NSString , " Two " as NSString , " Three " as NSString ] )
3594
+ expectCrashLater ( )
3595
+ _ = nsd as! [ Int : String ]
3596
+ }
3597
+
3598
+ DictionaryTestSuite
3599
+ . test ( " BridgedFromObjC.Nonverbatim.DowncastFailure.LeakTest " ) {
3600
+ let nsd = NSMutableDictionary (
3601
+ objects: ( 0 ..< 100 ) . map { TestObjCEquatableValueTy ( $0) } ,
3602
+ forKeys: ( 0 ..< 100 ) . map { TestObjCKeyTy ( $0) } )
3603
+ expectNotNil ( nsd as? [ TestBridgedKeyTy : TestBridgedEquatableValueTy ] )
3604
+ expectNotNil ( nsd as? [ TestBridgedKeyTy : NSObject ] )
3605
+ expectNotNil ( nsd as? [ NSObject : TestBridgedEquatableValueTy ] )
3606
+
3607
+ // Inserting a single key-value pair of a different type should make all these
3608
+ // downcasts fail.
3609
+ nsd. setObject ( " cuckoo " as NSString , forKey: " cuckoo " as NSString )
3610
+ expectNil ( nsd as? [ TestBridgedKeyTy : TestBridgedEquatableValueTy ] )
3611
+ expectNil ( nsd as? [ TestBridgedKeyTy : NSObject ] )
3612
+ expectNil ( nsd as? [ NSObject : TestBridgedEquatableValueTy ] )
3613
+ // No crash test here -- we're relying on the leak tests in tearDown.
3614
+ }
3615
+
3472
3616
//===---
3473
3617
// Dictionary -> NSDictionary bridging tests.
3474
3618
//
0 commit comments