Skip to content

Commit 4b7c678

Browse files
committed
[Volatile] Add executable tests for VolatileMappedRegister
1 parent 1c66aea commit 4b7c678

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

test/Volatile/volatile-exec.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-run-simple-swift(-parse-as-library -enable-experimental-feature Volatile) | %FileCheck %s
2+
3+
// REQUIRES: executable_test
4+
5+
import _Volatile
6+
7+
@main
8+
struct Main {
9+
static func main() {
10+
var byte: UInt8 = 42
11+
withUnsafePointer(to: &byte) { p in
12+
let volatilePointer = VolatileMappedRegister<UInt8>(unsafeBitPattern: UInt(bitPattern: p))
13+
print("byte = \(volatilePointer.load())")
14+
// CHECK: byte = 42
15+
volatilePointer.store(77)
16+
}
17+
print("byte = \(byte)")
18+
// CHECK: byte = 77
19+
20+
var heapPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16) // uninitialized content
21+
for i in 0 ..< 16 {
22+
let pointerWithOffset = heapPointer.advanced(by: i)
23+
let volatilePointer = VolatileMappedRegister<UInt8>(unsafeBitPattern: UInt(bitPattern: pointerWithOffset))
24+
volatilePointer.store(UInt8(0x61 + i))
25+
}
26+
heapPointer[15] = 0
27+
print(String(cString: heapPointer))
28+
// CHECK: abcdefghijklmno
29+
}
30+
}

test/embedded/volatile-exec.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-run-simple-swift(-parse-as-library -enable-experimental-feature Embedded -wmo -runtime-compatibility-version none) | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
// REQUIRES: executable_test
5+
// REQUIRES: optimized_stdlib
6+
// REQUIRES: OS=macosx || OS=linux-gnu
7+
8+
import _Volatile
9+
10+
@_silgen_name("putchar")
11+
@discardableResult
12+
func putchar(_: CInt) -> CInt
13+
14+
@main
15+
struct Main {
16+
static func main() {
17+
var byte: UInt8 = 42
18+
withUnsafePointer(to: &byte) { p in
19+
let volatilePointer = VolatileMappedRegister<UInt8>(unsafeBitPattern: UInt(bitPattern: p))
20+
print("byte = ", terminator: "")
21+
print(volatilePointer.load())
22+
// CHECK: byte = 42
23+
volatilePointer.store(77)
24+
}
25+
print("byte = ", terminator: "")
26+
print(byte)
27+
// CHECK: byte = 77
28+
29+
var heapPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16) // uninitialized content
30+
for i in 0 ..< 16 {
31+
let pointerWithOffset = heapPointer.advanced(by: i)
32+
let volatilePointer = VolatileMappedRegister<UInt8>(unsafeBitPattern: UInt(bitPattern: pointerWithOffset))
33+
volatilePointer.store(UInt8(0x61 + i))
34+
}
35+
for i in 0 ..< 16 { putchar(CInt(heapPointer[i])) }
36+
putchar(CInt(("\n" as Unicode.Scalar).value))
37+
// CHECK: abcdefghijklmnop
38+
}
39+
}

0 commit comments

Comments
 (0)