17
17
18
18
class TestNSData : XCTestCase {
19
19
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
+
123
21
// MARK: -
124
22
125
23
// 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 {
144
42
( " test_genericBuffers " , test_genericBuffers) ,
145
43
( " test_writeFailure " , test_writeFailure) ,
146
44
( " testBasicConstruction " , testBasicConstruction) ,
147
- ( " testBridgingCustom " , testBridgingCustom) ,
148
45
( " testBridgingDefault " , testBridgingDefault) ,
149
46
( " testBridgingMutable " , testBridgingMutable) ,
150
47
( " testCopyBytes_oversized " , testCopyBytes_oversized) ,
151
48
( " testCopyBytes_ranges " , testCopyBytes_ranges) ,
152
49
( " testCopyBytes_undersized " , testCopyBytes_undersized) ,
153
50
// ("testCopyBytes", testCopyBytes), Disabled to due failure, we want this passing - but API matching is more important right now
154
- ( " testCustomData " , testCustomData) ,
155
51
( " testCustomDeallocator " , testCustomDeallocator) ,
156
52
( " testDataInSet " , testDataInSet) ,
157
53
( " testEquality " , testEquality) ,
@@ -601,32 +497,7 @@ extension TestNSData {
601
497
}
602
498
}
603
499
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
+
630
501
631
502
func testBridgingDefault( ) {
632
503
let hello = dataFrom ( " hello " )
@@ -654,36 +525,7 @@ extension TestNSData {
654
525
655
526
}
656
527
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
+
687
529
func testEquality( ) {
688
530
let d1 = dataFrom ( " hello " )
689
531
let d2 = dataFrom ( " hello " )
0 commit comments