File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -229,4 +229,24 @@ UnsafeMutableRawPointerExtraTestSuite.test("moveInitialize:from:") {
229
229
check ( Check . RightOverlap)
230
230
}
231
231
232
+ UnsafeMutableRawPointerExtraTestSuite . test ( " withMemoryRebound " ) {
233
+ // test withMemoryRebound behaviour, post SE-0333.
234
+ let allocated = UnsafeMutableRawPointer . allocate ( byteCount: 32 , alignment: 8 )
235
+ defer { allocated. deallocate ( ) }
236
+ allocated. withMemoryRebound ( to: Int . self, capacity: 4 ) {
237
+ // Make sure the closure argument is a `UnsafeMutablePointer<Int>`
238
+ let ptrT : UnsafeMutablePointer < Int > = $0
239
+ // and that the pointee type is `Int`.
240
+ expectType ( Int . self, & ptrT. pointee)
241
+ }
242
+ let nonmutable = UnsafeRawPointer ( allocated)
243
+ nonmutable. withMemoryRebound ( to: UInt . self, capacity: 4 ) {
244
+ // Make sure the closure argument is a `UnsafePointer<UInt>`
245
+ let ptrT : UnsafePointer < UInt > = $0
246
+ // and that the element type is `Int`.
247
+ let mutablePtrT = UnsafeMutablePointer ( mutating: ptrT)
248
+ expectType ( UInt . self, & mutablePtrT. pointee)
249
+ }
250
+ }
251
+
232
252
runAllTests ( )
Original file line number Diff line number Diff line change @@ -1081,6 +1081,22 @@ Unsafe${'Mutable' if mutable else ''}BufferPointerTestSuite.test("withMemoryRebo
1081
1081
}
1082
1082
}
1083
1083
1084
+ % RawName = 'UnsafeMutableRawBufferPointer' if mutable else 'UnsafeRawBufferPointer'
1085
+ Unsafe${'Mutable' if mutable else ''}RawBufferPointerTestSuite.test("withMemoryRebound") {
1086
+ // test withMemoryRebound behaviour, post SE-0333.
1087
+ let allocated = UnsafeMutableRawBufferPointer.allocate(byteCount: 32, alignment: 8)
1088
+ defer { allocated.deallocate() }
1089
+ let ptrR = ${'allocated' if mutable else RawName + '(allocated)'}
1090
+ ptrR.withMemoryRebound(to: Int.self) {
1091
+ // Make sure the closure argument is a ${TypedName}
1092
+ let ptrT: ${TypedName}<Int> = $0
1093
+ expectEqual(ptrT.count, 32/MemoryLayout<Int>.stride)
1094
+ // and that the element type is `Int`.
1095
+ let mutablePtrT = ${'ptrT' if mutable else 'UnsafeMutableBufferPointer(mutating: ptrT)'}
1096
+ expectType(Int.self, &mutablePtrT[0])
1097
+ }
1098
+ }
1099
+
1084
1100
% end # mutable
1085
1101
1086
1102
runAllTests()
You can’t perform that action at this time.
0 commit comments