Skip to content

Commit

Permalink
tests: current context switching
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Oct 3, 2023
1 parent 0cd8ce9 commit 8435dbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/evm/src/machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait {
/// to divide a unique Stack/Memory simulated by a dict into
/// multiple sub-structures relative to a single context.
#[inline(always)]
fn set_active_execution_ctx(ref self: Machine, ctx: ExecutionContext) {
fn set_current_context(ref self: Machine, ctx: ExecutionContext) {
self.memory.set_active_segment(ctx.id);
self.stack.set_active_segment(ctx.id);
self.current_context = BoxTrait::new(ctx);
Expand Down
25 changes: 24 additions & 1 deletion crates/evm/src/tests/test_machine.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use evm::context::CallContextTrait;
use evm::machine::{Machine, MachineCurrentContextTrait};
use evm::tests::test_utils::{
evm_address, setup_machine_with_bytecode, setup_machine, starknet_address
evm_address, setup_machine_with_bytecode, setup_machine, starknet_address,
setup_execution_context
};


Expand All @@ -16,6 +17,28 @@ fn test_machine_default() {
assert(!machine.stopped(), 'ctx should not be stopped');
}

#[test]
#[available_gas(20000000)]
fn test_set_current_context() {
let mut machine: Machine = Default::default();

let first_ctx = machine.current_context.unbox();
assert(first_ctx.id == 0, 'wrong first id');
// We need to re-box the context into the machine, otherwise we have a "Variable Moved" error.
machine.current_context = BoxTrait::new(first_ctx);
assert(machine.stack.active_segment == 0, 'wrong initial stack segment');
assert(machine.memory.active_segment == 0, 'wrong initial memory segment');

// Create another context with id=1
let mut second_ctx = setup_execution_context();
second_ctx.id = 1;

machine.set_current_context(second_ctx);
assert(machine.stack.active_segment == 1, 'wrong updated stack segment');
assert(machine.memory.active_segment == 1, 'wrong updated stack segment');
assert(machine.current_context.unbox().id == 1, 'wrong updated id');
}

#[test]
#[available_gas(20000000)]
fn test_set_pc() {
Expand Down

0 comments on commit 8435dbf

Please sign in to comment.