Skip to content

Commit 9c8484b

Browse files
committed
[test] update bridged string tests
- move to a new file - create a new `NSString` object for each new test
1 parent 1f96583 commit 9c8484b

File tree

2 files changed

+97
-40
lines changed

2 files changed

+97
-40
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
expectNotNil(string)
40+
guard let string = NSString(utf8String: expected) else { return }
41+
42+
let bridged = String(string).utf8
43+
var p: ObjectIdentifier? = nil
44+
for (i, j) in zip(0..<3, bridged.indices) {
45+
let span = bridged.span
46+
let c = span.withUnsafeBufferPointer {
47+
let o = unsafeBitCast($0.baseAddress, to: ObjectIdentifier.self)
48+
if p == nil {
49+
p = o
50+
} else {
51+
expectEqual(p, o)
52+
}
53+
return $0[i]
54+
}
55+
expectEqual(c, bridged[j])
56+
}
57+
}
58+
}
59+
60+
strings.forEach { expected in
61+
suite.test("Span from Bridged String: \(expected)")
62+
.require(.stdlib_6_2).code {
63+
guard #available(SwiftStdlib 6.2, *) else { return }
64+
65+
let string = NSString(utf8String: expected)
66+
expectNotNil(string)
67+
guard let string = NSString(utf8String: expected) else { return }
68+
69+
let bridged = String(string)
70+
let utf8 = bridged.utf8
71+
let span = utf8.span
72+
expectEqual(span.count, expected.utf8.count)
73+
for (i,j) in zip(span.indices, expected.utf8.indices) {
74+
expectEqual(span[i], expected.utf8[j])
75+
}
76+
}
77+
}
78+
79+
strings.forEach { expected in
80+
suite.test("Span from Bridged String Substring: \(expected)")
81+
.require(.stdlib_6_2).code {
82+
guard #available(SwiftStdlib 6.2, *) else { return }
83+
84+
let string = NSString(utf8String: expected)
85+
expectNotNil(string)
86+
guard let string = NSString(utf8String: expected) else { return }
87+
88+
let bridged = String(string).dropFirst()
89+
let utf8 = bridged.utf8
90+
let span = utf8.span
91+
let expected = expected.dropFirst()
92+
expectEqual(span.count, expected.utf8.count)
93+
for (i,j) in zip(span.indices, expected.utf8.indices) {
94+
expectEqual(span[i], expected.utf8[j])
95+
}
96+
}
97+
}

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

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

0 commit comments

Comments
Β (0)