Files
swift-mirror/validation-test/stdlib/UnsafeBufferPointer.swift.gyb
rintaro ishizaki 4b51d85abb [test] Add %target-run-simple-swiftgyb
* Utilize %target-run-simple-swiftgyb where possible
2016-06-15 11:49:44 +09:00

141 lines
4.2 KiB
Plaintext

// 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<Float>', 'UnsafePointer<Float>'),
% ('UnsafeMutableBufferPointer', True, 'UnsafeMutableBufferPointer<Float>', 'UnsafeMutablePointer<Float>')
% ]:
${SelfName}TestSuite.test("AssociatedTypes") {
typealias Subject = ${SelfName}<OpaqueValue<Int>>
expectRandomAccessCollectionAssociatedTypes(
collectionType: Subject.self,
iteratorType: IndexingIterator<Subject>.self,
subSequenceType: ${'Mutable' if IsMutable else ''}RandomAccessSlice<Subject>.self,
indexType: Int.self,
indexDistanceType: Int.self,
indicesType: CountableRange<Int>.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<Float>(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<Float>(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<Float>(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<Float>(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()