Skip to content

Commit f528db5

Browse files
committed
[test] stdlib/StringTraps: Add some test coverage for String.index(before:) traps
1 parent 5b364c4 commit f528db5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/stdlib/StringTraps.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,35 @@ StringTraps.test("subscript(_:)/endIndex")
5656
_ = s[i]
5757
}
5858

59+
StringTraps.test("String.index(before:) trap on i > endIndex")
60+
.skip(
61+
.custom({ _isFastAssertConfiguration() },
62+
reason: "trap is not guaranteed to happen in -Ounchecked"))
63+
.code {
64+
guard #available(SwiftStdlib 5.7, *) else { return }
65+
66+
let long = String(repeating: "X", count: 1024)
67+
let short = "This is a short string"
68+
expectCrashLater()
69+
let i = short.index(before: long.endIndex)
70+
print(i)
71+
}
72+
73+
StringTraps.test("String.index(before:) trap on i == startIndex after scalar alignment")
74+
.skip(
75+
.custom({ _isFastAssertConfiguration() },
76+
reason: "trap is not guaranteed to happen in -Ounchecked"))
77+
.code {
78+
guard #available(SwiftStdlib 5.7, *) else { return }
79+
80+
let s = "🥯 Bagel with schmear"
81+
let i = s.utf8.index(after: s.utf8.startIndex)
82+
expectCrashLater()
83+
// `i` is equivalent to `s.startIndex` as far as `String` is concerned
84+
let j = s.index(before: i)
85+
print(j)
86+
}
87+
5988
StringTraps.test("UTF8ViewSubscript/endIndexSuccessor")
6089
.skip(.custom(
6190
{ _isFastAssertConfiguration() },

0 commit comments

Comments
 (0)