Skip to content

Commit 9f2c4b5

Browse files
committed
[test] rebinding raw memory
1 parent 69aa3c3 commit 9f2c4b5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

test/stdlib/UnsafeRawPointer.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,24 @@ UnsafeMutableRawPointerExtraTestSuite.test("moveInitialize:from:") {
229229
check(Check.RightOverlap)
230230
}
231231

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

validation-test/stdlib/UnsafeBufferPointer.swift.gyb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,22 @@ Unsafe${'Mutable' if mutable else ''}BufferPointerTestSuite.test("withMemoryRebo
10811081
}
10821082
}
10831083

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+
10841100
% end # mutable
10851101

10861102
runAllTests()

0 commit comments

Comments
 (0)