Skip to content

Commit

Permalink
add sumToN test
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii committed Jan 24, 2025
1 parent 5760c4a commit a0ae7c6
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions PolkaVM/Tests/PolkaVMTests/InvokePVMTest.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation
import Testing
import TracingUtils
import Utils

@testable import PolkaVM
Expand All @@ -9,17 +8,17 @@ import Utils
let empty = Data([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0])
let fibonacci = Data([
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 51, 128, 119, 0,
51, 8, 1, 51, 9, 1, 40, 3, 0, 149, 119, 255, 81, 7, 12, 100, 138, 200, 152,
8, 100, 169, 40, 243, 100, 135, 51, 8, 51, 9, 61, 7, 0, 0, 2, 0, 51, 8, 4,
51, 7, 0, 0, 2, 0, 1, 50, 0, 73, 154, 148, 170, 130, 4, 3,
51, 8, 1, 51, 9, 1, 40, 3, 0, 149, 119, 255, 81, 7, 12, 100, 138, 200,
152, 8, 100, 169, 40, 243, 100, 135, 51, 8, 51, 9, 61, 7, 0, 0, 2, 0,
51, 8, 4, 51, 7, 0, 0, 2, 0, 1, 50, 0, 73, 154, 148, 170, 130, 4, 3,
])
let sumToN = Data([
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 46, 0, 0, 0, 0, 0, 38, 128, 119, 0,
51, 8, 0, 100, 121, 40, 3, 0, 200, 137, 8, 149, 153, 255, 86, 9, 250,
61, 8, 0, 0, 2, 0, 51, 8, 4, 51, 7, 0, 0, 2, 0, 1, 50, 0, 73, 77, 18,
36, 24,
])
// let sumThree

struct InvokePVMTests {
init() {
// setupTestLogger()
}

@Test func testEmptyProgram() async throws {
let config = DefaultPvmConfig()
let (exitReason, gas, output) = await invokePVM(
Expand Down Expand Up @@ -62,7 +61,33 @@ struct InvokePVMTests {
}
}

@Test func testSumThree() async throws {}
@Test(arguments: [
(1, 1, 999_988),
(4, 10, 999_979),
(5, 15, 999_976),
])
func testSumToN(testCase: (input: UInt8, output: UInt8, gas: UInt64)) async throws {
let config = DefaultPvmConfig()
let (exitReason, gas, output) = await invokePVM(
config: config,
blob: sumToN,
pc: 0,
gas: Gas(1_000_000),
argumentData: Data([testCase.input]),
ctx: nil
)

let value = output?.withUnsafeBytes { $0.loadUnaligned(as: UInt32.self) } ?? 0

switch exitReason {
case .halt:
#expect(value == testCase.output)
#expect(gas == Gas(testCase.gas))
default:
Issue.record("Expected halt, got \(exitReason)")

Check warning on line 87 in PolkaVM/Tests/PolkaVMTests/InvokePVMTest.swift

View check run for this annotation

Codecov / codecov/patch

PolkaVM/Tests/PolkaVMTests/InvokePVMTest.swift#L87

Added line #L87 was not covered by tests
}
}

// TODO: add tests with a fake InvocationContext
@Test func testInvocationContext() async throws {}
}

0 comments on commit a0ae7c6

Please sign in to comment.