Skip to content

Commit 08304c3

Browse files
committed
[test] update bridged string tests
- move to a new file - create a new `NSString` object for each new test
1 parent 5f78809 commit 08304c3

File tree

2 files changed

+96
-40
lines changed

2 files changed

+96
-40
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===--- BridgedStringSpanTests.swift -------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// RUN: %target-run-stdlib-swift -enable-experimental-feature LifetimeDependence
14+
15+
// REQUIRES: executable_test
16+
// REQUIRES: objc_interop
17+
// REQUIRES: swift_feature_LifetimeDependence
18+
19+
import StdlibUnittest
20+
21+
import Foundation
22+
23+
var suite = TestSuite("EagerLazyBridging String Tests")
24+
defer { runAllTests() }
25+
26+
let strings = [
27+
"Hello, World!",
28+
"A long ASCII string exceeding 16 code units.",
29+
"πŸ‡―πŸ‡΅",
30+
"πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ",
31+
]
32+
33+
strings.forEach { expected in
34+
suite.test("Span from Bridged String Stability: \(expected)")
35+
.require(.stdlib_6_2).code {
36+
guard #available(SwiftStdlib 6.2, *) else { return }
37+
38+
let string = NSString(utf8String: expected)
39+
guard let string else { expectNotNil(string); return }
40+
41+
let bridged = String(string).utf8
42+
var p: ObjectIdentifier? = nil
43+
for (i, j) in zip(0..<3, bridged.indices) {
44+
let span = bridged.span
45+
let c = span.withUnsafeBufferPointer {
46+
let o = unsafeBitCast($0.baseAddress, to: ObjectIdentifier.self)
47+
if p == nil {
48+
p = o
49+
} else {
50+
expectEqual(p, o)
51+
}
52+
return $0[i]
53+
}
54+
expectEqual(c, bridged[j])
55+
}
56+
}
57+
}
58+
59+
strings.forEach { expected in
60+
suite.test("Span from Bridged String: \(expected)")
61+
.require(.stdlib_6_2).code {
62+
guard #available(SwiftStdlib 6.2, *) else { return }
63+
64+
let string = NSString(utf8String: expected)
65+
guard let string else { expectNotNil(string); return }
66+
67+
let bridged = String(string)
68+
let utf8 = bridged.utf8
69+
let span = utf8.span
70+
expectEqual(span.count, expected.utf8.count)
71+
for (i,j) in zip(span.indices, expected.utf8.indices) {
72+
expectEqual(span[i], expected.utf8[j])
73+
}
74+
}
75+
}
76+
77+
strings.forEach { expected in
78+
suite.test("Span from Bridged String Substring: \(expected)")
79+
.require(.stdlib_6_2).code {
80+
guard #available(SwiftStdlib 6.2, *) else { return }
81+
82+
let string = NSString(utf8String: expected)
83+
guard let string else { expectNotNil(string); return }
84+
85+
let bridged = String(string).dropFirst()
86+
let utf8 = bridged.utf8
87+
let span = utf8.span
88+
let expected = expected.dropFirst()
89+
expectEqual(span.count, expected.utf8.count)
90+
for (i,j) in zip(span.indices, expected.utf8.indices) {
91+
expectEqual(span[i], expected.utf8[j])
92+
}
93+
}
94+
}

β€Žtest/stdlib/Span/StringUTF8SpanProperty.swift

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// RUN: %target-run-stdlib-swift(-enable-experimental-feature LifetimeDependence -enable-experimental-feature Span -enable-experimental-feature AddressableTypes)
1414

1515
// REQUIRES: executable_test
16+
// REQUIRES: swift_feature_LifetimeDependence
17+
// REQUIRES: swift_feature_AddressableTypes
1618

1719
import StdlibUnittest
1820

@@ -83,43 +85,3 @@ suite.test("Span from Large Native String's Substring")
8385
expectEqual(span[i], u[i])
8486
}
8587
}
86-
87-
import Foundation
88-
89-
let strings: [NSString: String] = [
90-
"Hello, World!" as NSString: "Hello, World!",
91-
"A long ASCII string exceeding 16 code units." as NSString: "A long ASCII string exceeding 16 code units.",
92-
"πŸ‡―πŸ‡΅" as NSString: "πŸ‡―πŸ‡΅",
93-
NSString(utf8String: "πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ")!: "πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ",
94-
]
95-
96-
strings.forEach { string, expected in
97-
suite.test("Span from Bridged String: \(expected)")
98-
.require(.stdlib_6_2).code {
99-
guard #available(SwiftStdlib 6.2, *) else { return }
100-
101-
let bridged = String(string)
102-
let utf8 = bridged.utf8
103-
let span = utf8.span
104-
expectEqual(span.count, expected.utf8.count)
105-
for (i,j) in zip(span.indices, expected.utf8.indices) {
106-
expectEqual(span[i], expected.utf8[j])
107-
}
108-
}
109-
}
110-
111-
strings.forEach { string, expected in
112-
suite.test("Span from Bridged String Substring: \(expected)")
113-
.require(.stdlib_6_2).code {
114-
guard #available(SwiftStdlib 6.2, *) else { return }
115-
116-
let bridged = String(string).dropFirst()
117-
let utf8 = bridged.utf8
118-
let span = utf8.span
119-
let expected = expected.dropFirst()
120-
expectEqual(span.count, expected.utf8.count)
121-
for (i,j) in zip(span.indices, expected.utf8.indices) {
122-
expectEqual(span[i], expected.utf8[j])
123-
}
124-
}
125-
}

0 commit comments

Comments
Β (0)