@@ -12,6 +12,29 @@ func checkMatch<S: Collection, T: Collection>(_ x: S, _ y: T, _ i: S.Index)
12
12
expectEqual ( x [ i] , y [ i] )
13
13
}
14
14
15
+ func checkMatchContiguousStorage< S: Collection , T: Collection > ( _ x: S , _ y: T , expected: Bool )
16
+ where S. Element == T . Element , S. Element: Equatable
17
+ {
18
+ let xElement = x. withContiguousStorageIfAvailable { $0. first }
19
+ let yElement = y. withContiguousStorageIfAvailable { $0. first }
20
+
21
+ if expected {
22
+ expectEqual ( xElement, yElement)
23
+ } else {
24
+ expectNotEqual ( xElement, yElement)
25
+ }
26
+ }
27
+
28
+ func checkHasContiguousStorage< S: Collection > ( _ x: S , expected: Bool ) {
29
+ let hasStorage = x. withContiguousStorageIfAvailable { _ in true } ?? false
30
+ expectEqual ( hasStorage, expected)
31
+ }
32
+
33
+ func checkHasContiguousStorageSubstring( _ x: Substring . UTF8View ) {
34
+ let hasStorage = x. withContiguousStorageIfAvailable { _ in true } ?? false
35
+ expectTrue ( hasStorage)
36
+ }
37
+
15
38
SubstringTests . test ( " Equality " ) {
16
39
let s = " abcdefg "
17
40
let s1 = s [ s. index ( s. startIndex, offsetBy: 2 ) ..<
@@ -228,6 +251,24 @@ SubstringTests.test("UTF8View") {
228
251
expectEqual ( " " , String ( t. dropLast ( 100 ) ) !)
229
252
expectEqual ( " " , String ( u. dropFirst ( 100 ) ) !)
230
253
expectEqual ( " " , String ( u. dropLast ( 100 ) ) !)
254
+
255
+ let expectSubstringWCSIA : Bool
256
+ // This availability guard should refer to a concrete OS version in
257
+ // future.
258
+ if #available( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * ) {
259
+ expectSubstringWCSIA = true
260
+ } else {
261
+ expectSubstringWCSIA = false
262
+ }
263
+
264
+ checkHasContiguousStorage ( s. utf8, expected: true ) // Strings always do
265
+ checkHasContiguousStorage ( t, expected: expectSubstringWCSIA)
266
+ checkHasContiguousStorage ( u, expected: expectSubstringWCSIA)
267
+ checkHasContiguousStorageSubstring ( t)
268
+ checkHasContiguousStorageSubstring ( u)
269
+ checkMatchContiguousStorage ( Array ( s. utf8) , s. utf8, expected: true )
270
+ checkMatchContiguousStorage ( Array ( t) , t, expected: expectSubstringWCSIA)
271
+ checkMatchContiguousStorage ( Array ( u) , u, expected: expectSubstringWCSIA)
231
272
}
232
273
}
233
274
0 commit comments