// RUN: %target-run-simple-swiftgyb // REQUIRES: executable_test import StdlibUnittest import StdlibCollectionUnittest var UnsafeBufferPointerTestSuite = TestSuite("UnsafeBufferPointer") var UnsafeMutableBufferPointerTestSuite = TestSuite("UnsafeMutableBufferPointer") % for (SelfName, IsMutable, SelfType, PointerType) in [ % ('UnsafeBufferPointer', False, 'UnsafeBufferPointer', 'UnsafePointer'), % ('UnsafeMutableBufferPointer', True, 'UnsafeMutableBufferPointer', 'UnsafeMutablePointer') % ]: ${SelfName}TestSuite.test("AssociatedTypes") { typealias Subject = ${SelfName}> expectRandomAccessCollectionAssociatedTypes( collectionType: Subject.self, iteratorType: IndexingIterator.self, subSequenceType: ${'Mutable' if IsMutable else ''}RandomAccessSlice.self, indexType: Int.self, indexDistanceType: Int.self, indicesType: CountableRange.self) expect${'Mutable' if IsMutable else ''}CollectionType(Subject.self) } ${SelfName}TestSuite.test("nilBaseAddress") { let emptyBuffer = ${SelfType}(start: nil, count: 0) expectEmpty(emptyBuffer.baseAddress) expectEqual(0, emptyBuffer.count) expectTrue(emptyBuffer.startIndex == emptyBuffer.endIndex) var iter = emptyBuffer.makeIterator() expectEmpty(iter.next()) expectEqualSequence([], emptyBuffer) } ${SelfName}TestSuite.test("nonNilButEmpty") { let emptyAllocated = UnsafeMutablePointer(allocatingCapacity: 0) defer { emptyAllocated.deallocateCapacity(0) } let emptyBuffer = ${SelfType}(start: ${PointerType}(emptyAllocated), count: 0) expectEqual(emptyAllocated, emptyBuffer.baseAddress) expectEqual(0, emptyBuffer.count) expectTrue(emptyBuffer.startIndex == emptyBuffer.endIndex) var iter = emptyBuffer.makeIterator() expectEmpty(iter.next()) expectEqualSequence([], emptyBuffer) } ${SelfName}TestSuite.test("nonNilNonEmpty") { let count = 4 let allocated = UnsafeMutablePointer(allocatingCapacity: count) defer { allocated.deallocateCapacity(count) } allocated.initialize(with: 1.0, count: count) allocated[count - 1] = 2.0 let buffer = ${SelfType}(start: ${PointerType}(allocated), count: count - 1) expectEqual(allocated, buffer.baseAddress) expectEqual(count - 1, buffer.count) expectEqual(count - 1, buffer.endIndex - buffer.startIndex) allocated[1] = 0.0 expectEqual(1.0, buffer[0]) expectEqual(0.0, buffer[1]) expectEqual(1.0, buffer[2]) var iter = buffer.makeIterator() expectEqual(1.0, iter.next()) expectEqual(0.0, iter.next()) expectEqual(1.0, iter.next()) expectEmpty(iter.next()) expectEqualSequence([1.0, 0.0, 1.0], buffer) expectEqual(2.0, allocated[count-1]) } ${SelfName}TestSuite.test("badCount") .skip(.custom( { _isFastAssertConfiguration() }, reason: "this trap is not guaranteed to happen in -Ounchecked")) .code { expectCrashLater() let emptyAllocated = UnsafeMutablePointer(allocatingCapacity: 0) defer { emptyAllocated.deallocateCapacity(0) } let buffer = ${SelfType}(start: ${PointerType}(emptyAllocated), count: -1) _ = buffer } ${SelfName}TestSuite.test("badNilCount") .skip(.custom( { _isFastAssertConfiguration() }, reason: "this trap is not guaranteed to happen in -Ounchecked")) .code { expectCrashLater() let buffer = ${SelfType}(start: nil, count: 1) _ = buffer } % end UnsafeMutableBufferPointerTestSuite.test("changeElementViaBuffer") { let count = 4 let allocated = UnsafeMutablePointer(allocatingCapacity: count) defer { allocated.deallocateCapacity(count) } allocated.initialize(with: 1.0, count: count) allocated[count-1] = -1.0 var buffer = UnsafeMutableBufferPointer(start: allocated, count: count - 1) buffer[1] = 0.0 expectEqual(1.0, buffer[0]) expectEqual(0.0, buffer[1]) expectEqual(1.0, buffer[2]) expectEqual(1.0, allocated[0]) expectEqual(0.0, allocated[1]) expectEqual(1.0, allocated[2]) expectEqual(-1.0, allocated[count-1]) buffer.sort() expectEqual(0.0, buffer[0]) expectEqual(1.0, buffer[1]) expectEqual(1.0, buffer[2]) expectEqual(0.0, allocated[0]) expectEqual(1.0, allocated[1]) expectEqual(1.0, allocated[2]) expectEqual(-1.0, allocated[count-1]) } runAllTests()