Skip to content

Commit 0e7db11

Browse files
committed
[ArrayModule] Update DynamicArray tests
1 parent 721cf95 commit 0e7db11

File tree

2 files changed

+84
-19
lines changed

2 files changed

+84
-19
lines changed

Sources/ArrayModule/DynamicArray.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ extension DynamicArray where Element: ~Copyable {
133133
//MARK: - Span creation
134134

135135
extension DynamicArray where Element: ~Copyable {
136-
@available(SwiftStdlib 6.2, *)
136+
@available(SwiftStdlib 5.0, *)
137137
public var span: Span<Element> {
138138
@_lifetime(borrow self)
139139
@inlinable
@@ -142,7 +142,7 @@ extension DynamicArray where Element: ~Copyable {
142142
}
143143
}
144144

145-
@available(SwiftStdlib 6.2, *)
145+
@available(SwiftStdlib 5.0, *)
146146
public var mutableSpan: MutableSpan<Element> {
147147
@_lifetime(&self)
148148
@inlinable

Tests/ArrayTests/DynamicArrayTests.swift

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,50 @@ import ArrayModule
1818
#endif
1919

2020
#if compiler(>=6.2) && (compiler(>=6.3) || !os(Windows)) // FIXME: [2025-08-17] Windows has no 6.2 snapshot with OutputSpan
21+
22+
#if !COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
23+
/// Check if `left` and `right` contain equal elements in the same order.
24+
@available(SwiftStdlib 5.0, *)
25+
public func expectContainerContents<
26+
Element: Equatable,
27+
C2: Collection<Element>,
28+
>(
29+
_ left: borrowing DynamicArray<Element>,
30+
equalTo right: C2,
31+
_ message: @autoclosure () -> String = "",
32+
trapping: Bool = false,
33+
file: StaticString = #file,
34+
line: UInt = #line
35+
) {
36+
expectContainerContents(
37+
left.span,
38+
equalTo: right,
39+
message(), trapping: trapping, file: file, line: line)
40+
}
41+
42+
/// Check if `left` and `right` contain equal elements in the same order.
43+
@available(SwiftStdlib 5.0, *)
44+
public func expectContainerContents<
45+
E1: ~Copyable,
46+
C2: Collection,
47+
>(
48+
_ left: borrowing DynamicArray<E1>,
49+
equivalentTo right: C2,
50+
by areEquivalent: (borrowing E1, C2.Element) -> Bool,
51+
_ message: @autoclosure () -> String = "",
52+
trapping: Bool = false,
53+
file: StaticString = #file,
54+
line: UInt = #line
55+
) {
56+
expectContainerContents(
57+
left.span,
58+
equivalentTo: right,
59+
by: areEquivalent,
60+
message(), trapping: trapping, file: file, line: line)
61+
}
62+
#endif
63+
64+
2165
@available(SwiftStdlib 6.2, *)
2266
class DynamicArrayTests: CollectionTestCase {
2367
func test_validate_Container() {
@@ -26,7 +70,10 @@ class DynamicArrayTests: CollectionTestCase {
2670
let items = DynamicArray(consuming: tracker.rigidArray(layout: layout))
2771
let expected = (0 ..< layout.count).map { tracker.instance(for: $0) }
2872
expectEqual(tracker.instances, 2 * layout.count)
73+
expectContainerContents(items, equalTo: expected)
74+
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
2975
checkContainer(items, expectedContents: expected)
76+
#endif
3077
}
3178
}
3279
}
@@ -141,6 +188,7 @@ class DynamicArrayTests: CollectionTestCase {
141188
}
142189
}
143190

191+
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
144192
func test_init_copying_Container() {
145193
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
146194
withEvery("spanCounts", in: [
@@ -166,6 +214,7 @@ class DynamicArrayTests: CollectionTestCase {
166214
}
167215
}
168216
}
217+
#endif
169218

170219
func test_span() {
171220
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
@@ -240,10 +289,10 @@ class DynamicArrayTests: CollectionTestCase {
240289
withLifetimeTracking { tracker in
241290
var a = tracker.dynamicArray(layout: layout)
242291
var i = 0
243-
var span = a.nextMutableSpan(after: &i)
292+
var span = a.mutableSpan(after: &i)
244293
expectEqual(i, layout.count)
245294
expectEqual(span.count, layout.count)
246-
span = a.nextMutableSpan(after: &i)
295+
span = a.mutableSpan(after: &i)
247296
expectEqual(i, layout.count)
248297
expectTrue(span.isEmpty)
249298
}
@@ -274,6 +323,7 @@ class DynamicArrayTests: CollectionTestCase {
274323
}
275324
}
276325

326+
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
277327
func test_borrowElement() {
278328
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
279329
withLifetimeTracking { tracker in
@@ -285,7 +335,9 @@ class DynamicArrayTests: CollectionTestCase {
285335
}
286336
}
287337
}
338+
#endif
288339

340+
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
289341
func test_mutateElement() {
290342
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
291343
withLifetimeTracking { tracker in
@@ -299,6 +351,7 @@ class DynamicArrayTests: CollectionTestCase {
299351
}
300352
}
301353
}
354+
#endif
302355

303356
func test_edit() {
304357
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
@@ -324,7 +377,7 @@ class DynamicArrayTests: CollectionTestCase {
324377

325378
struct TestError: Error {}
326379

327-
expectThrows({
380+
expectThrows {
328381
try a.edit { span in
329382
expectEqual(tracker.instances, layout.capacity)
330383
while !span.isEmpty {
@@ -334,12 +387,13 @@ class DynamicArrayTests: CollectionTestCase {
334387
}
335388
throw TestError()
336389
}
337-
}) { error in
390+
}
391+
errorHandler: { error in
338392
expectTrue(error is TestError)
339393
}
340-
expectEquivalentElements(
341-
a.span,
342-
(0 ..< layout.count).map { -$0 },
394+
expectContainerContents(
395+
a,
396+
equivalentTo: (0 ..< layout.count).map { -$0 },
343397
by: { $0.payload == $1 })
344398
expectEqual(tracker.instances, layout.count)
345399
}
@@ -481,6 +535,7 @@ class DynamicArrayTests: CollectionTestCase {
481535
}
482536
}
483537

538+
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
484539
func test_removeAll_where() {
485540
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
486541
withLifetimeTracking { tracker in
@@ -494,6 +549,7 @@ class DynamicArrayTests: CollectionTestCase {
494549
}
495550
}
496551
}
552+
#endif
497553

498554
func test_popLast() {
499555
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
@@ -547,17 +603,18 @@ class DynamicArrayTests: CollectionTestCase {
547603
expectEqual(array.count, 100)
548604
expectEqual(array.capacity, 128)
549605

550-
let additions = RigidArray(capacity: 300) { span in
551-
for i in 0 ..< 300 {
552-
span.append(tracker.instance(for: 100 + i))
606+
do {
607+
let additions = RigidArray(capacity: 300) { span in
608+
for i in 0 ..< 300 {
609+
span.append(tracker.instance(for: 100 + i))
610+
}
553611
}
612+
array.append(copying: additions.span)
554613
}
555-
array.append(copying: additions.span)
556614
expectEqual(array.capacity, 400)
557615

558616
expectEqual(tracker.instances, 400)
559617
for i in 0 ..< 400 {
560-
expectEqual(array.borrowElement(at: i)[].payload, i)
561618
expectEqual(array[i].payload, i)
562619
}
563620

@@ -604,8 +661,10 @@ class DynamicArrayTests: CollectionTestCase {
604661
withEvery("additions", in: [0, 1, 10, 100]) { additions in
605662
withLifetimeTracking { tracker in
606663
var a = tracker.dynamicArray(layout: layout)
607-
let b = RigidArray(count: additions) {
608-
tracker.instance(for: layout.count + $0)
664+
let b = RigidArray(capacity: additions) { span in
665+
for i in 0 ..< additions {
666+
span.append(tracker.instance(for: layout.count + i))
667+
}
609668
}
610669
a.append(copying: b.span)
611670
let c = layout.count + additions
@@ -617,6 +676,7 @@ class DynamicArrayTests: CollectionTestCase {
617676
}
618677
}
619678

679+
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
620680
func test_append_copying_Container() {
621681
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
622682
withEvery("additions", in: [0, 1, 10, 100]) { additions in
@@ -640,6 +700,7 @@ class DynamicArrayTests: CollectionTestCase {
640700
}
641701
}
642702
}
703+
#endif
643704

644705
func test_insert_at() {
645706
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
@@ -692,9 +753,9 @@ class DynamicArrayTests: CollectionTestCase {
692753
var expected = Array(0 ..< layout.count)
693754
expected.insert(contentsOf: addition, at: i)
694755

695-
let rigidAddition = RigidArray(count: addition.count) {
696-
tracker.instance(for: addition[$0])
697-
}
756+
let rigidAddition = RigidArray(
757+
copying: (0 ..< addition.count).lazy.map { tracker.instance(for: addition[$0]) }
758+
)
698759
var a = tracker.dynamicArray(layout: layout)
699760
a.insert(copying: rigidAddition.span, at: i)
700761

@@ -707,6 +768,7 @@ class DynamicArrayTests: CollectionTestCase {
707768
}
708769
}
709770

771+
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
710772
func test_insert_copying_Container() {
711773
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
712774
withEvery("i", in: 0 ... layout.count) { i in
@@ -735,6 +797,7 @@ class DynamicArrayTests: CollectionTestCase {
735797
}
736798
}
737799
}
800+
#endif
738801

739802
func test_replaceSubrange_copying_Collection() {
740803
withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in
@@ -781,6 +844,7 @@ class DynamicArrayTests: CollectionTestCase {
781844
}
782845
}
783846

847+
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
784848
func test_replaceSubrange_copying_Container() {
785849
withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in
786850
withEveryRange("range", in: 0 ..< layout.count) { range in
@@ -807,5 +871,6 @@ class DynamicArrayTests: CollectionTestCase {
807871
}
808872
}
809873
}
874+
#endif
810875
}
811876
#endif

0 commit comments

Comments
 (0)