Skip to content

NSMutableData: Restore missing initialisers #1385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
/// Initializes a data object with the contents of another data object.
public init(data: Data) {
super.init()
_init(bytes: UnsafeMutableRawPointer(mutating: data._nsObject.bytes), length: length, copy: true)
_init(bytes: UnsafeMutableRawPointer(mutating: data._nsObject.bytes), length: data.count, copy: true)
}

/// Initializes a data object with the data from the location specified by a given URL.
Expand Down Expand Up @@ -966,7 +966,7 @@ open class NSMutableData : NSData {
}

// NOTE: the deallocator block here is implicitly @escaping by virtue of it being optional
public override init(bytes: UnsafeMutableRawPointer?, length: Int, copy: Bool = false, deallocator: (/*@escaping*/ (UnsafeMutableRawPointer, Int) -> Void)? = nil) {
fileprivate override init(bytes: UnsafeMutableRawPointer?, length: Int, copy: Bool = false, deallocator: (/*@escaping*/ (UnsafeMutableRawPointer, Int) -> Void)? = nil) {
super.init(bytes: bytes, length: length, copy: copy, deallocator: deallocator)
}

Expand All @@ -990,6 +990,47 @@ open class NSMutableData : NSData {
super.init(coder: aDecoder)
}

public override init(bytesNoCopy bytes: UnsafeMutableRawPointer, length: Int) {
super.init(bytesNoCopy: bytes, length: length)
}

public override init(bytesNoCopy bytes: UnsafeMutableRawPointer, length: Int, deallocator: ((UnsafeMutableRawPointer, Int) -> Void)? = nil) {
super.init(bytesNoCopy: bytes, length: length, deallocator: deallocator)
}

public override init(bytesNoCopy bytes: UnsafeMutableRawPointer, length: Int, freeWhenDone: Bool) {
super.init(bytesNoCopy: bytes, length: length, freeWhenDone: freeWhenDone)
}

public override init(data: Data) {
super.init(data: data)
}

public override init?(contentsOfFile path: String) {
super.init(contentsOfFile: path)
}

public override init(contentsOfFile path: String, options: NSData.ReadingOptions = []) throws {
try super.init(contentsOfFile: path, options: options)
}

public override init?(contentsOf url: URL) {
super.init(contentsOf: url)
}

public override init(contentsOf url: URL, options: NSData.ReadingOptions = []) throws {
try super.init(contentsOf: url, options: options)
}

public override init?(base64Encoded base64Data: Data, options: NSData.Base64DecodingOptions = []) {
super.init(base64Encoded: base64Data, options: options)
}

public override init?(base64Encoded base64Data: String, options: NSData.Base64DecodingOptions = []) {
super.init(base64Encoded: base64Data, options: options)
}


// MARK: - Funnel Methods
/// A pointer to the data contained by the mutable data object.
open var mutableBytes: UnsafeMutableRawPointer {
Expand Down
117 changes: 113 additions & 4 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ class TestNSData: XCTestCase {
("test_dataHash", test_dataHash),
("test_genericBuffers", test_genericBuffers),
("test_writeFailure", test_writeFailure),
("testBasicConstruction", testBasicConstruction),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this test removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a duplicate line, that test is called on line 198

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, I see it past the code fold

("testBridgingDefault", testBridgingDefault),
("testBridgingMutable", testBridgingMutable),
("testCopyBytes_oversized", testCopyBytes_oversized),
Expand Down Expand Up @@ -248,8 +247,14 @@ class TestNSData: XCTestCase {
("test_initializeWithBase64EncodedStringGetsDecodedData", test_initializeWithBase64EncodedStringGetsDecodedData),
("test_base64DecodeWithPadding1", test_base64DecodeWithPadding1),
("test_base64DecodeWithPadding2", test_base64DecodeWithPadding2),
("test_rangeOfData",test_rangeOfData),
("test_initMutableDataWithLength", test_initMutableDataWithLength),
("test_rangeOfData", test_rangeOfData),
("test_initNSMutableData()", test_initNSMutableData),
("test_initNSMutableDataWithLength", test_initNSMutableDataWithLength),
("test_initNSMutableDataWithCapacity", test_initNSMutableDataWithCapacity),
("test_initNSMutableDataFromData", test_initNSMutableDataFromData),
("test_initNSMutableDataFromBytes", test_initNSMutableDataFromBytes),
("test_initNSMutableDataContentsOf", test_initNSMutableDataContentsOf),
("test_initNSMutableDataBase64", test_initNSMutableDataBase64),
("test_replaceBytes", test_replaceBytes),
("test_replaceBytesWithNil", test_replaceBytesWithNil),
("test_initDataWithCapacity", test_initDataWithCapacity),
Expand Down Expand Up @@ -811,12 +816,116 @@ class TestNSData: XCTestCase {

}

func test_initMutableDataWithLength() {
// Check all of the NSMutableData constructors are available.
func test_initNSMutableData() {
let mData = NSMutableData()
XCTAssertNotNil(mData)
XCTAssertEqual(mData.length, 0)
}

func test_initNSMutableDataWithLength() {
let mData = NSMutableData(length: 30)
XCTAssertNotNil(mData)
XCTAssertEqual(mData!.length, 30)
}

func test_initNSMutableDataWithCapacity() {
let mData = NSMutableData(capacity: 30)
XCTAssertNotNil(mData)
XCTAssertEqual(mData!.length, 0)
}

func test_initNSMutableDataFromData() {
let data = Data(bytes: [1, 2, 3])
let mData = NSMutableData(data: data)
XCTAssertEqual(mData.length, 3)
XCTAssertEqual(NSData(data: data), mData)
}

func test_initNSMutableDataFromBytes() {
let data = Data([1, 2, 3, 4, 5, 6])
var testBytes: [UInt8] = [1, 2, 3, 4, 5, 6]

let md1 = NSMutableData(bytes: &testBytes, length: testBytes.count)
XCTAssertEqual(md1, NSData(data: data))

let md2 = NSMutableData(bytes: nil, length: 0)
XCTAssertEqual(md2.length, 0)

let testBuffer = malloc(testBytes.count)!
let md3 = NSMutableData(bytesNoCopy: testBuffer, length: testBytes.count)
md3.replaceBytes(in: NSRange(location: 0, length: testBytes.count), withBytes: &testBytes)
XCTAssertEqual(md3, NSData(data: data))

let md4 = NSMutableData(bytesNoCopy: &testBytes, length: testBytes.count, deallocator: nil)
XCTAssertEqual(md4.length, testBytes.count)

let md5 = NSMutableData(bytesNoCopy: &testBytes, length: testBytes.count, freeWhenDone: false)
XCTAssertEqual(md5, NSData(data: data))
}

func test_initNSMutableDataContentsOf() {
let testDir = testBundle().resourcePath
let filename = testDir!.appending("/NSStringTestData.txt")
let url = URL(fileURLWithPath: filename)

func testText(_ mData: NSMutableData?) {
guard let mData = mData else {
XCTFail("Contents of file are Nil")
return
}
if let txt = String(data: Data(referencing: mData), encoding: .ascii) {
XCTAssertEqual(txt, "swift-corelibs-foundation")
} else {
XCTFail("Cant convert to string")
}
}

let contents1 = NSMutableData(contentsOfFile: filename)
XCTAssertNotNil(contents1)
testText(contents1)

let contents2 = try? NSMutableData(contentsOfFile: filename, options: [])
XCTAssertNotNil(contents2)
testText(contents2)

let contents3 = NSMutableData(contentsOf: url)
XCTAssertNotNil(contents3)
testText(contents3)

let contents4 = try? NSMutableData(contentsOf: url, options: [])
XCTAssertNotNil(contents4)
testText(contents4)

// Test failure to read
let badFilename = "does not exist"
let badUrl = URL(fileURLWithPath: badFilename)

XCTAssertNil(NSMutableData(contentsOfFile: badFilename))
XCTAssertNil(try? NSMutableData(contentsOfFile: badFilename, options: []))
XCTAssertNil(NSMutableData(contentsOf: badUrl))
XCTAssertNil(try? NSMutableData(contentsOf: badUrl, options: []))
}

func test_initNSMutableDataBase64() {
let srcData = Data([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
let base64Data = srcData.base64EncodedData()
let base64String = srcData.base64EncodedString()
XCTAssertEqual(base64String, "AQIDBAUGBwgJAA==")

let mData1 = NSMutableData(base64Encoded: base64Data)
XCTAssertNotNil(mData1)
XCTAssertEqual(mData1!, NSData(data: srcData))

let mData2 = NSMutableData(base64Encoded: base64String)
XCTAssertNotNil(mData2)
XCTAssertEqual(mData2!, NSData(data: srcData))

// Test bad input
XCTAssertNil(NSMutableData(base64Encoded: Data([1,2,3]), options: []))
XCTAssertNil(NSMutableData(base64Encoded: "x", options: []))
}

func test_replaceBytes() {
var data = Data(bytes: [0, 0, 0, 0, 0])
let newData = Data(bytes: [1, 2, 3, 4, 5])
Expand Down