Skip to content

Commit c594482

Browse files
committed
[test] add tests of String-from-C-string inout conversion overloads
1 parent 0285602 commit c594482

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

test/stdlib/StringAPICString.swift

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,30 @@ CStringTests.test("String.cString.with.String.input") {
280280
expectTrue(str.isEmpty)
281281
}
282282

283+
CStringTests.test("String.cString.with.inout.UInt8.conversion") {
284+
var c = UInt8.zero
285+
var str = String(cString: &c)
286+
expectTrue(str.isEmpty)
287+
c = 100
288+
expectCrashLater(
289+
withMessage: "input of String.init(cString:) must be null-terminated"
290+
)
291+
str = String(cString: &c)
292+
expectUnreachable()
293+
}
294+
295+
CStringTests.test("String.cString.with.inout.CChar.conversion") {
296+
var c = CChar.zero
297+
var str = String(cString: &c)
298+
expectTrue(str.isEmpty)
299+
c = 100
300+
expectCrashLater(
301+
withMessage: "input of String.init(cString:) must be null-terminated"
302+
)
303+
str = String(cString: &c)
304+
expectUnreachable()
305+
}
306+
283307
CStringTests.test("String.validatingUTF8.with.Array.input") {
284308
do {
285309
let (u8p, dealloc) = getASCIIUTF8()
@@ -317,6 +341,19 @@ CStringTests.test("String.validatingUTF8.with.String.input") {
317341
expectEqual(str?.isEmpty, true)
318342
}
319343

344+
CStringTests.test("String.validatingUTF8.with.inout.conversion") {
345+
var c = CChar.zero
346+
var str = String(validatingUTF8: &c)
347+
expectNotNil(str)
348+
expectEqual(str?.isEmpty, true)
349+
c = 100
350+
expectCrashLater(
351+
withMessage: "input of String.init(validatingUTF8:) must be null-terminated"
352+
)
353+
str = String(validatingUTF8: &c)
354+
expectUnreachable()
355+
}
356+
320357
CStringTests.test("String.decodeCString.with.Array.input") {
321358
do {
322359
let (u8p, dealloc) = getASCIIUTF8()
@@ -359,7 +396,23 @@ CStringTests.test("String.decodeCString.with.String.input") {
359396
expectEqual(result?.result.isEmpty, true)
360397
}
361398

362-
CStringTests.test("String.decodingCString.with.Array.input") {
399+
CStringTests.test("String.decodeCString.with.inout.conversion") {
400+
var c = Unicode.UTF8.CodeUnit.zero
401+
var result = String.decodeCString(
402+
&c, as: Unicode.UTF8.self, repairingInvalidCodeUnits: true
403+
)
404+
expectNotNil(result)
405+
expectEqual(result?.result.isEmpty, true)
406+
expectEqual(result?.repairsMade, false)
407+
c = 100
408+
expectCrashLater(
409+
withMessage: "input of decodeCString(_:as:repairingInvalidCodeUnits:) must be null-terminated"
410+
)
411+
result = String.decodeCString(&c, as: Unicode.UTF8.self)
412+
expectUnreachable()
413+
}
414+
415+
CStringTests.test("String.init.decodingCString.with.Array.input") {
363416
do {
364417
let (u8p, dealloc) = getASCIIUTF8()
365418
defer { dealloc() }
@@ -393,5 +446,17 @@ CStringTests.test("String.init.decodingCString.with.String.input") {
393446
expectTrue(str.isEmpty)
394447
}
395448

449+
CStringTests.test("String.init.decodingCString.with.inout.conversion") {
450+
var c = Unicode.UTF8.CodeUnit.zero
451+
var str = String(decodingCString: &c, as: Unicode.UTF8.self)
452+
expectEqual(str.isEmpty, true)
453+
c = 100
454+
expectCrashLater(
455+
withMessage: "input of String.init(decodingCString:as:) must be null-terminated"
456+
)
457+
str = String(decodingCString: &c, as: Unicode.UTF8.self)
458+
expectUnreachable()
459+
}
460+
396461
runAllTests()
397462

0 commit comments

Comments
 (0)