-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathInlineSpanProperties.swift
117 lines (102 loc) · 3.53 KB
/
InlineSpanProperties.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===--- InlineSpanProperties.swift ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-stdlib-swift(-enable-experimental-feature AddressableTypes -enable-experimental-feature LifetimeDependence -enable-experimental-feature ValueGenerics)
// REQUIRES: executable_test
// REQUIRES: swift_feature_AddressableTypes
// REQUIRES: swift_feature_LifetimeDependence
// REQUIRES: swift_feature_ValueGenerics
import StdlibUnittest
var suite = TestSuite("Span properties backed by inline storage")
defer { runAllTests() }
suite.test("CollectionOfOne.span property")
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
.skip(.custom(
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
reason: "Requires Swift 6.2's standard library"
))
.code {
guard #available(SwiftStdlib 6.2, *) else { return }
var s = "A long string that is absolutely not smol at all."
let u = Array(s.utf8)
let c = CollectionOfOne(consume s)
s = ""
expectCrashLater()
let span = c.span
expectEqual(span.count, 1)
let v = Array(span[0].utf8)
expectEqual(u, v)
}
suite.test("CollectionOfOne.span property (simple)")
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
.skip(.custom(
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
reason: "Requires Swift 6.2's standard library"
))
.code {
guard #available(SwiftStdlib 6.2, *) else { return }
let c = CollectionOfOne(Int.random(in: 0..<100000))
expectCrashLater()
let span = c.span
expectEqual(span.count, c.indices.count)
expectEqual(span[0], c[0])
}
struct Padded: BitwiseCopyable {
var storage: (Int64, Int8)
}
suite.test("CollectionOfOne.span stride test")
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
.skip(.custom(
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
reason: "Requires Swift 6.2's standard library"
))
.code {
guard #available(SwiftStdlib 6.2, *) else { return }
let c = CollectionOfOne(Padded(storage: (-1, 1)))
expectCrashLater()
let span = c.span
let bytes = span.bytes
expectEqual(bytes.byteCount, MemoryLayout.size(ofValue: c))
}
suite.test("InlineArray.span property")
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
.skip(.custom(
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
reason: "Requires Swift 6.2's standard library"
))
.code {
guard #available(SwiftStdlib 6.2, *) else { return }
var s = InlineArray<5, Int>(repeating: 0)
s[3] = .random(in: 0..<1000)
expectCrashLater()
let span = s.span
expectEqual(span.count, s.count)
for i in s.indices {
expectEqual(span[i], s[i])
}
}
suite.test("InlineArray.span property (String)")
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
.skip(.custom(
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
reason: "Requires Swift 6.2's standard library"
))
.code {
guard #available(SwiftStdlib 6.2, *) else { return }
var s = InlineArray<5, String>(repeating: "0")
s[3] = String(Int.random(in: 0..<1000))
expectCrashLater()
let span = s.span
expectEqual(span.count, s.count)
for i in s.indices {
expectEqual(span[i], s[i])
}
}