1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
//===--- BridgedStringSpanTests.swift -------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
import StdlibUnittest
import Foundation
var suite = TestSuite("EagerLazyBridging String Tests")
defer { runAllTests() }
let strings = [
"Hello, World!",
"123456789",
"A long ASCII string exceeding 16 code units.",
"🇯🇵",
"🏂☃❅❆❄︎⛄️❄️",
"z",
"",
]
strings.forEach { expected in
suite.test("Span from Bridged String Stability: \(expected)")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }
guard let nss = expectNotNil(NSString(utf8String: expected)) else { return }
let bridged = String(nss).utf8
var p: ObjectIdentifier? = nil
for (i, j) in zip(0..<3, bridged.indices) {
guard let span = expectNotNil(bridged._span) else { continue }
let c = span.withUnsafeBufferPointer {
let o = unsafeBitCast($0.baseAddress, to: ObjectIdentifier.self)
if p == nil {
p = o
} else {
expectEqual(p, o)
}
return $0[i]
}
expectEqual(c, bridged[j])
}
}
}
strings.forEach { expected in
suite.test("Span from Bridged String: \(expected)")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }
guard let nss = expectNotNil(NSString(utf8String: expected)) else { return }
let bridged = String(nss)
let utf8 = bridged.utf8
guard let span = expectNotNil(utf8._span) else { return }
expectEqual(span.count, expected.utf8.count)
for (i,j) in zip(span.indices, expected.utf8.indices) {
expectEqual(span[i], expected.utf8[j])
}
}
}
strings.forEach { expected in
suite.test("UTF8Span from Bridged String: \(expected)")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }
guard let nss = expectNotNil(NSString(utf8String: expected)) else { return }
let bridged = String(nss)
guard let utf8 = expectNotNil(bridged._utf8Span) else { return }
expectEqual(utf8.count, expected.utf8.count)
for (i,j) in zip(utf8.span.indices, expected.utf8.indices) {
expectEqual(utf8.span[i], expected.utf8[j])
}
}
}
strings.forEach { expected in
suite.test("Span from Bridged String Substring: \(expected)")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }
guard let nss = expectNotNil(NSString(utf8String: expected)) else { return }
let bridged = String(nss).dropFirst()
let utf8 = bridged.utf8
guard let span = expectNotNil(utf8._span) else { return }
let expected = expected.dropFirst()
expectEqual(span.count, expected.utf8.count)
for (i,j) in zip(span.indices, expected.utf8.indices) {
expectEqual(span[i], expected.utf8[j])
}
}
strings.forEach { expected in
suite.test("UTF8Span from Bridged String Substring: \(expected)")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }
guard let nss = expectNotNil(NSString(utf8String: expected)) else { return }
let bridged = String(nss).dropFirst()
guard let utf8 = expectNotNil(bridged._utf8Span) else { return }
let expected = expected.dropFirst()
expectEqual(utf8.count, expected.utf8.count)
for (i,j) in zip(utf8.span.indices, expected.utf8.indices) {
expectEqual(utf8.span[i], expected.utf8[j])
}
}
}
}
|