Files
swift-mirror/validation-test/stdlib/StringUTF8.swift
Saleem Abdulrasool 1c41c0887c validation-test: adjust stdlib tests for Windows
The embedded shell script in the RUN command for lit is problematic for
non-sh shell environments (i.e. Windows).  This adjusts the tests to
uniformly build the code for the ObjC runtime.  However, the Objective-C
code is only built under the same circumstances that it is currently
enabled - the availability of the needed frameworks.  The empty object
on other runtimes will have no material impact.  The swift side of it
checks whether the runtime is built with ObjC interop.  This allows us
to largely use the same command line for all the targets.  The last
missing piece is that the `-fobjc-runtime` requires that we run a modern
ObjC runtime.  We enable this unconditionally in lit for the non-Apple
targets.

This improves the validation test coverage for the standard library on
Windows.
2019-06-03 08:36:22 -07:00

116 lines
3.2 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %target-clang -fobjc-arc %S/Inputs/NSSlowString/NSSlowString.m -c -o %t/NSSlowString.o
// RUN: %target-build-swift -I %S/Inputs/NSSlowString/ %t/NSSlowString.o %s -o %t/String
// RUN: %target-codesign %t/String
// RUN: %target-run %t/String
// REQUIRES: executable_test
// XFAIL: interpret
import StdlibUnittest
import StdlibCollectionUnittest
import StdlibUnicodeUnittest
#if _runtime(_ObjC)
import NSSlowString
import Foundation // For NSRange
#endif
extension String {
func withFastUTF8IfAvailable<R>(
_ f: (UnsafeBufferPointer<UInt8>) throws -> R
) rethrows -> R? {
return try utf8.withContiguousStorageIfAvailable(f)
}
var isFastUTF8: Bool {
return withFastUTF8IfAvailable({ _ in return 0 }) != nil
}
mutating func makeNative() { self += "" }
var isASCII: Bool { return utf8.allSatisfy { $0 < 0x7f } }
}
var UTF8Tests = TestSuite("StringUTF8Tests")
var strings: Array<String> = [
"abcd",
"abcdefghijklmnop",
"abcde\u{301}fghijk",
"a\u{301}",
"👻",
"Spooky long string. 👻",
"в чащах юга жил-был цитрус? да, но фальшивый экземпляр",
"",
]
let kCFStringEncodingASCII: UInt32 = 0x0600
#if _runtime(_ObjC)
var utf16ByteSequences = utf16Tests.flatMap { $0.value }.map { $0.encoded }
private func testForeignContiguous(slowString: NSSlowString, string: String) {
// Lazily bridged strings are not contiguous UTF-8
var slowString = NSSlowString(string: string) as String
expectFalse(slowString.isFastUTF8)
expectEqualSequence(string.utf8, slowString.utf8)
// They become fast when mutated
slowString.makeNative()
expectTrue(slowString.isFastUTF8)
expectEqualSequence(
string.utf8, slowString.withFastUTF8IfAvailable(Array.init)!)
// Contiguous ASCII CFStrings provide access, even if lazily bridged
if string.isASCII {
let cfString = string.withCString {
CFStringCreateWithCString(nil, $0, kCFStringEncodingASCII)!
} as String
expectTrue(cfString.isFastUTF8)
expectEqualSequence(
string.utf8, cfString.withFastUTF8IfAvailable(Array.init)!)
}
}
#endif
UTF8Tests.test("Contiguous Access") {
for string in strings {
print(string)
// Native strings are contiguous UTF-8
expectTrue(string.isFastUTF8)
expectEqualSequence(
Array(string.utf8), string.withFastUTF8IfAvailable(Array.init)!)
// FIXME: Bridge small non-ASCII as StringStorage
// expectTrue(((string as NSString) as String).isFastUTF8)
var copy = string
expectTrue(copy.isFastUTF8)
copy.makeNative()
expectTrue(copy.isFastUTF8)
// FIXME: Bridge small non-ASCII as StringStorage
// expectTrue(((copy as NSString) as String).isFastUTF8)
#if _runtime(_ObjC)
testForeignContiguous(slowString: NSSlowString(string: string),
string: string)
#endif
}
#if _runtime(_ObjC)
for bytes in utf16ByteSequences {
bytes.withContiguousStorageIfAvailable {
let slowString = NSSlowString(characters: $0.baseAddress!,
length: UInt($0.count))
let string = String(decoding: $0, as: UTF16.self)
testForeignContiguous(slowString: slowString, string: string)
}
}
#endif
}
runAllTests()