Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions test/stdlib/subString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,17 @@ func checkMatch<S: Collection, T: Collection>(_ x: S, _ y: T, _ i: S.Index)
expectEqual(x[i], y[i])
}

func checkMatchContiguousStorage<S: Collection, T: Collection>(_ x: S, _ y: T, expected: Bool)
func checkMatchContiguousStorage<S: Collection, T: Collection>(_ x: S, _ y: T)
where S.Element == T.Element, S.Element: Equatable
{
let xElement = x.withContiguousStorageIfAvailable { $0.first }
let yElement = y.withContiguousStorageIfAvailable { $0.first }

if expected {
expectEqual(xElement, yElement)
} else {
expectNotEqual(xElement, yElement)
}
expectEqual(xElement, yElement)
}

func checkHasContiguousStorage<S: Collection>(_ x: S, expected: Bool) {
let hasStorage = x.withContiguousStorageIfAvailable { _ in true } ?? false
expectEqual(hasStorage, expected)
func checkHasContiguousStorage<S: Collection>(_ x: S) {
expectTrue(x.withContiguousStorageIfAvailable { _ in true } ?? false)
}

func checkHasContiguousStorageSubstring(_ x: Substring.UTF8View) {
Expand Down Expand Up @@ -252,23 +247,21 @@ SubstringTests.test("UTF8View") {
expectEqual("", String(u.dropFirst(100))!)
expectEqual("", String(u.dropLast(100))!)

let expectSubstringWCSIA: Bool
// This availability guard should refer to a concrete OS version in
// future.
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
expectSubstringWCSIA = true
} else {
expectSubstringWCSIA = false
}

checkHasContiguousStorage(s.utf8, expected: true) // Strings always do
checkHasContiguousStorage(t, expected: expectSubstringWCSIA)
checkHasContiguousStorage(u, expected: expectSubstringWCSIA)
checkHasContiguousStorage(s.utf8) // Strings always do
checkHasContiguousStorageSubstring(t)
checkHasContiguousStorageSubstring(u)
checkMatchContiguousStorage(Array(s.utf8), s.utf8, expected: true)
checkMatchContiguousStorage(Array(t), t, expected: expectSubstringWCSIA)
checkMatchContiguousStorage(Array(u), u, expected: expectSubstringWCSIA)
checkMatchContiguousStorage(Array(s.utf8), s.utf8)

// The specialization for Substring.withContiguousStorageIfAvailable was
// added in https://github.com/apple/swift/pull/29146.
guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else {
return
}
checkHasContiguousStorage(t)
checkHasContiguousStorage(u)
checkMatchContiguousStorage(Array(t), t)
checkMatchContiguousStorage(Array(u), u)
}
}

Expand Down