Skip to content

Commit ea7f5a8

Browse files
committed
[test] new stdlib load/store behaviours
1 parent e923ab5 commit ea7f5a8

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

test/stdlib/UnsafeRawBufferPointer.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,47 @@ UnsafeRawBufferPointerTestSuite.test("load.after")
498498
}
499499
}
500500

501+
UnsafeRawBufferPointerTestSuite.test("load.aligned") {
502+
var data: [UInt8] = [0, 0, 0, 0, .max, .max, .max, .max]
503+
data.withUnsafeBytes {
504+
let x = $0.load(fromByteOffset: 4, as: UInt32.self)
505+
expectEqual(x, .max)
506+
}
507+
data.withUnsafeMutableBytes {
508+
let x = $0.load(fromByteOffset: 0, as: UInt32.self)
509+
expectEqual(x, 0)
510+
}
511+
}
512+
513+
UnsafeRawBufferPointerTestSuite.test("load.invalid")
514+
.skip(.custom({ !_isDebugAssertConfiguration() }, // require debugAssert
515+
reason: "This tests a debug precondition.."))
516+
.code {
517+
let data: [UInt8] = [0, 0, 0, .max, .max, .max, .max, 0]
518+
let i = data.firstIndex(of: .max)!
519+
expectCrashLater()
520+
_ = data.withUnsafeBytes {
521+
$0.load(fromByteOffset: i, as: UInt32.self)
522+
}
523+
}
524+
525+
UnsafeRawBufferPointerTestSuite.test("load.unaligned")
526+
.skip(.custom({ // require SwiftStdlib 5.7
527+
if #available(SwiftStdlib 5.7, *) { return false } else { return true }
528+
}, reason: "Requires stdlib from Swift 5.7"))
529+
.code {
530+
var data: [UInt8] = [0, 0, 0, .max, .max, .max, .max, 0]
531+
let i = data.firstIndex(of: .max)!
532+
data.withUnsafeBytes {
533+
let x = $0.loadUnaligned(fromByteOffset: i, as: UInt32.self)
534+
expectEqual(x, .max)
535+
}
536+
data.withUnsafeMutableBytes {
537+
let x = $0.loadUnaligned(fromByteOffset: i-1, as: UInt32.self)
538+
expectEqual(UInt32(littleEndian: x), 0xffffff00)
539+
}
540+
}
541+
501542
UnsafeRawBufferPointerTestSuite.test("store.before")
502543
.skip(.custom(
503544
{ !_isDebugAssertConfiguration() },
@@ -521,6 +562,33 @@ UnsafeRawBufferPointerTestSuite.test("store.after")
521562
}
522563
}
523564

565+
UnsafeRawBufferPointerTestSuite.test("store.invalid")
566+
.skip(.custom({ !_isDebugAssertConfiguration() }, // require debugAssert
567+
reason: "This tests a debug precondition.."))
568+
.skip(.custom({ // require SwiftStdlib 5.7
569+
if #available(SwiftStdlib 5.7, *) { return false } else { return true }
570+
}, reason: "Requires stdlib from Swift 5.7"))
571+
.code {
572+
let t = "Text that is longer than fits in a small String."
573+
let p1 = UnsafeMutableRawPointer.allocate(
574+
byteCount: MemoryLayout<String>.size,
575+
alignment: MemoryLayout<String>.alignment
576+
)
577+
defer { p1.deallocate() }
578+
expectCrashLater()
579+
p1.storeBytes(of: t, as: String.self)
580+
expectUnreachable()
581+
}
582+
583+
UnsafeRawBufferPointerTestSuite.test("store.valid") {
584+
let value32 = UInt32.max
585+
var value64 = Int64.zero
586+
withUnsafeMutableBytes(of: &value64) {
587+
$0.storeBytes(of: value32, toByteOffset: MemoryLayout<UInt32>.stride, as: UInt32.self)
588+
}
589+
expectEqual(value64, 0xffffffff << 32)
590+
}
591+
524592
UnsafeRawBufferPointerTestSuite.test("copy.bytes.overflow")
525593
.skip(.custom(
526594
{ !_isDebugAssertConfiguration() },

test/stdlib/UnsafeRawPointer.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,69 @@ UnsafeMutableRawPointerExtraTestSuite.test("load/store") {
9494
expectEqual(6, p1.load(fromByteOffset: 2 * MemoryLayout<Int>.stride, as: Int.self))
9595
}
9696

97+
UnsafeMutableRawPointerExtraTestSuite.test("load.unaligned")
98+
.skip(.custom({
99+
if #available(SwiftStdlib 5.7, *) { return false }
100+
return true
101+
}, reason: "Requires Swift 5.7's stdlib"))
102+
.code {
103+
var data: [UInt8] = [0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0x0]
104+
var result: UInt32
105+
result = data.withUnsafeBytes {
106+
$0.loadUnaligned(fromByteOffset: 3, as: UInt32.self)
107+
}
108+
expectEqual(result, 0xffff_ffff)
109+
result = data.withUnsafeMutableBytes {
110+
$0[0] = 0
111+
return $0.loadUnaligned(fromByteOffset: 1, as: UInt32.self)
112+
}
113+
expectEqual(result, 0xffff_0000)
114+
}
115+
116+
UnsafeMutableRawPointerExtraTestSuite.test("load.invalid")
117+
.skip(.custom({ !_isDebugAssertConfiguration() },
118+
reason: "This tests a debug precondition.."))
119+
.code {
120+
let data: [UInt8] = [0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0x0]
121+
expectCrashLater()
122+
_ = data.withUnsafeBytes {
123+
$0.load(fromByteOffset: 3, as: UInt32.self)
124+
}
125+
expectUnreachable()
126+
}
127+
128+
UnsafeMutableRawPointerExtraTestSuite.test("load.invalid.mutable")
129+
.skip(.custom({ !_isDebugAssertConfiguration() },
130+
reason: "This tests a debug precondition.."))
131+
.code {
132+
var data: [UInt8] = [0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0x0]
133+
expectCrashLater()
134+
_ = data.withUnsafeMutableBytes {
135+
$0.load(fromByteOffset: 1, as: UInt32.self)
136+
}
137+
expectUnreachable()
138+
}
139+
140+
UnsafeMutableRawPointerExtraTestSuite.test("store.invalid")
141+
.skip(.custom({ !_isDebugAssertConfiguration() },
142+
reason: "This tests a debug precondition.."))
143+
.skip(.custom({
144+
if #available(SwiftStdlib 5.7, *) { return false }
145+
return true
146+
}, reason: "Requires Swift 5.7's stdlib"))
147+
.code {
148+
Missile.missilesLaunched = 0
149+
let m = Missile(0)
150+
let p1 = UnsafeMutableRawPointer.allocate(
151+
byteCount: MemoryLayout<Missile>.size,
152+
alignment: MemoryLayout<Missile>.alignment
153+
)
154+
defer { p1.deallocate() }
155+
expectCrashLater()
156+
p1.storeBytes(of: m, as: Missile.self)
157+
expectUnreachable()
158+
}
159+
97160
UnsafeMutableRawPointerExtraTestSuite.test("copyMemory") {
98161
let sizeInBytes = 4 * MemoryLayout<Int>.stride
99162
let rawPtr = UnsafeMutableRawPointer.allocate(

0 commit comments

Comments
 (0)