Skip to content

Commit

Permalink
test: add test_instructions (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieuauger committed Dec 4, 2022
1 parent 4709534 commit b05f2dc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// Starkware dependencies
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.uint256 import Uint256

Expand Down Expand Up @@ -314,3 +315,19 @@ func test__exec_signextend__should_signextend_0_and_1{
assert index0 = Uint256(2, 0);
return ();
}

@external
func test__exec_stop{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}() {
alloc_locals;
let (bytecode) = alloc();
let ctx: model.ExecutionContext* = TestHelpers.init_context(0, bytecode);
assert ctx.stopped = FALSE;

let stopped_ctx = StopAndArithmeticOperations.exec_stop(ctx);

assert stopped_ctx.stopped = TRUE;

return ();
}
38 changes: 38 additions & 0 deletions tests/cairo_files/test_instructions.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT

%lang starknet

// Starkware dependencies
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin

// Local dependencies
from utils.utils import Helpers
from kakarot.model import model
from kakarot.instructions import EVMInstructions
from tests.utils.utils import TestHelpers

@external
func test__unknown_opcode{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}() {
alloc_locals;
let (bytecode) = alloc();
let ctx: model.ExecutionContext* = TestHelpers.init_context(0, bytecode);
EVMInstructions.unknown_opcode(ctx);

return ();
}

@external
func test__not_implemented_opcode{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}() {
alloc_locals;
let (bytecode) = alloc();
let ctx: model.ExecutionContext* = TestHelpers.init_context(0, bytecode);
EVMInstructions.not_implemented_opcode(ctx);

return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ async def test__exec_signextend__should_signextend_0_and_1(
self, arithmetic_operations
):
await arithmetic_operations.test__exec_signextend__should_signextend_0_and_1().call()

async def test__exec_stop(self, arithmetic_operations):
await arithmetic_operations.test__exec_stop().call()
29 changes: 29 additions & 0 deletions tests/units/test_instructions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import re

import pytest
import pytest_asyncio
from starkware.starknet.testing.starknet import Starknet


@pytest_asyncio.fixture(scope="module")
async def instructions(starknet: Starknet):
return await starknet.deploy(
source="./tests/cairo_files/test_instructions.cairo",
cairo_path=["src"],
disable_hint_validation=False,
)


@pytest.mark.asyncio
class TestInstructions:
async def test__unknown_opcode(self, instructions):
with pytest.raises(Exception) as e:
await instructions.test__unknown_opcode().call()
message = re.search(r"Error message: (.*)", e.value.message)[1] # type: ignore
assert message == "Kakarot: UnknownOpcode"

async def test__not_implemented_opcode(self, instructions):
with pytest.raises(Exception) as e:
await instructions.test__not_implemented_opcode().call()
message = re.search(r"Error message: (.*)", e.value.message)[1] # type: ignore
assert message == "Kakarot: NotImplementedOpcode"

0 comments on commit b05f2dc

Please sign in to comment.