Files
swift-collections/Tests/ContainersTests/RefTests.swift
T
Karoy Lorentey 842f125c47 Invert dependencies to ContainersPreview, moving all container APIs there
- Move `InputSpan` into its own module, `SpanPreview`. Add dependencies to it.
- Invert dependencies to `ContainersPreview`, so that no production target imports it, but it now depends on its previous dependents.
- Move all generic APIs and conformances using `Producer`/`Drain`/`Container` to the `ContainersPreview` module.
- Remove `UnstableContainersPreview` conditions around `Iterable` APIs

In summary, importing `BasicContainers` and `DequeModule` continue to get us the basic definitions for `RigidArray`, `UniqueSet`, `RigidDeque` etc., without most of their unstable APIs. (With the exception of core APIs that require `InputSpan`.)

To get any API that uses an unstable protocol like `Container` or `Drain`, we’ll now need to import `ContainersPreview` in addition to the type-defining module.

This will provide a slightly less horrible migration path when (if) the container/producer protocol family gets adopted/adapted into the stdlib.
2026-08-25 18:29:58 -07:00

145 lines
3.6 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.4) && UnstableContainersPreview
import XCTest
#if COLLECTIONS_SINGLE_MODULE
import Collections
#else
import InternalCollectionsUtilities
import _CollectionsTestSupport
import ContainersPreview
#endif
@available(SwiftStdlib 6.4, *)
final class RefTests: CollectionTestCase {
struct NoncopyablePayload: ~Copyable {
var value: Int
init(_ value: Int) { self.value = value }
}
// MARK: init(_:) and value
@available(SwiftStdlib 6.4, *)
func test_init_and_value_int() {
let x = 42
let ref = Ref(x)
expectEqual(ref.value, 42)
}
@available(SwiftStdlib 6.4, *)
func test_init_and_value_string() {
let s = "hello"
let ref = Ref(s)
expectEqual(ref.value, "hello")
}
@available(SwiftStdlib 6.4, *)
func test_init_and_value_noncopyable() {
let payload = NoncopyablePayload(99)
let ref = Ref(payload)
expectEqual(ref.value.value, 99)
}
@available(SwiftStdlib 6.4, *)
func test_value_reflects_original() {
let array = [1, 2, 3]
let ref = Ref(array)
expectEqual(ref.value, [1, 2, 3])
}
@available(SwiftStdlib 6.4, *)
func test_value_reference_type() {
let tracker = LifetimeTracker()
let obj = tracker.instance(for: 7)
let ref = Ref(obj)
expectTrue(ref.value === obj)
expectEqual(ref.value.payload, 7)
}
// MARK: init(unsafeAddress:borrowing:)
@available(SwiftStdlib 6.4, *)
func test_init_unsafeAddress_borrowing() {
let x = 123
withUnsafePointer(to: x) { pointer in
let ref = Ref(unsafeAddress: pointer, borrowing: x)
expectEqual(ref.value, 123)
}
}
// MARK: Ref is Copyable
@available(SwiftStdlib 6.4, *)
func test_ref_is_copyable() {
let x = 42
let ref1 = Ref(x)
let ref2 = ref1
expectEqual(ref1.value, 42)
expectEqual(ref2.value, 42)
}
// MARK: Optional.borrow()
@available(SwiftStdlib 6.4, *)
func test_optional_borrow_some() {
let x: Int? = 77
if let ref = x._borrow() {
expectEqual(ref.value, 77)
} else {
expectFailure("Expected non-nil Ref from .some optional")
}
}
@available(SwiftStdlib 6.4, *)
func test_optional_borrow_none() {
let x: Int? = nil
let ref = x._borrow()
expectNil(ref)
}
@available(SwiftStdlib 6.4, *)
func test_optional_borrow_string() {
let x: String? = "world"
if let ref = x._borrow() {
expectEqual(ref.value, "world")
} else {
expectFailure("Expected non-nil Ref from .some optional")
}
}
@available(SwiftStdlib 6.4, *)
func test_optional_borrow_noncopyable() {
let x: NoncopyablePayload? = NoncopyablePayload(33)
if let ref = x._borrow() {
expectEqual(ref.value.value, 33)
} else {
expectFailure("Expected non-nil Ref from .some optional")
}
}
@available(SwiftStdlib 6.4, *)
func test_optional_borrow_reference_type() {
let tracker = LifetimeTracker()
let obj = tracker.instance(for: 5)
let x: LifetimeTracked<Int>? = obj
if let ref = x._borrow() {
expectTrue(ref.value === obj)
expectEqual(ref.value.payload, 5)
} else {
expectFailure("Expected non-nil Ref from .some optional")
}
}
}
#endif