@@ -23,6 +23,12 @@ class TestNSStream : XCTestCase {
23
23
( " test_InputStreamWithFile " , test_InputStreamWithFile) ,
24
24
( " test_InputStreamHasBytesAvailable " , test_InputStreamHasBytesAvailable) ,
25
25
( " test_InputStreamInvalidPath " , test_InputStreamInvalidPath) ,
26
+ ( " test_outputStreamCreationToFile " , test_outputStreamCreationToFile) ,
27
+ ( " test_outputStreamCreationToBuffer " , test_outputStreamCreationToBuffer) ,
28
+ ( " test_outputStreamCreationWithUrl " , test_outputStreamCreationWithUrl) ,
29
+ ( " test_outputStreamCreationToMemory " , test_outputStreamCreationToMemory) ,
30
+ ( " test_outputStreamHasSpaceAvailable " , test_outputStreamHasSpaceAvailable) ,
31
+ ( " test_ouputStreamWithInvalidPath " , test_ouputStreamWithInvalidPath) ,
26
32
]
27
33
}
28
34
@@ -118,19 +124,111 @@ class TestNSStream : XCTestCase {
118
124
XCTAssertEqual ( Stream . Status. error, fileStream. streamStatus)
119
125
}
120
126
121
- private func createTestFile( _ path: String , _contents: Data ) -> String ? {
127
+ func test_outputStreamCreationToFile( ) {
128
+ let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) !)
129
+ if filePath != nil {
130
+ let outputStream = NSOutputStream ( toFileAtPath: filePath!, append: true )
131
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
132
+ var myString = " Hello world! "
133
+ let encodedData = [ UInt8] ( myString. utf8)
134
+ outputStream? . open ( )
135
+ XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
136
+ let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
137
+ outputStream? . close ( )
138
+ XCTAssertEqual ( myString. characters. count, result)
139
+ XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
140
+ removeTestFile ( filePath!)
141
+ } else {
142
+ XCTFail ( " Unable to create temp file " ) ;
143
+ }
144
+ }
145
+
146
+ func test_outputStreamCreationToBuffer( ) {
147
+ var buffer = Array < UInt8 > ( repeating: 0 , count: 12 )
148
+ var myString = " Hello world! "
149
+ let encodedData = [ UInt8] ( myString. utf8)
150
+ let outputStream = NSOutputStream ( toBuffer: UnsafeMutablePointer < UInt8 > ( buffer) , capacity: 12 )
151
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream. streamStatus)
152
+ outputStream. open ( )
153
+ XCTAssertEqual ( Stream . Status. open, outputStream. streamStatus)
154
+ let result : Int ? = outputStream. write ( encodedData, maxLength: encodedData. count)
155
+ outputStream. close ( )
156
+ XCTAssertEqual ( Stream . Status. closed, outputStream. streamStatus)
157
+ XCTAssertEqual ( myString. characters. count, result)
158
+ XCTAssertEqual ( NSString ( bytes: & buffer, length: buffer. count, encoding: String . Encoding. utf8. rawValue) , myString. _bridgeToObject ( ) )
159
+ }
160
+
161
+ func test_outputStreamCreationWithUrl( ) {
162
+ let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) !)
163
+ if filePath != nil {
164
+ let outputStream = NSOutputStream ( url: URL ( fileURLWithPath: filePath!) , append: true )
165
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
166
+ var myString = " Hello world! "
167
+ let encodedData = [ UInt8] ( myString. utf8)
168
+ outputStream!. open ( )
169
+ XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
170
+ let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
171
+ outputStream? . close ( )
172
+ XCTAssertEqual ( myString. characters. count, result)
173
+ XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
174
+ removeTestFile ( filePath!)
175
+ } else {
176
+ XCTFail ( " Unable to create temp file " ) ;
177
+ }
178
+ }
179
+
180
+ func test_outputStreamCreationToMemory( ) {
181
+ var buffer = Array < UInt8 > ( repeating: 0 , count: 12 )
182
+ var myString = " Hello world! "
183
+ let encodedData = [ UInt8] ( myString. utf8)
184
+ let outputStream = NSOutputStream . outputStreamToMemory ( )
185
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream. streamStatus)
186
+ outputStream. open ( )
187
+ XCTAssertEqual ( Stream . Status. open, outputStream. streamStatus)
188
+ let result : Int ? = outputStream. write ( encodedData, maxLength: encodedData. count)
189
+ XCTAssertEqual ( myString. characters. count, result)
190
+ //verify the data written
191
+ let dataWritten = outputStream. propertyForKey ( NSStreamDataWrittenToMemoryStreamKey)
192
+ if let nsdataWritten = dataWritten as? NSData {
193
+ nsdataWritten. getBytes ( UnsafeMutablePointer < UInt8 > ( buffer) , length: result!)
194
+ XCTAssertEqual ( NSString ( bytes: & buffer, length: buffer. count, encoding: String . Encoding. utf8. rawValue) , myString. _bridgeToObject ( ) )
195
+ outputStream. close ( )
196
+ } else {
197
+ XCTFail ( " Unable to get data from memeory. " )
198
+ }
199
+ }
200
+
201
+ func test_outputStreamHasSpaceAvailable( ) {
202
+ let buffer = Array < UInt8 > ( repeating: 0 , count: 12 )
203
+ var myString = " Welcome To Hello world ! "
204
+ let encodedData = [ UInt8] ( myString. utf8)
205
+ let outputStream = NSOutputStream ( toBuffer: UnsafeMutablePointer < UInt8 > ( buffer) , capacity: 12 )
206
+ outputStream. open ( )
207
+ XCTAssertTrue ( outputStream. hasSpaceAvailable)
208
+ _ = outputStream. write ( encodedData, maxLength: encodedData. count)
209
+ XCTAssertFalse ( outputStream. hasSpaceAvailable)
210
+ }
211
+
212
+ func test_ouputStreamWithInvalidPath( ) {
213
+ let outputStream = NSOutputStream ( toFileAtPath: " http:///home/sdsfsdfd " , append: true )
214
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
215
+ outputStream? . open ( )
216
+ XCTAssertEqual ( Stream . Status. error, outputStream!. streamStatus)
217
+ }
218
+
219
+ private func createTestFile( _ path: String , _contents: Data ) -> String ? {
122
220
let tempDir = " /tmp/TestFoundation_Playground_ " + NSUUID( ) . UUIDString + " / "
123
221
do {
124
222
try FileManager . default ( ) . createDirectory ( atPath: tempDir, withIntermediateDirectories: false , attributes: nil )
125
- if FileManager . default ( ) . createFile ( atPath: tempDir + " / " + path, contents: _contents, attributes: nil ) {
126
- return tempDir + path
223
+ if FileManager . default ( ) . createFile ( atPath: tempDir + " / " + path, contents: _contents,
224
+ attributes: nil ) {
225
+ return tempDir + path
127
226
} else {
128
227
return nil
129
228
}
130
229
} catch _ {
131
230
return nil
132
231
}
133
-
134
232
}
135
233
136
234
private func removeTestFile( _ location: String ) {
@@ -142,4 +240,3 @@ class TestNSStream : XCTestCase {
142
240
}
143
241
}
144
242
145
-
0 commit comments