|
1 | 1 | import XCTest
|
2 | 2 |
|
| 3 | +import WasmKit |
| 4 | +import WasmTypes |
3 | 5 | @testable import WASI
|
4 | 6 |
|
5 | 7 | final class WASITests: XCTestCase {
|
@@ -134,4 +136,37 @@ final class WASITests: XCTestCase {
|
134 | 136 | XCTAssertEqual(error, .ELOOP)
|
135 | 137 | }
|
136 | 138 | }
|
| 139 | + |
| 140 | + func testWASIAbi() throws { |
| 141 | + let engine = Engine() |
| 142 | + let store = Store(engine: engine) |
| 143 | + let memory = try Memory(store: store, type: .init(min: 1)) |
| 144 | + |
| 145 | + // Test union size and alignment end-to-end |
| 146 | + let start = UnsafeGuestRawPointer(memorySpace: memory, offset: 0) |
| 147 | + var pointer = start |
| 148 | + let read = WASIAbi.Subscription.Union.fdRead(.init(0)) |
| 149 | + let write = WASIAbi.Subscription.Union.fdWrite(.init(0)) |
| 150 | + let clock = WASIAbi.Subscription.Union.clock(.init(id: .REALTIME, timeout: 42, precision: 0, flags: [])) |
| 151 | + let event = WASIAbi.Event(userData: 3, error: .EIO, eventType: .fdRead, fdReadWrite: .init(nBytes: 37, flags: [.hangup])) |
| 152 | + WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 1, union: read)) |
| 153 | + XCTAssertEqual(pointer.offset, 48) |
| 154 | + WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 2, union: write)) |
| 155 | + XCTAssertEqual(pointer.offset, 48 * 2) |
| 156 | + WASIAbi.Subscription.writeToGuest(at: &pointer, value: .init(userData: 3, union: clock)) |
| 157 | + XCTAssertEqual(pointer.offset, 48 * 3) |
| 158 | + WASIAbi.Event.writeToGuest(at: &pointer, value: event) |
| 159 | + XCTAssertEqual(pointer.offset, 48 * 3 + 32) |
| 160 | + |
| 161 | + // Test that reading back yields same result |
| 162 | + pointer = start |
| 163 | + XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 1, union: read)) |
| 164 | + XCTAssertEqual(pointer.offset, 48) |
| 165 | + XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 2, union: write)) |
| 166 | + XCTAssertEqual(pointer.offset, 48 * 2) |
| 167 | + XCTAssertEqual(WASIAbi.Subscription.readFromGuest(&pointer), .init(userData: 3, union: clock)) |
| 168 | + XCTAssertEqual(pointer.offset, 48 * 3) |
| 169 | + XCTAssertEqual(WASIAbi.Event.readFromGuest(&pointer), event) |
| 170 | + XCTAssertEqual(pointer.offset, 48 * 3 + 32) |
| 171 | + } |
137 | 172 | }
|
0 commit comments