Skip to content

Commit 2757088

Browse files
committed
[test] stdlib/StringTraps: Add some test coverage for String.UnicodeScalarView
1 parent 6f9edc8 commit 2757088

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

test/stdlib/StringTraps.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,67 @@ StringTraps.test("UTF8ViewIndex/offsetCrash")
169169
_ = s8.utf8[i]
170170
}
171171

172+
StringTraps.test("UnicodeScalarView index(before:) trap on startIndex")
173+
.skip(
174+
.custom({ _isFastAssertConfiguration() },
175+
reason: "trap is not guaranteed to happen in -Ounchecked"))
176+
.code {
177+
guard #available(SwiftStdlib 5.7, *) else { return }
178+
179+
let s = "abc"
180+
var i = s.unicodeScalars.endIndex
181+
i = s.unicodeScalars.index(before: i)
182+
i = s.unicodeScalars.index(before: i)
183+
i = s.unicodeScalars.index(before: i)
184+
expectCrashLater()
185+
i = s.unicodeScalars.index(before: i)
186+
}
187+
188+
StringTraps.test("UnicodeScalarView index(before:) trap on startIndex after scalar alignment")
189+
.skip(
190+
.custom({ _isFastAssertConfiguration() },
191+
reason: "trap is not guaranteed to happen in -Ounchecked"))
192+
.code {
193+
guard #available(SwiftStdlib 5.7, *) else { return }
194+
195+
let s = "🥦 Floret of broccoli"
196+
var i = s.utf8.index(after: s.utf8.startIndex)
197+
expectCrashLater()
198+
// `i` is equivalent to `s.startIndex` as far as `String.UnicodeScalarView` is
199+
// concerned
200+
i = s.unicodeScalars.index(before: i)
201+
}
202+
203+
StringTraps.test("UnicodeScalarView index(after:) trap on endIndex")
204+
.skip(
205+
.custom({ _isFastAssertConfiguration() },
206+
reason: "trap is not guaranteed to happen in -Ounchecked"))
207+
.code {
208+
guard #available(SwiftStdlib 5.7, *) else { return }
209+
210+
let s = "abc"
211+
var i = s.unicodeScalars.startIndex
212+
i = s.unicodeScalars.index(after: i)
213+
i = s.unicodeScalars.index(after: i)
214+
i = s.unicodeScalars.index(after: i)
215+
expectCrashLater()
216+
i = s.unicodeScalars.index(after: i)
217+
}
218+
219+
StringTraps.test("UnicodeScalarView index(after:) trap on i > endIndex")
220+
.skip(
221+
.custom({ _isFastAssertConfiguration() },
222+
reason: "trap is not guaranteed to happen in -Ounchecked"))
223+
.code {
224+
guard #available(SwiftStdlib 5.7, *) else { return }
225+
226+
let long = "abcd"
227+
var i = long.unicodeScalars.endIndex
228+
229+
let s = "abc"
230+
expectCrashLater()
231+
i = s.unicodeScalars.index(after: i)
232+
}
233+
172234
runAllTests()
173235

0 commit comments

Comments
 (0)