Skip to content

Commit 2dcd822

Browse files
authored
Merge pull request swiftlang#24874 from Catfish-Man/bulk-tests
Test foreign strings on invalid content more thoroughly
2 parents 3847dbc + 7561f95 commit 2dcd822

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

test/stdlib/Inputs/NSSlowString/NSSlowString.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ - (instancetype)initWithString:(NSString *)name {
1818
return self;
1919
}
2020

21+
- (instancetype)initWithCharacters:(const unichar * _Nonnull)chars length:(NSUInteger)count {
22+
NSString *str = [[NSString alloc] initWithCharacters: chars length: count];
23+
self = [self initWithString: str];
24+
return self;
25+
}
26+
2127
- (NSUInteger)length {
2228
return self.stringHolder.length;
2329
}
@@ -34,4 +40,4 @@ - (void *) _fastCharacterContents {
3440
return nil;
3541
}
3642

37-
@end
43+
@end

validation-test/stdlib/StringUTF8.swift

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import StdlibUnittest
1616
import StdlibCollectionUnittest
17+
import StdlibUnicodeUnittest
1718

1819
#if _runtime(_ObjC)
1920
import NSSlowString
@@ -49,6 +50,35 @@ var strings: Array<String> = [
4950

5051
let kCFStringEncodingASCII: UInt32 = 0x0600
5152

53+
#if _runtime(_ObjC)
54+
55+
var utf16ByteSequences = utf16Tests.flatMap { $0.value }.map { $0.encoded }
56+
57+
private func testForeignContiguous(slowString: NSSlowString, string: String) {
58+
// Lazily bridged strings are not contiguous UTF-8
59+
var slowString = NSSlowString(string: string) as String
60+
expectFalse(slowString.isFastUTF8)
61+
expectEqualSequence(string.utf8, slowString.utf8)
62+
63+
// They become fast when mutated
64+
slowString.makeNative()
65+
expectTrue(slowString.isFastUTF8)
66+
expectEqualSequence(
67+
string.utf8, slowString.withFastUTF8IfAvailable(Array.init)!)
68+
69+
// Contiguous ASCII CFStrings provide access, even if lazily bridged
70+
if string.isASCII {
71+
let cfString = string.withCString {
72+
CFStringCreateWithCString(nil, $0, kCFStringEncodingASCII)!
73+
} as String
74+
expectTrue(cfString.isFastUTF8)
75+
expectEqualSequence(
76+
string.utf8, cfString.withFastUTF8IfAvailable(Array.init)!)
77+
}
78+
}
79+
80+
#endif
81+
5282
UTF8Tests.test("Contiguous Access") {
5383
for string in strings {
5484
print(string)
@@ -70,28 +100,20 @@ UTF8Tests.test("Contiguous Access") {
70100
// expectTrue(((copy as NSString) as String).isFastUTF8)
71101

72102
#if _runtime(_ObjC)
73-
// Lazily bridged strings are not contiguous UTF-8
74-
var slowString = NSSlowString(string: string) as String
75-
expectFalse(slowString.isFastUTF8)
76-
expectEqualSequence(string.utf8, slowString.utf8)
77-
78-
// They become fast when mutated
79-
slowString.makeNative()
80-
expectTrue(slowString.isFastUTF8)
81-
expectEqualSequence(
82-
string.utf8, slowString.withFastUTF8IfAvailable(Array.init)!)
83-
84-
// Contiguous ASCII CFStrings provide access, even if lazily bridged
85-
if string.isASCII {
86-
let cfString = string.withCString {
87-
CFStringCreateWithCString(nil, $0, kCFStringEncodingASCII)!
88-
} as String
89-
expectTrue(cfString.isFastUTF8)
90-
expectEqualSequence(
91-
string.utf8, cfString.withFastUTF8IfAvailable(Array.init)!)
92-
}
103+
testForeignContiguous(slowString: NSSlowString(string: string),
104+
string: string)
93105
#endif
94106
}
107+
#if _runtime(_ObjC)
108+
for bytes in utf16ByteSequences {
109+
bytes.withContiguousStorageIfAvailable {
110+
let slowString = NSSlowString(characters: $0.baseAddress!,
111+
length: UInt($0.count))
112+
let string = String(decoding: $0, as: UTF16.self)
113+
testForeignContiguous(slowString: slowString, string: string)
114+
}
115+
}
116+
#endif
95117
}
96118

97119
runAllTests()

0 commit comments

Comments
 (0)