@@ -1500,7 +1500,7 @@ SetTestSuite.test("BridgedFromObjC.Nonverbatim.Insert") {
1500
1500
let identity2 = s. _rawIdentifier ( )
1501
1501
// Storage identity may or may not change depending on allocation behavior.
1502
1502
// (s is eagerly bridged to a regular uniquely referenced native Set.)
1503
- expectTrue ( ( count1 < capacity1) == ( identity1 == s . _rawIdentifier ( ) ) )
1503
+ expectTrue ( ( count1 < capacity1) == ( identity1 == identity2 ) )
1504
1504
1505
1505
expectTrue ( isNativeSet ( s) )
1506
1506
expectEqual ( 4 , s. count)
@@ -1526,7 +1526,7 @@ SetTestSuite.test("BridgedFromObjC.Nonverbatim.UpdateWith") {
1526
1526
let identity2 = s. _rawIdentifier ( )
1527
1527
// Storage identity may or may not change depending on allocation behavior.
1528
1528
// (s is eagerly bridged to a regular uniquely referenced native Set.)
1529
- expectTrue ( ( count1 < capacity1) == ( identity1 == s . _rawIdentifier ( ) ) )
1529
+ expectTrue ( ( count1 < capacity1) == ( identity1 == identity2 ) )
1530
1530
1531
1531
expectTrue ( isNativeSet ( s) )
1532
1532
expectEqual ( 4 , s. count)
@@ -4425,4 +4425,192 @@ SetTestSuite.test("localHashSeeds") {
4425
4425
expectFalse ( smallElements. elementsEqual ( sliceElements) )
4426
4426
}
4427
4427
4428
+ SetTestSuite . test ( " RemoveAt.InvalidatesIndices " ) {
4429
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4430
+ let i = s. firstIndex ( of: 20 ) !
4431
+ let j = s. firstIndex ( of: 10 ) !
4432
+
4433
+ s. remove ( at: j)
4434
+
4435
+ expectCrashLater ( )
4436
+ _ = s [ i]
4437
+ }
4438
+
4439
+ SetTestSuite . test ( " RemoveValueForKey.InvalidatesIndices " ) {
4440
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4441
+ let i = s. firstIndex ( of: 20 ) !
4442
+
4443
+ s. remove ( 10 )
4444
+
4445
+ expectCrashLater ( )
4446
+ _ = s [ i]
4447
+ }
4448
+
4449
+ SetTestSuite . test ( " ResizeOnInsertion.InvalidatesIndices " ) {
4450
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4451
+ let i = s. firstIndex ( of: 20 ) !
4452
+ expectEqual ( s [ i] , 20 )
4453
+
4454
+ for i in 0 ..< ( s. capacity - s. count) {
4455
+ s. insert ( 100 + i)
4456
+ }
4457
+ expectEqual ( s [ i] , 20 )
4458
+ s. insert ( 0 )
4459
+
4460
+ expectCrashLater ( )
4461
+ _ = s [ i]
4462
+ }
4463
+
4464
+ SetTestSuite . test ( " ResizeOnUpdate.InvalidatesIndices " ) {
4465
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4466
+ let i = s. firstIndex ( of: 20 ) !
4467
+ expectEqual ( s [ i] , 20 )
4468
+
4469
+ for i in 0 ..< ( s. capacity - s. count) {
4470
+ s. update ( with: 100 + i)
4471
+ }
4472
+ expectEqual ( s [ i] , 20 )
4473
+ s. update ( with: 0 )
4474
+
4475
+ expectCrashLater ( )
4476
+ _ = s [ i]
4477
+ }
4478
+
4479
+ SetTestSuite . test ( " RemoveAll.InvalidatesIndices " ) {
4480
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4481
+ let i = s. firstIndex ( of: 20 ) !
4482
+ expectEqual ( s [ i] , 20 )
4483
+
4484
+ s. removeAll ( keepingCapacity: true )
4485
+
4486
+ expectCrashLater ( )
4487
+ _ = s [ i]
4488
+ }
4489
+
4490
+ SetTestSuite . test ( " ReserveCapacity.InvalidatesIndices " ) {
4491
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4492
+ let i = s. firstIndex ( of: 20 ) !
4493
+ expectEqual ( s [ i] , 20 )
4494
+
4495
+ s. reserveCapacity ( 0 )
4496
+ expectEqual ( s [ i] , 20 )
4497
+
4498
+ s. reserveCapacity ( s. capacity)
4499
+ expectEqual ( s [ i] , 20 )
4500
+
4501
+ s. reserveCapacity ( s. capacity * 10 )
4502
+
4503
+ expectCrashLater ( )
4504
+ _ = s [ i]
4505
+ }
4506
+
4507
+ SetTestSuite . test ( " IndexValidation.Subscript.Getter.AcrossInstances " ) {
4508
+ // The mutation count may happen to be the same across any two sets.
4509
+ // The probability of this is low, but it could happen -- so check a bunch of
4510
+ // these cases at once; a trap will definitely occur at least once.
4511
+ let sets = ( 0 ..< 10 ) . map { _ in Set < Int > ( [ 10 , 20 , 30 , 40 ] ) }
4512
+ let indices = sets. map { $0. firstIndex ( of: 20 ) ! }
4513
+ let s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4514
+
4515
+ expectCrashLater ( )
4516
+ for i in indices {
4517
+ _ = s [ i]
4518
+ }
4519
+ _fixLifetime ( sets)
4520
+ }
4521
+
4522
+ SetTestSuite . test ( " IndexValidation.Subscript.Getter.AfterRemoval " ) {
4523
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4524
+ let i = s. firstIndex ( of: 20 ) !
4525
+ expectEqual ( s [ i] , 20 )
4526
+
4527
+ s. remove ( 10 )
4528
+
4529
+ expectCrashLater ( )
4530
+ _ = s [ i]
4531
+ }
4532
+
4533
+ SetTestSuite . test ( " IndexValidation.Subscript.Getter.AfterGrow " ) {
4534
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4535
+ let i = s. firstIndex ( of: 20 ) !
4536
+ let identifier = s. _rawIdentifier ( )
4537
+ expectEqual ( s [ i] , 20 )
4538
+
4539
+ for i in 0 ..< ( s. capacity - s. count) {
4540
+ s. insert ( 100 + i)
4541
+ }
4542
+ expectEqual ( s. _rawIdentifier ( ) , identifier)
4543
+ expectEqual ( s [ i] , 20 )
4544
+
4545
+ s. insert ( 0 )
4546
+
4547
+ expectNotEqual ( s. _rawIdentifier ( ) , identifier)
4548
+ expectCrashLater ( )
4549
+ _ = s [ i]
4550
+ }
4551
+
4552
+ SetTestSuite . test ( " IndexValidation.RangeSubscript.AfterRemoval " ) {
4553
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4554
+ let i = s. firstIndex ( of: 20 ) !
4555
+ let j = s. index ( after: i)
4556
+ expectTrue ( i < j)
4557
+
4558
+ s. remove ( 40 )
4559
+
4560
+ expectTrue ( i < j)
4561
+ expectCrashLater ( )
4562
+ _ = s [ i..< j]
4563
+ }
4564
+
4565
+ SetTestSuite . test ( " IndexValidation.RangeSubscript.AfterGrow " ) {
4566
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4567
+ let i = s. firstIndex ( of: 20 ) !
4568
+ let j = s. index ( after: i)
4569
+ expectTrue ( i < j)
4570
+ let identifier = s. _rawIdentifier ( )
4571
+
4572
+ for i in 0 ..< ( s. capacity - s. count) {
4573
+ s. insert ( 100 + i)
4574
+ }
4575
+ expectEqual ( s. _rawIdentifier ( ) , identifier)
4576
+ expectTrue ( i < j)
4577
+
4578
+ s. insert ( 0 )
4579
+
4580
+ expectTrue ( i < j)
4581
+ expectNotEqual ( s. _rawIdentifier ( ) , identifier)
4582
+ expectCrashLater ( )
4583
+ _ = s [ i..< j]
4584
+ }
4585
+
4586
+ SetTestSuite . test ( " IndexValidation.RemoveAt.AfterRemoval " ) {
4587
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4588
+ let i = s. firstIndex ( of: 20 ) !
4589
+ expectEqual ( s [ i] , 20 )
4590
+
4591
+ s. remove ( 10 )
4592
+
4593
+ expectCrashLater ( )
4594
+ s. remove ( at: i)
4595
+ }
4596
+
4597
+ SetTestSuite . test ( " IndexValidation.RemoveAt.AfterGrow " ) {
4598
+ var s : Set < Int > = [ 10 , 20 , 30 , 40 ]
4599
+ let i = s. firstIndex ( of: 20 ) !
4600
+ let identifier = s. _rawIdentifier ( )
4601
+ expectEqual ( s [ i] , 20 )
4602
+
4603
+ for i in 0 ..< ( s. capacity - s. count) {
4604
+ s. insert ( 100 + i)
4605
+ }
4606
+ expectEqual ( s. _rawIdentifier ( ) , identifier)
4607
+ expectEqual ( s [ i] , 20 )
4608
+
4609
+ s. insert ( 0 )
4610
+
4611
+ expectNotEqual ( s. _rawIdentifier ( ) , identifier)
4612
+ expectCrashLater ( )
4613
+ s. remove ( at: i)
4614
+ }
4615
+
4428
4616
runAllTests ( )
0 commit comments