@@ -280,6 +280,30 @@ CStringTests.test("String.cString.with.String.input") {
280
280
expectTrue ( str. isEmpty)
281
281
}
282
282
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
+
283
307
CStringTests . test ( " String.validatingUTF8.with.Array.input " ) {
284
308
do {
285
309
let ( u8p, dealloc) = getASCIIUTF8 ( )
@@ -317,6 +341,19 @@ CStringTests.test("String.validatingUTF8.with.String.input") {
317
341
expectEqual ( str? . isEmpty, true )
318
342
}
319
343
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
+
320
357
CStringTests . test ( " String.decodeCString.with.Array.input " ) {
321
358
do {
322
359
let ( u8p, dealloc) = getASCIIUTF8 ( )
@@ -359,7 +396,23 @@ CStringTests.test("String.decodeCString.with.String.input") {
359
396
expectEqual ( result? . result. isEmpty, true )
360
397
}
361
398
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 " ) {
363
416
do {
364
417
let ( u8p, dealloc) = getASCIIUTF8 ( )
365
418
defer { dealloc ( ) }
@@ -393,5 +446,17 @@ CStringTests.test("String.init.decodingCString.with.String.input") {
393
446
expectTrue ( str. isEmpty)
394
447
}
395
448
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
+
396
461
runAllTests ( )
397
462
0 commit comments