forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestStream.swift
242 lines (225 loc) · 11.1 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
// 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 DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
class TestStream : XCTestCase {
static var allTests: [(String, (TestStream) -> () throws -> Void)] {
return [
("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),
]
}
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(Stream.Status.notOpen, dataStream.streamStatus)
dataStream.open()
XCTAssertEqual(Stream.Status.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(Stream.Status.closed, dataStream.streamStatus)
if(result > 0){
let output = NSString(bytes: &buffer, length: 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)!
//Initialiser with url
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
if testFile != nil {
let url = URL(fileURLWithPath: testFile!)
let urlStream: InputStream = InputStream(url: url)!
XCTAssertEqual(Stream.Status.notOpen, urlStream.streamStatus)
urlStream.open()
XCTAssertEqual(Stream.Status.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(Stream.Status.closed, urlStream.streamStatus)
XCTAssertEqual(messageData.count, result)
if(result > 0) {
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
XCTAssertEqual(message, output!)
}
}
removeTestFile(testFile!)
} else {
XCTFail("Unable to create temp file")
}
}
func test_InputStreamWithFile() {
let message: NSString = "Hello, playground"
let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)!
//Initialiser with file
let testFile = createTestFile("testFile_in.txt", _contents: messageData)
if testFile != nil {
let fileStream: InputStream = InputStream(fileAtPath: testFile!)!
XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus)
fileStream.open()
XCTAssertEqual(Stream.Status.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(Stream.Status.closed, fileStream.streamStatus)
XCTAssertEqual(messageData.count, result)
if(result > 0){
let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue)
XCTAssertEqual(message, output!)
}
}
removeTestFile(testFile!)
} else {
XCTFail("Unable to create temp file")
}
}
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(Stream.Status.notOpen, fileStream.streamStatus)
fileStream.open()
XCTAssertEqual(Stream.Status.error, fileStream.streamStatus)
}
func test_outputStreamCreationToFile() {
let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
if filePath != nil {
let outputStream = OutputStream(toFileAtPath: filePath!, append: true)
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
var myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
outputStream?.open()
XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus)
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
outputStream?.close()
XCTAssertEqual(myString.count, result)
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
removeTestFile(filePath!)
} else {
XCTFail("Unable to create temp file");
}
}
func test_outputStreamCreationToBuffer() {
var buffer = Array<UInt8>(repeating: 0, count: 12)
var myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: 12)
XCTAssertEqual(Stream.Status.notOpen, outputStream.streamStatus)
outputStream.open()
XCTAssertEqual(Stream.Status.open, outputStream.streamStatus)
let result: Int? = outputStream.write(encodedData, maxLength: encodedData.count)
outputStream.close()
XCTAssertEqual(Stream.Status.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() {
let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
if filePath != nil {
let outputStream = OutputStream(url: URL(fileURLWithPath: filePath!), append: true)
XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus)
var myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
outputStream!.open()
XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus)
let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count)
outputStream?.close()
XCTAssertEqual(myString.count, result)
XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus)
removeTestFile(filePath!)
} else {
XCTFail("Unable to create temp file");
}
}
func test_outputStreamCreationToMemory(){
var buffer = Array<UInt8>(repeating: 0, count: 12)
var myString = "Hello world!"
let encodedData = [UInt8](myString.utf8)
let outputStream = OutputStream.toMemory()
XCTAssertEqual(Stream.Status.notOpen, outputStream.streamStatus)
outputStream.open()
XCTAssertEqual(Stream.Status.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)
var 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(Stream.Status.notOpen, outputStream!.streamStatus)
outputStream?.open()
XCTAssertEqual(Stream.Status.error, outputStream!.streamStatus)
}
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) {
do {
try FileManager.default.removeItem(atPath: location)
} catch _ {
}
}
}