Skip to content

Commit 69aa3c3

Browse files
committed
[test] rebinding typed memory to wider and narrower types
1 parent 2eb37a8 commit 69aa3c3

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

test/stdlib/UnsafePointer.swift.gyb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,28 @@ ${SelfName}TestSuite.test("withMemoryRebound") {
470470
}
471471
}
472472

473+
${SelfName}TestSuite.test("withMemoryReboundSE0333") {
474+
let mutablePtrI = UnsafeMutablePointer<Int>.allocate(capacity: 4)
475+
defer { mutablePtrI.deallocate() }
476+
let ptrI = ${SelfName}<Int>(mutablePtrI)
477+
// test rebinding to a wider type with the same alignment
478+
ptrI.withMemoryRebound(to: (Int, Int).self, capacity: 2) {
479+
// Make sure the closure argument isa $SelfName
480+
let ptrT: ${SelfName}<(Int, Int)> = $0
481+
// and that the element type is (Int, Int).
482+
let mutablePtrT = UnsafeMutablePointer(mutating: ptrT)
483+
expectType((Int, Int).self, &mutablePtrT.pointee)
484+
// test rebinding to a narrower type
485+
ptrT.withMemoryRebound(to: UInt.self, capacity: 4) {
486+
// Make sure the closure argument isa $SelfName
487+
let ptrU: ${SelfName}<UInt> = $0
488+
// and that the element type is UInt.
489+
let mutablePtrU = UnsafeMutablePointer(mutating: ptrU)
490+
expectType(UInt.self, &mutablePtrU.pointee)
491+
}
492+
}
493+
}
494+
473495
% end
474496

475497
runAllTests()

validation-test/stdlib/UnsafeBufferPointer.swift.gyb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,4 +1054,33 @@ UnsafeMutableBufferPointerTestSuite.test("nonmutating-swapAt-withARC") {
10541054

10551055
% end # SelfName
10561056

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+
10571086
runAllTests()

0 commit comments

Comments
 (0)