@@ -238,6 +238,68 @@ class TestData : TestDataSuper {
238
238
let data3 = Data ( bytes: [ 1 , 2 , 3 , 4 , 5 ] [ 1 ..< 3 ] )
239
239
expectEqual ( 2 , data3. count)
240
240
}
241
+
242
+ func testInitializationWithBufferPointer( ) {
243
+ let nilBuffer = UnsafeBufferPointer < UInt8 > ( start: nil , count: 0 )
244
+ let data = Data ( buffer: nilBuffer)
245
+ expectEqual ( data, Data ( ) )
246
+
247
+ let validPointer = UnsafeMutablePointer< UInt8> . allocate( capacity: 2 )
248
+ validPointer [ 0 ] = 0xCA
249
+ validPointer [ 1 ] = 0xFE
250
+ defer { validPointer. deallocate ( ) }
251
+
252
+ let emptyBuffer = UnsafeBufferPointer < UInt8 > ( start: validPointer, count: 0 )
253
+ let data2 = Data ( buffer: emptyBuffer)
254
+ expectEqual ( data2, Data ( ) )
255
+
256
+ let shortBuffer = UnsafeBufferPointer < UInt8 > ( start: validPointer, count: 1 )
257
+ let data3 = Data ( buffer: shortBuffer)
258
+ expectEqual ( data3, Data ( [ 0xCA ] ) )
259
+
260
+ let fullBuffer = UnsafeBufferPointer < UInt8 > ( start: validPointer, count: 2 )
261
+ let data4 = Data ( buffer: fullBuffer)
262
+ expectEqual ( data4, Data ( [ 0xCA , 0xFE ] ) )
263
+
264
+ let tuple : ( UInt16 , UInt16 , UInt16 , UInt16 ) = ( 0xFF , 0xFE , 0xFD , 0xFC )
265
+ withUnsafeBytes ( of: tuple) {
266
+ // If necessary, port this to big-endian.
267
+ let tupleBuffer : UnsafeBufferPointer < UInt8 > = $0. bindMemory ( to: UInt8 . self)
268
+ let data5 = Data ( buffer: tupleBuffer)
269
+ expectEqual ( data5, Data ( [ 0xFF , 0x00 , 0xFE , 0x00 , 0xFD , 0x00 , 0xFC , 0x00 ] ) )
270
+ }
271
+ }
272
+
273
+ func testInitializationWithMutableBufferPointer( ) {
274
+ let nilBuffer = UnsafeMutableBufferPointer < UInt8 > ( start: nil , count: 0 )
275
+ let data = Data ( buffer: nilBuffer)
276
+ expectEqual ( data, Data ( ) )
277
+
278
+ let validPointer = UnsafeMutablePointer< UInt8> . allocate( capacity: 2 )
279
+ validPointer [ 0 ] = 0xCA
280
+ validPointer [ 1 ] = 0xFE
281
+ defer { validPointer. deallocate ( ) }
282
+
283
+ let emptyBuffer = UnsafeMutableBufferPointer < UInt8 > ( start: validPointer, count: 0 )
284
+ let data2 = Data ( buffer: emptyBuffer)
285
+ expectEqual ( data2, Data ( ) )
286
+
287
+ let shortBuffer = UnsafeMutableBufferPointer < UInt8 > ( start: validPointer, count: 1 )
288
+ let data3 = Data ( buffer: shortBuffer)
289
+ expectEqual ( data3, Data ( [ 0xCA ] ) )
290
+
291
+ let fullBuffer = UnsafeMutableBufferPointer < UInt8 > ( start: validPointer, count: 2 )
292
+ let data4 = Data ( buffer: fullBuffer)
293
+ expectEqual ( data4, Data ( [ 0xCA , 0xFE ] ) )
294
+
295
+ var tuple : ( UInt16 , UInt16 , UInt16 , UInt16 ) = ( 0xFF , 0xFE , 0xFD , 0xFC )
296
+ withUnsafeMutableBytes ( of: & tuple) {
297
+ // If necessary, port this to big-endian.
298
+ let tupleBuffer : UnsafeMutableBufferPointer < UInt8 > = $0. bindMemory ( to: UInt8 . self)
299
+ let data5 = Data ( buffer: tupleBuffer)
300
+ expectEqual ( data5, Data ( [ 0xFF , 0x00 , 0xFE , 0x00 , 0xFD , 0x00 , 0xFC , 0x00 ] ) )
301
+ }
302
+ }
241
303
242
304
func testMutableData( ) {
243
305
let hello = dataFrom ( " hello " )
@@ -3742,6 +3804,8 @@ class TestData : TestDataSuper {
3742
3804
var DataTests = TestSuite ( " TestData " )
3743
3805
DataTests . test ( " testBasicConstruction " ) { TestData ( ) . testBasicConstruction ( ) }
3744
3806
DataTests . test ( " testInitializationWithArray " ) { TestData ( ) . testInitializationWithArray ( ) }
3807
+ DataTests . test ( " testInitializationWithBufferPointer " ) { TestData ( ) . testInitializationWithBufferPointer ( ) }
3808
+ DataTests . test ( " testInitializationWithMutableBufferPointer " ) { TestData ( ) . testInitializationWithMutableBufferPointer ( ) }
3745
3809
DataTests . test ( " testMutableData " ) { TestData ( ) . testMutableData ( ) }
3746
3810
DataTests . test ( " testCustomData " ) { TestData ( ) . testCustomData ( ) }
3747
3811
DataTests . test ( " testBridgingDefault " ) { TestData ( ) . testBridgingDefault ( ) }
0 commit comments