@@ -1054,4 +1054,33 @@ UnsafeMutableBufferPointerTestSuite.test("nonmutating-swapAt-withARC") {
1054
1054
1055
1055
% end # SelfName
1056
1056
1057
+ % for mutable in [True, False]:
1058
+ % TypedName = 'UnsafeMutableBufferPointer' if mutable else 'UnsafeBufferPointer'
1059
+ Unsafe${'Mutable' if mutable else ''}BufferPointerTestSuite.test("withMemoryRebound") {
1060
+ // test withMemoryRebound behaviour, post SE-0333.
1061
+ let mutableBufferI = UnsafeMutableBufferPointer<Int>.allocate(capacity: 4)
1062
+ defer { mutableBufferI.deallocate() }
1063
+ let ptrI = ${'mutableBufferI' if mutable else TypedName + '(mutableBufferI)'}
1064
+ // test rebinding to a wider type with the same alignment
1065
+ ptrI.withMemoryRebound(to: (Int, Int).self) {
1066
+ // Make sure the closure argument is a ${TypedName}
1067
+ let ptrT: ${TypedName}<(Int, Int)> = $0
1068
+ expectEqual(ptrT.count, 2)
1069
+ // and that the element type is (Int, Int).
1070
+ let mutablePtrT = ${'ptrT' if mutable else 'UnsafeMutableBufferPointer(mutating: ptrT)'}
1071
+ expectType((Int, Int).self, &mutablePtrT[0])
1072
+ // test rebinding to a narrower type
1073
+ ptrT.withMemoryRebound(to: UInt.self) {
1074
+ // Make sure the closure argument is a ${TypedName}
1075
+ let ptrU: ${TypedName}<UInt> = $0
1076
+ expectEqual(ptrU.count, 4)
1077
+ // and that the element type is UInt.
1078
+ let mutablePtrU = ${'ptrU' if mutable else 'UnsafeMutableBufferPointer(mutating: ptrU)'}
1079
+ expectType(UInt.self, &mutablePtrU[0])
1080
+ }
1081
+ }
1082
+ }
1083
+
1084
+ % end # mutable
1085
+
1057
1086
runAllTests()
0 commit comments