|
| 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_outputStreamCreationToFile", test_outputStreamCreationToFile), |
| 22 | + ("test_outputStreamCreationToBuffer", test_outputStreamCreationToBuffer), |
| 23 | + ("test_outputStreamCreationWithUrl", test_outputStreamCreationWithUrl), |
| 24 | + ("test_outputStreamHasSpaceAvailable", test_outputStreamHasSpaceAvailable), |
| 25 | + ("test_ouputStreamWithInvalidPath", test_ouputStreamWithInvalidPath), |
| 26 | + ] |
| 27 | + } |
| 28 | + |
| 29 | + func test_outputStreamCreationToFile() { |
| 30 | + let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)!) |
| 31 | + if filePath != nil { |
| 32 | + let outputStream = NSOutputStream(toFileAtPath: filePath!, append: true) |
| 33 | + XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus) |
| 34 | + var myString = "Hello world!" |
| 35 | + let encodedData = [UInt8](myString.utf8) |
| 36 | + outputStream?.open() |
| 37 | + XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus) |
| 38 | + let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count) |
| 39 | + outputStream?.close() |
| 40 | + XCTAssertEqual(myString.characters.count, result) |
| 41 | + XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus) |
| 42 | + removeTestFile(filePath!) |
| 43 | + } else { |
| 44 | + XCTFail("Unable to create temp file"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + func test_outputStreamCreationToBuffer() { |
| 49 | + var buffer = Array<UInt8>(repeating: 0, count: 12) |
| 50 | + var myString = "Hello world!" |
| 51 | + let encodedData = [UInt8](myString.utf8) |
| 52 | + let outputStream = NSOutputStream(toBuffer: UnsafeMutablePointer<UInt8>(buffer), capacity: 12) |
| 53 | + XCTAssertEqual(Stream.Status.notOpen, outputStream.streamStatus) |
| 54 | + outputStream.open() |
| 55 | + XCTAssertEqual(Stream.Status.open, outputStream.streamStatus) |
| 56 | + let result: Int? = outputStream.write(encodedData, maxLength: encodedData.count) |
| 57 | + outputStream.close() |
| 58 | + XCTAssertEqual(Stream.Status.closed, outputStream.streamStatus) |
| 59 | + XCTAssertEqual(myString.characters.count, result) |
| 60 | + XCTAssertEqual(NSString(bytes: &buffer, length: buffer.count, encoding: String.Encoding.utf8.rawValue),myString._bridgeToObject()) |
| 61 | + } |
| 62 | + |
| 63 | + func test_outputStreamCreationWithUrl() { |
| 64 | + let filePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256)!) |
| 65 | + if filePath != nil { |
| 66 | + let outputStream = NSOutputStream(url: URL(fileURLWithPath: filePath!), append: true) |
| 67 | + XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus) |
| 68 | + var myString = "Hello world!" |
| 69 | + let encodedData = [UInt8](myString.utf8) |
| 70 | + outputStream!.open() |
| 71 | + XCTAssertEqual(Stream.Status.open, outputStream!.streamStatus) |
| 72 | + let result: Int? = outputStream?.write(encodedData, maxLength: encodedData.count) |
| 73 | + outputStream?.close() |
| 74 | + XCTAssertEqual(myString.characters.count, result) |
| 75 | + XCTAssertEqual(Stream.Status.closed, outputStream!.streamStatus) |
| 76 | + removeTestFile(filePath!) |
| 77 | + } else { |
| 78 | + XCTFail("Unable to create temp file"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + func test_outputStreamHasSpaceAvailable() { |
| 83 | + let buffer = Array<UInt8>(repeating: 0, count: 12) |
| 84 | + var myString = "Welcome To Hello world !" |
| 85 | + let encodedData = [UInt8](myString.utf8) |
| 86 | + let outputStream = NSOutputStream(toBuffer: UnsafeMutablePointer<UInt8>(buffer), capacity: 12) |
| 87 | + outputStream.open() |
| 88 | + XCTAssertTrue(outputStream.hasSpaceAvailable) |
| 89 | + _ = outputStream.write(encodedData, maxLength: encodedData.count) |
| 90 | + XCTAssertFalse(outputStream.hasSpaceAvailable) |
| 91 | + } |
| 92 | + |
| 93 | + func test_ouputStreamWithInvalidPath(){ |
| 94 | + let outputStream = NSOutputStream(toFileAtPath: "http:///home/sdsfsdfd", append: true) |
| 95 | + XCTAssertEqual(Stream.Status.notOpen, outputStream!.streamStatus) |
| 96 | + outputStream?.open() |
| 97 | + XCTAssertEqual(Stream.Status.error, outputStream!.streamStatus) |
| 98 | + } |
| 99 | + |
| 100 | + private func createTestFile(_ path: String, _contents: Data) -> String? { |
| 101 | + let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().UUIDString + "/" |
| 102 | + do { |
| 103 | + try FileManager.default().createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil) |
| 104 | + if FileManager.default().createFile(atPath: tempDir + "/" + path, contents: _contents, |
| 105 | + attributes: nil) { |
| 106 | + return tempDir + path |
| 107 | + } else { |
| 108 | + return nil |
| 109 | + } |
| 110 | + } catch _ { |
| 111 | + return nil |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private func removeTestFile(_ location: String) { |
| 116 | + do { |
| 117 | + try FileManager.default().removeItem(atPath: location) |
| 118 | + } catch _ { |
| 119 | + |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
0 commit comments