@@ -26,6 +26,9 @@ class TestNSString : XCTestCase {
2626 ( " test_FromASCIIData " , test_FromASCIIData ) ,
2727 ( " test_FromUTF8Data " , test_FromUTF8Data ) ,
2828 ( " test_FromMalformedUTF8Data " , test_FromMalformedUTF8Data ) ,
29+ ( " test_FromASCIINSData " , test_FromASCIINSData ) ,
30+ ( " test_FromUTF8NSData " , test_FromUTF8NSData ) ,
31+ ( " test_FromMalformedUTF8NSData " , test_FromMalformedUTF8NSData ) ,
2932 ]
3033 }
3134
@@ -71,4 +74,27 @@ class TestNSString : XCTestCase {
7174 let string = NSString ( bytes: bytes, length: bytes. count, encoding: NSUTF8StringEncoding)
7275 XCTAssertNil ( string)
7376 }
77+
78+ func test_FromASCIINSData( ) {
79+ let bytes : [ UInt8 ] = [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 , 0x21 ] // "Hello Swift!"
80+ let data = NSData ( bytes: bytes, length: bytes. count)
81+ let string = NSString ( data: data, encoding: NSASCIIStringEncoding)
82+ XCTAssertNotNil ( string)
83+ XCTAssertTrue ( string!. isEqualToString ( " Hello Swift! " ) )
84+ }
85+
86+ func test_FromUTF8NSData( ) {
87+ let bytes : [ UInt8 ] = [ 0x49 , 0x20 , 0xE2 , 0x9D , 0xA4 , 0xEF , 0xB8 , 0x8F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 ] // "I ❤️ Swift"
88+ let data = NSData ( bytes: bytes, length: bytes. count)
89+ let string = NSString ( data: data, encoding: NSUTF8StringEncoding)
90+ XCTAssertNotNil ( string)
91+ XCTAssertTrue ( string? . isEqualToString ( " I ❤️ Swift " ) ?? false )
92+ }
93+
94+ func test_FromMalformedUTF8NSData( ) {
95+ let bytes : [ UInt8 ] = [ 0xFF ]
96+ let data = NSData ( bytes: bytes, length: bytes. count)
97+ let string = NSString ( data: data, encoding: NSUTF8StringEncoding)
98+ XCTAssertNil ( string)
99+ }
74100}
0 commit comments