mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
[ContainersPreview] Add more detailed tests for MutableRef’s API surface
This commit is contained in:
3 files changed
+254
-42
No files matched your search
@@ -1,38 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 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 XCTest
|
||||
#if COLLECTIONS_SINGLE_MODULE
|
||||
import Collections
|
||||
#else
|
||||
import _CollectionsTestSupport
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.3) && UnstableContainersPreview
|
||||
final class InoutTests: XCTestCase {
|
||||
func test_basic() {
|
||||
var x = 0
|
||||
var y = MutableRef(&x)
|
||||
|
||||
var v = y.value
|
||||
XCTAssertEqual(v, 0)
|
||||
|
||||
y.value += 10
|
||||
|
||||
v = y.value
|
||||
XCTAssertEqual(v, 10)
|
||||
XCTAssertEqual(x, 10)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,250 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2024 - 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 XCTest
|
||||
#if COLLECTIONS_SINGLE_MODULE
|
||||
import Collections
|
||||
#else
|
||||
import _CollectionsTestSupport
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.3) && UnstableContainersPreview
|
||||
final class MutableRefTests: CollectionTestCase {
|
||||
struct NoncopyablePayload: ~Copyable {
|
||||
var value: Int
|
||||
init(_ value: Int) { self.value = value }
|
||||
}
|
||||
|
||||
func test_basic() {
|
||||
var x = 0
|
||||
var y = MutableRef(&x)
|
||||
|
||||
var v = y.value
|
||||
XCTAssertEqual(v, 0)
|
||||
|
||||
y.value += 10
|
||||
|
||||
v = y.value
|
||||
XCTAssertEqual(v, 10)
|
||||
XCTAssertEqual(x, 10)
|
||||
}
|
||||
|
||||
// MARK: init(_:) and value (read)
|
||||
|
||||
func test_init_and_value_read_int() {
|
||||
var x = 42
|
||||
let ref = MutableRef(&x)
|
||||
expectEqual(ref.value, 42)
|
||||
}
|
||||
|
||||
func test_init_and_value_read_string() {
|
||||
var s = "hello"
|
||||
let ref = MutableRef(&s)
|
||||
expectEqual(ref.value, "hello")
|
||||
}
|
||||
|
||||
func test_init_and_value_read_noncopyable() {
|
||||
var payload = NoncopyablePayload(99)
|
||||
let ref = MutableRef(&payload)
|
||||
expectEqual(ref.value.value, 99)
|
||||
}
|
||||
|
||||
// MARK: value (write)
|
||||
|
||||
func test_value_write_int() {
|
||||
var x = 0
|
||||
var ref = MutableRef(&x)
|
||||
ref.value = 123
|
||||
expectEqual(ref.value, 123)
|
||||
expectEqual(x, 123)
|
||||
}
|
||||
|
||||
func test_value_write_string() {
|
||||
var s = "before"
|
||||
var ref = MutableRef(&s)
|
||||
ref.value = "after"
|
||||
expectEqual(ref.value, "after")
|
||||
expectEqual(s, "after")
|
||||
}
|
||||
|
||||
func test_value_write_noncopyable() {
|
||||
var payload = NoncopyablePayload(1)
|
||||
var ref = MutableRef(&payload)
|
||||
ref.value.value = 42
|
||||
expectEqual(ref.value.value, 42)
|
||||
expectEqual(payload.value, 42)
|
||||
}
|
||||
|
||||
// MARK: value (in-place mutation)
|
||||
|
||||
func test_value_inplace_mutation() {
|
||||
var x = 10
|
||||
var ref = MutableRef(&x)
|
||||
ref.value += 32
|
||||
expectEqual(ref.value, 42)
|
||||
expectEqual(x, 42)
|
||||
}
|
||||
|
||||
func test_value_inplace_mutation_array() {
|
||||
var array = [1, 2, 3]
|
||||
var ref = MutableRef(&array)
|
||||
ref.value.append(4)
|
||||
expectEqual(ref.value, [1, 2, 3, 4])
|
||||
expectEqual(array, [1, 2, 3, 4])
|
||||
}
|
||||
|
||||
// MARK: init(unsafeAddress:mutating:)
|
||||
|
||||
func test_init_unsafeAddress_mutating() {
|
||||
var x = 789
|
||||
let pointer = unsafe withUnsafeMutablePointer(to: &x) { $0 }
|
||||
var ref = unsafe MutableRef(unsafeAddress: pointer, mutating: &x)
|
||||
expectEqual(ref.value, 789)
|
||||
ref.value = 111
|
||||
expectEqual(ref.value, 111)
|
||||
}
|
||||
|
||||
// MARK: init(unsafeImmortalAddress:)
|
||||
|
||||
func test_init_unsafeImmortalAddress() {
|
||||
var x = 555
|
||||
let pointer = unsafe withUnsafeMutablePointer(to: &x) { $0 }
|
||||
var ref = unsafe MutableRef(unsafeImmortalAddress: pointer)
|
||||
expectEqual(ref.value, 555)
|
||||
ref.value = 666
|
||||
expectEqual(ref.value, 666)
|
||||
}
|
||||
|
||||
// MARK: Reference type identity
|
||||
|
||||
func test_reference_type_identity() {
|
||||
let tracker = LifetimeTracker()
|
||||
var obj: LifetimeTracked<Int>? = tracker.instance(for: 7)
|
||||
let ref = MutableRef(&obj)
|
||||
expectNotNil(ref.value)
|
||||
expectEqual(ref.value!.payload, 7)
|
||||
}
|
||||
|
||||
// MARK: Multiple writes through MutableRef
|
||||
|
||||
func test_multiple_writes() {
|
||||
var x = 0
|
||||
var ref = MutableRef(&x)
|
||||
ref.value = 1
|
||||
expectEqual(ref.value, 1)
|
||||
ref.value = 2
|
||||
expectEqual(ref.value, 2)
|
||||
ref.value = 3
|
||||
expectEqual(ref.value, 3)
|
||||
expectEqual(x, 3)
|
||||
}
|
||||
|
||||
// MARK: Optional.mutate()
|
||||
|
||||
func test_optional_mutate_some() {
|
||||
var x: Int? = 77
|
||||
if var ref = x.mutate() {
|
||||
expectEqual(ref.value, 77)
|
||||
ref.value = 88
|
||||
} else {
|
||||
expectTrue(false, "Expected non-nil MutableRef from .some optional")
|
||||
}
|
||||
expectEqual(x, 88)
|
||||
}
|
||||
|
||||
func test_optional_mutate_none() {
|
||||
var x: Int? = nil
|
||||
let ref = x.mutate()
|
||||
expectNil(ref)
|
||||
}
|
||||
|
||||
func test_optional_mutate_noncopyable() {
|
||||
var x: NoncopyablePayload? = NoncopyablePayload(33)
|
||||
if var ref = x.mutate() {
|
||||
expectEqual(ref.value.value, 33)
|
||||
ref.value.value = 44
|
||||
} else {
|
||||
expectTrue(false, "Expected non-nil MutableRef from .some optional")
|
||||
}
|
||||
expectEqual(x!.value, 44)
|
||||
}
|
||||
|
||||
// MARK: Optional.insert(_:)
|
||||
|
||||
func test_optional_insert_into_nil() {
|
||||
var x: Int? = nil
|
||||
do {
|
||||
var ref = x.insert(42)
|
||||
expectEqual(ref.value, 42)
|
||||
ref.value = 99
|
||||
}
|
||||
expectEqual(x, 99)
|
||||
}
|
||||
|
||||
func test_optional_insert_into_some() {
|
||||
var x: Int? = 10
|
||||
do {
|
||||
var ref = x.insert(20)
|
||||
expectEqual(ref.value, 20)
|
||||
ref.value = 30
|
||||
}
|
||||
expectEqual(x, 30)
|
||||
}
|
||||
|
||||
func test_optional_insert_noncopyable() {
|
||||
var x: NoncopyablePayload? = nil
|
||||
do {
|
||||
var ref = x.insert(NoncopyablePayload(55))
|
||||
expectEqual(ref.value.value, 55)
|
||||
ref.value.value = 66
|
||||
}
|
||||
expectEqual(x!.value, 66)
|
||||
}
|
||||
|
||||
// MARK: Deprecated API
|
||||
|
||||
@available(*, deprecated)
|
||||
func test_subscript_deprecated_read() {
|
||||
var x = 11
|
||||
let ref = MutableRef(&x)
|
||||
expectEqual(ref[], 11)
|
||||
}
|
||||
|
||||
@available(*, deprecated)
|
||||
func test_subscript_deprecated_write() {
|
||||
var x = 0
|
||||
var ref = MutableRef(&x)
|
||||
ref[] = 22
|
||||
expectEqual(ref[], 22)
|
||||
expectEqual(x, 22)
|
||||
}
|
||||
|
||||
@available(*, deprecated)
|
||||
func test_Inout_typealias() {
|
||||
var x = 88
|
||||
var ref: Inout<Int> = Inout(&x)
|
||||
ref.value = 99
|
||||
expectEqual(ref.value, 99)
|
||||
expectEqual(x, 99)
|
||||
}
|
||||
|
||||
@available(*, deprecated)
|
||||
func test_Target_typealias() {
|
||||
var x = 77
|
||||
let ref = MutableRef(&x)
|
||||
let v: MutableRef<Int>.Target = ref.value
|
||||
expectEqual(v, 77)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -62,8 +62,8 @@
|
||||
7D2A779A2F27F2BF005780C7 /* RigidDeque+Testing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A776B2F27F2BF005780C7 /* RigidDeque+Testing.swift */; };
|
||||
7D2A779B2F27F2BF005780C7 /* UniqueDeque+Experimental.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77712F27F2BF005780C7 /* UniqueDeque+Experimental.swift */; };
|
||||
7D2A779C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A775C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift */; };
|
||||
7D2A779F2F27F367005780C7 /* InoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A779E2F27F367005780C7 /* InoutTests.swift */; };
|
||||
7D2A77A02F27F367005780C7 /* RefTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A779D2F27F367005780C7 /* RefTests.swift */; };
|
||||
7D2F598E2FAC229B00D43FE7 /* MutableRefTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2F598D2FAC229B00D43FE7 /* MutableRefTests.swift */; };
|
||||
7D479CB02F450E4600A8CB59 /* RigidSet+BorrowingSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */; };
|
||||
7D479CB32F45296A00A8CB59 /* UniqueSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB12F45296000A8CB59 /* UniqueSet.swift */; };
|
||||
7D479CB52F452D6900A8CB59 /* UniqueSetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */; };
|
||||
@@ -657,7 +657,7 @@
|
||||
7D2A77752F27F2BF005780C7 /* UniqueDeque+Removals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Removals.swift"; sourceTree = "<group>"; };
|
||||
7D2A77762F27F2BF005780C7 /* UniqueDeque+Replacements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Replacements.swift"; sourceTree = "<group>"; };
|
||||
7D2A779D2F27F367005780C7 /* RefTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefTests.swift; sourceTree = "<group>"; };
|
||||
7D2A779E2F27F367005780C7 /* InoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InoutTests.swift; sourceTree = "<group>"; };
|
||||
7D2F598D2FAC229B00D43FE7 /* MutableRefTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableRefTests.swift; sourceTree = "<group>"; };
|
||||
7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+BorrowingSequence.swift"; sourceTree = "<group>"; };
|
||||
7D479CB12F45296000A8CB59 /* UniqueSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueSet.swift; sourceTree = "<group>"; };
|
||||
7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueSetTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1414,7 +1414,7 @@
|
||||
7D7646052FAAA2B9008639E5 /* UniqueBoxTests.swift */,
|
||||
7D72F5972F75E5E6004AB792 /* BorrowingSequenceTests.swift */,
|
||||
7D2A779D2F27F367005780C7 /* RefTests.swift */,
|
||||
7D2A779E2F27F367005780C7 /* InoutTests.swift */,
|
||||
7D2F598D2FAC229B00D43FE7 /* MutableRefTests.swift */,
|
||||
);
|
||||
path = ContainersTests;
|
||||
sourceTree = "<group>";
|
||||
@@ -3109,6 +3109,7 @@
|
||||
7DEBDB8729CCE44A00ADC226 /* MinimalMutableRandomAccessCollection.swift in Sources */,
|
||||
7DEBDB7829CCE44A00ADC226 /* Assertions.swift in Sources */,
|
||||
7DE9220D29CA8576004483EB /* TestBigString.swift in Sources */,
|
||||
7D2F598E2FAC229B00D43FE7 /* MutableRefTests.swift in Sources */,
|
||||
7DEBDB8329CCE44A00ADC226 /* CheckHashable.swift in Sources */,
|
||||
7DE921FF29CA8576004483EB /* Utilities.swift in Sources */,
|
||||
7DEBDB8F29CCE44A00ADC226 /* CheckEquatable.swift in Sources */,
|
||||
@@ -3134,7 +3135,6 @@
|
||||
7DE9220129CA8576004483EB /* TreeDictionary.Keys Tests.swift in Sources */,
|
||||
7DE9220729CA8576004483EB /* BitSet.Counted Tests.swift in Sources */,
|
||||
7DE9220F29CA8576004483EB /* OrderedDictionary Tests.swift in Sources */,
|
||||
7D2A779F2F27F367005780C7 /* InoutTests.swift in Sources */,
|
||||
7D2A77A02F27F367005780C7 /* RefTests.swift in Sources */,
|
||||
7D48BCDF2E51558700D7F6F7 /* TestPrintable.swift in Sources */,
|
||||
7D48BCE02E51558700D7F6F7 /* LifetimeTrackedStruct.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user