@@ -22,6 +22,16 @@ class TestNSString : XCTestCase {
22
22
var allTests : [ ( String , ( ) -> ( ) ) ] {
23
23
return [
24
24
( " test_BridgeConstruction " , test_BridgeConstruction ) ,
25
+ ( " test_isEqualToStringWithSwiftString " , test_isEqualToStringWithSwiftString ) ,
26
+ ( " test_FromASCIIData " , test_FromASCIIData ) ,
27
+ ( " test_FromUTF8Data " , test_FromUTF8Data ) ,
28
+ ( " test_FromMalformedUTF8Data " , test_FromMalformedUTF8Data ) ,
29
+ ( " test_FromASCIINSData " , test_FromASCIINSData ) ,
30
+ ( " test_FromUTF8NSData " , test_FromUTF8NSData ) ,
31
+ ( " test_FromMalformedUTF8NSData " , test_FromMalformedUTF8NSData ) ,
32
+ ( " test_FromNullTerminatedCStringInASCII " , test_FromNullTerminatedCStringInASCII ) ,
33
+ ( " test_FromNullTerminatedCStringInUTF8 " , test_FromNullTerminatedCStringInUTF8 ) ,
34
+ ( " test_FromMalformedNullTerminatedCStringInUTF8 " , test_FromMalformedNullTerminatedCStringInUTF8 ) ,
25
35
]
26
36
}
27
37
@@ -42,4 +52,79 @@ class TestNSString : XCTestCase {
42
52
let cluster : NSString = " ✌🏾 "
43
53
XCTAssertEqual ( cluster. length, 3 )
44
54
}
55
+
56
+ func test_isEqualToStringWithSwiftString( ) {
57
+ let string : NSString = " literal "
58
+ let swiftString = " literal "
59
+ XCTAssertTrue ( string. isEqualToString ( swiftString) )
60
+ }
61
+
62
+ internal let mockASCIIStringBytes : [ UInt8 ] = [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 , 0x21 ]
63
+ internal let mockASCIIString = " Hello Swift! "
64
+ internal let mockUTF8StringBytes : [ UInt8 ] = [ 0x49 , 0x20 , 0xE2 , 0x9D , 0xA4 , 0xEF , 0xB8 , 0x8F , 0x20 , 0x53 , 0x77 , 0x69 , 0x66 , 0x74 ]
65
+ internal let mockUTF8String = " I ❤️ Swift "
66
+ internal let mockMalformedUTF8StringBytes : [ UInt8 ] = [ 0xFF ]
67
+
68
+ func test_FromASCIIData( ) {
69
+ let bytes = mockASCIIStringBytes
70
+ let string = NSString ( bytes: bytes, length: bytes. count, encoding: NSASCIIStringEncoding)
71
+ XCTAssertNotNil ( string)
72
+ XCTAssertTrue ( string? . isEqualToString ( mockASCIIString) ?? false )
73
+ }
74
+
75
+ func test_FromUTF8Data( ) {
76
+ let bytes = mockUTF8StringBytes
77
+ let string = NSString ( bytes: bytes, length: bytes. count, encoding: NSUTF8StringEncoding)
78
+ XCTAssertNotNil ( string)
79
+ XCTAssertTrue ( string? . isEqualToString ( mockUTF8String) ?? false )
80
+ }
81
+
82
+ func test_FromMalformedUTF8Data( ) {
83
+ let bytes = mockMalformedUTF8StringBytes
84
+ let string = NSString ( bytes: bytes, length: bytes. count, encoding: NSUTF8StringEncoding)
85
+ XCTAssertNil ( string)
86
+ }
87
+
88
+ func test_FromASCIINSData( ) {
89
+ let bytes = mockASCIIStringBytes
90
+ let data = NSData ( bytes: bytes, length: bytes. count)
91
+ let string = NSString ( data: data, encoding: NSASCIIStringEncoding)
92
+ XCTAssertNotNil ( string)
93
+ XCTAssertTrue ( string? . isEqualToString ( mockASCIIString) ?? false )
94
+ }
95
+
96
+ func test_FromUTF8NSData( ) {
97
+ let bytes = mockUTF8StringBytes
98
+ let data = NSData ( bytes: bytes, length: bytes. count)
99
+ let string = NSString ( data: data, encoding: NSUTF8StringEncoding)
100
+ XCTAssertNotNil ( string)
101
+ XCTAssertTrue ( string? . isEqualToString ( mockUTF8String) ?? false )
102
+ }
103
+
104
+ func test_FromMalformedUTF8NSData( ) {
105
+ let bytes = mockMalformedUTF8StringBytes
106
+ let data = NSData ( bytes: bytes, length: bytes. count)
107
+ let string = NSString ( data: data, encoding: NSUTF8StringEncoding)
108
+ XCTAssertNil ( string)
109
+ }
110
+
111
+ func test_FromNullTerminatedCStringInASCII( ) {
112
+ let bytes = mockASCIIStringBytes + [ 0x00 ]
113
+ let string = NSString ( CString: bytes. map { Int8 ( bitPattern: $0) } , encoding: NSASCIIStringEncoding)
114
+ XCTAssertNotNil ( string)
115
+ XCTAssertTrue ( string? . isEqualToString ( mockASCIIString) ?? false )
116
+ }
117
+
118
+ func test_FromNullTerminatedCStringInUTF8( ) {
119
+ let bytes = mockUTF8StringBytes + [ 0x00 ]
120
+ let string = NSString ( CString: bytes. map { Int8 ( bitPattern: $0) } , encoding: NSUTF8StringEncoding)
121
+ XCTAssertNotNil ( string)
122
+ XCTAssertTrue ( string? . isEqualToString ( mockUTF8String) ?? false )
123
+ }
124
+
125
+ func test_FromMalformedNullTerminatedCStringInUTF8( ) {
126
+ let bytes = mockMalformedUTF8StringBytes + [ 0x00 ]
127
+ let string = NSString ( CString: bytes. map { Int8 ( bitPattern: $0) } , encoding: NSUTF8StringEncoding)
128
+ XCTAssertNil ( string)
129
+ }
45
130
}
0 commit comments