@@ -361,6 +361,31 @@ UnsafeMutableBufferPointerTestSuite.test("withContiguous(Mutable)StorageIfAvaila
361
361
expectEqual(result1, result2)
362
362
}
363
363
364
+ UnsafeMutableRawBufferPointerTestSuite.test("withContiguous(Mutable)StorageIfAvailable") {
365
+ for c in [0, 1, 4] {
366
+ let pointer = UnsafeMutableRawPointer.allocate(byteCount: c, alignment: c)
367
+ defer { pointer.deallocate() }
368
+
369
+ let mutableBuffer = UnsafeMutableRawBufferPointer(start: pointer, count: c)
370
+ mutableBuffer.copyBytes(from: 0..<UInt8(c))
371
+
372
+ let r: ()? = mutableBuffer.withContiguousMutableStorageIfAvailable {
373
+ for i in $0.indices { $0[i] = 2*($0[i]+1) }
374
+ }
375
+ expectNotNil(r)
376
+ let s = mutableBuffer.withContiguousStorageIfAvailable {
377
+ $0.reduce(0, +)
378
+ }
379
+ expectEqual(s.map(Int.init), c*(c+1))
380
+
381
+ let immutableBuffer = UnsafeRawBufferPointer(mutableBuffer)
382
+ let t = immutableBuffer.withContiguousStorageIfAvailable {
383
+ $0.reduce(0, +)
384
+ }
385
+ expectEqual(s, t)
386
+ }
387
+ }
388
+
364
389
UnsafeMutableBufferPointerTestSuite.test("initialize(from: Slice)") {
365
390
let o = 91
366
391
let c = 1_000
@@ -386,6 +411,7 @@ UnsafeMutableBufferPointerTestSuite.test("Slice.withContiguousStorageIfAvailable
386
411
let c = test.collection.map { MinimalEquatableValue($0.value) }
387
412
let buffer = UnsafeMutableBufferPointer<MinimalEquatableValue>.allocate(
388
413
capacity: test.collection.count)
414
+ defer { buffer.deallocate() }
389
415
var (it, idx) = buffer.initialize(from: c)
390
416
expectEqual(buffer.endIndex, idx)
391
417
expectNil(it.next())
@@ -402,6 +428,28 @@ UnsafeMutableBufferPointerTestSuite.test("Slice.withContiguousStorageIfAvailable
402
428
}
403
429
}
404
430
431
+ UnsafeMutableRawBufferPointerTestSuite.test("Slice.withContiguousStorageIfAvailable") {
432
+ for test in subscriptRangeTests {
433
+ let c = test.collection.map({ UInt8($0.value/1000) })
434
+ let buffer = UnsafeMutableRawBufferPointer.allocate(
435
+ byteCount: test.collection.count, alignment: 8
436
+ )
437
+ defer { buffer.deallocate() }
438
+ buffer.copyBytes(from: c)
439
+
440
+ let expected = test.expected.map({ UInt8($0.value/1000) })
441
+ let r1: ()? = buffer[test.bounds].withContiguousStorageIfAvailable {
442
+ expectTrue(expected.elementsEqual($0))
443
+ }
444
+ expectNotNil(r1)
445
+ let r2: ()? = buffer[test.bounds].withContiguousMutableStorageIfAvailable {
446
+ for i in $0.indices { $0[i] += 1 }
447
+ }
448
+ expectNotNil(r2)
449
+ expectTrue(buffer[test.bounds].elementsEqual(expected.map({ $0+1 })))
450
+ }
451
+ }
452
+
405
453
UnsafeMutableBufferPointerTestSuite.test("sort") {
406
454
var values = (0..<1000).map({ _ in Int.random(in: 0..<100) })
407
455
let sortedValues = values.sorted()
@@ -948,6 +996,8 @@ UnsafeMutable${'Raw' if IsRaw else ''}BufferPointerTestSuite.test("subscript/${R
948
996
if _isDebugAssertConfiguration() || isOutOfBounds() {
949
997
if expectedValues == nil { expectCrashLater() }
950
998
}
999
+ % else:
1000
+ _ = isOutOfBounds
951
1001
% end
952
1002
let range = test.rangeSelection.${RangeName}(in: buffer)
953
1003
0 commit comments