forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestStream.swift
307 lines (273 loc) · 14.3 KB
/
TestStream.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
#if canImport(SwiftFoundation) && !DEPLOYMENT_RUNTIME_OBJC
@testable import SwiftFoundation
#else
@testable import Foundation
#endif
#endif
private extension Data {
init(reading input: InputStream) {
self.init()
input.open()
let bufferSize = 1024
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
while input.hasBytesAvailable {
let read = input.read(buffer, maxLength: bufferSize)
self.append(buffer, count: read)
}
buffer.deallocate()
input.close()
}
}
class TestStream : XCTestCase {
func test_InputStreamWithData(){
let message: NSString = "Hello, playground"
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
let dataStream: InputStream = InputStream(data: messageData)
XCTAssertEqual(.notOpen, dataStream.streamStatus)
dataStream.open()
XCTAssertEqual(.open, dataStream.streamStatus)
var buffer = [UInt8](repeating: 0, count: 20)
if dataStream.hasBytesAvailable {
let result: Int = dataStream.read(&buffer, maxLength: buffer.count)
dataStream.close()
XCTAssertEqual(.closed, dataStream.streamStatus)
if(result > 0) {
let output = NSString(bytes: &buffer, length: buffer.firstIndex(of: 0) ?? buffer.count, encoding: String.Encoding.utf8.rawValue)
XCTAssertEqual(message, output!)
}
}
}
func test_InputStreamWithUrl() {
let message: NSString = "Hello, playground"
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
guard let testFile = createTestFile("testFile_in.txt", _contents: messageData) else {
XCTFail("Unable to create temp file")
return
}
//Initialiser with url
let url = URL(fileURLWithPath: testFile)
let urlStream: InputStream = InputStream(url: url)!
XCTAssertEqual(.notOpen, urlStream.streamStatus)
urlStream.open()
XCTAssertEqual(.open, urlStream.streamStatus)
var buffer = [UInt8](repeating: 0, count: 20)
if urlStream.hasBytesAvailable {
let result :Int = urlStream.read(&buffer, maxLength: buffer.count)
urlStream.close()
XCTAssertEqual(.closed, urlStream.streamStatus)
XCTAssertEqual(messageData.count, result)
if(result > 0) {
let output = NSString(bytes: &buffer, length: buffer.firstIndex(of: 0) ?? buffer.count, encoding: String.Encoding.utf8.rawValue)
XCTAssertEqual(message, output!)
}
}
removeTestFile(testFile)
}
func test_InputStreamWithFile() {
let message: NSString = "Hello, playground"
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
guard let testFile = createTestFile("testFile_in.txt", _contents: messageData) else {
XCTFail("Unable to create temp file")
return
}
//Initialiser with file
let fileStream: InputStream = InputStream(fileAtPath: testFile)!
XCTAssertEqual(.notOpen, fileStream.streamStatus)
fileStream.open()
XCTAssertEqual(.open, fileStream.streamStatus)
var buffer = [UInt8](repeating: 0, count: 20)
if fileStream.hasBytesAvailable {
let result: Int = fileStream.read(&buffer, maxLength: buffer.count)
fileStream.close()
XCTAssertEqual(.closed, fileStream.streamStatus)
XCTAssertEqual(messageData.count, result)
if(result > 0) {
let output = NSString(bytes: &buffer, length: buffer.firstIndex(of: 0) ?? buffer.count, encoding: String.Encoding.utf8.rawValue)
XCTAssertEqual(message, output!)
}
}
removeTestFile(testFile)
}
func test_InputStreamHasBytesAvailable() {
let message: NSString = "Hello, playground"
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
let stream: InputStream = InputStream(data: messageData)
var buffer = [UInt8](repeating: 0, count: 20)
stream.open()
XCTAssertTrue(stream.hasBytesAvailable)
_ = stream.read(&buffer, maxLength: buffer.count)
XCTAssertFalse(stream.hasBytesAvailable)
}
func test_InputStreamInvalidPath() {
let fileStream: InputStream = InputStream(fileAtPath: NSTemporaryDirectory() + "file.txt")!
XCTAssertEqual(.notOpen, fileStream.streamStatus)
fileStream.open()
XCTAssertEqual(.error, fileStream.streamStatus)
}
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT // Stream.seek(to:) is an internal API method
func test_InputStreamSeekToPosition() {
let str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras congue laoreet facilisis. Sed porta tristique orci. Fusce ut nisl dignissim, tempor tortor id, molestie neque. Nam non tincidunt mi. Integer ac diam quis leo aliquam congue et non magna. In porta mauris suscipit erat pulvinar, sed fringilla quam ornare. Nulla vulputate et ligula vitae sollicitudin. Nulla vel vehicula risus. Quisque eu urna ullamcorper, tincidunt ante vitae, aliquet sem. Suspendisse nec turpis placerat, porttitor ex vel, tristique orci. Maecenas pretium, augue non elementum imperdiet, diam ex vestibulum tortor, non ultrices ante enim iaculis ex. Fusce ut nisl dignissim, tempor tortor id, molestie neque. Nam non tincidunt mi. Integer ac diam quis leo aliquam congue et non magna. In porta mauris suscipit erat pulvinar, sed fringilla quam ornare. Nulla vulputate et ligula vitae sollicitudin. Nulla vel vehicula risus. Quisque eu urna ullamcorper, tincidunt ante vitae, aliquet sem. Suspendisse nec turpis placerat, porttitor ex vel, tristique orci. Maecenas pretium, augue non elementum imperdiet, diam ex vestibulum tortor, non ultrices ante enim iaculis ex.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras congue laoreet facilisis. Sed porta tristique orci. Fusce ut nisl dignissim, tempor tortor id, molestie neque. Nam non tincidunt mi. Integer ac diam quis leo aliquam congue et non magna. In porta mauris suscipit erat pulvinar, sed fringilla quam ornare. Nulla vulputate et ligula vitae sollicitudin. Nulla vel vehicula risus. Quisque eu urna ullamcorper, tincidunt ante vitae, aliquet sem. Suspendisse nec turpis placerat, porttitor ex vel."
XCTAssert(str.count > 1024) // str.count must be bigger than buffersize inside InputStream.seek func.
func testSubdata(_ pos: UInt64) throws -> Data? {
guard let data = str.data(using: .utf8) else {
XCTFail()
return nil
}
let stream = InputStream(data: data)
stream.open()
try stream.seek(to: pos)
let streamData = Data(reading: stream)
let subdata = data[Int(pos)..<data.count]
XCTAssertEqual(streamData, subdata)
return subdata
}
var sum = 0
for i in 0...str.count {
do {
sum += try testSubdata(UInt64(i))!.count
} catch _ {
XCTFail()
}
}
XCTAssertEqual(((1 + str.count) * str.count)/2, sum) // Test on sum of arithmetic sequence :)
XCTAssertEqual(try testSubdata(UInt64(str.count))!.count, 0) // It shouldbe end
do {
try testSubdata(UInt64(str.count + 1)) // out of boundaries
XCTFail()
} catch let error as InputStream._Error {
XCTAssertEqual(error, .cantSeekInputStream)
} catch {
XCTFail()
}
}
#endif
func test_outputStreamCreationToFile() {
guard let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)) else {
XCTFail("Unable to create temp file");
return
}
let outputStream = OutputStream(toFileAtPath: filePath, append: true)
XCTAssertEqual(.notOpen, outputStream!.streamStatus)
let myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
outputStream?.open()
XCTAssertEqual(.open, outputStream!.streamStatus)
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
outputStream?.close()
XCTAssertEqual(myString.count, result)
XCTAssertEqual(.closed, outputStream!.streamStatus)
removeTestFile(filePath)
}
func test_outputStreamCreationToBuffer() {
var buffer = Array<UInt8>(repeating: 0, count: 12)
let myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: 12)
XCTAssertEqual(.notOpen, outputStream.streamStatus)
outputStream.open()
XCTAssertEqual(.open, outputStream.streamStatus)
let result: Int? = outputStream.write(encodedData, maxLength: encodedData.count)
outputStream.close()
XCTAssertEqual(.closed, outputStream.streamStatus)
XCTAssertEqual(myString.count, result)
XCTAssertEqual(NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue), NSString(string: myString))
}
func test_outputStreamCreationWithUrl() {
guard let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)) else {
XCTFail("Unable to create temp file");
return
}
let outputStream = OutputStream(url: URL(fileURLWithPath: filePath), append: true)
XCTAssertEqual(.notOpen, outputStream!.streamStatus)
let myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
outputStream!.open()
XCTAssertEqual(.open, outputStream!.streamStatus)
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
outputStream?.close()
XCTAssertEqual(myString.count, result)
XCTAssertEqual(.closed, outputStream!.streamStatus)
removeTestFile(filePath)
}
func test_outputStreamCreationToMemory(){
var buffer = Array<UInt8>(repeating: 0, count: 12)
let myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
let outputStream = OutputStream.toMemory()
XCTAssertEqual(.notOpen, outputStream.streamStatus)
outputStream.open()
XCTAssertEqual(.open, outputStream.streamStatus)
let result: Int? = outputStream.write(encodedData, maxLength: encodedData.count)
XCTAssertEqual(myString.count, result)
//verify the data written
let dataWritten = outputStream.property(forKey: Stream.PropertyKey.dataWrittenToMemoryStreamKey)
if let nsdataWritten = dataWritten as? NSData {
nsdataWritten.getBytes(UnsafeMutablePointer(mutating: buffer), length: result!)
XCTAssertEqual(NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue), NSString(string: myString))
outputStream.close()
} else {
XCTFail("Unable to get data from memeory.")
}
}
func test_outputStreamHasSpaceAvailable() {
let buffer = Array<UInt8>(repeating: 0, count: 12)
let myString = "Welcome To Hello world !"
let encodedData = [UInt8](myString.utf8)
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: 12)
outputStream.open()
XCTAssertTrue(outputStream.hasSpaceAvailable)
_ = outputStream.write(encodedData, maxLength: encodedData.count)
XCTAssertFalse(outputStream.hasSpaceAvailable)
}
func test_ouputStreamWithInvalidPath(){
let outputStream = OutputStream(toFileAtPath: "http:///home/sdsfsdfd", append: true)
XCTAssertEqual(.notOpen, outputStream!.streamStatus)
outputStream?.open()
XCTAssertEqual(.error, outputStream!.streamStatus)
}
static var allTests: [(String, (TestStream) -> () throws -> Void)] {
var tests: [(String, (TestStream) -> () throws -> Void)] = [
("test_InputStreamWithData", test_InputStreamWithData),
("test_InputStreamWithUrl", test_InputStreamWithUrl),
("test_InputStreamWithFile", test_InputStreamWithFile),
("test_InputStreamHasBytesAvailable", test_InputStreamHasBytesAvailable),
("test_InputStreamInvalidPath", test_InputStreamInvalidPath),
("test_outputStreamCreationToFile", test_outputStreamCreationToFile),
("test_outputStreamCreationToBuffer", test_outputStreamCreationToBuffer),
("test_outputStreamCreationWithUrl", test_outputStreamCreationWithUrl),
("test_outputStreamCreationToMemory", test_outputStreamCreationToMemory),
("test_outputStreamHasSpaceAvailable", test_outputStreamHasSpaceAvailable),
("test_ouputStreamWithInvalidPath", test_ouputStreamWithInvalidPath),
]
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
tests.append(("test_InputStreamSeekToPosition", test_InputStreamSeekToPosition))
#endif
return tests
}
private func createTestFile(_ path: String, _contents: Data) -> String? {
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
do {
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
if FileManager.default.createFile(atPath: tempDir + "/" + path, contents: _contents,
attributes: nil) {
return tempDir + path
} else {
return nil
}
} catch {
return nil
}
}
private func removeTestFile(_ location: String) {
try? FileManager.default.removeItem(atPath: location)
}
}