Skip to content

Commit 202f84e

Browse files
committed
[stdlib] Update String COW tests
Removing the _StringCore reference from string indices fixes an extra reallocation when mutating a string with heap buffer storage.
1 parent 2d75c12 commit 202f84e

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

validation-test/stdlib/String.swift

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ StringTests.test("ForeignIndexes/subscript(Index)/OutOfBoundsTrap") {
202202

203203
let i = donor.index(_nth: 3)
204204
expectCrashLater()
205-
acceptor[i]
205+
_ = acceptor[i]
206206
}
207207

208208
StringTests.test("String/subscript(_:Range)") {
@@ -229,7 +229,7 @@ StringTests.test("ForeignIndexes/subscript(Range)/OutOfBoundsTrap/1") {
229229

230230
let r = donor.startIndex..<donor.index(_nth: 4)
231231
expectCrashLater()
232-
acceptor[r]
232+
_ = acceptor[r]
233233
}
234234

235235
StringTests.test("ForeignIndexes/subscript(Range)/OutOfBoundsTrap/2") {
@@ -240,7 +240,7 @@ StringTests.test("ForeignIndexes/subscript(Range)/OutOfBoundsTrap/2") {
240240

241241
let r = donor.index(_nth: 4)..<donor.index(_nth: 5)
242242
expectCrashLater()
243-
acceptor[r]
243+
_ = acceptor[r]
244244
}
245245

246246
StringTests.test("ForeignIndexes/replaceSubrange/OutOfBoundsTrap/1") {
@@ -497,9 +497,7 @@ StringTests.test("COW/removeSubrange/start") {
497497

498498
// No more reallocations are expected.
499499
str.removeSubrange(str.startIndex..<str.index(_nth: 1))
500-
// FIXME: extra reallocation, should be expectEqual()
501-
expectNotEqual(heapStrIdentity, str.bufferID)
502-
// end FIXME
500+
expectEqual(heapStrIdentity, str.bufferID)
503501
expectEqual(literalIdentity, slice.bufferID)
504502
expectEqual("345678", str)
505503
expectEqual("12345678", slice)
@@ -526,9 +524,7 @@ StringTests.test("COW/removeSubrange/start") {
526524

527525
// No more reallocations are expected.
528526
str.removeSubrange(str.startIndex..<str.index(_nth: 1))
529-
// FIXME: extra reallocation, should be expectEqual()
530-
expectNotEqual(heapStrIdentity2, str.bufferID)
531-
// end FIXME
527+
expectEqual(heapStrIdentity2, str.bufferID)
532528
expectEqual(heapStrIdentity1, slice.bufferID)
533529
expectEqual("5678", str)
534530
expectEqual("345678", slice)
@@ -559,19 +555,15 @@ StringTests.test("COW/removeSubrange/end") {
559555
// No more reallocations are expected.
560556
str.append(UnicodeScalar("x"))
561557
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
562-
// FIXME: extra reallocation, should be expectEqual()
563-
expectNotEqual(heapStrIdentity, str.bufferID)
564-
// end FIXME
558+
expectEqual(heapStrIdentity, str.bufferID)
565559
expectEqual(literalIdentity, slice.bufferID)
566560
expectEqual("1234567", str)
567561
expectEqual("12345678", slice)
568562

569563
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
570564
str.append(UnicodeScalar("x"))
571565
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
572-
// FIXME: extra reallocation, should be expectEqual()
573-
//expectNotEqual(heapStrIdentity, str.bufferID)
574-
// end FIXME
566+
expectEqual(heapStrIdentity, str.bufferID)
575567
expectEqual(literalIdentity, slice.bufferID)
576568
expectEqual("123456", str)
577569
expectEqual("12345678", slice)
@@ -599,19 +591,15 @@ StringTests.test("COW/removeSubrange/end") {
599591
// No more reallocations are expected.
600592
str.append(UnicodeScalar("x"))
601593
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
602-
// FIXME: extra reallocation, should be expectEqual()
603-
expectNotEqual(heapStrIdentity, str.bufferID)
604-
// end FIXME
594+
expectEqual(heapStrIdentity, str.bufferID)
605595
expectEqual(heapStrIdentity1, slice.bufferID)
606596
expectEqual("12345", str)
607597
expectEqual("123456", slice)
608598

609599
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
610600
str.append(UnicodeScalar("x"))
611601
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
612-
// FIXME: extra reallocation, should be expectEqual()
613-
//expectNotEqual(heapStrIdentity, str.bufferID)
614-
// end FIXME
602+
expectEqual(heapStrIdentity, str.bufferID)
615603
expectEqual(heapStrIdentity1, slice.bufferID)
616604
expectEqual("1234", str)
617605
expectEqual("123456", slice)
@@ -634,16 +622,14 @@ StringTests.test("COW/replaceSubrange/end") {
634622
slice.replaceSubrange(slice.endIndex..<slice.endIndex, with: "a")
635623
expectNotEqual(literalIdentity, slice.bufferID)
636624
expectEqual(literalIdentity, str.bufferID)
637-
let heapStrIdentity = str.bufferID
625+
let heapStrIdentity = slice.bufferID
638626
expectEqual("1234567a", slice)
639627
expectEqual("12345678", str)
640628

641629
// No more reallocations are expected.
642630
slice.replaceSubrange(
643631
slice.index(_nthLast: 1)..<slice.endIndex, with: "b")
644-
// FIXME: extra reallocation, should be expectEqual()
645-
expectNotEqual(heapStrIdentity, slice.bufferID)
646-
// end FIXME
632+
expectEqual(heapStrIdentity, slice.bufferID)
647633
expectEqual(literalIdentity, str.bufferID)
648634

649635
expectEqual("1234567b", slice)
@@ -675,9 +661,7 @@ StringTests.test("COW/replaceSubrange/end") {
675661
// No more reallocations are expected.
676662
slice.replaceSubrange(
677663
slice.index(_nthLast: 1)..<slice.endIndex, with: "b")
678-
// FIXME: extra reallocation, should be expectEqual()
679-
expectNotEqual(heapStrIdentity2, slice.bufferID)
680-
// end FIXME
664+
expectEqual(heapStrIdentity2, slice.bufferID)
681665
expectEqual(heapStrIdentity1, str.bufferID)
682666

683667
expectEqual("1234567b", slice)

0 commit comments

Comments
 (0)