|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | + |
| 10 | +#if DEPLOYMENT_RUNTIME_OBJC || os(Linux) |
| 11 | + import Foundation |
| 12 | + import XCTest |
| 13 | +#else |
| 14 | + import SwiftFoundation |
| 15 | + import SwiftXCTest |
| 16 | +#endif |
| 17 | + |
| 18 | +class TestNSStream : XCTestCase { |
| 19 | + static var allTests: [(String, (TestNSStream) -> () throws -> Void)] { |
| 20 | + return [ |
| 21 | + ("test_InputStreamWithData", test_InputStreamWithData), |
| 22 | + ("test_InputStreamWithUrl", test_InputStreamWithUrl), |
| 23 | + ("test_InputStreamWithFile", test_InputStreamWithFile), |
| 24 | + ("test_InputStreamHasBytesAvailable", test_InputStreamHasBytesAvailable), |
| 25 | + ("test_InputStreamInvalidPath", test_InputStreamInvalidPath), |
| 26 | + ] |
| 27 | + } |
| 28 | + |
| 29 | + func test_InputStreamWithData(){ |
| 30 | + let message: NSString = "Hello, playground" |
| 31 | + let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)! |
| 32 | + let dataStream: InputStream = InputStream(data: messageData) |
| 33 | + XCTAssertEqual(Stream.Status.notOpen, dataStream.streamStatus) |
| 34 | + dataStream.open() |
| 35 | + XCTAssertEqual(Stream.Status.open, dataStream.streamStatus) |
| 36 | + var buffer = [UInt8](repeating: 0, count: 20) |
| 37 | + if dataStream.hasBytesAvailable { |
| 38 | + let result: Int = dataStream.read(&buffer, maxLength: buffer.count) |
| 39 | + dataStream.close() |
| 40 | + XCTAssertEqual(Stream.Status.closed, dataStream.streamStatus) |
| 41 | + if(result > 0){ |
| 42 | + let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue) |
| 43 | + XCTAssertEqual(message, output!) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + func test_InputStreamWithUrl() { |
| 49 | + let message: NSString = "Hello, playground" |
| 50 | + let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)! |
| 51 | + //Initialiser with url |
| 52 | + let testFile = createTestFile("testFile_in.txt", _contents: messageData) |
| 53 | + if testFile != nil { |
| 54 | + let url = URL(fileURLWithPath: testFile!) |
| 55 | + let urlStream: InputStream = InputStream(url: url)! |
| 56 | + XCTAssertEqual(Stream.Status.notOpen, urlStream.streamStatus) |
| 57 | + urlStream.open() |
| 58 | + XCTAssertEqual(Stream.Status.open, urlStream.streamStatus) |
| 59 | + var buffer = [UInt8](repeating: 0, count: 20) |
| 60 | + if urlStream.hasBytesAvailable { |
| 61 | + let result :Int = urlStream.read(&buffer, maxLength: buffer.count) |
| 62 | + urlStream.close() |
| 63 | + XCTAssertEqual(Stream.Status.closed, urlStream.streamStatus) |
| 64 | + XCTAssertEqual(messageData.count, result) |
| 65 | + if(result > 0) { |
| 66 | + let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue) |
| 67 | + XCTAssertEqual(message, output!) |
| 68 | + } |
| 69 | + } |
| 70 | + removeTestFile(testFile!) |
| 71 | + } else { |
| 72 | + XCTFail("Unable to create temp file") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + func test_InputStreamWithFile() { |
| 77 | + let message: NSString = "Hello, playground" |
| 78 | + let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)! |
| 79 | + //Initialiser with file |
| 80 | + let testFile = createTestFile("testFile_in.txt", _contents: messageData) |
| 81 | + if testFile != nil { |
| 82 | + let fileStream: InputStream = InputStream(fileAtPath: testFile!)! |
| 83 | + XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus) |
| 84 | + fileStream.open() |
| 85 | + XCTAssertEqual(Stream.Status.open, fileStream.streamStatus) |
| 86 | + var buffer = [UInt8](repeating: 0, count: 20) |
| 87 | + if fileStream.hasBytesAvailable { |
| 88 | + let result: Int = fileStream.read(&buffer, maxLength: buffer.count) |
| 89 | + fileStream.close() |
| 90 | + XCTAssertEqual(Stream.Status.closed, fileStream.streamStatus) |
| 91 | + XCTAssertEqual(messageData.count, result) |
| 92 | + if(result > 0){ |
| 93 | + let output = NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue) |
| 94 | + XCTAssertEqual(message, output!) |
| 95 | + } |
| 96 | + } |
| 97 | + removeTestFile(testFile!) |
| 98 | + } else { |
| 99 | + XCTFail("Unable to create temp file") |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + func test_InputStreamHasBytesAvailable() { |
| 104 | + let message: NSString = "Hello, playground" |
| 105 | + let messageData: Data = message.data(using: String.Encoding.utf8.rawValue)! |
| 106 | + let stream: InputStream = InputStream(data: messageData) |
| 107 | + var buffer = [UInt8](repeating: 0, count: 20) |
| 108 | + stream.open() |
| 109 | + XCTAssertTrue(stream.hasBytesAvailable) |
| 110 | + _ = stream.read(&buffer, maxLength: buffer.count) |
| 111 | + XCTAssertFalse(stream.hasBytesAvailable) |
| 112 | + } |
| 113 | + |
| 114 | + func test_InputStreamInvalidPath() { |
| 115 | + let fileStream: InputStream = InputStream(fileAtPath: "/tmp/file.txt")! |
| 116 | + XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus) |
| 117 | + fileStream.open() |
| 118 | + XCTAssertEqual(Stream.Status.error, fileStream.streamStatus) |
| 119 | + } |
| 120 | + |
| 121 | + private func createTestFile(_ path: String,_contents: Data) -> String? { |
| 122 | + let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().UUIDString + "/" |
| 123 | + do { |
| 124 | + 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 |
| 127 | + } else { |
| 128 | + return nil |
| 129 | + } |
| 130 | + } catch _ { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + private func removeTestFile(_ location: String) { |
| 137 | + do { |
| 138 | + try FileManager.default().removeItem(atPath: location) |
| 139 | + } catch _ { |
| 140 | + |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | + |
0 commit comments