Skip to content

Commit 1e5d690

Browse files
committed
[test] add tests of String-from-C-string String overloads
1 parent 7d2151f commit 1e5d690

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/stdlib/StringAPICString.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,19 @@ CStringTests.test("String.cString.with.Array.CChar.input") {
267267
expectUnreachable()
268268
}
269269

270+
CStringTests.test("String.cString.with.String.input") {
271+
let (u8p, dealloc) = getASCIIUTF8()
272+
defer { dealloc() }
273+
var str = String(cString: "ab")
274+
str.withCString {
275+
$0.withMemoryRebound(to: UInt8.self, capacity: getUTF8Length(u8p)+1) {
276+
expectEqualCString(u8p, $0)
277+
}
278+
}
279+
str = String(cString: "")
280+
expectTrue(str.isEmpty)
281+
}
282+
270283
CStringTests.test("String.validatingUTF8.with.Array.input") {
271284
do {
272285
let (u8p, dealloc) = getASCIIUTF8()
@@ -289,6 +302,21 @@ CStringTests.test("String.validatingUTF8.with.Array.input") {
289302
expectUnreachable()
290303
}
291304

305+
CStringTests.test("String.validatingUTF8.with.String.input") {
306+
let (u8p, dealloc) = getASCIIUTF8()
307+
defer { dealloc() }
308+
var str = String(validatingUTF8: "ab")
309+
expectNotNil(str)
310+
str?.withCString {
311+
$0.withMemoryRebound(to: UInt8.self, capacity: getUTF8Length(u8p)+1) {
312+
expectEqualCString(u8p, $0)
313+
}
314+
}
315+
str = String(validatingUTF8: "")
316+
expectNotNil(str)
317+
expectEqual(str?.isEmpty, true)
318+
}
319+
292320
CStringTests.test("String.decodeCString.with.Array.input") {
293321
do {
294322
let (u8p, dealloc) = getASCIIUTF8()
@@ -312,6 +340,25 @@ CStringTests.test("String.decodeCString.with.Array.input") {
312340
expectUnreachable()
313341
}
314342

343+
CStringTests.test("String.decodeCString.with.String.input") {
344+
let (u8p, dealloc) = getASCIIUTF8()
345+
defer { dealloc() }
346+
var result = String.decodeCString(
347+
"ab", as: Unicode.UTF8.self, repairingInvalidCodeUnits: true
348+
)
349+
expectNotNil(result)
350+
expectEqual(result?.repairsMade, false)
351+
result?.result.withCString {
352+
$0.withMemoryRebound(to: UInt8.self, capacity: getUTF8Length(u8p)+1) {
353+
expectEqualCString(u8p, $0)
354+
}
355+
}
356+
result = String.decodeCString("", as: Unicode.UTF8.self)
357+
expectNotNil(result)
358+
expectEqual(result?.repairsMade, false)
359+
expectEqual(result?.result.isEmpty, true)
360+
}
361+
315362
CStringTests.test("String.decodingCString.with.Array.input") {
316363
do {
317364
let (u8p, dealloc) = getASCIIUTF8()
@@ -333,5 +380,18 @@ CStringTests.test("String.decodingCString.with.Array.input") {
333380
expectUnreachable()
334381
}
335382

383+
CStringTests.test("String.init.decodingCString.with.String.input") {
384+
let (u8p, dealloc) = getASCIIUTF8()
385+
defer { dealloc() }
386+
var str = String(decodingCString: "ab", as: Unicode.UTF8.self)
387+
str.withCString {
388+
$0.withMemoryRebound(to: UInt8.self, capacity: getUTF8Length(u8p)+1) {
389+
expectEqualCString(u8p, $0)
390+
}
391+
}
392+
str = String(decodingCString: "", as: Unicode.UTF8.self)
393+
expectTrue(str.isEmpty)
394+
}
395+
336396
runAllTests()
337397

0 commit comments

Comments
 (0)