1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
// RUN: %target-run-simple-swiftgyb
// REQUIRES: executable_test
// REQUIRES: reflection
import StdlibUnittest
protocol TestProtocol1 {}
// Check that the generic parameter is called 'Pointee'.
#if _runtime(_ObjC)
extension AutoreleasingUnsafeMutablePointer where Pointee : TestProtocol1 {
var _memoryIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
#endif
extension UnsafePointer where Pointee : TestProtocol1 {
var _memoryIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension UnsafeMutablePointer where Pointee : TestProtocol1 {
var _memoryIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
// Check that the generic parameter is called 'Element'.
extension UnsafeBufferPointer.Iterator where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension UnsafeBufferPointer where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
extension UnsafeMutableBufferPointer where Element : TestProtocol1 {
var _elementIsTestProtocol1: Bool {
fatalError("not implemented")
}
}
var UnsafePointerTestSuite = TestSuite("UnsafePointer")
var UnsafeMutablePointerTestSuite = TestSuite("UnsafeMutablePointer")
var UnsafeRawPointerTestSuite = TestSuite("UnsafeRawPointer")
var UnsafeMutableRawPointerTestSuite = TestSuite("UnsafeMutableRawPointer")
var OpaquePointerTestSuite = TestSuite("OpaquePointer")
% for (SelfName, SelfType) in [
% ('UnsafePointer', 'UnsafePointer<Float>'),
% ('UnsafeMutablePointer', 'UnsafeMutablePointer<Float>'),
% ('UnsafeRawPointer', 'UnsafeRawPointer'),
% ('UnsafeMutableRawPointer', 'UnsafeMutableRawPointer')]:
${SelfName}TestSuite.test("initFromOpaquePointer") {
let other = OpaquePointer(bitPattern: 0x12345678)!
let ptr = ${SelfType}(other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))
let optionalOther: Optional = other
let optionalPointer = ${SelfType}(optionalOther)
expectNotNil(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))
let nilOther: Optional<OpaquePointer> = nil
let nilPointer = ${SelfType}(nilOther)
expectNil(nilPointer)
}
% end
% for (SelfName, SelfType) in [
% ('UnsafeRawPointer', 'UnsafeRawPointer'),
% ('UnsafeMutableRawPointer', 'UnsafeMutableRawPointer')]:
UnsafeMutableRawPointerTestSuite.test("initFromMutating") {
let other = UnsafeRawPointer(bitPattern: 0x12345678)!
let ptr = UnsafeMutableRawPointer(mutating: other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))
let optionalOther: Optional = other
let optionalPointer = UnsafeMutableRawPointer(mutating: optionalOther)
expectNotNil(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))
let nilOther: Optional<UnsafeRawPointer> = nil
let nilPointer = UnsafeMutableRawPointer(mutating: nilOther)
expectNil(nilPointer)
}
% end
% for (SelfName, SelfType) in [
% ('UnsafePointer', 'UnsafePointer<Float>'),
% ('UnsafeMutablePointer', 'UnsafeMutablePointer<Float>'),
% ('UnsafeRawPointer', 'UnsafeRawPointer'),
% ('UnsafeMutableRawPointer', 'UnsafeMutableRawPointer'),
% ('OpaquePointer', 'OpaquePointer')]:
${SelfName}TestSuite.test("convertFromNil") {
let ptr: ${SelfType}? = nil
expectEqual(0, unsafeBitCast(ptr, to: Int.self))
}
${SelfName}TestSuite.test("initFromInteger") {
do {
let word: Int = 0x12345678
let ptr = ${SelfType}(bitPattern: word)
expectNotNil(ptr)
expectEqual(word, unsafeBitCast(ptr, to: Int.self))
}
do {
let uword: UInt = 0x12345678
let ptr = ${SelfType}(bitPattern: uword)
expectNotNil(ptr)
expectEqual(uword, unsafeBitCast(ptr, to: UInt.self))
}
}
${SelfName}TestSuite.test("initFromNilBitPattern") {
do {
let word = unsafeBitCast(nil as ${SelfType}?, to: Int.self)
let ptr = ${SelfType}(bitPattern: word)
expectNil(ptr)
expectEqual(word, unsafeBitCast(ptr, to: Int.self))
}
do {
let uword = unsafeBitCast(nil as ${SelfType}?, to: UInt.self)
let ptr = ${SelfType}(bitPattern: uword)
expectNil(ptr)
expectEqual(uword, unsafeBitCast(ptr, to: UInt.self))
}
}
${SelfName}TestSuite.test("Hashable") {
let ptrs = [
${SelfType}(bitPattern: 0x12345678)!,
${SelfType}(bitPattern: 0x87654321 as UInt)!,
]
checkHashable(ptrs, equalityOracle: { $0 == $1 })
}
${SelfName}TestSuite.test("toInteger") {
do {
let word: Int = 0x12345678
let ptr = ${SelfType}(bitPattern: word)
expectEqual(word, Int(bitPattern: ptr))
}
do {
let ptr: ${SelfType}? = nil
expectEqual(Int(0), Int(bitPattern: ptr))
}
do {
let uword: UInt = 0x12345678
let ptr = ${SelfType}(bitPattern: uword)
expectEqual(uword, UInt(bitPattern: ptr))
}
do {
let ptr: ${SelfType}? = nil
expectEqual(UInt(0), UInt(bitPattern: ptr))
}
}
% end
% for (SelfName, SelfType) in [
% ('UnsafePointer', 'UnsafePointer<Float>'),
% ('UnsafeRawPointer', 'UnsafeRawPointer'),
% ('UnsafeMutableRawPointer', 'UnsafeMutableRawPointer')]:
${SelfName}TestSuite.test("initFromUnsafeMutablePointer") {
let other = UnsafeMutablePointer<Float>(bitPattern: 0x12345678)!
let ptr = ${SelfType}(other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))
let optionalOther: Optional = other
let optionalPointer = ${SelfType}(optionalOther)
expectNotNil(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))
let nilOther: Optional<UnsafeMutablePointer<Float>> = nil
let nilPointer = ${SelfType}(nilOther)
expectNil(nilPointer)
}
% end
% for (SelfName, SelfType) in [
% ('UnsafePointer', 'UnsafePointer<Float>'),
% ('UnsafeRawPointer', 'UnsafeRawPointer')]:
${SelfName}TestSuite.test("initFromUnsafePointer") {
let other = UnsafePointer<Float>(bitPattern: 0x12345678)!
let ptr = ${SelfType}(other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))
let optionalOther: Optional = other
let optionalPointer = ${SelfType}(optionalOther)
expectNotNil(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))
let nilOther: Optional<UnsafePointer<Float>> = nil
let nilPointer = ${SelfType}(nilOther)
expectNil(nilPointer)
}
% end
UnsafeRawPointerTestSuite.test("initFromUnsafeMutableRawPointer") {
let other = UnsafeMutableRawPointer(bitPattern: 0x12345678)!
let ptr = UnsafeRawPointer(other)
expectEqual(0x12345678, unsafeBitCast(ptr, to: Int.self))
let optionalOther: Optional = other
let optionalPointer = UnsafeRawPointer(optionalOther)
expectNotNil(optionalPointer)
expectEqual(0x12345678, unsafeBitCast(optionalPointer, to: Int.self))
let nilOther: Optional<UnsafeMutableRawPointer> = nil
let nilPointer = UnsafeRawPointer(nilOther)
expectNil(nilPointer)
}
enum Check {
case LeftOverlap
case RightOverlap
case Disjoint
}
class Missile {
static var missilesLaunched = 0
let number: Int
init(_ number: Int) { self.number = number }
deinit { Missile.missilesLaunched += 1 }
}
func checkPointerCorrectness(_ check: Check,
_ withMissiles: Bool = false,
_ f: (UnsafeMutablePointer<Missile>) ->
(UnsafeMutablePointer<Missile>, _ count: Int) -> Void) {
let ptr = UnsafeMutablePointer<Missile>.allocate(capacity: 4)
switch check {
case .RightOverlap:
ptr.initialize(to: Missile(1))
(ptr + 1).initialize(to: Missile(2))
if withMissiles {
(ptr + 2).initialize(to: Missile(3))
}
f(ptr + 1)(ptr, 2)
expectEqual(1, ptr[1].number)
expectEqual(2, ptr[2].number)
case .LeftOverlap:
if withMissiles {
ptr.initialize(to: Missile(1))
}
(ptr + 1).initialize(to: Missile(2))
(ptr + 2).initialize(to: Missile(3))
f(ptr)(ptr + 1, 2)
expectEqual(2, ptr[0].number)
expectEqual(3, ptr[1].number)
case .Disjoint:
if withMissiles {
ptr.initialize(to: Missile(0))
(ptr + 1).initialize(to: Missile(1))
}
(ptr + 2).initialize(to: Missile(2))
(ptr + 3).initialize(to: Missile(3))
f(ptr)(ptr + 2, 2)
expectEqual(2, ptr[0].number)
expectEqual(3, ptr[1].number)
// backwards
let ptr2 = UnsafeMutablePointer<Missile>.allocate(capacity: 4)
ptr2.initialize(to: Missile(0))
(ptr2 + 1).initialize(to: Missile(1))
if withMissiles {
(ptr2 + 2).initialize(to: Missile(2))
(ptr2 + 3).initialize(to: Missile(3))
}
f(ptr2 + 2)(ptr2, 2)
expectEqual(0, ptr2[2].number)
expectEqual(1, ptr2[3].number)
}
}
func checkPtr(
_ f: @escaping ((UnsafeMutablePointer<Missile>) -> (UnsafeMutablePointer<Missile>, _ count: Int) -> Void),
_ m: Bool
) -> (Check) -> Void {
return { checkPointerCorrectness($0, m, f) }
}
func checkPtr(
_ f: @escaping ((UnsafeMutablePointer<Missile>) -> (UnsafePointer<Missile>, _ count: Int) -> Void),
_ m: Bool
) -> (Check) -> Void {
return {
checkPointerCorrectness($0, m) { destPtr in
return { f(destPtr)(UnsafeMutablePointer($0), $1) }
}
}
}
#if !os(WASI)
// Trap tests aren't available on WASI.
UnsafeMutablePointerTestSuite.test("moveAssign:from:") {
let check = checkPtr(UnsafeMutablePointer.moveAssign, true)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.LeftOverlap)
}
}
UnsafeMutablePointerTestSuite.test("moveAssign:from:.Right") {
let check = checkPtr(UnsafeMutablePointer.moveAssign, true)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.RightOverlap)
}
}
#endif
UnsafeMutablePointerTestSuite.test("assign:from:") {
let check = checkPtr(UnsafeMutablePointer.assign, true)
check(Check.LeftOverlap)
check(Check.Disjoint)
check(Check.RightOverlap)
}
UnsafeMutablePointerTestSuite.test("moveInitialize:from:") {
let check = checkPtr(UnsafeMutablePointer.moveInitialize, false)
check(Check.LeftOverlap)
check(Check.Disjoint)
check(Check.RightOverlap)
}
#if !os(WASI)
// Trap tests aren't available on WASI.
UnsafeMutablePointerTestSuite.test("initialize:from:") {
let check = checkPtr(UnsafeMutablePointer.initialize(from:count:), false)
check(Check.Disjoint)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.LeftOverlap)
}
}
UnsafeMutablePointerTestSuite.test("initialize:from:.Right") {
let check = checkPtr(UnsafeMutablePointer.initialize(from:count:), false)
// This check relies on _debugPrecondition() so will only trigger in -Onone mode.
if _isDebugAssertConfiguration() {
expectCrashLater()
check(Check.RightOverlap)
}
}
#endif
UnsafeMutablePointerTestSuite.test("initialize:from:/immutable") {
let ptr = UnsafeMutablePointer<Missile>.allocate(capacity: 3)
defer {
ptr.deinitialize(count: 3)
ptr.deallocate()
}
let source = (0..<3).map(Missile.init)
source.withUnsafeBufferPointer { bufferPtr in
ptr.initialize(from: bufferPtr.baseAddress!, count: 3)
expectEqual(0, ptr[0].number)
expectEqual(1, ptr[1].number)
expectEqual(2, ptr[2].number)
}
}
UnsafeMutablePointerTestSuite.test("assign/immutable") {
let ptr = UnsafeMutablePointer<Missile>.allocate(capacity: 2)
defer {
ptr.deinitialize(count: 2)
ptr.deallocate()
}
ptr.initialize(to: Missile(1))
(ptr + 1).initialize(to: Missile(2))
let source = (3..<5).map(Missile.init)
source.withUnsafeBufferPointer { bufferPtr in
ptr.assign(from: bufferPtr.baseAddress!, count: 2)
expectEqual(3, ptr[0].number)
expectEqual(4, ptr[1].number)
}
}
% for (SelfName, SelfType) in [
% ('UnsafePointer', 'UnsafePointer<Float>'),
% ('UnsafeMutablePointer', 'UnsafeMutablePointer<Float>'),
% ('UnsafeRawPointer', 'UnsafeRawPointer'),
% ('UnsafeMutableRawPointer', 'UnsafeMutableRawPointer')]:
${SelfName}TestSuite.test("customMirror") {
// Ensure that the custom mirror works properly, including when the raw value
// is greater than Int.max
let reallyBigInt: UInt = UInt(Int.max) + 1
let ptr = ${SelfType}(bitPattern: reallyBigInt)!
let mirror = ptr.customMirror
expectEqual(1, mirror.children.count)
#if _pointerBitWidth(_32)
expectEqual("18446744071562067968", String(describing: mirror.children.first!.1))
#elseif _pointerBitWidth(_64)
expectEqual("9223372036854775808", String(describing: mirror.children.first!.1))
#else
fatalError("Unimplemented")
#endif
}
${SelfName}TestSuite.test("customPlaygroundQuickLook") {
// Ensure that the custom playground quicklook works properly, including when
// the raw value is greater than Int.max
let reallyBigInt: UInt = UInt(Int.max) + 1
let ptr = ${SelfType}(bitPattern: reallyBigInt)!
if case let .text(desc) = ptr.customPlaygroundQuickLook {
#if _pointerBitWidth(_32)
expectEqual("${SelfName}(0xFFFFFFFF80000000)", desc)
#elseif _pointerBitWidth(_64)
expectEqual("${SelfName}(0x8000000000000000)", desc)
#else
fatalError("Unimplemented")
#endif
} else {
expectTrue(false)
}
}
${SelfName}TestSuite.test("Comparable") {
var addresses: [UInt] = [
0x00000001,
0xFF00FF00,
0x00001111,
0x00000002,
0xFFFFFFFF,
0x00001111
]
#if arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le)
addresses += [
0xFFFFFFFF80000000,
0x8000000000000000
]
#endif
let instances = addresses.map { ($0, ${SelfType}(bitPattern: $0)!) }
func comparisonOracle(i: Int, j: Int) -> ExpectedComparisonResult {
return instances[i].0 <=> instances[j].0
}
checkComparable(instances.map { $0.1 }, oracle: comparisonOracle)
}
% end
% for SelfName in ['UnsafePointer', 'UnsafeMutablePointer']:
${SelfName}TestSuite.test("withMemoryRebound") {
let mutablePtrI = UnsafeMutablePointer<Int>.allocate(capacity: 4)
defer { mutablePtrI.deallocate() }
let ptrI = ${SelfName}<Int>(mutablePtrI)
ptrI.withMemoryRebound(to: UInt.self, capacity: 4) {
// Make sure the closure argument isa $SelfName
let ptrU: ${SelfName}<UInt> = $0
// and that the element type is UInt.
let mutablePtrU = UnsafeMutablePointer(mutating: ptrU)
expectType(UInt.self, &mutablePtrU.pointee)
}
}
${SelfName}TestSuite.test("withMemoryReboundSE0333") {
let mutablePtrI = UnsafeMutablePointer<Int>.allocate(capacity: 4)
defer { mutablePtrI.deallocate() }
let ptrI = ${SelfName}<Int>(mutablePtrI)
// test rebinding to a wider type with the same alignment
ptrI.withMemoryRebound(to: (Int, Int).self, capacity: 2) {
// Make sure the closure argument isa $SelfName
let ptrT: ${SelfName}<(Int, Int)> = $0
// and that the element type is (Int, Int).
let mutablePtrT = UnsafeMutablePointer(mutating: ptrT)
expectType((Int, Int).self, &mutablePtrT.pointee)
// test rebinding to a narrower type
ptrT.withMemoryRebound(to: UInt.self, capacity: 4) {
// Make sure the closure argument isa $SelfName
let ptrU: ${SelfName}<UInt> = $0
// and that the element type is UInt.
let mutablePtrU = UnsafeMutablePointer(mutating: ptrU)
expectType(UInt.self, &mutablePtrU.pointee)
}
}
}
${SelfName}TestSuite.test("withMemoryReboundSE0333.capacity1") {
let mutablePtr = UnsafeMutablePointer<(Int,Bool,Int64)>.allocate(capacity: 1)
let i = Int.random(in: 0 ... .max)
mutablePtr.initialize(to: (i, i.isMultiple(of: 2), Int64(0xff & i)))
defer { mutablePtr.deallocate() }
let ptr = ${SelfName}<(Int, Bool, Int64)>(mutablePtr)
// test rebinding to a shorter type with the same alignment
ptr.withMemoryRebound(to: (Int, Bool).self, capacity: 1) {
// Make sure the closure argument is a ${SelfName}
let ptrT: ${SelfName}<(Int, Bool)> = $0
// and that the element is (Int, Bool).
let mutablePtrT = UnsafeMutablePointer(mutating: ptrT)
expectType((Int, Bool).self, &mutablePtrT.pointee)
expectEqual($0.pointee.0, i)
expectEqual($0.pointee.1, i.isMultiple(of: 2))
}
}
${SelfName}TestSuite.test("pointer(to:)") {
struct Example {
var a = false
var b = 0
var c: String { "\(a),\(b)" }
}
let allocated = UnsafeMutablePointer<Example>.allocate(capacity: 1)
allocated.pointee = Example()
defer { allocated.deallocate() }
let p = ${SelfName}(allocated)
expectEqual(p.pointer(to: \.a)!.pointee, false)
expectEqual(p.pointer(to: \.b)!.pointee, 0)
expectNil(p.pointer(to: \.c))
% if SelfName == 'UnsafeMutablePointer':
p.pointer(to: \.a)!.pointee = true
p.pointer(to: \.b)!.pointee += 1
expectEqual(p.pointer(to: \.a as KeyPath)!.pointee, true)
expectEqual(p.pointer(to: \.b as KeyPath)!.pointee, 1)
% end
}
#if !os(WASI)
${SelfName}TestSuite.test("pointer(to:).overflow") {
struct Example {
var a = false
var b = 0
var c: String { "\(a),\(b)" }
var d = 0.0
}
let o = MemoryLayout<Example>.offset(of: \.b)!
let p = UnsafePointer<Example>(bitPattern: -o)
expectNotNil(p)
guard let p else { fatalError() }
if _isDebugAssertConfiguration() {
expectCrashLater()
}
let intPointer = p.pointer(to: \.b)
expectNil(intPointer) // nil because of overflow
let stringPointer = p.pointer(to: \.c)
expectNil(stringPointer) // nil because c is a computed property
let doublePointer = p.pointer(to: \.d)
expectNotNil(doublePointer)
}
#endif
% end
runAllTests()
|