|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +import Utils |
| 4 | + |
| 5 | +@testable import PolkaVM |
| 6 | + |
| 7 | +// standard programs |
| 8 | +let empty = Data([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0]) |
| 9 | +let fibonacci = Data([ |
| 10 | + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 51, 128, 119, 0, |
| 11 | + 51, 8, 1, 51, 9, 1, 40, 3, 0, 149, 119, 255, 81, 7, 12, 100, 138, 200, |
| 12 | + 152, 8, 100, 169, 40, 243, 100, 135, 51, 8, 51, 9, 61, 7, 0, 0, 2, 0, |
| 13 | + 51, 8, 4, 51, 7, 0, 0, 2, 0, 1, 50, 0, 73, 154, 148, 170, 130, 4, 3, |
| 14 | +]) |
| 15 | +let sumToN = Data([ |
| 16 | + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 46, 0, 0, 0, 0, 0, 38, 128, 119, 0, |
| 17 | + 51, 8, 0, 100, 121, 40, 3, 0, 200, 137, 8, 149, 153, 255, 86, 9, 250, |
| 18 | + 61, 8, 0, 0, 2, 0, 51, 8, 4, 51, 7, 0, 0, 2, 0, 1, 50, 0, 73, 77, 18, |
| 19 | + 36, 24, |
| 20 | +]) |
| 21 | +let sumToNWithHostCall = Data([ |
| 22 | + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 48, 0, 0, 0, 0, 0, 40, 128, 119, 0, |
| 23 | + 51, 8, 0, 100, 121, 40, 3, 0, 200, 137, 8, 149, 153, 255, 86, 9, 250, |
| 24 | + 61, 8, 0, 0, 2, 0, 51, 8, 4, 51, 7, 0, 0, 2, 0, 10, 1, 1, 50, 0, 73, |
| 25 | + 77, 18, 36, 104, |
| 26 | +]) |
| 27 | + |
| 28 | +struct InvokePVMTests { |
| 29 | + @Test func testEmptyProgram() async throws { |
| 30 | + let config = DefaultPvmConfig() |
| 31 | + let (exitReason, gas, output) = await invokePVM( |
| 32 | + config: config, |
| 33 | + blob: empty, |
| 34 | + pc: 0, |
| 35 | + gas: Gas(1_000_000), |
| 36 | + argumentData: Data(), |
| 37 | + ctx: nil |
| 38 | + ) |
| 39 | + #expect(exitReason == .panic(.trap)) |
| 40 | + #expect(gas == Gas(0)) |
| 41 | + #expect(output == nil) |
| 42 | + } |
| 43 | + |
| 44 | + @Test(arguments: [ |
| 45 | + (2, 2, 999_980), |
| 46 | + (8, 34, 999_944), |
| 47 | + (9, 55, 999_938), |
| 48 | + ]) |
| 49 | + func testFibonacci(testCase: (input: UInt8, output: UInt8, gas: UInt64)) async throws { |
| 50 | + let config = DefaultPvmConfig() |
| 51 | + let (exitReason, gas, output) = await invokePVM( |
| 52 | + config: config, |
| 53 | + blob: fibonacci, |
| 54 | + pc: 0, |
| 55 | + gas: Gas(1_000_000), |
| 56 | + argumentData: Data([testCase.input]), |
| 57 | + ctx: nil |
| 58 | + ) |
| 59 | + |
| 60 | + let value = output?.withUnsafeBytes { $0.loadUnaligned(as: UInt32.self) } ?? 0 |
| 61 | + |
| 62 | + switch exitReason { |
| 63 | + case .halt: |
| 64 | + #expect(value == testCase.output) |
| 65 | + #expect(gas == Gas(testCase.gas)) |
| 66 | + default: |
| 67 | + Issue.record("Expected halt, got \(exitReason)") |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test(arguments: [ |
| 72 | + (1, 1, 999_988), |
| 73 | + (4, 10, 999_979), |
| 74 | + (5, 15, 999_976), |
| 75 | + ]) |
| 76 | + func testSumToN(testCase: (input: UInt8, output: UInt8, gas: UInt64)) async throws { |
| 77 | + let config = DefaultPvmConfig() |
| 78 | + let (exitReason, gas, output) = await invokePVM( |
| 79 | + config: config, |
| 80 | + blob: sumToN, |
| 81 | + pc: 0, |
| 82 | + gas: Gas(1_000_000), |
| 83 | + argumentData: Data([testCase.input]), |
| 84 | + ctx: nil |
| 85 | + ) |
| 86 | + |
| 87 | + let value = output?.withUnsafeBytes { $0.loadUnaligned(as: UInt32.self) } ?? 0 |
| 88 | + |
| 89 | + switch exitReason { |
| 90 | + case .halt: |
| 91 | + #expect(value == testCase.output) |
| 92 | + #expect(gas == Gas(testCase.gas)) |
| 93 | + default: |
| 94 | + Issue.record("Expected halt, got \(exitReason)") |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test func testInvocationContext() async throws { |
| 99 | + let config = DefaultPvmConfig() |
| 100 | + |
| 101 | + struct TestInvocationContext: InvocationContext { |
| 102 | + public typealias ContextType = Void |
| 103 | + |
| 104 | + public var context: ContextType = () |
| 105 | + |
| 106 | + public func dispatch(index _: UInt32, state: VMState) async -> ExecOutcome { |
| 107 | + // perform output * 2 |
| 108 | + do { |
| 109 | + let (ouputAddr, len): (UInt32, UInt32) = state.readRegister(Registers.Index(raw: 7), Registers.Index(raw: 8)) |
| 110 | + let output = try state.readMemory(address: ouputAddr, length: Int(len)) |
| 111 | + let value = output.withUnsafeBytes { $0.load(as: UInt32.self) } |
| 112 | + let newOutput = withUnsafeBytes(of: value << 1) { Data($0) } |
| 113 | + try state.writeMemory(address: ouputAddr, values: newOutput) |
| 114 | + return .continued |
| 115 | + } catch { |
| 116 | + return .exit(.panic(.trap)) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + let (exitReason, _, output) = await invokePVM( |
| 122 | + config: config, |
| 123 | + blob: sumToNWithHostCall, |
| 124 | + pc: 0, |
| 125 | + gas: Gas(1_000_000), |
| 126 | + argumentData: Data([5]), |
| 127 | + ctx: TestInvocationContext() |
| 128 | + ) |
| 129 | + |
| 130 | + let value = output?.withUnsafeBytes { $0.loadUnaligned(as: UInt32.self) } ?? 0 |
| 131 | + |
| 132 | + switch exitReason { |
| 133 | + case .halt: |
| 134 | + #expect(value == 30) |
| 135 | + default: |
| 136 | + Issue.record("Expected halt, got \(exitReason)") |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments