Skip to content

Commit 9ef5ed0

Browse files
authored
[SR-14424] Add 'moveInitialize' test case for small string 'init(unsafeUninitializedCapacity:initializingUTF8With:)' (swiftlang#36742)
* [SR-14424] Add 'moveInitialize' test case for small string 'init(unsafeUninitializedCapacity:initializingUTF8With:)' ...and make an adjustment to an existing test case. * [stdlib][test] Update test cases for small string unsafeUninitializedCapacity based on reviewer feedback. * [fixup]
1 parent 228112f commit 9ef5ed0

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

test/stdlib/StringCreate.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,34 @@ if #available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
9494
}
9595
expectEqual(str1, str3)
9696

97-
// Write into uninitialized space
97+
// Write into uninitialized space.
9898
let str4 = String(unsafeUninitializedCapacity: 3) {
9999
$0[0] = UInt8(ascii: "4")
100100
$0[1] = UInt8(ascii: "2")
101101
$0[2] = UInt8(ascii: "X")
102+
// Deinitialize memory used for scratch space before returning.
103+
($0.baseAddress! + 2).deinitialize(count: 1)
102104
return 2
103105
}
104106
expectEqual(str1, str4)
107+
108+
let str5 = String(unsafeUninitializedCapacity: 3) {
109+
$0[1] = UInt8(ascii: "4")
110+
$0[2] = UInt8(ascii: "2")
111+
// Move the initialized UTF-8 code units to the start of the buffer,
112+
// leaving the non-overlapping source memory uninitialized.
113+
$0.baseAddress!.moveInitialize(from: $0.baseAddress! + 1, count: 2)
114+
return 2
115+
}
116+
expectEqual(str1, str5)
117+
118+
// Ensure reasonable behavior despite a deliberate API contract violation.
119+
let str6 = String(unsafeUninitializedCapacity: 3) {
120+
$0[0] = UInt8(ascii: "4")
121+
$0[1] = UInt8(ascii: "2")
122+
$0[2] = UInt8(ascii: "X")
123+
return 2
124+
}
125+
expectEqual(str1, str6)
105126
}
106127
}

0 commit comments

Comments
 (0)