Files
swift-collections/Tests/FutureTests/ContiguousStorageTests.swift

76 lines
1.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
import XCTest
import Future
@available(SwiftStdlib 6.2, *)
final class ContiguousStorageTests: XCTestCase {
@available(SwiftStdlib 6.2, *)
func testBorrowArrayStorage() throws {
let capacity = 10
var a: [Int] = []
a = Array(0..<capacity)
let span = a.span
XCTAssertEqual(span.count, capacity)
for i in span.indices {
XCTAssertEqual(span[i], a[i])
}
// This should end the borrow, and the mutation should work rdar://126298676
// _ = consume span
// a = []
}
private struct Skipper: ~Escapable {
private var span: Span<Int>
var startIndex: Int { 0 }
var endIndex: Int { span.count }
func index(after i: Int) -> Int { i+2 }
@lifetime(copy contiguous)
init(_ contiguous: borrowing Span<Int>) {
span = copy contiguous
}
subscript(_ p: Int) -> Int { span[p] }
}
@available(SwiftStdlib 6.2, *)
@inline(never)
@lifetime(borrow array)
private func skip(
along array: borrowing Array<Int>
) -> Skipper {
Skipper(array.span)
}
@available(SwiftStdlib 6.2, *)
func testSpanWrapper() {
let capacity = 8
let a = Array(0..<capacity)
let skipper = skip(along: a)
var i = skipper.startIndex
var s: [Int] = []
while i < skipper.endIndex {
s.append(skipper[i])
i = skipper.index(after: i)
}
XCTAssertEqual(s, [0, 2, 4, 6])
}
}