// RUN: %target-run-simple-swiftgyb // REQUIRES: executable_test // // Test corner cases specific to UnsafeRawBufferPointer. // General Collection behavior tests are in // validation-test/stdlib/UnsafeBufferPointer.swift. import StdlibUnittest var UnsafeRawBufferPointerTestSuite = TestSuite("UnsafeRawBufferPointer") // View an in-memory value's bytes. // Use copyBytes to overwrite the value's bytes. UnsafeRawBufferPointerTestSuite.test("initFromValue") { var value1: Int32 = -1 var value2: Int32 = 0 // Immutable view of value1's bytes. withUnsafeBytes(of: &value1) { bytes1 in expectEqual(bytes1.count, 4) for b in bytes1 { expectEqual(b, 0xFF) } // Mutable view of value2's bytes. withUnsafeMutableBytes(of: &value2) { bytes2 in expectEqual(bytes1.count, bytes2.count) bytes2.copyBytes(from: bytes1) } } expectEqual(value2, value1) } // Test mutability and subscript getter/setters. UnsafeRawBufferPointerTestSuite.test("nonmutating_subscript_setter") { var value1: Int32 = -1 var value2: Int32 = 0 withUnsafeMutableBytes(of: &value1) { bytes1 in withUnsafeMutableBytes(of: &value2) { bytes2 in bytes2[0...stride expectEqual(it.next()!, 2) expectEqual(idx, 24) let expected: [Int64] = [5,4,3] expected.withUnsafeBytes { expectEqualSequence($0,buffer[0...stride expectEqual(it.next()!, 2) expectEqual(idx, 24) let expected: [Int64] = [5,4,3] expected.withUnsafeBytes { expectEqualSequence($0,buffer[0...stride expectNil(it.next()) expectEqual(idx, buffer.endIndex) source.withUnsafeBytes { expectEqualSequence($0,buffer) } expectEqualSequence([5, 4, 3],bound) } UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).invalidNilPtr") { let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) let source: [Int64] = [5, 4, 3, 2, 1] expectCrashLater() _ = buffer.initializeMemory(as: Int64.self, from: source) } UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).validNilPtr") { let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) let source: [Int64] = [] var (it, bound) = buffer.initializeMemory(as: Int64.self, from: source) let idx = bound.endIndex * MemoryLayout.stride expectNil(it.next()) expectEqual(idx, source.endIndex) } // Directly test the byte Sequence produced by withUnsafeBytes. UnsafeRawBufferPointerTestSuite.test("withUnsafeBytes.Sequence") { let array1: [Int32] = [0, 1, 2, 3] array1.withUnsafeBytes { bytes1 in // Initialize an array from a sequence of bytes. let byteArray = [UInt8](bytes1) for (b1, b2) in zip(byteArray, bytes1) { expectEqual(b1, b2) } } } // Test the empty buffer. UnsafeRawBufferPointerTestSuite.test("empty") { let emptyBytes = UnsafeRawBufferPointer(start: nil, count: 0) for _ in emptyBytes { expectUnreachable() } let emptyMutableBytes = UnsafeMutableRawBufferPointer.allocate(count: 0) for _ in emptyMutableBytes { expectUnreachable() } emptyMutableBytes.deallocate() } // Store a sequence of integers to raw memory, and reload them as structs. // Store structs to raw memory, and reload them as integers. UnsafeRawBufferPointerTestSuite.test("reinterpret") { struct Pair { var x: Int32 var y: Int32 } let numPairs = 2 let bytes = UnsafeMutableRawBufferPointer.allocate( count: MemoryLayout.stride * numPairs) defer { bytes.deallocate() } for i in 0..<(numPairs * 2) { bytes.storeBytes(of: Int32(i), toByteOffset: i * MemoryLayout.stride, as: Int32.self) } let pair1 = bytes.load(as: Pair.self) let pair2 = bytes.load(fromByteOffset: MemoryLayout.stride, as: Pair.self) expectEqual(0, pair1.x) expectEqual(1, pair1.y) expectEqual(2, pair2.x) expectEqual(3, pair2.y) bytes.storeBytes(of: Pair(x: -1, y: 0), as: Pair.self) for i in 0...stride { expectEqual(0xFF, bytes[i]) } let bytes2 = UnsafeMutableRawBufferPointer( rebasing: bytes[MemoryLayout.stride...stride { expectEqual(0, bytes2[i]) } } // Store, load, subscript, and slice at all valid byte offsets. UnsafeRawBufferPointerTestSuite.test("inBounds") { let numInts = 4 let bytes = UnsafeMutableRawBufferPointer.allocate( count: MemoryLayout.stride * numInts) defer { bytes.deallocate() } for i in 0...stride, as: Int.self) } for i in 0...stride, as: Int.self) expectEqual(x, i) } for i in 0...stride + offset], byte) } } } let median = (numInts/2 * MemoryLayout.stride) var firstHalf = bytes[0..