Skip to content

Commit a9a740d

Browse files
committed
[test] modify to be compatible with minimal stdlib
- validate that unaligned loads work for types with large alignments - in the minimal stdlib, only `Builtin` types can be used for this
1 parent d1220fd commit a9a740d

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

validation-test/stdlib/UnsafeRawPointerInternal.swift

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,54 @@
22
// RUN: %target-codesign %t.out
33
// RUN: %target-run %t.out
44
// REQUIRES: executable_test
5-
// UNSUPPORTED: freestanding
65

76
import Swift
87
import StdlibUnittest
98

10-
var UnsafeRawPointerTestSuite =
11-
TestSuite("UnsafeRawPointerTestSuite")
9+
var UnsafeRawPointerTestSuite = TestSuite("UnsafeRawPointerTestSuite")
1210

13-
UnsafeRawPointerTestSuite.test("load.unaligned.SIMD")
11+
UnsafeRawPointerTestSuite.test("load.unaligned.largeAlignment")
1412
.skip(.custom({
15-
if #available(SwiftStdlib 5.7, *) { return false }
16-
return true
17-
}, reason: "Requires Swift 5.7's stdlib"))
13+
if #available(SwiftStdlib 5.7, *) { return false } else { return true }
14+
}, reason: "Requires standard library from Swift 5.7"))
1815
.code {
1916
guard #available(SwiftStdlib 5.7, *) else { return }
20-
var bytes: [UInt8] = Array(0..<64)
2117
var offset = 3
22-
let vector16 = bytes.withUnsafeBytes { buffer -> SIMD16<UInt8> in
23-
let aligned = buffer.baseAddress!.alignedUp(for: SIMD16<UInt8>.self)
18+
let int128 = withUnsafeTemporaryAllocation(of: UInt8.self, capacity: 64) {
19+
temporary -> Builtin.Int128 in
20+
_ = temporary.initialize(from: 0..<64)
21+
let buffer = UnsafeRawBufferPointer(temporary)
22+
let aligned = buffer.baseAddress!.alignedUp(for: Builtin.Int128.self)
2423
offset += buffer.baseAddress!.distance(to: aligned)
25-
return buffer.loadUnaligned(fromByteOffset: offset, as: SIMD16<UInt8>.self)
24+
return buffer.loadUnaligned(fromByteOffset: offset, as: Builtin.Int128.self)
2625
}
27-
expectEqual(Int(vector16[0]), offset)
28-
var lastIndex = vector16.indices.last!
29-
expectEqual(Int(vector16[lastIndex]), offset+lastIndex)
26+
withUnsafeBytes(of: int128) {
27+
expectEqual(Int($0[0]), offset)
28+
let lastIndex = $0.indices.last!
29+
expectEqual(Int($0[lastIndex]), offset+lastIndex)
30+
}
31+
}
3032

31-
offset = 11
32-
let vector32 = bytes.withUnsafeMutableBytes { buffer -> SIMD32<UInt8> in
33-
let aligned = buffer.baseAddress!.alignedUp(for: SIMD32<UInt8>.self)
33+
UnsafeRawPointerTestSuite.test("load.unaligned.largeAlignment.mutablePointer")
34+
.skip(.custom({
35+
if #available(SwiftStdlib 5.7, *) { return false } else { return true }
36+
}, reason: "Requires standard library from Swift 5.7"))
37+
.code {
38+
guard #available(SwiftStdlib 5.7, *) else { return }
39+
var offset = 11
40+
let int128 = withUnsafeTemporaryAllocation(of: UInt8.self, capacity: 64) {
41+
temporary -> Builtin.Int128 in
42+
let buffer = UnsafeMutableRawBufferPointer(temporary)
43+
buffer.copyBytes(from: 0..<64)
44+
let aligned = buffer.baseAddress!.alignedUp(for: Builtin.Int128.self)
3445
offset += buffer.baseAddress!.distance(to: aligned)
35-
return buffer.loadUnaligned(fromByteOffset: offset, as: SIMD32<UInt8>.self)
46+
return buffer.loadUnaligned(fromByteOffset: offset, as: Builtin.Int128.self)
47+
}
48+
withUnsafeBytes(of: int128) {
49+
expectEqual(Int($0[0]), offset)
50+
let lastIndex = $0.indices.last!
51+
expectEqual(Int($0[lastIndex]), offset+lastIndex)
3652
}
37-
expectEqual(Int(vector32[0]), offset)
38-
lastIndex = vector32.indices.last!
39-
expectEqual(Int(vector32[lastIndex]), offset+lastIndex)
4053
}
4154

4255
runAllTests()

0 commit comments

Comments
 (0)