-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWapcHostTests.swift
86 lines (75 loc) · 2.94 KB
/
WapcHostTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import XCTest
@testable import WapcHost
final class WapcHostTests: XCTestCase {
override func setUp() {
self.continueAfterFailure = false
}
func testGuestCallRust() throws {
let host = createHost("hello")
XCTAssertNotNil(host)
var wapcHost = host!
let op = "wapc:sample!Hello"
let bd: [UInt8] = Array("this is a test".utf8)
let callResult = try wapcHost.guestCall(op: op, bd: bd)
XCTAssertEqual(callResult, 1)
}
func testStressCallingRust() throws {
let host = createHost("hello")
XCTAssertNotNil(host)
var wapcHost = host!
let op = "wapc:sample!Hello"
let bd: [UInt8] = Array("this is a test".utf8)
for _ in 0...5000 {
let callResult = try wapcHost.guestCall(op: op, bd: bd)
XCTAssertEqual(callResult, 1)
}
}
func testGuestCallAssemblyScript() throws {
let host = createHost("hello_as")
XCTAssertNotNil(host)
var wapcHost = host!
let op = "hello"
let bd: [UInt8] = Array("this is a test".utf8);
let callResult = try wapcHost.guestCall(op: op, bd: bd);
XCTAssertEqual(callResult, 1)
}
func testGuestCallTinyGo() throws {
let host = createHost("hello_tinygo")
XCTAssertNotNil(host)
var wapcHost = host!
let op = "hello"
let bd: [UInt8] = Array("this is a test".utf8);
let callResult = try wapcHost.guestCall(op: op, bd: bd);
XCTAssertEqual(callResult, 1)
}
func testGuestCallZig() throws {
let host = createHost("hello_zig")
XCTAssertNotNil(host)
var wapcHost = host!
let op = "hello"
let bd: [UInt8] = Array("this is a test".utf8);
let callResult = try wapcHost.guestCall(op: op, bd: bd);
XCTAssertEqual(callResult, 1)
}
private func createHost(_ module: String) -> WapcHost? {
if let host = try? WapcHost(module: loadModule(module: module)) {
return host
} else {
return nil
}
}
private func loadModule(module: String) -> [UInt8] {
if let helloWasm = Bundle.module.path(forResource: module, ofType: "wasm") {
var bytes = [UInt8]()
if let data = NSData(contentsOfFile: helloWasm) {
var buffer = [UInt8](repeating: 0, count: data.length)
data.getBytes(&buffer, length: data.length)
bytes = buffer
}
return bytes
} else {
print("Unable to load module: ", module)
}
return []
}
}