Skip to content

Commit e618f79

Browse files
committed
[test] MutableSpan.mutableBytes property
1 parent 77431f5 commit e618f79

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

test/stdlib/Span/MutableRawSpanTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,38 @@ suite.test("Basic Initializer")
3636
}
3737
}
3838

39+
private struct Padded: BitwiseCopyable {
40+
var storage: (Int64, Int8)
41+
}
42+
43+
suite.test("Initializer from MutableSpan")
44+
.require(.stdlib_6_2).code {
45+
guard #available(SwiftStdlib 6.2, *) else { return }
46+
47+
var array = [0, 1, 2].map({ Padded(storage: (Int64($0), Int8($0))) })
48+
array.withUnsafeMutableBufferPointer {
49+
var span = MutableSpan(_unsafeElements: $0)
50+
var rawSpan = MutableRawSpan(_elements: &span)
51+
52+
expectEqual(rawSpan.byteCount, $0.count * MemoryLayout<Padded>.stride)
53+
54+
rawSpan.storeBytes(of: 15, as: Int64.self)
55+
}
56+
expectEqual(array[0].storage.0, 15)
57+
58+
var slice = array.prefix(1)
59+
slice.withUnsafeMutableBufferPointer {
60+
expectEqual($0.count, 1)
61+
var span = MutableSpan(_unsafeElements: $0)
62+
var rawSpan = MutableRawSpan(_elements: &span)
63+
64+
expectEqual(rawSpan.byteCount, MemoryLayout<Padded>.size)
65+
66+
rawSpan.storeBytes(of: 3, as: Int64.self)
67+
}
68+
expectEqual(slice[0].storage.0, 3)
69+
}
70+
3971
suite.test("isEmpty property")
4072
.skip(.custom(
4173
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },

test/stdlib/Span/MutableSpanTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,28 @@ suite.test("RawSpan from MutableSpan")
122122
let span = MutableSpan(_unsafeStart: p, count: c)
123123
let bytes = span.bytes
124124
expectEqual(bytes.byteCount, count*MemoryLayout<Int>.stride)
125+
let v = bytes.unsafeLoad(
126+
fromByteOffset: MemoryLayout<Int>.stride, as: Int.self
127+
)
128+
expectEqual(v, 1)
129+
}
130+
}
131+
132+
suite.test("MutableRawSpan from MutableSpan")
133+
.require(.stdlib_6_2).code {
134+
guard #available(SwiftStdlib 6.2, *) else { return }
135+
136+
let count = 4
137+
var array = Array(0..<count)
138+
expectEqual(array[0], 0)
139+
array.withUnsafeMutableBufferPointer {
140+
let (p, c) = ($0.baseAddress!, $0.count)
141+
var span = MutableSpan(_unsafeStart: p, count: c)
142+
var bytes = span.mutableBytes
143+
expectEqual(bytes.byteCount, count*MemoryLayout<Int>.stride)
144+
bytes.storeBytes(of: 1, as: Int.self)
125145
}
146+
expectEqual(array[0], 1)
126147
}
127148

128149
suite.test("indices property")

0 commit comments

Comments
 (0)