mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
256 lines
7.5 KiB
Swift
256 lines
7.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2025 - 2026 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Testing
|
|
|
|
#if COLLECTIONS_SINGLE_MODULE
|
|
import Collections
|
|
#else
|
|
import _CollectionsTestSupport
|
|
import TrailingElementsModule
|
|
#endif
|
|
|
|
struct Point {
|
|
var x: Int
|
|
var y: Int
|
|
}
|
|
|
|
struct Coordinates {
|
|
var numCoordinates: Int
|
|
}
|
|
|
|
extension Coordinates: TrailingElements {
|
|
typealias Element = Point
|
|
var trailingCount: Int { numCoordinates }
|
|
}
|
|
|
|
struct OneByteWithPointers: @unsafe TrailingElements {
|
|
var byte: UInt8
|
|
|
|
init(count: Int) {
|
|
self.byte = UInt8(count)
|
|
}
|
|
|
|
typealias Element = OpaquePointer
|
|
var trailingCount: Int { Int(byte) }
|
|
}
|
|
|
|
struct ManyBytesWithPointers: @unsafe TrailingElements {
|
|
var bytes: (
|
|
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
|
|
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
|
|
UInt8, UInt8)
|
|
|
|
init(count: Int) {
|
|
self.bytes = (
|
|
UInt8(count), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
}
|
|
|
|
typealias Element = OpaquePointer
|
|
var trailingCount: Int { Int(bytes.0) }
|
|
}
|
|
|
|
struct TrackedList: TrailingElements {
|
|
typealias Element = LifetimeTracked<Int>
|
|
var trailingCount: Int
|
|
}
|
|
|
|
struct InitializationError: Error {}
|
|
|
|
@Suite("Trailing elements tests")
|
|
struct TrailingElementsTests {
|
|
@Test func simpleCoordinates() {
|
|
var coords = TrailingArray(
|
|
header: Coordinates(numCoordinates: 3)
|
|
) { outputSpan in
|
|
outputSpan.append(Point(x: 1, y: 2))
|
|
outputSpan.append(Point(x: 2, y: 3))
|
|
outputSpan.append(Point(x: 3, y: 4))
|
|
}
|
|
|
|
#expect(coords[0].x == 1)
|
|
#expect(coords[1].x == 2)
|
|
#expect(coords[2].x == 3)
|
|
|
|
coords[1].y = 17
|
|
#expect(coords[1].y == 17)
|
|
}
|
|
|
|
@Test func repeatingInit() {
|
|
var coords = TrailingArray(
|
|
header: Coordinates(numCoordinates: 3), repeating: Point(x: 1, y: 1))
|
|
|
|
#expect(coords[0].x == 1)
|
|
#expect(coords[1].x == 1)
|
|
#expect(coords[2].x == 1)
|
|
|
|
coords[1].x = 17
|
|
#expect(coords[0].x == 1)
|
|
#expect(coords[1].x == 17)
|
|
#expect(coords[2].x == 1)
|
|
}
|
|
|
|
@Test func temporaryCoordinates() {
|
|
TrailingArray.withTemporaryValue(
|
|
header: Coordinates(numCoordinates: 3)
|
|
) { outputSpan in
|
|
outputSpan.append(Point(x: 1, y: 2))
|
|
outputSpan.append(Point(x: 2, y: 3))
|
|
outputSpan.append(Point(x: 3, y: 4))
|
|
} body: { coords in
|
|
#expect(coords[0].x == 1)
|
|
#expect(coords[1].x == 2)
|
|
#expect(coords[2].x == 3)
|
|
}
|
|
}
|
|
|
|
@Test(arguments: 0 ... 10)
|
|
func underalignedByteOnHeap(count: Int) {
|
|
var pointers = unsafe TrailingArray(
|
|
header: OneByteWithPointers(count: count)
|
|
) { outputSpan in
|
|
for i in 0..<count {
|
|
unsafe outputSpan.append(OpaquePointer(bitPattern: i+1)!)
|
|
}
|
|
}
|
|
|
|
for i in 0..<count {
|
|
#expect(unsafe pointers[i] == OpaquePointer(bitPattern: i+1))
|
|
}
|
|
|
|
unsafe pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
|
|
let p1 = unsafe headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
|
|
let p2 = elementsPtr.baseAddress
|
|
#expect(unsafe UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
|
|
}
|
|
}
|
|
|
|
@Test(arguments: 0 ... 10)
|
|
func underalignedByteOnStack(count: Int) {
|
|
unsafe TrailingArray.withTemporaryValue(header: OneByteWithPointers(count: count)) { outputSpan in
|
|
for i in 0..<count {
|
|
unsafe outputSpan.append(OpaquePointer(bitPattern: i+1)!)
|
|
}
|
|
} body: { pointers in
|
|
for i in 0..<count {
|
|
#expect(unsafe pointers[i] == OpaquePointer(bitPattern: i+1))
|
|
}
|
|
|
|
unsafe pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
|
|
let p1 = unsafe headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
|
|
let p2 = elementsPtr.baseAddress
|
|
#expect(unsafe UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test(arguments: 0 ... 10)
|
|
func underalignedBytesOnHeap(count: Int) {
|
|
var pointers = unsafe TrailingArray(header: ManyBytesWithPointers(count: count)) { outputSpan in
|
|
for i in 0..<count {
|
|
unsafe outputSpan.append(OpaquePointer(bitPattern: i+1)!)
|
|
}
|
|
}
|
|
|
|
for i in 0..<count {
|
|
#expect(unsafe pointers[i] == OpaquePointer(bitPattern: i+1))
|
|
}
|
|
|
|
unsafe pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
|
|
let p1 = unsafe headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
|
|
let p2 = elementsPtr.baseAddress
|
|
#expect(unsafe UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
|
|
}
|
|
}
|
|
|
|
@Test(arguments: 0 ... 10)
|
|
func underalignedBytesOnStack(count: Int) {
|
|
unsafe TrailingArray.withTemporaryValue(header: ManyBytesWithPointers(count: count)) { outputSpan in
|
|
for i in 0..<count {
|
|
unsafe outputSpan.append(OpaquePointer(bitPattern: i+1)!)
|
|
}
|
|
} body: { pointers in
|
|
for i in 0..<count {
|
|
#expect(unsafe pointers[i] == OpaquePointer(bitPattern: i+1))
|
|
}
|
|
|
|
unsafe pointers.withUnsafeMutablePointers { headerPtr, elementsPtr in
|
|
let p1 = unsafe headerPtr.advanced(by: MemoryLayout<OneByteWithPointers>.stride)
|
|
let p2 = elementsPtr.baseAddress
|
|
#expect(unsafe UnsafeMutableRawPointer(p1) == UnsafeMutableRawPointer(p2))
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test(arguments: 0 ... 3)
|
|
func throwingInitializerOnHeap(initializedCount: Int) {
|
|
let tracker = LifetimeTracker()
|
|
#expect(throws: InitializationError.self) {
|
|
_ = try TrailingArray(
|
|
header: TrackedList(trailingCount: 3)
|
|
) { (outputSpan) throws(InitializationError) in
|
|
for _ in 0 ..< initializedCount {
|
|
outputSpan.append(tracker.instance(for: 0))
|
|
}
|
|
throw InitializationError()
|
|
}
|
|
}
|
|
#expect(tracker.instances == 0)
|
|
}
|
|
|
|
@Test(arguments: 0 ... 3)
|
|
func throwingInitializerOnStack(initializedCount: Int) {
|
|
let tracker = LifetimeTracker()
|
|
let initializer:
|
|
(inout OutputSpan<LifetimeTracked<Int>>) throws(InitializationError) -> Void = {
|
|
outputSpan throws(InitializationError) in
|
|
for _ in 0 ..< initializedCount {
|
|
outputSpan.append(tracker.instance(for: 0))
|
|
}
|
|
throw InitializationError()
|
|
}
|
|
let body: (inout TrailingArray<TrackedList>) throws(InitializationError) -> Void = { _ in
|
|
Issue.record("body shouldn't be called")
|
|
}
|
|
#expect(throws: InitializationError.self) {
|
|
try TrailingArray<TrackedList>.withTemporaryValue(
|
|
header: TrackedList(trailingCount: 3),
|
|
initializingTrailingElementsWith: initializer,
|
|
body: body)
|
|
}
|
|
#expect(tracker.instances == 0)
|
|
}
|
|
|
|
@Test func trackedElementsAreReleased() {
|
|
let tracker = LifetimeTracker()
|
|
do {
|
|
let tracked = TrailingArray(header: TrackedList(trailingCount: 3)) { outputSpan in
|
|
for _ in 0 ..< 3 {
|
|
outputSpan.append(tracker.instance(for: 0))
|
|
}
|
|
}
|
|
#expect(tracker.instances == 3)
|
|
#expect(tracked.count == 3)
|
|
}
|
|
#expect(tracker.instances == 0)
|
|
|
|
TrailingArray.withTemporaryValue(header: TrackedList(trailingCount: 2)) { outputSpan in
|
|
outputSpan.append(tracker.instance(for: 0))
|
|
outputSpan.append(tracker.instance(for: 0))
|
|
} body: { tracked in
|
|
#expect(tracker.instances == 2)
|
|
}
|
|
#expect(tracker.instances == 0)
|
|
}
|
|
}
|