Skip to content

Commit 4058424

Browse files
committed
[test] Test new unaligned store behaviour
1 parent ea7f5a8 commit 4058424

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

test/stdlib/UnsafeRawBufferPointer.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,33 @@ UnsafeRawBufferPointerTestSuite.test("store.after")
562562
}
563563
}
564564

565+
UnsafeRawBufferPointerTestSuite.test("store.unaligned")
566+
.skip(.custom({
567+
if #available(SwiftStdlib 5.7, *) { return false }
568+
return true
569+
}, reason: "Requires Swift 5.7's stdlib"))
570+
.code {
571+
let count = MemoryLayout<UInt>.stride * 2
572+
let p1 = UnsafeMutableRawBufferPointer.allocate(
573+
byteCount: count,
574+
alignment: MemoryLayout<UInt>.alignment
575+
)
576+
defer { p1.deallocate() }
577+
p1.copyBytes(from: repeatElement(UInt8.zero, count: count))
578+
let value = UInt.max
579+
let offset = 3
580+
p1.storeBytes(of: value, toByteOffset: offset, as: UInt.self)
581+
expectEqual(p1.load(fromByteOffset: offset-1, as: UInt8.self),
582+
0)
583+
expectEqual(p1.load(fromByteOffset: offset, as: UInt8.self),
584+
.max)
585+
let storedLength = MemoryLayout<UInt>.size
586+
expectEqual(p1.load(fromByteOffset: offset-1+storedLength, as: UInt8.self),
587+
.max)
588+
expectEqual(p1.load(fromByteOffset: offset+storedLength, as: UInt8.self),
589+
0)
590+
}
591+
565592
UnsafeRawBufferPointerTestSuite.test("store.invalid")
566593
.skip(.custom({ !_isDebugAssertConfiguration() }, // require debugAssert
567594
reason: "This tests a debug precondition.."))

test/stdlib/UnsafeRawPointer.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,35 @@ UnsafeMutableRawPointerExtraTestSuite.test("load.invalid.mutable")
137137
expectUnreachable()
138138
}
139139

140+
UnsafeMutableRawPointerExtraTestSuite.test("store.unaligned")
141+
.skip(.custom({
142+
if #available(SwiftStdlib 5.7, *) { return false }
143+
return true
144+
}, reason: "Requires Swift 5.7's stdlib"))
145+
.code {
146+
let count = MemoryLayout<UInt>.stride * 2
147+
let p1 = UnsafeMutableRawPointer.allocate(
148+
byteCount: count,
149+
alignment: MemoryLayout<UInt>.alignment
150+
)
151+
defer { p1.deallocate() }
152+
Array(repeating: UInt8.zero, count: count).withUnsafeBufferPointer {
153+
p1.copyBytes(from: UnsafeRawPointer($0.baseAddress!), count: $0.count)
154+
}
155+
let value = UInt.max
156+
let offset = 3
157+
p1.storeBytes(of: value, toByteOffset: offset, as: UInt.self)
158+
expectEqual(p1.load(fromByteOffset: offset-1, as: UInt8.self),
159+
0)
160+
expectEqual(p1.load(fromByteOffset: offset, as: UInt8.self),
161+
.max)
162+
let storedLength = MemoryLayout<UInt>.size
163+
expectEqual(p1.load(fromByteOffset: offset-1+storedLength, as: UInt8.self),
164+
.max)
165+
expectEqual(p1.load(fromByteOffset: offset+storedLength, as: UInt8.self),
166+
0)
167+
}
168+
140169
UnsafeMutableRawPointerExtraTestSuite.test("store.invalid")
141170
.skip(.custom({ !_isDebugAssertConfiguration() },
142171
reason: "This tests a debug precondition.."))

0 commit comments

Comments
 (0)