Skip to content

Commit a5e04e0

Browse files
committed
[test] span properties for Array family
1 parent 7bbb834 commit a5e04e0

File tree

2 files changed

+113
-10
lines changed

2 files changed

+113
-10
lines changed

Diff for: test/stdlib/BridgedArrayNonContiguous.swift

+39-10
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,48 @@ import Foundation
2323
var suite = TestSuite("EagerLazyBridgingTests")
2424
defer { runAllTests() }
2525

26-
var x: NSObject? = nil
27-
2826
suite.test("Bridged NSArray without direct memory sharing") {
2927

30-
var arr = Array(repeating: NSObject(), count: 100) as [AnyObject]
31-
let nsArray:NSArray = NSArray(objects: &arr, count: 100)
32-
for _ in 0 ..< 5 {
33-
let tmp = nsArray as! [NSObject]
34-
x = tmp.withContiguousStorageIfAvailable {
35-
$0[0]
36-
}
28+
var arr = (0..<100).map({ _ in NSObject() as AnyObject})
29+
let identifiers = arr.map(ObjectIdentifier.init)
30+
31+
let nsArray = arr.withUnsafeBufferPointer {
32+
NSArray(objects: $0.baseAddress, count: $0.count)
33+
}
34+
arr = []
35+
36+
for i in 0..<nsArray.count {
37+
let bridged = nsArray as! [NSObject]
38+
let x: NSObject? = bridged.withContiguousStorageIfAvailable { $0[i] }
39+
3740
expectNotNil(x)
41+
if let x {
42+
expectEqual(identifiers[i], ObjectIdentifier(x))
43+
}
44+
}
45+
}
46+
47+
suite.test("Bridged NSArray as Span")
48+
.skip(.custom(
49+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
50+
reason: "Requires Swift 6.2's standard library"
51+
))
52+
.code {
53+
guard #available(SwiftStdlib 6.2, *) else { return }
54+
55+
var arr = (0..<100).map({ _ in NSObject() as AnyObject})
56+
let identifiers = arr.map(ObjectIdentifier.init)
57+
58+
let nsArray = arr.withUnsafeBufferPointer {
59+
NSArray(objects: $0.baseAddress, count: $0.count)
3860
}
61+
arr = []
62+
63+
for i in 0..<nsArray.count {
64+
let bridged = nsArray as! [NSObject]
65+
let span = bridged.span
3966

40-
x = nil
67+
let x: NSObject = span[i]
68+
expectEqual(identifiers[i], ObjectIdentifier(x))
69+
}
4170
}

Diff for: test/stdlib/Span/ArrayStorage.swift

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===--- ArrayStorage.swift ---- test span properties for Array types -----===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// RUN: %target-run-stdlib-swift
14+
15+
// REQUIRES: executable_test
16+
17+
import StdlibUnittest
18+
19+
var suite = TestSuite("ArrayStorageProperty")
20+
defer { runAllTests() }
21+
22+
suite.test("Array.span property")
23+
.skip(.custom(
24+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
25+
reason: "Requires Swift 6.2's standard library"
26+
))
27+
.code {
28+
guard #available(SwiftStdlib 6.2, *) else { return }
29+
30+
let capacity = 4
31+
let a = Array(0..<capacity)
32+
let span = a.span
33+
expectEqual(span.count, capacity)
34+
expectEqual(span[0], a[0])
35+
}
36+
37+
suite.test("ContiguousArray.span property")
38+
.skip(.custom(
39+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
40+
reason: "Requires Swift 6.2's standard library"
41+
))
42+
.code {
43+
guard #available(SwiftStdlib 6.2, *) else { return }
44+
45+
let capacity = 4
46+
let a = ContiguousArray(0..<capacity)
47+
let span = a.span
48+
expectEqual(span.count, capacity)
49+
expectEqual(span[0], a[0])
50+
}
51+
52+
suite.test("ArraySlice.span property")
53+
.skip(.custom(
54+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
55+
reason: "Requires Swift 6.2's standard library"
56+
))
57+
.code {
58+
guard #available(SwiftStdlib 6.2, *) else { return }
59+
60+
let capacity = 4
61+
let a = Array(0..<capacity)
62+
let span1 = a.span
63+
expectEqual(span1.count, capacity)
64+
expectEqual(span1[0], a[0])
65+
66+
let s = a[...]
67+
let span2 = s.span
68+
expectEqual(span2.count, capacity)
69+
expectEqual(span2[0], a[0])
70+
71+
let i1 = span1.withUnsafeBufferPointer { Int(bitPattern: $0.baseAddress) }
72+
let i2 = span1.withUnsafeBufferPointer { Int(bitPattern: $0.baseAddress) }
73+
expectEqual(i1, i2)
74+
}

0 commit comments

Comments
 (0)