Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
test: infinite loop test to check max_steps
Browse files Browse the repository at this point in the history
  • Loading branch information
zarboq authored and tdelabro committed Jan 20, 2023
1 parent ef15186 commit fde8bab
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/cli/commands/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::{
pub fn run_single_test(
test_name: &str,
test_path: &PathBuf,
max_steps: u64,
) -> Result<TestResult, TestCommandError> {
let (_, path_to_compiled, _) = compile_and_list_entrypoints(test_path.to_owned())?;
let file = File::open(path_to_compiled).unwrap();
Expand All @@ -21,7 +22,7 @@ pub fn run_single_test(
test_name,
&mut setup_hint_processor(),
Some(setup_hooks()),
1000000
max_steps,
)
}

Expand Down
4 changes: 3 additions & 1 deletion src/hints/expect_revert/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ fn expect_revert(
#[case] expected_success: TestStatus,
) -> Result<(), TestCommandError> {
let path = std::path::PathBuf::from(path);
let result = run_single_test("test_expect_revert", &path).expect("Should be Ok").success;
let result = run_single_test("test_expect_revert", &path, 1000000)
.expect("Should be Ok")
.success;
assert_eq!(expected_success, result);
Ok(())
}
2 changes: 1 addition & 1 deletion src/hints/mock_call/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn mock_call(
#[case] expected_success: TestStatus,
) -> Result<(), TestCommandError> {
let path = std::path::PathBuf::from(path);
let result = run_single_test("test_mock_call", &path).expect("Should be Ok").success;
let result = run_single_test("test_mock_call", &path, 1000000).expect("Should be Ok").success;
assert_eq!(expected_success, result);
Ok(())
}
2 changes: 1 addition & 1 deletion src/hints/skip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::cli::commands::test::{tests::run_single_test, TestCommandError, TestS
#[case("src/hints/skip/test_cairo_programs/skip.cairo", TestStatus::FAILURE)]
fn skip(#[case] path: &str, #[case] expected_success: TestStatus) -> Result<(), TestCommandError> {
let path = std::path::PathBuf::from(path);
let result = run_single_test("test_skip", &path).expect("Should be Ok").success;
let result = run_single_test("test_skip", &path, 1000000).expect("Should be Ok").success;
assert_eq!(expected_success, result);
Ok(())
}
3 changes: 1 addition & 2 deletions src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn pre_step_instruction(
) -> Result<(), VirtualMachineError> {
let instruction = vm.decode_current_instruction()?;

ensure_max_steps_not_reached(vm, exec_scopes, _constants)?;
ensure_max_steps_not_reached(vm, exec_scopes)?;

if instruction.opcode == Opcode::Call {
let (operands, _operands_mem_addresses, _deduced_operands) =
Expand Down Expand Up @@ -79,7 +79,6 @@ pub fn post_step_instruction(
pub fn ensure_max_steps_not_reached(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
_constants: &HashMap<String, BigInt>,
) -> Result<(), VirtualMachineError> {
if *vm.get_current_step() >= exec_scopes.get::<u64>(MAX_STEPS_VAR_NAME)? as usize {
// TODO: find a better way to express custom errors
Expand Down
27 changes: 17 additions & 10 deletions src/hooks/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use std::path::PathBuf;
use rstest::rstest;

use crate::cli::commands::{test::TestArgs, CommandExecution};
use crate::cli::commands::test::{tests::run_single_test, TestCommandError, TestStatus};

#[test]
fn test_infinite_loop() {
TestArgs {
root: PathBuf::from("./test_cairo_contracts"),
max_steps: 10000,
}
.exec()
.unwrap();
#[rstest]
#[case(
"src/hooks/test_cairo_programs/infinite_loop.cairo",
TestStatus::SUCCESS
)]
fn test_infinite_loop(
#[case] path: &str,
#[case] expected_success: TestStatus,
) -> Result<(), TestCommandError> {
let path = std::path::PathBuf::from(path);
let result = run_single_test("test_infinite_loop_failing_test", &path, 1000)
.expect("Should be Ok")
.success;
assert_eq!(expected_success, result);
Ok(())
}
2 changes: 1 addition & 1 deletion test_cairo_contracts/test_invalid_program.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ func get_balance{
}() -> (res: felt) {
let (res) = balance.read();
return (res=res);
}
}

0 comments on commit fde8bab

Please sign in to comment.