Skip to content

Commit f1f054d

Browse files
committed
Eliminate the AllOnesData subclass (for now) hitting linker problems on linux
1 parent 195448d commit f1f054d

File tree

1 file changed

+3
-161
lines changed

1 file changed

+3
-161
lines changed

TestFoundation/TestNSData.swift

Lines changed: 3 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -17,109 +17,7 @@
1717

1818
class TestNSData: XCTestCase {
1919

20-
// This is a type of Data that starts off as a storage of all 0x01s, but it only creates that buffer when needed. When mutated it converts into a more traditional data storage backed by a buffer.
21-
public class AllOnesData : NSMutableData {
22-
23-
private var _length : Int = 0
24-
var _pointer : UnsafeMutableBufferPointer<UInt8>? = nil {
25-
willSet {
26-
if let p = _pointer { free(p.baseAddress) }
27-
}
28-
}
29-
public override init(bytes: UnsafeMutableRawPointer?, length: Int, copy: Bool = false, deallocator: (@escaping (UnsafeMutableRawPointer, Int) -> Void)? = nil) {
30-
_length = length
31-
_pointer = nil
32-
super.init(bytes: bytes, length: length, copy: copy, deallocator: deallocator)
33-
}
34-
35-
36-
public override init() {
37-
_length = 0
38-
_pointer = nil
39-
super.init()
40-
}
41-
public convenience init?(length : Int) {
42-
self.init()
43-
_length = length
44-
_pointer = nil
45-
}
46-
47-
public required init?(coder aDecoder: NSCoder) {
48-
// Not tested
49-
fatalError()
50-
}
51-
52-
deinit {
53-
if let p = _pointer {
54-
free(p.baseAddress)
55-
}
56-
}
57-
58-
public override var length : Int {
59-
get {
60-
return _length
61-
}
62-
set {
63-
if let ptr = _pointer {
64-
// Copy the data to our new length buffer
65-
let newBuffer = malloc(newValue)!
66-
if newValue <= _length {
67-
memmove(newBuffer, ptr.baseAddress!, newValue)
68-
} else if newValue > _length {
69-
memmove(newBuffer, ptr.baseAddress!, _length)
70-
memset(newBuffer + _length, 1, newValue - _length)
71-
}
72-
let bytePtr = newBuffer.bindMemory(to: UInt8.self, capacity: newValue)
73-
_pointer = UnsafeMutableBufferPointer(start: bytePtr, count: newValue)
74-
} else {
75-
_length = newValue
76-
}
77-
}
78-
}
79-
80-
public override var bytes : UnsafeRawPointer {
81-
if let d = _pointer {
82-
return UnsafeRawPointer(d.baseAddress!)
83-
} else {
84-
// Need to allocate the buffer now.
85-
// It doesn't matter if the buffer is uniquely referenced or not here.
86-
let buffer = malloc(length)
87-
memset(buffer!, 1, length)
88-
let bytePtr = buffer!.bindMemory(to: UInt8.self, capacity: length)
89-
let result = UnsafeMutableBufferPointer(start: bytePtr, count: length)
90-
_pointer = result
91-
return UnsafeRawPointer(result.baseAddress!)
92-
}
93-
}
94-
95-
override public var mutableBytes: UnsafeMutableRawPointer {
96-
let newBufferLength = _length
97-
let newBuffer = malloc(newBufferLength)
98-
if let ptr = _pointer {
99-
// Copy the existing data to the new box, then return its pointer
100-
memmove(newBuffer!, ptr.baseAddress!, newBufferLength)
101-
} else {
102-
// Set new data to 1s
103-
memset(newBuffer!, 1, newBufferLength)
104-
}
105-
let bytePtr = newBuffer!.bindMemory(to: UInt8.self, capacity: newBufferLength)
106-
let result = UnsafeMutableBufferPointer(start: bytePtr, count: newBufferLength)
107-
_pointer = result
108-
_length = newBufferLength
109-
return UnsafeMutableRawPointer(result.baseAddress!)
110-
}
111-
112-
override public func getBytes(_ buffer: UnsafeMutableRawPointer, length: Int) {
113-
if let d = _pointer {
114-
// Get the real data from the buffer
115-
memmove(buffer, d.baseAddress!, length)
116-
} else {
117-
// A more efficient implementation of getBytes in the case where no one has asked for our backing bytes
118-
memset(buffer, 1, length)
119-
}
120-
}
121-
}
122-
20+
12321
// MARK: -
12422

12523
// String of course has its own way to get data, but this way tests our own data struct
@@ -144,14 +42,12 @@ class TestNSData: XCTestCase {
14442
("test_genericBuffers", test_genericBuffers),
14543
("test_writeFailure", test_writeFailure),
14644
("testBasicConstruction", testBasicConstruction),
147-
("testBridgingCustom", testBridgingCustom),
14845
("testBridgingDefault", testBridgingDefault),
14946
("testBridgingMutable", testBridgingMutable),
15047
("testCopyBytes_oversized", testCopyBytes_oversized),
15148
("testCopyBytes_ranges", testCopyBytes_ranges),
15249
("testCopyBytes_undersized", testCopyBytes_undersized),
15350
// ("testCopyBytes", testCopyBytes), Disabled to due failure, we want this passing - but API matching is more important right now
154-
("testCustomData", testCustomData),
15551
("testCustomDeallocator", testCustomDeallocator),
15652
("testDataInSet", testDataInSet),
15753
("testEquality", testEquality),
@@ -601,32 +497,7 @@ extension TestNSData {
601497
}
602498
}
603499

604-
func testCustomData() {
605-
let length = 5
606-
let allOnesData = Data(referencing: AllOnesData(length: length)!)
607-
XCTAssertEqual(1, allOnesData[0], "First byte of all 1s data should be 1")
608-
609-
// Double the length
610-
var allOnesCopyToMutate = allOnesData
611-
allOnesCopyToMutate.count = allOnesData.count * 2
612-
613-
XCTAssertEqual(allOnesData.count, length, "The length of the initial data should not have changed")
614-
XCTAssertEqual(allOnesCopyToMutate.count, length * 2, "The length should have changed")
615-
616-
// Force the second data to create its storage
617-
allOnesCopyToMutate.withUnsafeMutableBytes { (bytes : UnsafeMutablePointer<UInt8>) in
618-
XCTAssertEqual(bytes.pointee, 1, "First byte should be 1")
619-
620-
// Mutate the second data
621-
bytes.pointee = 0
622-
XCTAssertEqual(bytes.pointee, 0, "First byte should be 0")
623-
XCTAssertEqual(allOnesCopyToMutate[0], 0, "First byte accessed via other method should still be 0")
624-
625-
// Verify that the first data is still 1
626-
XCTAssertEqual(allOnesData[0], 1, "The first byte should still be 1")
627-
}
628-
629-
}
500+
630501

631502
func testBridgingDefault() {
632503
let hello = dataFrom("hello")
@@ -654,36 +525,7 @@ extension TestNSData {
654525

655526
}
656527

657-
func testBridgingCustom() {
658-
// Let's use an AllOnesData with some Objective-C code
659-
let allOnes = AllOnesData(length: 64)!
660-
661-
// Type-erased
662-
let data = Data(referencing: allOnes)
663-
664-
// Create a home for our test data
665-
let x = NSString()
666-
let dirPath = (NSTemporaryDirectory().bridge()).stringByAppendingPathComponent(NSUUID().uuidString)
667-
try! FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: true, attributes: nil)
668-
let filePath = (dirPath.bridge()).stringByAppendingPathComponent("temp_file")
669-
guard FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil) else { XCTAssertTrue(false, "Unable to create temporary file"); return}
670-
guard let fh = FileHandle(forWritingAtPath: filePath) else { XCTAssertTrue(false, "Unable to open temporary file"); return }
671-
defer { try! FileManager.default.removeItem(atPath: dirPath) }
672-
673-
// Now use this data with some Objective-C code that takes NSData arguments
674-
fh.write(data)
675-
676-
// Get the data back
677-
do {
678-
let url = URL(fileURLWithPath: filePath)
679-
let readData = try Data.init(contentsOf: url)
680-
XCTAssertEqual(data.count, readData.count, "The length of the data is not the same")
681-
} catch {
682-
XCTAssertTrue(false, "Unable to read back data")
683-
return
684-
}
685-
}
686-
528+
687529
func testEquality() {
688530
let d1 = dataFrom("hello")
689531
let d2 = dataFrom("hello")

0 commit comments

Comments
 (0)