@@ -267,6 +267,19 @@ CStringTests.test("String.cString.with.Array.CChar.input") {
267
267
expectUnreachable ( )
268
268
}
269
269
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
+
270
283
CStringTests . test ( " String.validatingUTF8.with.Array.input " ) {
271
284
do {
272
285
let ( u8p, dealloc) = getASCIIUTF8 ( )
@@ -289,6 +302,21 @@ CStringTests.test("String.validatingUTF8.with.Array.input") {
289
302
expectUnreachable ( )
290
303
}
291
304
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
+
292
320
CStringTests . test ( " String.decodeCString.with.Array.input " ) {
293
321
do {
294
322
let ( u8p, dealloc) = getASCIIUTF8 ( )
@@ -312,6 +340,25 @@ CStringTests.test("String.decodeCString.with.Array.input") {
312
340
expectUnreachable ( )
313
341
}
314
342
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
+
315
362
CStringTests . test ( " String.decodingCString.with.Array.input " ) {
316
363
do {
317
364
let ( u8p, dealloc) = getASCIIUTF8 ( )
@@ -333,5 +380,18 @@ CStringTests.test("String.decodingCString.with.Array.input") {
333
380
expectUnreachable ( )
334
381
}
335
382
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
+
336
396
runAllTests ( )
337
397
0 commit comments