@@ -225,5 +225,113 @@ CStringTests.test("Substring.withCString") {
225225 }
226226}
227227
228+ CStringTests . test ( " String.cString.with.Array.UInt8.input " ) {
229+ do {
230+ let ( u8p, dealloc) = getASCIIUTF8 ( )
231+ defer { dealloc ( ) }
232+ let cstr = UnsafePointer ( u8p)
233+ let buffer = UnsafeBufferPointer ( start: cstr, count: getUTF8Length ( u8p) + 1 )
234+ let str = String ( cString: Array ( buffer) )
235+ str. withCString {
236+ $0. withMemoryRebound ( to: UInt8 . self, capacity: buffer. count) {
237+ expectEqualCString ( u8p, $0)
238+ }
239+ }
240+ }
241+ // no need to test every case; that is covered in other tests
242+ expectCrashLater (
243+ withMessage: " input of String.init(cString:) must be null-terminated "
244+ )
245+ _ = String ( cString: [ ] as [ UInt8 ] )
246+ expectUnreachable ( )
247+ }
248+
249+ CStringTests . test ( " String.cString.with.Array.CChar.input " ) {
250+ do {
251+ let ( u8p, dealloc) = getASCIIUTF8 ( )
252+ defer { dealloc ( ) }
253+ let cstr = UnsafeRawPointer ( u8p) . assumingMemoryBound ( to: CChar . self)
254+ let buffer = UnsafeBufferPointer ( start: cstr, count: getUTF8Length ( u8p) + 1 )
255+ let str = String ( cString: Array ( buffer) )
256+ str. withCString {
257+ $0. withMemoryRebound ( to: UInt8 . self, capacity: buffer. count) {
258+ expectEqualCString ( u8p, $0)
259+ }
260+ }
261+ }
262+ // no need to test every case; that is covered in other tests
263+ expectCrashLater (
264+ withMessage: " input of String.init(cString:) must be null-terminated "
265+ )
266+ _ = String ( cString: [ ] as [ CChar ] )
267+ expectUnreachable ( )
268+ }
269+
270+ CStringTests . test ( " String.validatingUTF8.with.Array.input " ) {
271+ do {
272+ let ( u8p, dealloc) = getASCIIUTF8 ( )
273+ defer { dealloc ( ) }
274+ let cstr = UnsafeRawPointer ( u8p) . assumingMemoryBound ( to: CChar . self)
275+ let buffer = UnsafeBufferPointer ( start: cstr, count: getUTF8Length ( u8p) + 1 )
276+ let str = String ( validatingUTF8: Array ( buffer) )
277+ expectNotNil ( str)
278+ str? . withCString {
279+ $0. withMemoryRebound ( to: UInt8 . self, capacity: buffer. count) {
280+ expectEqualCString ( u8p, $0)
281+ }
282+ }
283+ }
284+ // no need to test every case; that is covered in other tests
285+ expectCrashLater (
286+ withMessage: " input of String.init(validatingUTF8:) must be null-terminated "
287+ )
288+ _ = String ( validatingUTF8: [ ] )
289+ expectUnreachable ( )
290+ }
291+
292+ CStringTests . test ( " String.decodeCString.with.Array.input " ) {
293+ do {
294+ let ( u8p, dealloc) = getASCIIUTF8 ( )
295+ defer { dealloc ( ) }
296+ let cstr = UnsafeRawPointer ( u8p) . assumingMemoryBound ( to: Unicode . UTF8. CodeUnit. self)
297+ let buffer = UnsafeBufferPointer ( start: cstr, count: getUTF8Length ( u8p) + 1 )
298+ let result = String . decodeCString ( Array ( buffer) , as: Unicode . UTF8. self)
299+ expectNotNil ( result)
300+ expectEqual ( result? . repairsMade, false )
301+ result? . result. withCString {
302+ $0. withMemoryRebound ( to: UInt8 . self, capacity: buffer. count) {
303+ expectEqualCString ( u8p, $0)
304+ }
305+ }
306+ }
307+ // no need to test every case; that is covered in other tests
308+ expectCrashLater (
309+ withMessage: " input of decodeCString(_:as:repairingInvalidCodeUnits:) must be null-terminated "
310+ )
311+ _ = String . decodeCString ( [ ] , as: Unicode . UTF8. self)
312+ expectUnreachable ( )
313+ }
314+
315+ CStringTests . test ( " String.decodingCString.with.Array.input " ) {
316+ do {
317+ let ( u8p, dealloc) = getASCIIUTF8 ( )
318+ defer { dealloc ( ) }
319+ let cstr = UnsafeRawPointer ( u8p) . assumingMemoryBound ( to: Unicode . UTF8. CodeUnit. self)
320+ let buffer = UnsafeBufferPointer ( start: cstr, count: getUTF8Length ( u8p) + 1 )
321+ let str = String ( decodingCString: Array ( buffer) , as: Unicode . UTF8. self)
322+ str. withCString {
323+ $0. withMemoryRebound ( to: UInt8 . self, capacity: buffer. count) {
324+ expectEqualCString ( u8p, $0)
325+ }
326+ }
327+ }
328+ // no need to test every case; that is covered in other tests
329+ expectCrashLater (
330+ withMessage: " input of decodeCString(_:as:repairingInvalidCodeUnits:) must be null-terminated "
331+ )
332+ _ = String ( decodingCString: [ ] , as: Unicode . UTF8. self)
333+ expectUnreachable ( )
334+ }
335+
228336runAllTests ( )
229337
0 commit comments