Skip to content

Commit 6b73161

Browse files
committed
[test] Array-family span properties
1 parent 4d858d5 commit 6b73161

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===--- ArraySpanProperties.swift ----------------------------------------===//
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("Array-Backed Span Properties")
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)