From 8e6c9531b045ebf3783a433828caa5c3339e293b Mon Sep 17 00:00:00 2001 From: TurcFort07 <37303126+hurrikaanig@users.noreply.github.com> Date: Mon, 13 Feb 2023 20:29:54 +0100 Subject: [PATCH] feat [breaking changes]: Extensible VirtualMachineError and removed PartialEq trait (#783) * feat: Extensible VirtualMachinError and removed PartialEq trait * Fix matches in tests * Fix match in find_element_hint test * Merge with main; remove assert_eq * Fix math_utils tests * Fix relocatable tests * Remove uneeded test * Fix calculate_isqrt_b test --------- Co-authored-by: Juanma --- Cargo.lock | 7 + Cargo.toml | 1 + .../builtin_hint_processor/blake2s_utils.rs | 47 +- .../builtin_hint_processor_definition.rs | 57 +-- .../cairo_keccak/keccak_hints.rs | 13 +- .../builtin_hint_processor/dict_hint_utils.rs | 105 +++-- .../builtin_hint_processor/dict_manager.rs | 13 +- .../find_element_hint.rs | 92 ++-- .../builtin_hint_processor/hint_utils.rs | 37 +- .../builtin_hint_processor/math_utils.rs | 295 ++++++------ .../memcpy_hint_utils.rs | 7 +- .../builtin_hint_processor/memset_utils.rs | 23 +- .../builtin_hint_processor/pow_utils.rs | 29 +- .../secp/bigint_utils.rs | 9 +- .../builtin_hint_processor/secp/ec_utils.rs | 23 +- .../secp/field_utils.rs | 81 ++-- .../builtin_hint_processor/secp/secp_utils.rs | 32 +- .../builtin_hint_processor/secp/signature.rs | 23 +- .../builtin_hint_processor/segments.rs | 5 +- .../builtin_hint_processor/set.rs | 20 +- .../builtin_hint_processor/sha256_utils.rs | 7 +- .../builtin_hint_processor/signature.rs | 3 +- .../squash_dict_utils.rs | 77 ++-- .../builtin_hint_processor/uint256_utils.rs | 81 ++-- .../builtin_hint_processor/usort.rs | 7 +- src/hint_processor/hint_processor_utils.rs | 33 +- src/math_utils.rs | 33 +- src/serde/deserialize_program.rs | 5 +- src/types/exec_scope.rs | 33 +- src/types/relocatable.rs | 224 ++++++---- src/utils.rs | 22 +- src/vm/context/run_context.rs | 62 +-- src/vm/decoding/decoder.rs | 93 ++-- src/vm/errors/hint_errors.rs | 2 +- src/vm/errors/vm_errors.rs | 5 +- src/vm/errors/vm_exception.rs | 20 +- src/vm/runners/builtin_runner/ec_op.rs | 58 --- src/vm/runners/builtin_runner/mod.rs | 78 ++-- src/vm/runners/cairo_runner.rs | 176 ++++---- src/vm/security.rs | 23 +- src/vm/trace/mod.rs | 11 +- src/vm/vm_core.rs | 422 +++++++++++------- src/vm/vm_memory/memory.rs | 12 +- src/vm/vm_memory/memory_segments.rs | 33 +- tests/bitwise_test.rs | 14 +- tests/pedersen_test.rs | 5 +- tests/skip_instruction_test.rs | 4 +- tests/struct_test.rs | 8 +- 48 files changed, 1343 insertions(+), 1127 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64f16c7d08..dabc670b47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,6 +62,12 @@ dependencies = [ "rand", ] +[[package]] +name = "assert_matches" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" + [[package]] name = "atty" version = "0.2.14" @@ -178,6 +184,7 @@ dependencies = [ name = "cairo-vm" version = "0.1.3" dependencies = [ + "assert_matches", "bincode", "cairo-felt", "clap 3.2.23", diff --git a/Cargo.toml b/Cargo.toml index e1cc94871a..29b7f9e967 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.1.0" } [dev-dependencies] iai = "0.1" +assert_matches = "1.5.0" [dev-dependencies.rusty-hook] version = "0.11" diff --git a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs index 681e7f64db..802c438faa 100644 --- a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs +++ b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs @@ -231,6 +231,7 @@ mod tests { utils::test_utils::*, vm::{errors::memory_errors::MemoryError, vm_memory::memory::Memory}, }; + use assert_matches::assert_matches; use std::any::Any; #[test] @@ -245,7 +246,7 @@ mod tests { //Create hint data let ids_data = ids_data!["output"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::CantSubOffset( 5, 26 @@ -266,11 +267,11 @@ mod tests { //Create hint data let ids_data = ids_data!["output"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((2, 0)) - ))) + x + ))) if x == MaybeRelocatable::from((2, 0)) ); } @@ -286,11 +287,11 @@ mod tests { //Create hint data let ids_data = ids_data!["output"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal( - VirtualMachineError::ExpectedRelocatable(MaybeRelocatable::from((1, 0))) - )) + VirtualMachineError::ExpectedRelocatable(x) + )) if x == MaybeRelocatable::from((1, 0)) ); } @@ -316,7 +317,7 @@ mod tests { //Create hint data let ids_data = ids_data!["output"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::BigintToU32Fail) ); @@ -334,11 +335,11 @@ mod tests { //Create hint data let ids_data = ids_data!["output"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((2, 0)) - ))) + x + ))) if x == MaybeRelocatable::from((2, 0)) ); } @@ -355,7 +356,7 @@ mod tests { //Create hint data let ids_data = ids_data!["blake2s_ptr_end"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check the inserted data let expected_data: [u32; 204] = [ 1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, @@ -398,15 +399,17 @@ mod tests { vm.memory = memory![((1, 0), (2, 0)), ((2, 0), (2, 0))]; let ids_data = ids_data!["blake2s_ptr_end"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((2, 0)), - MaybeRelocatable::from((2, 0)), - MaybeRelocatable::from(Felt::new(1795745351)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((2, 0)) && + y == MaybeRelocatable::from((2, 0)) && + z == MaybeRelocatable::from(Felt::new(1795745351)) ); } @@ -418,7 +421,7 @@ mod tests { //Initialize fp vm.run_context.fp = 1; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code), Err(HintError::FailedToGetIds) ); @@ -436,7 +439,7 @@ mod tests { vm.segments.add(&mut vm.memory); let ids_data = ids_data!["data", "high", "low"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check data ptr check_memory![ vm.memory, @@ -464,7 +467,7 @@ mod tests { vm.segments.add(&mut vm.memory); let ids_data = ids_data!["data", "high", "low"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check data ptr check_memory![ vm.memory, @@ -492,7 +495,7 @@ mod tests { add_segments!(vm, 1); let ids_data = ids_data!["data", "high", "low"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check data ptr check_memory![ vm.memory, @@ -520,7 +523,7 @@ mod tests { vm.segments.add(&mut vm.memory); let ids_data = ids_data!["data", "high", "low"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check data ptr check_memory![ vm.memory, diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index bf83fa45b8..eda4129e31 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -461,6 +461,7 @@ mod tests { vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use num_traits::{One, Zero}; use std::any::Any; @@ -502,15 +503,17 @@ mod tests { //Insert something into ap vm.memory = memory![((1, 6), (1, 6))]; //ids and references are not needed for this test - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 6)), - MaybeRelocatable::from((1, 6)), - MaybeRelocatable::from((3, 0)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 6)) && + y == MaybeRelocatable::from((1, 6)) && + z == MaybeRelocatable::from((3, 0)) ); } @@ -518,9 +521,9 @@ mod tests { fn run_unknown_hint() { let hint_code = "random_invalid_code"; let mut vm = vm!(); - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code), - Err(HintError::UnknownHint(hint_code.to_string())), + Err(HintError::UnknownHint(x)) if x == *hint_code.to_string() ); } @@ -551,11 +554,11 @@ mod tests { vm.memory = memory![((1, 1), (1, 0))]; let ids_data = ids_data!["len"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 1)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 1)) ); } @@ -588,9 +591,9 @@ mod tests { // initialize ids vm.memory = memory![((0, 2), 5)]; let ids_data = ids_data!["continue_copying"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::VariableNotInScopeError("n".to_string())) + Err(HintError::VariableNotInScopeError(x)) if x == *"n".to_string() ); } @@ -609,15 +612,17 @@ mod tests { vm.memory = memory![((1, 1), 5)]; let ids_data = ids_data!["continue_copying"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 1)), - MaybeRelocatable::from(Felt::new(5)), - MaybeRelocatable::from(Felt::zero()) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 1)) && + y == MaybeRelocatable::from(Felt::new(5)) && + z == MaybeRelocatable::from(Felt::zero()) ); } @@ -641,7 +646,7 @@ mod tests { // new vm scope is not created so that the hint raises an error: // initialize memory segments add_segments!(vm, 1); - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code), Err(HintError::FromScopeError( ExecScopeError::ExitMainScopeError @@ -656,7 +661,7 @@ mod tests { let mut vm = vm!(); let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Ok(()) ); @@ -706,9 +711,9 @@ mod tests { ]; let ids_data = ids_data!["length", "data", "high", "low"]; let mut exec_scopes = scope![("__keccak_max_size", Felt::new(2))]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::KeccakMaxSize(Felt::new(5), Felt::new(2))) + Err(HintError::KeccakMaxSize(x, y)) if x == Felt::new(5) && y == Felt::new(2) ); } @@ -752,9 +757,9 @@ mod tests { ]; let ids_data = ids_data!["length", "data", "high", "low"]; let mut exec_scopes = scope![("__keccak_max_size", Felt::new(10))]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::InvalidWordSize(Felt::new(-1))) + Err(HintError::InvalidWordSize(x)) if x == Felt::new(-1) ); } @@ -794,7 +799,7 @@ mod tests { ((1, 8), 0) ]; let ids_data = non_continuous_ids_data![("keccak_state", -7), ("high", -3), ("low", -2)]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::NoneInMemoryRange)) ); @@ -842,7 +847,7 @@ mod tests { assert_eq!(exec_scopes.data.len(), 1); let hint_data = HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new()); - assert_eq!( + assert_matches!( hint_processor.execute_hint( &mut vm, exec_scopes, @@ -854,7 +859,7 @@ mod tests { assert_eq!(exec_scopes.data.len(), 2); let hint_data = HintProcessorData::new_default(String::from("enter_scope_custom_a"), HashMap::new()); - assert_eq!( + assert_matches!( hint_processor.execute_hint( &mut vm, exec_scopes, diff --git a/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs b/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs index 4cb0d3435c..7a93f6b650 100644 --- a/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs +++ b/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs @@ -269,6 +269,7 @@ mod tests { vm_core::VirtualMachine, vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use std::any::Any; #[test] @@ -285,7 +286,7 @@ mod tests { vm.run_context.fp = 3; //Create ids let ids_data = ids_data!["low", "high", "inputs"]; - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); } #[test] @@ -298,10 +299,10 @@ mod tests { //Create ids let ids_data = ids_data!["low", "high", "inputs"]; let error = run_hint!(vm, ids_data, hint_code); - assert!(matches!( + assert_matches!( error, Err(HintError::Internal(VirtualMachineError::MemoryError(_))) - )); + ); } #[test] @@ -315,7 +316,7 @@ mod tests { run_context!(vm, 0, 1, 1); let ids_data = ids_data!["n_bytes"]; - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -343,7 +344,7 @@ mod tests { run_context!(vm, 0, 1, 1); let ids_data = ids_data!["n_bytes"]; - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -370,7 +371,7 @@ mod tests { run_context!(vm, 0, 1, 1); let ids_data = ids_data!["n_bytes"]; - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, diff --git a/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs b/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs index c79d744288..b62d590c54 100644 --- a/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs @@ -267,6 +267,7 @@ mod tests { utils::test_utils::*, vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; + use assert_matches::assert_matches; use std::collections::HashMap; #[test] @@ -305,7 +306,7 @@ mod tests { let hint_code = "if '__dict_manager' not in globals():\n from starkware.cairo.common.dict import DictManager\n __dict_manager = DictManager()\n\nmemory[ap] = __dict_manager.new_dict(segments, initial_dict)\ndel initial_dict"; let mut vm = vm!(); //ids and references are not needed for this test - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code), Err(HintError::NoInitialDict) ); @@ -321,15 +322,17 @@ mod tests { )]; vm.memory = memory![((1, 0), 1)]; //ids and references are not needed for this test - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 0)), - MaybeRelocatable::from(1), - MaybeRelocatable::from((0, 0)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 0)) && + y == MaybeRelocatable::from(1) && + z == MaybeRelocatable::from((0, 0)) ); } @@ -346,7 +349,7 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); dict_manager!(&mut exec_scopes, 2, (5, 12)); //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that value variable (at address (1,1)) contains the proper value assert_eq!( vm.memory @@ -372,9 +375,9 @@ mod tests { //Execute the hint let mut exec_scopes = ExecutionScopes::new(); dict_manager!(&mut exec_scopes, 2, (5, 12)); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::NoValueForKey(MaybeRelocatable::from(6))) + Err(HintError::NoValueForKey(x)) if x == MaybeRelocatable::from(6) ); } #[test] @@ -391,7 +394,7 @@ mod tests { add_segments!(vm, 1); let ids_data = ids_data!["key", "value", "dict_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::NoDictTracker(2)) ); @@ -435,7 +438,7 @@ mod tests { //Initialize fp vm.run_context.fp = 1; let ids_data = ids_data!["default_value"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ); @@ -459,7 +462,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 17) check_dictionary![exec_scopes, 2, (5, 17)]; //Check that the tracker's current_ptr has moved accordingly @@ -486,7 +489,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 17) check_dictionary![exec_scopes, 2, (5, 17)]; //Check that the tracker's current_ptr has moved accordingly @@ -513,7 +516,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 17) check_dictionary![exec_scopes, 2, (5, 17)]; //Check that the tracker's current_ptr has moved accordingly @@ -541,9 +544,9 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::NoValueForKey(MaybeRelocatable::from(5))) + Err(HintError::NoValueForKey(x)) if x == MaybeRelocatable::from(5) ); } @@ -564,7 +567,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 20) check_dictionary![exec_scopes, 2, (5, 20)]; //Check that the tracker's current_ptr has moved accordingly @@ -588,7 +591,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 20) check_dictionary![exec_scopes, 2, (5, 10)]; //Check that the tracker's current_ptr has moved accordingly @@ -612,13 +615,15 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::WrongPrevValue( - MaybeRelocatable::from(11), - MaybeRelocatable::from(10), - MaybeRelocatable::from(5) - )) + x, + y, + z + )) if x == MaybeRelocatable::from(11) && + y == MaybeRelocatable::from(10) && + z == MaybeRelocatable::from(5) ); } @@ -639,9 +644,9 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::NoValueForKey(MaybeRelocatable::from(6))) + Err(HintError::NoValueForKey(x)) if x == MaybeRelocatable::from(6) ); } @@ -662,7 +667,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 20) check_dictionary![exec_scopes, 2, (5, 20)]; //Check that the tracker's current_ptr has moved accordingly @@ -686,7 +691,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 10) check_dictionary![exec_scopes, 2, (5, 10)]; //Check that the tracker's current_ptr has moved accordingly @@ -710,13 +715,15 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::WrongPrevValue( - MaybeRelocatable::from(11), - MaybeRelocatable::from(10), - MaybeRelocatable::from(5) - )) + x, + y, + z + )) if x == MaybeRelocatable::from(11) && + y == MaybeRelocatable::from(10) && + z == MaybeRelocatable::from(5) ); } @@ -737,13 +744,15 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::WrongPrevValue( - MaybeRelocatable::from(10), - MaybeRelocatable::from(17), - MaybeRelocatable::from(6) - )) + x, + y, + z + )) if x == MaybeRelocatable::from(10) && + y == MaybeRelocatable::from(17) && + z == MaybeRelocatable::from(6) ); } @@ -764,7 +773,7 @@ mod tests { // dict_ptr.new_value = (2, 3) let ids_data = ids_data!["key", "prev_value", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that the dictionary was updated with the new key-value pair (5, 20) check_dictionary![exec_scopes, 2, (5, 20)]; //Check that the tracker's current_ptr has moved accordingly @@ -784,7 +793,7 @@ mod tests { //Execute the hint let mut exec_scopes = ExecutionScopes::new(); dict_manager!(exec_scopes, 2); - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that a new exec scope has been created assert_eq!(exec_scopes.data.len(), 2); //Check that this scope contains the expected initial-dict @@ -811,7 +820,7 @@ mod tests { //Execute the hint let mut exec_scopes = ExecutionScopes::new(); dict_manager!(exec_scopes, 2, (1, 2), (3, 4), (5, 6)); - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check that a new exec scope has been created assert_eq!(exec_scopes.data.len(), 2); //Check that this scope contains the expected initial-dict @@ -844,7 +853,7 @@ mod tests { add_segments!(vm, 1); let ids_data = ids_data!["dict_accesses_end"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::NoDictTracker(2)) ); @@ -864,7 +873,7 @@ mod tests { //Create ids let ids_data = ids_data!["squashed_dict_start", "squashed_dict_end"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::NoDictTracker(2)) ); @@ -884,7 +893,7 @@ mod tests { //Create ids let ids_data = ids_data!["squashed_dict_start", "squashed_dict_end"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check the updated pointer check_dict_ptr!(exec_scopes, 2, (2, 3)); } @@ -902,12 +911,12 @@ mod tests { //Create ids let ids_data = ids_data!["squashed_dict_start", "squashed_dict_end"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::MismatchedDictPtr( - relocatable!(2, 0), - relocatable!(2, 3) - )) + x, + y + )) if x == relocatable!(2, 0) && y == relocatable!(2, 3) ); } @@ -925,7 +934,7 @@ mod tests { // new_value here is (1,7) let ids_data = ids_data!["key", "new_value", "dict_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::DICT_WRITE, &mut exec_scopes), Ok(()) ); diff --git a/src/hint_processor/builtin_hint_processor/dict_manager.rs b/src/hint_processor/builtin_hint_processor/dict_manager.rs index 16c199ec58..7a7f2ae0ad 100644 --- a/src/hint_processor/builtin_hint_processor/dict_manager.rs +++ b/src/hint_processor/builtin_hint_processor/dict_manager.rs @@ -211,6 +211,7 @@ impl DictTracker { mod tests { use super::*; use crate::{relocatable, utils::test_utils::*, vm::vm_core::VirtualMachine}; + use assert_matches::assert_matches; #[test] fn create_dict_manager() { @@ -247,7 +248,7 @@ mod tests { let mut vm = vm!(); let mut dict_manager = DictManager::new(); let base = dict_manager.new_dict(&mut vm, HashMap::new()); - assert_eq!(base, Ok(MaybeRelocatable::from((0, 0)))); + assert_matches!(base, Ok(x) if x == MaybeRelocatable::from((0, 0))); assert!(dict_manager.trackers.contains_key(&0)); assert_eq!( dict_manager.trackers.get(&0), @@ -261,7 +262,7 @@ mod tests { let mut dict_manager = DictManager::new(); let mut vm = vm!(); let base = dict_manager.new_default_dict(&mut vm, &MaybeRelocatable::from(5), None); - assert_eq!(base, Ok(MaybeRelocatable::from((0, 0)))); + assert_matches!(base, Ok(x) if x == MaybeRelocatable::from((0, 0))); assert!(dict_manager.trackers.contains_key(&0)); assert_eq!( dict_manager.trackers.get(&0), @@ -281,7 +282,7 @@ mod tests { let mut initial_dict = HashMap::::new(); initial_dict.insert(MaybeRelocatable::from(5), MaybeRelocatable::from(5)); let base = dict_manager.new_dict(&mut vm, initial_dict.clone()); - assert_eq!(base, Ok(MaybeRelocatable::from((0, 0)))); + assert_matches!(base, Ok(x) if x == MaybeRelocatable::from((0, 0))); assert!(dict_manager.trackers.contains_key(&0)); assert_eq!( dict_manager.trackers.get(&0), @@ -304,7 +305,7 @@ mod tests { &MaybeRelocatable::from(7), Some(initial_dict.clone()), ); - assert_eq!(base, Ok(MaybeRelocatable::from((0, 0)))); + assert_matches!(base, Ok(x) if x == MaybeRelocatable::from((0, 0))); assert!(dict_manager.trackers.contains_key(&0)); assert_eq!( dict_manager.trackers.get(&0), @@ -324,7 +325,7 @@ mod tests { .trackers .insert(0, DictTracker::new_empty(&relocatable!(0, 0))); let mut vm = vm!(); - assert_eq!( + assert_matches!( dict_manager.new_dict(&mut vm, HashMap::new()), Err(HintError::CantCreateDictionaryOnTakenSegment(0)) ); @@ -338,7 +339,7 @@ mod tests { DictTracker::new_default_dict(&relocatable!(0, 0), &MaybeRelocatable::from(6), None), ); let mut vm = vm!(); - assert_eq!( + assert_matches!( dict_manager.new_dict(&mut vm, HashMap::new()), Err(HintError::CantCreateDictionaryOnTakenSegment(0)) ); diff --git a/src/hint_processor/builtin_hint_processor/find_element_hint.rs b/src/hint_processor/builtin_hint_processor/find_element_hint.rs index c3fe3ab836..150e0a4b5d 100644 --- a/src/hint_processor/builtin_hint_processor/find_element_hint.rs +++ b/src/hint_processor/builtin_hint_processor/find_element_hint.rs @@ -144,10 +144,11 @@ mod tests { }, hint_processor_definition::HintProcessor, }, - types::relocatable::MaybeRelocatable, + types::relocatable::{MaybeRelocatable, Relocatable}, utils::test_utils::*, vm::vm_core::VirtualMachine, }; + use assert_matches::assert_matches; use num_traits::{One, Zero}; use std::any::Any; @@ -220,7 +221,7 @@ mod tests { #[test] fn element_found_by_search() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT.to_string()), Ok(()) ); @@ -231,7 +232,7 @@ mod tests { fn element_found_by_oracle() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); let mut exec_scopes = scope![("find_element_index", Felt::one())]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT, &mut exec_scopes), Ok(()) ); @@ -244,9 +245,9 @@ mod tests { "key".to_string(), MaybeRelocatable::from(Felt::new(7)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::NoValueForKeyFindElement(Felt::new(7))) + Err(HintError::NoValueForKeyFindElement(x)) if x == Felt::new(7) ); } @@ -254,7 +255,7 @@ mod tests { fn element_not_found_oracle() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); let mut exec_scopes = scope![("find_element_index", Felt::new(2))]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT, &mut exec_scopes), Err(HintError::KeyNotFound) ); @@ -265,11 +266,11 @@ mod tests { let mut vm = vm!(); vm.run_context.fp = 5; let ids_data = ids_data!["array_ptr", "elm_size", "n_elms", "index", "key"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 4)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 4)) ); } @@ -279,11 +280,11 @@ mod tests { "elm_size".to_string(), MaybeRelocatable::from((7, 8)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 1)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 1)) ); } @@ -293,9 +294,9 @@ mod tests { "elm_size".to_string(), MaybeRelocatable::Int(Felt::zero()), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::ValueOutOfRange(Felt::zero())) + Err(HintError::ValueOutOfRange(x)) if x == Felt::zero() ); } @@ -305,9 +306,9 @@ mod tests { "elm_size".to_string(), MaybeRelocatable::Int(Felt::new(-1)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::ValueOutOfRange(Felt::new(-1))) + Err(HintError::ValueOutOfRange(x)) if x == Felt::new(-1) ); } @@ -316,11 +317,11 @@ mod tests { let relocatable = MaybeRelocatable::from((1, 2)); let (mut vm, ids_data) = init_vm_ids_data(HashMap::from([("n_elms".to_string(), relocatable.clone())])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - relocatable - ))) + inner + ))) if inner == relocatable ); } @@ -330,25 +331,25 @@ mod tests { "n_elms".to_string(), MaybeRelocatable::Int(Felt::new(-1)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::ValueOutOfRange(Felt::new(-1))) + Err(HintError::ValueOutOfRange(x)) if x == Felt::new(-1) ); } #[test] fn find_elm_empty_scope() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); - assert_eq!(run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), Ok(())); } #[test] fn find_elm_n_elms_gt_max_size() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); let mut exec_scopes = scope![("find_element_max_size", Felt::one())]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT, &mut exec_scopes), - Err(HintError::FindElemMaxSize(Felt::one(), Felt::new(2))) + Err(HintError::FindElemMaxSize(x, y)) if x == Felt::one() && y == Felt::new(2) ); } @@ -356,11 +357,14 @@ mod tests { fn find_elm_key_not_int() { let relocatable = MaybeRelocatable::from((1, 4)); let (mut vm, ids_data) = - init_vm_ids_data(HashMap::from([("key".to_string(), relocatable.clone())])); - assert_eq!( + init_vm_ids_data(HashMap::from([("key".to_string(), relocatable)])); + assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - relocatable + MaybeRelocatable::RelocatableValue(Relocatable { + segment_index: 1, + offset: 4 + }) ))) ); } @@ -368,7 +372,7 @@ mod tests { #[test] fn search_sorted_lower() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), Ok(()) ); @@ -382,7 +386,7 @@ mod tests { "key".to_string(), MaybeRelocatable::Int(Felt::new(7)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), Ok(()) ); @@ -395,11 +399,11 @@ mod tests { "elm_size".to_string(), MaybeRelocatable::from((7, 8)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 1)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 1)) ); } @@ -409,9 +413,9 @@ mod tests { "elm_size".to_string(), MaybeRelocatable::Int(Felt::zero()), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), - Err(HintError::ValueOutOfRange(Felt::zero())) + Err(HintError::ValueOutOfRange(x)) if x.is_zero() ); } @@ -421,9 +425,9 @@ mod tests { "elm_size".to_string(), MaybeRelocatable::Int(Felt::new(-1)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), - Err(HintError::ValueOutOfRange(Felt::new(-1))) + Err(HintError::ValueOutOfRange(x)) if x == Felt::new(-1) ); } @@ -433,11 +437,11 @@ mod tests { "n_elms".to_string(), MaybeRelocatable::from((2, 2)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 2)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 2)) ); } @@ -447,16 +451,16 @@ mod tests { "n_elms".to_string(), MaybeRelocatable::Int(Felt::new(-1)), )])); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), - Err(HintError::ValueOutOfRange(Felt::new(-1))) + Err(HintError::ValueOutOfRange(x)) if x == Felt::new(-1) ); } #[test] fn search_sorted_lower_empty_scope() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), Ok(()) ); @@ -466,14 +470,14 @@ mod tests { fn search_sorted_lower_n_elms_gt_max_size() { let (mut vm, ids_data) = init_vm_ids_data(HashMap::new()); let mut exec_scopes = scope![("find_element_max_size", Felt::one())]; - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, hint_code::SEARCH_SORTED_LOWER, &mut exec_scopes ), - Err(HintError::FindElemMaxSize(Felt::one(), Felt::new(2))) + Err(HintError::FindElemMaxSize(x, y)) if x == Felt::one() && y == Felt::new(2) ); } } diff --git a/src/hint_processor/builtin_hint_processor/hint_utils.rs b/src/hint_processor/builtin_hint_processor/hint_utils.rs index 2617a1c15f..e081692504 100644 --- a/src/hint_processor/builtin_hint_processor/hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/hint_utils.rs @@ -128,6 +128,7 @@ mod tests { vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; #[test] fn get_ptr_from_var_name_immediate_value() { @@ -137,9 +138,9 @@ mod tests { hint_ref.offset2 = OffsetValue::Value(2); let ids_data = HashMap::from([("imm".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_ptr_from_var_name("imm", &vm, &ids_data, &ApTracking::new()), - Ok(relocatable!(0, 2)) + Ok(x) if x == relocatable!(0, 2) ); } @@ -150,9 +151,9 @@ mod tests { let hint_ref = HintReference::new_simple(0); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_maybe_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()), - Ok(mayberelocatable!(0, 0)) + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -163,7 +164,7 @@ mod tests { let hint_ref = HintReference::new_simple(0); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_maybe_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()), Err(HintError::FailedToGetIds) ); @@ -176,9 +177,9 @@ mod tests { let hint_ref = HintReference::new_simple(0); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_ptr_from_var_name("value", &vm, &ids_data, &ApTracking::new()), - Ok(relocatable!(0, 0)) + Ok(x) if x == relocatable!(0, 0) ); } @@ -189,11 +190,11 @@ mod tests { let hint_ref = HintReference::new_simple(0); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_ptr_from_var_name("value", &vm, &ids_data, &ApTracking::new()), Err(HintError::Internal( - VirtualMachineError::ExpectedRelocatable(MaybeRelocatable::from((1, 0))) - )) + VirtualMachineError::ExpectedRelocatable(x) + )) if x == MaybeRelocatable::from((1, 0)) ); } @@ -204,9 +205,9 @@ mod tests { let hint_ref = HintReference::new_simple(0); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()), - Ok(relocatable!(1, 0)) + Ok(x) if x == relocatable!(1, 0) ); } @@ -217,7 +218,7 @@ mod tests { let hint_ref = HintReference::new_simple(-8); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_relocatable_from_var_name("value", &vm, &ids_data, &ApTracking::new()), Err(HintError::FailedToGetIds) ); @@ -230,9 +231,9 @@ mod tests { let hint_ref = HintReference::new_simple(0); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_integer_from_var_name("value", &vm, &ids_data, &ApTracking::new()), - Ok(Cow::Borrowed(&Felt::new(1))) + Ok(Cow::Borrowed(x)) if x == &Felt::new(1) ); } @@ -243,11 +244,11 @@ mod tests { let hint_ref = HintReference::new_simple(0); let ids_data = HashMap::from([("value".to_string(), hint_ref)]); - assert_eq!( + assert_matches!( get_integer_from_var_name("value", &vm, &ids_data, &ApTracking::new()), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 0)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 0)) ); } } diff --git a/src/hint_processor/builtin_hint_processor/math_utils.rs b/src/hint_processor/builtin_hint_processor/math_utils.rs index 043232c0ca..00db41f974 100644 --- a/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -556,6 +556,7 @@ mod tests { vm_core::VirtualMachine, vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use felt::felt_str; use num_traits::Zero; use std::{any::Any, ops::Shl}; @@ -629,7 +630,7 @@ mod tests { //Create ids_data let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal( VirtualMachineError::NoRangeCheckBuiltin @@ -646,7 +647,7 @@ mod tests { //Create ids_data & hint_data let ids_data = ids_data!["b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ); @@ -663,11 +664,11 @@ mod tests { //Create ids_data let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 4)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 4)) ); } @@ -682,11 +683,11 @@ mod tests { //Create ids_data let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 4)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 4)) ); } @@ -712,7 +713,7 @@ mod tests { //Create ids_data & hint_data let ids_data = ids_data!["a", "b", "range_check_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes, &constants), Ok(()) ); @@ -730,7 +731,7 @@ mod tests { add_segments!(vm, 1); let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check result check_memory![vm.memory, ((1, 0), 0)]; } @@ -745,15 +746,17 @@ mod tests { //Create ids_data & hint_data let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 0)), - MaybeRelocatable::Int(Felt::one()), - MaybeRelocatable::Int(Felt::zero()) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 0)) && + y == MaybeRelocatable::Int(Felt::one()) && + z == MaybeRelocatable::Int(Felt::zero()) ); } @@ -765,7 +768,7 @@ mod tests { vm.memory = memory![((1, 8), 1), ((1, 9), 2)]; //Create ids_data & hint_data let ids_data = ids_data!["a", "c"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ); @@ -782,7 +785,7 @@ mod tests { //Create ids_data & hint_data let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Hint would return an error if the assertion fails } @@ -797,9 +800,9 @@ mod tests { //Create ids_data & hint_data let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::AssertNNValueOutOfRange(Felt::new(-1))) + Err(HintError::AssertNNValueOutOfRange(x)) if x == Felt::new(-1) ); } @@ -813,9 +816,9 @@ mod tests { vm.memory = memory![((1, 0), (-1))]; let ids_data = ids_data!["incorrect_id"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::FailedToGetIds), + Err(HintError::FailedToGetIds) ); } @@ -829,11 +832,11 @@ mod tests { vm.memory = memory![((1, 0), (10, 10))]; let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 3)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 3)) ); } @@ -847,7 +850,7 @@ mod tests { vm.memory = memory![((1, 0), 1)]; let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal( VirtualMachineError::NoRangeCheckBuiltin @@ -864,11 +867,11 @@ mod tests { vm.run_context.fp = 4; let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 3)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 3)) ); } @@ -893,9 +896,9 @@ mod tests { let ids_data = ids_data!["a", "b", "range_check_ptr"]; add_segments!(vm, 1); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes, &constants), - Err(HintError::NonLeFelt(Felt::new(2), Felt::one())) + Err(HintError::NonLeFelt(x, y)) if x == Felt::new(2) && y == Felt::one() ); } @@ -919,11 +922,11 @@ mod tests { vm.memory = memory![((1, 0), (1, 0)), ((1, 1), 1), ((1, 2), (2, 0))]; let ids_data = ids_data!["a", "b", "range_check_ptr"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes, &constants), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 0)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 0)) ); } @@ -947,11 +950,11 @@ mod tests { vm.memory = memory![((1, 0), 1), ((1, 1), (1, 0)), ((1, 2), (2, 0))]; let ids_data = ids_data!["a", "b", "range_check_builtin"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes, &constants), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 1)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 1)) ); } @@ -998,12 +1001,13 @@ mod tests { vm.memory = memory![((1, 8), 1), ((1, 9), 1)]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::AssertNotEqualFail( - MaybeRelocatable::from(Felt::one()), - MaybeRelocatable::from(Felt::one()) - )) + x, + y + )) if x == MaybeRelocatable::from(Felt::one()) && + y == MaybeRelocatable::from(Felt::one()) ); } @@ -1017,7 +1021,7 @@ mod tests { vm.memory = memory![((1, 8), 1), ((1, 9), 3)]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); } #[test] @@ -1040,7 +1044,7 @@ mod tests { ]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); } #[test] @@ -1053,12 +1057,13 @@ mod tests { vm.memory = memory![((1, 8), (1, 0)), ((1, 9), (1, 0))]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::AssertNotEqualFail( - MaybeRelocatable::from((1, 0)), - MaybeRelocatable::from((1, 0)) - )) + x, + y + )) if x == MaybeRelocatable::from((1, 0)) && + y == MaybeRelocatable::from((1, 0)) ); } @@ -1072,7 +1077,7 @@ mod tests { vm.memory = memory![((1, 8), (0, 1)), ((1, 9), (0, 0))]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); } #[test] @@ -1085,12 +1090,12 @@ mod tests { vm.memory = memory![((1, 8), (2, 0)), ((1, 9), (1, 0))]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::DiffIndexComp( - relocatable!(2, 0), - relocatable!(1, 0) - ))) + x, + y + ))) if x == relocatable!(2, 0) && y == relocatable!(1, 0) ); } @@ -1104,14 +1109,14 @@ mod tests { vm.memory = memory![((1, 8), (1, 0)), ((1, 9), 1)]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal( VirtualMachineError::DiffTypeComparison( - MaybeRelocatable::from((1, 0)), - MaybeRelocatable::from(Felt::one()) + x, + y ) - )) + )) if x == MaybeRelocatable::from((1, 0)) && y == MaybeRelocatable::from(Felt::one()) ); } @@ -1127,7 +1132,7 @@ mod tests { //Create ids let ids_data = ids_data!["value"]; - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); } #[test] @@ -1141,12 +1146,12 @@ mod tests { vm.memory = memory![((1, 4), 0)]; //Create ids let ids_data = ids_data!["value"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::AssertNotZero( - Felt::zero(), - felt::PRIME_STR.to_string() - )) + x, + y + )) if x == Felt::zero() && y == *felt::PRIME_STR.to_string() ); } @@ -1161,7 +1166,7 @@ mod tests { vm.memory = memory![((1, 4), 0)]; //Create invalid id key let ids_data = ids_data!["incorrect_id"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ); @@ -1178,11 +1183,11 @@ mod tests { vm.memory = memory![((1, 4), (1, 0))]; //Create ids_data & hint_data let ids_data = ids_data!["value"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 4)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 4)) ); } @@ -1196,7 +1201,7 @@ mod tests { vm.memory = memory![((1, 4), 1)]; let ids_data = ids_data!["value"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::SplitIntNotZero) ); @@ -1212,7 +1217,7 @@ mod tests { vm.memory = memory![((1, 4), 0)]; let ids_data = ids_data!["value"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); } #[test] @@ -1226,7 +1231,7 @@ mod tests { add_segments!(vm, 2); let ids_data = ids_data!["output", "value", "base", "bound"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); check_memory![vm.memory, ((2, 0), 2)]; } @@ -1246,9 +1251,9 @@ mod tests { add_segments!(vm, 2); let ids_data = ids_data!["output", "value", "base", "bound"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::SplitIntLimbOutOfRange(Felt::new(100))) + Err(HintError::SplitIntLimbOutOfRange(x)) if x == Felt::new(100) ); } @@ -1305,11 +1310,11 @@ mod tests { //Dont insert ids.is_positive as we need to modify it inside the hint let ids_data = ids_data!["value", "is_positive"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::ValueOutsideValidRange(felt_str!( + Err(HintError::ValueOutsideValidRange(x)) if x == felt_str!( "618502761706184546546682988428055018603476541694452277432519575032261771265" - ))) + ) ); } @@ -1324,15 +1329,17 @@ mod tests { vm.memory = memory![((1, 0), 2), ((1, 1), 4)]; let ids_data = ids_data!["value", "is_positive"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 1)), - MaybeRelocatable::from(Felt::new(4)), - MaybeRelocatable::from(Felt::one()) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 1)) && + y == MaybeRelocatable::from(Felt::new(4)) && + z == MaybeRelocatable::from(Felt::one()) ); } @@ -1347,7 +1354,7 @@ mod tests { //Create ids let ids_data = ids_data!["value", "root"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check that root (0,1) has the square root of 81 check_memory![vm.memory, ((1, 1), 9)]; } @@ -1363,11 +1370,11 @@ mod tests { //Create ids let ids_data = ids_data!["value", "root"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::ValueOutside250BitRange(felt_str!( + Err(HintError::ValueOutside250BitRange(x)) if x == felt_str!( "3618502788666131213697322783095070105623107215331596699973092056135872020400" - ))) + ) ); } @@ -1382,15 +1389,17 @@ mod tests { //Create ids let ids_data = ids_data!["value", "root"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 1)), - MaybeRelocatable::from(Felt::new(7)), - MaybeRelocatable::from(Felt::new(9)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 1)) && + y == MaybeRelocatable::from(Felt::new(7)) && + z == MaybeRelocatable::from(Felt::new(9)) ); } @@ -1420,12 +1429,12 @@ mod tests { //Create ids let ids_data = ids_data!["r", "q", "div", "value"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::OutOfValidRange( - Felt::new(-5), - felt_str!("340282366920938463463374607431768211456") - )) + x, + y + )) if x == Felt::new(-5) && y == felt_str!("340282366920938463463374607431768211456") ) } @@ -1439,7 +1448,7 @@ mod tests { vm.memory = memory![((1, 2), 5), ((1, 3), 7)]; //Create ids_data let ids_data = ids_data!["r", "q", "div", "value"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal( VirtualMachineError::NoRangeCheckBuiltin @@ -1458,15 +1467,17 @@ mod tests { //Create ids_data let ids_data = ids_data!["r", "q", "div", "value"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 0)), - MaybeRelocatable::Int(Felt::new(5)), - MaybeRelocatable::Int(Felt::new(2)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 0)) && + y == MaybeRelocatable::Int(Felt::new(5)) && + z == MaybeRelocatable::Int(Felt::new(2)) ); } @@ -1481,7 +1492,7 @@ mod tests { //Create ids let ids_data = ids_data!["a", "b", "iv", "vlue"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ) @@ -1528,12 +1539,12 @@ mod tests { //Create ids let ids_data = ids_data!["r", "biased_q", "range_check_ptr", "div", "value", "bound"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::OutOfValidRange( - Felt::new(-5), - felt_str!("340282366920938463463374607431768211456") - )) + x, + y + )) if x == Felt::new(-5) && y == felt_str!("340282366920938463463374607431768211456") ) } @@ -1547,7 +1558,7 @@ mod tests { vm.memory = memory![((1, 3), 5), ((1, 4), 10), ((1, 5), 29)]; //Create ids let ids_data = ids_data!["r", "biased_q", "range_check_ptr", "div", "value", "bound"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal( VirtualMachineError::NoRangeCheckBuiltin @@ -1566,15 +1577,17 @@ mod tests { //Create ids let ids_data = ids_data!["r", "biased_q", "range_check_ptr", "div", "value", "bound"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 1)), - MaybeRelocatable::Int(Felt::new(10)), - MaybeRelocatable::Int(Felt::new(31)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 1)) && + y == MaybeRelocatable::Int(Felt::new(10)) && + z == MaybeRelocatable::Int(Felt::new(31)) ); } @@ -1589,7 +1602,7 @@ mod tests { //Create ids let ids_data = ids_data!["r", "b", "r", "d", "v", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ) @@ -1606,7 +1619,7 @@ mod tests { //Create ids let ids_data = ids_data!["value", "high", "low"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Hint would return an error if the assertion fails //Check ids.high and ids.low values check_memory![vm.memory, ((1, 1), 0), ((1, 2), 1)]; @@ -1630,9 +1643,9 @@ mod tests { //Create ids let ids_data = ids_data!["value", "high", "low"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::ValueOutside250BitRange(Felt::one().shl(251_u32))) + Err(HintError::ValueOutside250BitRange(x)) if x == Felt::one().shl(251_u32) ); } @@ -1655,7 +1668,7 @@ mod tests { ("high".to_string(), HintReference::new(-3, 1, true, true)), ]); //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts check_memory![ vm.memory, @@ -1679,7 +1692,7 @@ mod tests { //Create ids_data & hint_data let ids_data = ids_data!["low"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ); @@ -1705,15 +1718,17 @@ mod tests { ]); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((2, 0)), - MaybeRelocatable::from(Felt::new(99)), - MaybeRelocatable::from(felt_str!("335438970432432812899076431678123043273")) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((2, 0)) && + y == MaybeRelocatable::from(Felt::new(99)) && + z == MaybeRelocatable::from(felt_str!("335438970432432812899076431678123043273")) ); } @@ -1737,15 +1752,17 @@ mod tests { ("high".to_string(), HintReference::new(-3, 1, true, true)), ]); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((2, 1)), - MaybeRelocatable::from(Felt::new(99)), - MaybeRelocatable::from(Felt::new(0)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((2, 1)) && + y == MaybeRelocatable::from(Felt::new(99)) && + z == MaybeRelocatable::from(Felt::new(0)) ); } @@ -1764,11 +1781,11 @@ mod tests { ("high".to_string(), HintReference::new(-3, 1, true, true)), ]); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 3)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 3)) ); } @@ -1784,7 +1801,7 @@ mod tests { //Create ids let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); } #[test] @@ -1797,9 +1814,9 @@ mod tests { vm.memory = memory![((1, 1), 3), ((1, 2), 2)]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::AssertLtFelt(Felt::new(3), Felt::new(2))) + Err(HintError::AssertLtFelt(x, y)) if x == Felt::new(3) && y == Felt::new(2) ); } @@ -1814,7 +1831,7 @@ mod tests { //Create Incorrects ids let ids_data = ids_data!["a"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ); @@ -1830,11 +1847,11 @@ mod tests { vm.memory = memory![((1, 1), (1, 0)), ((1, 2), 2)]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 1)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 1)) ); } @@ -1848,11 +1865,11 @@ mod tests { vm.memory = memory![((1, 1), 1), ((1, 2), (1, 0))]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 2)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 2)) ); } @@ -1867,11 +1884,11 @@ mod tests { vm.memory = memory![((1, 1), 1)]; let ids_data = ids_data!["a", "b"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 2)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 2)) ); } } diff --git a/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs b/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs index 2d0f48640f..2404fc1021 100644 --- a/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs @@ -82,6 +82,7 @@ mod tests { vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; #[test] fn get_integer_from_var_name_valid() { @@ -123,11 +124,11 @@ mod tests { //Insert ids.variable into memory as a RelocatableValue vm.memory = memory![((1, 0), (1, 1))]; - assert_eq!( + assert_matches!( get_integer_from_var_name(var_name, &vm, &ids_data, &ApTracking::default()), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 0)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 0)) ); } } diff --git a/src/hint_processor/builtin_hint_processor/memset_utils.rs b/src/hint_processor/builtin_hint_processor/memset_utils.rs index 0f0c6d78ea..387c3e2999 100644 --- a/src/hint_processor/builtin_hint_processor/memset_utils.rs +++ b/src/hint_processor/builtin_hint_processor/memset_utils.rs @@ -71,6 +71,7 @@ mod tests { vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use num_traits::{One, Zero}; #[test] @@ -95,11 +96,11 @@ mod tests { // insert a relocatable value in the address of ids.len so that it raises an error. vm.memory = memory![((1, 1), (1, 0))]; let ids_data = ids_data!["n"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 1)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 1)) ); } @@ -153,9 +154,9 @@ mod tests { // we create a memory gap so that there is None in (0, 1), the actual addr of continue_loop vm.memory = memory![((1, 2), 5)]; let ids_data = ids_data!["continue_loop"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::VariableNotInScopeError("n".to_string())) + Err(HintError::VariableNotInScopeError(x)) if x == *"n".to_string() ); } @@ -171,15 +172,17 @@ mod tests { // a value is written in the address so the hint cant insert value there vm.memory = memory![((1, 0), 5)]; let ids_data = ids_data!["continue_loop"]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 0)), - MaybeRelocatable::from(Felt::new(5)), - MaybeRelocatable::from(Felt::zero()) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 0)) && + y == MaybeRelocatable::from(Felt::new(5)) && + z == MaybeRelocatable::from(Felt::zero()) ); } } diff --git a/src/hint_processor/builtin_hint_processor/pow_utils.rs b/src/hint_processor/builtin_hint_processor/pow_utils.rs index e1eddd54c4..ca3757af47 100644 --- a/src/hint_processor/builtin_hint_processor/pow_utils.rs +++ b/src/hint_processor/builtin_hint_processor/pow_utils.rs @@ -47,6 +47,7 @@ mod tests { vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use num_traits::One; use std::any::Any; @@ -58,7 +59,7 @@ mod tests { vm.run_context.fp = 12; vm.memory = memory![((1, 11), 3)]; let ids_data = non_continuous_ids_data![("prev_locs", -5), ("locs", 0)]; - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts check_memory![vm.memory, ((1, 12), 1)]; } @@ -73,7 +74,7 @@ mod tests { //Create incorrect ids let ids_data = ids_data!["locs"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::FailedToGetIds) ); @@ -89,11 +90,11 @@ mod tests { //Create hint_data let ids_data = non_continuous_ids_data![("prev_locs", -5), ("locs", -12)]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 10)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 10)) ); } @@ -109,11 +110,11 @@ mod tests { vm.memory = memory![((1, 10), (1, 11))]; add_segments!(vm, 1); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 10)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 10)) ); } @@ -128,15 +129,17 @@ mod tests { //Insert ids into memory vm.memory = memory![((1, 10), 3), ((1, 11), 3)]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 11)), - MaybeRelocatable::from(Felt::new(3)), - MaybeRelocatable::from(Felt::one()) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 11)) && + y == MaybeRelocatable::from(Felt::new(3)) && + z == MaybeRelocatable::from(Felt::one()) ); } } diff --git a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index d729956299..b7623e539b 100644 --- a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -78,6 +78,7 @@ mod tests { use crate::utils::test_utils::*; use crate::vm::runners::builtin_runner::RangeCheckBuiltinRunner; use crate::vm::vm_core::VirtualMachine; + use assert_matches::assert_matches; use num_traits::One; use std::any::Any; use std::ops::Shl; @@ -96,7 +97,7 @@ mod tests { run_context!(vm, 0, 6, 6); //Create hint_data let ids_data = non_continuous_ids_data![("res", 5)]; - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -127,9 +128,9 @@ mod tests { run_context!(vm, 0, 6, 6); //Create hint_data let ids_data = non_continuous_ids_data![("res", 5)]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::VariableNotInScopeError("value".to_string())) + Err(HintError::VariableNotInScopeError(x)) if x == *"value".to_string() ); } @@ -142,7 +143,7 @@ mod tests { let mut exec_scopes = scope![("value", bigint!(-1))]; //Create hint_data let ids_data = non_continuous_ids_data![("res", 5)]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::BigIntToBigUintFail) ); diff --git a/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs b/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs index a6d0025a1e..9dc693eaf5 100644 --- a/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs @@ -425,6 +425,7 @@ mod tests { vm_core::VirtualMachine, vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use std::any::Any; #[test] @@ -439,7 +440,7 @@ mod tests { let ids_data = ids_data!["point"]; let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -462,11 +463,11 @@ mod tests { Ok(()) ); //Check 'value' is defined in the vm scope - assert_eq!( + assert_matches!( exec_scopes.get::("value"), - Ok(bigint_str!( + Ok(x) if x == bigint_str!( "115792089237316195423569751828682367333329274433232027476421668138471189901786" - )) + ) ); } @@ -490,7 +491,7 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -561,7 +562,7 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -629,7 +630,7 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -712,7 +713,7 @@ mod tests { ) ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, HashMap::new(), @@ -789,7 +790,7 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -860,7 +861,7 @@ mod tests { ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, HashMap::new(), @@ -917,7 +918,7 @@ mod tests { let ids_data = ids_data!["scalar"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts check_memory![&vm.memory, ((1, 2), 0)]; diff --git a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs index 792de16c30..6c6a89493f 100644 --- a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs @@ -175,6 +175,7 @@ mod tests { vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use std::any::Any; #[test] @@ -187,7 +188,7 @@ mod tests { let ids_data = non_continuous_ids_data![("val", -5), ("q", 0)]; vm.memory = memory![((1, 4), 0), ((1, 5), 0), ((1, 6), 0)]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -225,7 +226,7 @@ mod tests { let ids_data = non_continuous_ids_data![("val", -5), ("q", 0)]; vm.memory = memory![((1, 4), 0), ((1, 5), 0), ((1, 6), 150)]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -245,9 +246,9 @@ mod tests { .map(|(k, v)| (k.to_string(), v)) .collect() ), - Err(HintError::SecpVerifyZero(bigint_str!( + Err(HintError::SecpVerifyZero(x)) if x == bigint_str!( "897946605976106752944343961220884287276604954404454400" - ))) + ) ); } @@ -264,7 +265,7 @@ mod tests { let ids_data = non_continuous_ids_data![("val", -5), ("q", 0)]; vm.memory = memory![((1, 4), 0), ((1, 5), 0), ((1, 6), 0), ((1, 9), 55)]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -286,11 +287,13 @@ mod tests { ), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 9)), - MaybeRelocatable::from(Felt::new(55_i32)), - MaybeRelocatable::from(Felt::zero()) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 9)) && + y == MaybeRelocatable::from(Felt::new(55_i32)) && + z == MaybeRelocatable::from(Felt::zero()) ); } @@ -314,7 +317,7 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -338,11 +341,11 @@ mod tests { ); //Check 'value' is defined in the vm scope - assert_eq!( + assert_matches!( exec_scopes.get::("value"), - Ok(bigint_str!( + Ok(x) if x == bigint_str!( "59863107065205964761754162760883789350782881856141750" - )) + ) ); } @@ -359,7 +362,7 @@ mod tests { let ids_data = HashMap::from([("x".to_string(), HintReference::new_simple(-5))]); //Skip ids.x values insert so the hint fails. //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -380,8 +383,8 @@ mod tests { .collect() ), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 20)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 20)) ); } @@ -405,7 +408,7 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -454,7 +457,7 @@ mod tests { //Skip ids.x.d0, ids.x.d1, ids.x.d2 inserts so the hints fails //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, ids_data, @@ -475,8 +478,8 @@ mod tests { .collect() ), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 10)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 10)) ); } @@ -496,7 +499,7 @@ mod tests { exec_scopes.assign_or_update_variable("x", any_box!(BigInt::zero())); //Create hint data //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Ok(()) ); @@ -522,7 +525,7 @@ mod tests { exec_scopes.assign_or_update_variable("x", any_box!(bigint!(123890i32))); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Ok(()) ); @@ -546,9 +549,9 @@ mod tests { //Skip `x` assignment //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code), - Err(HintError::VariableNotInScopeError("x".to_string())) + Err(HintError::VariableNotInScopeError(x)) if x == *"x".to_string() ); } @@ -567,15 +570,17 @@ mod tests { let mut exec_scopes = ExecutionScopes::new(); exec_scopes.assign_or_update_variable("x", any_box!(BigInt::zero())); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from(vm.run_context.get_ap()), - MaybeRelocatable::from(Felt::new(55i32)), - MaybeRelocatable::from(Felt::new(1i32)) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from(vm.run_context.get_ap()) && + y == MaybeRelocatable::from(Felt::new(55i32)) && + z == MaybeRelocatable::from(Felt::new(1i32)) ); } @@ -593,7 +598,7 @@ mod tests { )), ); //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, HashMap::new(), @@ -617,19 +622,19 @@ mod tests { ); //Check 'value' is defined in the vm scope - assert_eq!( + assert_matches!( exec_scopes.get::("value"), - Ok(bigint_str!( + Ok(x) if x == bigint_str!( "19429627790501903254364315669614485084365347064625983303617500144471999752609" - )) + ) ); //Check 'x_inv' is defined in the vm scope - assert_eq!( + assert_matches!( exec_scopes.get::("x_inv"), - Ok(bigint_str!( + Ok(x) if x == bigint_str!( "19429627790501903254364315669614485084365347064625983303617500144471999752609" - )) + ) ); } @@ -639,7 +644,7 @@ mod tests { let mut vm = vm_with_range_check!(); //Skip `x` assignment //Execute the hint - assert_eq!( + assert_matches!( run_hint!( vm, HashMap::new(), @@ -659,7 +664,7 @@ mod tests { .map(|(k, v)| (k.to_string(), v)) .collect() ), - Err(HintError::VariableNotInScopeError("x".to_string())) + Err(HintError::VariableNotInScopeError(x)) if x == *"x".to_string() ); } } diff --git a/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs b/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs index 6806b1f8d9..69c8a507c6 100644 --- a/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs @@ -93,6 +93,7 @@ pub fn pack_from_relocatable(rel: Relocatable, vm: &VirtualMachine) -> Result::new())]; //Execute the hint //Hint should produce an error if assertion fails - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Ok(()) ); @@ -565,7 +566,7 @@ mod tests { let mut exec_scopes = scope![("current_access_indices", vec![Felt::new(29)])]; //Execute the hint //Hint should produce an error if assertion fails - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Err(HintError::CurrentAccessIndicesNotEmpty) ); @@ -591,7 +592,7 @@ mod tests { let ids_data = ids_data!["n_used_accesses"]; //Execute the hint //Hint would fail is assertion fails - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); } #[test] @@ -613,13 +614,13 @@ mod tests { //Create hint_data let ids_data = ids_data!["n_used_accesses"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::NumUsedAccessesAssertFail( - Felt::new(5), + x, 4, - Felt::new(5) - )) + y + )) if x == Felt::new(5) && y == Felt::new(5) ); } @@ -642,11 +643,11 @@ mod tests { //Create hint_data let ids_data = ids_data!["n_used_accesses"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::Internal(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((1, 0)) - ))) + x + ))) if x == MaybeRelocatable::from((1, 0)) ); } @@ -658,7 +659,7 @@ mod tests { //Store scope variables let mut exec_scopes = scope![("keys", Vec::::new())]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Ok(()) ); @@ -672,7 +673,7 @@ mod tests { //Store scope variables let mut exec_scopes = scope![("keys", vec![Felt::new(3)])]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), Err(HintError::KeysNotEmpty) ); @@ -684,9 +685,9 @@ mod tests { //Create vm let mut vm = vm!(); //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, HashMap::new(), hint_code), - Err(HintError::VariableNotInScopeError(String::from("keys"))) + Err(HintError::VariableNotInScopeError(x)) if x == *String::from("keys") ); } @@ -703,7 +704,7 @@ mod tests { //Create hint_data let ids_data = ids_data!["next_key"]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check the value of ids.next_key check_memory![vm.memory, ((1, 0), 3)]; //Check local variables @@ -725,7 +726,7 @@ mod tests { //Create hint_data let ids_data = ids_data!["next_key"]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::EmptyKeys) ); @@ -761,7 +762,7 @@ mod tests { ]; let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check scope variables check_scope!( &exec_scopes, @@ -814,7 +815,7 @@ mod tests { ]; let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check scope variables check_scope!( &exec_scopes, @@ -867,7 +868,7 @@ mod tests { "n_accesses" ]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check scope variables check_scope!( &exec_scopes, @@ -915,12 +916,12 @@ mod tests { "n_accesses" ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(HintError::SquashDictMaxSizeExceeded( - Felt::one(), - Felt::new(2) - )) + x, + y + )) if x == Felt::one() && y == Felt::new(2) ); } @@ -953,7 +954,7 @@ mod tests { "n_accesses" ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::PtrDiffNotDivisibleByDictAccessSize) ); @@ -993,11 +994,11 @@ mod tests { "n_accesses" ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::NAccessesTooBig(felt_str!( + Err(HintError::NAccessesTooBig(x)) if x == felt_str!( "3618502761706184546546682988428055018603476541694452277432519575032261771265" - ))) + ) ); } @@ -1043,7 +1044,7 @@ mod tests { ]; let mut exec_scopes = ExecutionScopes::new(); //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(())); //Check scope variables check_scope!(&exec_scopes, [("access_indices", HashMap::from([( felt_str!("3618502761706184546546682988428055018603476541694452277432519575032261771265"), diff --git a/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/src/hint_processor/builtin_hint_processor/uint256_utils.rs index c2fe43d626..9dc14fcb4e 100644 --- a/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -238,6 +238,7 @@ mod tests { vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; use felt::felt_str; use std::any::Any; @@ -257,7 +258,7 @@ mod tests { ((1, 7), ("340282366920938463463374607431768211456", 10)) ]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts check_memory![&vm.memory, ((1, 12), 0), ((1, 13), 1)]; } @@ -280,15 +281,17 @@ mod tests { ((1, 12), 2) ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 12)), - MaybeRelocatable::from(Felt::new(2)), - MaybeRelocatable::from(Felt::zero()) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 12)) && + y == MaybeRelocatable::from(Felt::new(2)) && + z == MaybeRelocatable::from(Felt::zero()) ); } @@ -303,7 +306,7 @@ mod tests { //Insert ids.a into memory vm.memory = memory![((1, 7), ("850981239023189021389081239089023", 10))]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts //ids.low, ids.high check_memory![ @@ -324,7 +327,7 @@ mod tests { //Insert ids.a into memory vm.memory = memory![((1, 7), ("400066369019890261321163226850167045262", 10))]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts //ids.low, ids.high @@ -349,15 +352,17 @@ mod tests { ((1, 10), 0) ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 10)), - MaybeRelocatable::from(Felt::zero()), - MaybeRelocatable::from(felt_str!("7249717543555297151")) + x, + y, + z ) - ))) + ))) if x == MaybeRelocatable::from((1, 10)) && + y == MaybeRelocatable::from(Felt::zero()) && + z == MaybeRelocatable::from(felt_str!("7249717543555297151")) ); } @@ -371,7 +376,7 @@ mod tests { let ids_data = non_continuous_ids_data![("n", -5), ("root", 0)]; vm.memory = memory![((1, 0), 17), ((1, 1), 7)]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts //ids.root.low, ids.root.high check_memory![&vm.memory, ((1, 5), 48805497317890012913_u128), ((1, 6), 0)]; @@ -390,11 +395,11 @@ mod tests { ((1, 1), ("340282366920938463463374607431768211458", 10)) ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::AssertionFailed(String::from( + Err(HintError::AssertionFailed(x)) if x == *String::from( "assert 0 <= 340282366920938463463374607431768211456 < 2 ** 128" - ))) + ) ); } @@ -409,15 +414,17 @@ mod tests { //Insert ids.n.low into memory vm.memory = memory![((1, 0), 17), ((1, 1), 7), ((1, 5), 1)]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 5)), - MaybeRelocatable::from(Felt::one()), - MaybeRelocatable::from(felt_str!("48805497317890012913")), + x, + y, + z, ) - ))) + ))) if x == MaybeRelocatable::from((1, 5)) && + y == MaybeRelocatable::from(Felt::one()) && + z == MaybeRelocatable::from(felt_str!("48805497317890012913")) ); } @@ -438,7 +445,7 @@ mod tests { ) )]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory insert //memory[ap] = 1 if 0 <= (ids.a.high % PRIME) < 2 ** 127 else 0 check_memory![&vm.memory, ((1, 5), 1)]; @@ -461,7 +468,7 @@ mod tests { ) )]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory insert //memory[ap] = 1 if 0 <= (ids.a.high % PRIME) < 2 ** 127 else 0 check_memory![&vm.memory, ((1, 5), 0)]; @@ -477,15 +484,17 @@ mod tests { let ids_data = non_continuous_ids_data![("a", -4)]; vm.memory = memory![((1, 1), 1), ((1, 5), 55)]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 5)), - MaybeRelocatable::from(Felt::new(55)), - MaybeRelocatable::from(Felt::one()), + x, + y, + z, ) - ))) + ))) if x == MaybeRelocatable::from((1, 5)) && + y == MaybeRelocatable::from(Felt::new(55)) && + z == MaybeRelocatable::from(Felt::one()) ); } @@ -501,7 +510,7 @@ mod tests { //Insert ids into memory vm.memory = memory![((1, 4), 89), ((1, 5), 72), ((1, 6), 3), ((1, 7), 7)]; //Execute the hint - assert_eq!(run_hint!(vm, ids_data, hint_code), Ok(())); + assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(())); //Check hint memory inserts //ids.quotient.low, ids.quotient.high, ids.remainder.low, ids.remainder.high check_memory![ @@ -531,15 +540,17 @@ mod tests { ((1, 10), 0) ]; //Execute the hint - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::Internal(VirtualMachineError::MemoryError( MemoryError::InconsistentMemory( - MaybeRelocatable::from((1, 10)), - MaybeRelocatable::from(Felt::zero()), - MaybeRelocatable::from(Felt::new(10)), + x, + y, + z, ) - ))) + ))) if x == MaybeRelocatable::from((1, 10)) && + y == MaybeRelocatable::from(Felt::zero()) && + z == MaybeRelocatable::from(Felt::new(10)) ); } } diff --git a/src/hint_processor/builtin_hint_processor/usort.rs b/src/hint_processor/builtin_hint_processor/usort.rs index 98f1011aec..557e1a4ee6 100644 --- a/src/hint_processor/builtin_hint_processor/usort.rs +++ b/src/hint_processor/builtin_hint_processor/usort.rs @@ -151,11 +151,12 @@ mod tests { vm_core::VirtualMachine, vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; #[test] fn usort_with_max_size() { let mut exec_scopes = scope![("usort_max_size", 1_u64)]; - assert_eq!(usort_enter_scope(&mut exec_scopes), Ok(())); + assert_matches!(usort_enter_scope(&mut exec_scopes), Ok(())); } #[test] @@ -167,9 +168,9 @@ mod tests { //Create hint_data let ids_data = ids_data!["input", "input_len"]; let mut exec_scopes = scope![("usort_max_size", 1_u64)]; - assert_eq!( + assert_matches!( run_hint!(vm, ids_data, USORT_BODY, &mut exec_scopes), - Err(HintError::UsortOutOfRange(1, Felt::new(5_i32))) + Err(HintError::UsortOutOfRange(1, x)) if x == Felt::new(5_i32) ); } } diff --git a/src/hint_processor/hint_processor_utils.rs b/src/hint_processor/hint_processor_utils.rs index 2af3de0e20..f2b40624aa 100644 --- a/src/hint_processor/hint_processor_utils.rs +++ b/src/hint_processor/hint_processor_utils.rs @@ -201,6 +201,7 @@ mod tests { errors::memory_errors::MemoryError, vm_core::VirtualMachine, vm_memory::memory::Memory, }, }; + use assert_matches::assert_matches; #[test] fn get_integer_from_reference_with_immediate_value() { @@ -224,9 +225,9 @@ mod tests { let mut hint_ref = HintReference::new(0, 0, false, true); hint_ref.offset1 = OffsetValue::Reference(Register::FP, 2_i32, false); - assert_eq!( + assert_matches!( get_offset_value_reference(&vm, &hint_ref, &ApTracking::new(), &hint_ref.offset1), - Ok(mayberelocatable!(1, 2)) + Ok(x) if x == mayberelocatable!(1, 2) ); } @@ -237,7 +238,7 @@ mod tests { let mut hint_ref = HintReference::new(0, 0, false, true); hint_ref.offset1 = OffsetValue::Reference(Register::FP, -2_i32, false); - assert_eq!( + assert_matches!( get_offset_value_reference(&vm, &hint_ref, &ApTracking::new(), &hint_ref.offset1), Err(HintError::FailedToGetIds) ); @@ -248,13 +249,13 @@ mod tests { let mut vm = vm!(); vm.memory = memory![((1, 0), (2, 0))]; - assert_eq!( + assert_matches!( get_ptr_from_reference( &vm, &HintReference::new(0, 0, false, false), &ApTracking::new() ), - Ok(relocatable!(1, 0)) + Ok(x) if x == relocatable!(1, 0) ); } @@ -263,13 +264,13 @@ mod tests { let mut vm = vm!(); vm.memory = memory![((1, 0), (3, 0))]; - assert_eq!( + assert_matches!( get_ptr_from_reference( &vm, &HintReference::new(0, 0, false, true), &ApTracking::new() ), - Ok(relocatable!(3, 0)) + Ok(x) if x == relocatable!(3, 0) ); } @@ -280,9 +281,9 @@ mod tests { let mut hint_ref = HintReference::new(0, 0, true, false); hint_ref.offset2 = OffsetValue::Value(2); - assert_eq!( + assert_matches!( get_ptr_from_reference(&vm, &hint_ref, &ApTracking::new()), - Ok(relocatable!(4, 2)) + Ok(x) if x == relocatable!(4, 2) ); } @@ -293,7 +294,7 @@ mod tests { let mut hint_reference = HintReference::new(0, 0, false, false); hint_reference.offset1 = OffsetValue::Immediate(Felt::new(2_i32)); - assert_eq!( + assert_matches!( compute_addr_from_reference(&hint_reference, &vm, &ApTracking::new()), Err(HintError::NoRegisterInReference) ); @@ -307,7 +308,7 @@ mod tests { let mut hint_reference = HintReference::new(0, 0, false, false); hint_reference.offset1 = OffsetValue::Reference(Register::FP, -1, true); - assert_eq!( + assert_matches!( compute_addr_from_reference(&hint_reference, &vm, &ApTracking::new()), Err(HintError::FailedToGetIds) ); @@ -320,7 +321,7 @@ mod tests { let mut hint_ap_tracking = ApTracking::new(); hint_ap_tracking.group = 1; - assert_eq!( + assert_matches!( apply_ap_tracking_correction(&relocatable!(1, 0), &ref_ap_tracking, &hint_ap_tracking), Ok(relocatable!(1, 0)) ); @@ -333,7 +334,7 @@ mod tests { let mut hint_ap_tracking = ApTracking::new(); hint_ap_tracking.group = 2; - assert_eq!( + assert_matches!( apply_ap_tracking_correction(&relocatable!(1, 0), &ref_ap_tracking, &hint_ap_tracking), Err(HintError::InvalidTrackingGroup(1, 2)) ); @@ -344,9 +345,9 @@ mod tests { let mut vm = vm!(); vm.memory = memory![((1, 0), (0, 0))]; let hint_ref = HintReference::new_simple(0); - assert_eq!( + assert_matches!( get_maybe_relocatable_from_reference(&vm, &hint_ref, &ApTracking::new()), - Ok(mayberelocatable!(0, 0)) + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -355,7 +356,7 @@ mod tests { let mut vm = vm!(); vm.memory = Memory::new(); let hint_ref = HintReference::new_simple(0); - assert_eq!( + assert_matches!( get_maybe_relocatable_from_reference(&vm, &hint_ref, &ApTracking::new()), Err(HintError::FailedToGetIds) ); diff --git a/src/math_utils.rs b/src/math_utils.rs index 498dab9e2a..942021f5ce 100644 --- a/src/math_utils.rs +++ b/src/math_utils.rs @@ -161,6 +161,7 @@ pub fn ec_double_slope(point: &(BigInt, BigInt), alpha: &BigInt, prime: &BigInt) mod tests { use super::*; use crate::utils::test_utils::*; + use assert_matches::assert_matches; use num_traits::Num; #[test] @@ -227,37 +228,37 @@ mod tests { fn compute_safe_div() { let x = Felt::new(26); let y = Felt::new(13); - assert_eq!(safe_div(&x, &y), Ok(Felt::new(2))); + assert_matches!(safe_div(&x, &y), Ok(i) if i == Felt::new(2)); } #[test] fn compute_safe_div_non_divisor() { let x = Felt::new(25); let y = Felt::new(4); - assert_eq!( - safe_div(&x, &y), + let result = safe_div(&x, &y); + assert_matches!( + result, Err(VirtualMachineError::SafeDivFail( - Felt::new(25), - Felt::new(4) - )) - ); + i, j + )) if i == Felt::new(25) && j == Felt::new(4)); } #[test] fn compute_safe_div_by_zero() { let x = Felt::new(25); let y = Felt::zero(); - assert_eq!(safe_div(&x, &y), Err(VirtualMachineError::DividedByZero)); + let result = safe_div(&x, &y); + assert_matches!(result, Err(VirtualMachineError::DividedByZero)); } #[test] fn compute_safe_div_usize() { - assert_eq!(safe_div_usize(26, 13), Ok(2)); + assert_matches!(safe_div_usize(26, 13), Ok(2)); } #[test] fn compute_safe_div_usize_non_divisor() { - assert_eq!( + assert_matches!( safe_div_usize(25, 4), Err(VirtualMachineError::SafeDivFailUsize(25, 4)) ); @@ -265,7 +266,7 @@ mod tests { #[test] fn compute_safe_div_usize_by_zero() { - assert_eq!( + assert_matches!( safe_div_usize(25, 0), Err(VirtualMachineError::DividedByZero) ); @@ -530,13 +531,13 @@ mod tests { #[test] fn calculate_isqrt_a() { let n = biguint!(81); - assert_eq!(isqrt(&n), Ok(biguint!(9))); + assert_matches!(isqrt(&n), Ok(x) if x == biguint!(9)); } #[test] fn calculate_isqrt_b() { let n = biguint_str!("4573659632505831259480"); - assert_eq!(isqrt(&n.pow(2_u32)), Ok(n)); + assert_matches!(isqrt(&n.pow(2_u32)), Ok(num) if num == n); } #[test] @@ -544,20 +545,20 @@ mod tests { let n = biguint_str!( "3618502788666131213697322783095070105623107215331596699973092056135872020481" ); - assert_eq!(isqrt(&n.pow(2_u32)), Ok(n)); + assert_matches!(isqrt(&n.pow(2_u32)), Ok(inner) if inner == n); } #[test] fn calculate_isqrt_zero() { let n = BigUint::zero(); - assert_eq!(isqrt(&n), Ok(BigUint::zero())); + assert_matches!(isqrt(&n), Ok(inner) if inner.is_zero()); } #[test] fn safe_div_bigint_by_zero() { let x = BigInt::one(); let y = BigInt::zero(); - assert_eq!( + assert_matches!( safe_div_bigint(&x, &y), Err(VirtualMachineError::DividedByZero) ) diff --git a/src/serde/deserialize_program.rs b/src/serde/deserialize_program.rs index 4b567b32da..84a8c799ef 100644 --- a/src/serde/deserialize_program.rs +++ b/src/serde/deserialize_program.rs @@ -371,6 +371,7 @@ pub fn deserialize_program( #[cfg(test)] mod tests { use super::*; + use assert_matches::assert_matches; use felt::felt_str; use num_traits::Zero; use std::{fs::File, io::BufReader}; @@ -705,10 +706,10 @@ mod tests { let deserialization_result = deserialize_program(reader, Some("missing_function")); assert!(deserialization_result.is_err()); - assert!(matches!( + assert_matches!( deserialization_result, Err(ProgramError::EntrypointNotFound(_)) - )); + ); } #[test] diff --git a/src/types/exec_scope.rs b/src/types/exec_scope.rs index 6506b405bd..1721d02f17 100644 --- a/src/types/exec_scope.rs +++ b/src/types/exec_scope.rs @@ -186,6 +186,7 @@ impl Default for ExecutionScopes { #[cfg(test)] mod tests { use super::*; + use assert_matches::assert_matches; use felt::Felt; use num_traits::One; @@ -375,13 +376,16 @@ mod tests { scopes.insert_box("list_u64", list_u64); - assert_eq!(scopes.get_list::("list_u64"), Ok(vec![20_u64, 18_u64])); + assert_matches!( + scopes.get_list::("list_u64"), + Ok(x) if x == vec![20_u64, 18_u64] + ); - assert_eq!( + assert_matches!( scopes.get_list::("no_variable"), Err(HintError::VariableNotInScopeError( - "no_variable".to_string() - )) + x + )) if x == *"no_variable".to_string() ); } @@ -393,20 +397,20 @@ mod tests { scopes.assign_or_update_variable("u64", u64); - assert_eq!(scopes.get_ref::("u64"), Ok(&9_u64)); - assert_eq!(scopes.get_mut_ref::("u64"), Ok(&mut 9_u64)); + assert_matches!(scopes.get_ref::("u64"), Ok(&9_u64)); + assert_matches!(scopes.get_mut_ref::("u64"), Ok(&mut 9_u64)); - assert_eq!( + assert_matches!( scopes.get_mut_ref::("no_variable"), Err(HintError::VariableNotInScopeError( - "no_variable".to_string() - )) + x + )) if x == *"no_variable".to_string() ); - assert_eq!( + assert_matches!( scopes.get_ref::("no_variable"), Err(HintError::VariableNotInScopeError( - "no_variable".to_string() - )) + x + )) if x == *"no_variable".to_string() ); } @@ -417,7 +421,10 @@ mod tests { let mut scopes = ExecutionScopes::new(); scopes.assign_or_update_variable("bigint", bigint); - assert_eq!(scopes.get_mut_ref::("bigint"), Ok(&mut Felt::new(12))); + assert_matches!( + scopes.get_mut_ref::("bigint"), + Ok(x) if x == &mut Felt::new(12) + ); } #[test] diff --git a/src/types/relocatable.rs b/src/types/relocatable.rs index 65811e553e..731481afca 100644 --- a/src/types/relocatable.rs +++ b/src/types/relocatable.rs @@ -374,6 +374,7 @@ pub fn relocate_address( mod tests { use super::*; use crate::{relocatable, utils::test_utils::mayberelocatable}; + use assert_matches::assert_matches; use felt::felt_str; use num_traits::{One, Zero}; @@ -381,44 +382,55 @@ mod tests { fn add_bigint_to_int() { let addr = MaybeRelocatable::from(Felt::new(7i32)); let added_addr = addr.add_int(&Felt::new(2i32)); - assert_eq!(Ok(MaybeRelocatable::Int(Felt::new(9i32))), added_addr); + assert_matches!( + added_addr, + Ok(MaybeRelocatable::Int(num)) if num == Felt::new(9) + ); } #[test] fn add_usize_to_int() { let addr = MaybeRelocatable::from(Felt::new(7_i32)); let added_addr = addr.add_usize(2); - assert_eq!(MaybeRelocatable::Int(Felt::new(9_i32)), added_addr); + assert_eq!(MaybeRelocatable::Int(Felt::new(9)), added_addr); } #[test] fn add_bigint_to_relocatable() { let addr = MaybeRelocatable::RelocatableValue(relocatable!(7, 65)); let added_addr = addr.add_int(&Felt::new(2)); - assert_eq!(Ok(MaybeRelocatable::from((7, 67))), added_addr); + assert_matches!( + added_addr, + Ok(MaybeRelocatable::RelocatableValue(Relocatable { + segment_index: 7, + offset: 67 + })) + ); } #[test] fn add_int_mod_offset_exceeded() { let addr = MaybeRelocatable::from((0, 0)); let error = addr.add_int(&felt_str!("18446744073709551616")); - assert_eq!( + assert_matches!( error, - Err(VirtualMachineError::OffsetExceeded(felt_str!( + Err(VirtualMachineError::OffsetExceeded(x)) if x == felt_str!( "18446744073709551616" - ))) - ); - assert_eq!( - error.unwrap_err().to_string(), - "Offset 18446744073709551616 exceeds maximum offset value" + ) ); } #[test] fn add_usize_to_relocatable() { let addr = MaybeRelocatable::RelocatableValue(relocatable!(7, 65)); - let added_addr = addr.add_int(&Felt::new(2)); - assert_eq!(Ok(MaybeRelocatable::from((7, 67))), added_addr); + let added_addr = addr.add_usize(2); + assert_matches!( + added_addr, + MaybeRelocatable::RelocatableValue(Relocatable { + segment_index: 7, + offset: 67 + }) + ); } #[test] @@ -428,18 +440,24 @@ mod tests { 16 )); let added_addr = addr.add_int(&Felt::one()); - assert_eq!(Ok(MaybeRelocatable::Int(Felt::new(4_i32))), added_addr); + assert_matches!( + added_addr, + Ok(MaybeRelocatable::Int(num)) if num == Felt::new(4) + ); } #[test] fn add_bigint_to_relocatable_prime() { - let addr = MaybeRelocatable::RelocatableValue(relocatable!(1, 9)); + let addr = MaybeRelocatable::from((1, 9)); let added_addr = addr.add_int(&felt_str!( "3618502788666131213697322783095070105623107215331596699973092056135872020481" )); - assert_eq!( - Ok(MaybeRelocatable::RelocatableValue(relocatable!(1, 9))), - added_addr + assert_matches!( + added_addr, + Ok(MaybeRelocatable::RelocatableValue(Relocatable { + segment_index: 1, + offset: 9 + })) ); } @@ -450,7 +468,10 @@ mod tests { )); let addr_b = &MaybeRelocatable::from(Felt::new(17_i32)); let added_addr = addr_a.add(addr_b); - assert_eq!(Ok(MaybeRelocatable::from(Felt::new(24_i32))), added_addr); + assert_matches!( + added_addr, + Ok(MaybeRelocatable::Int(num)) if num == Felt::new(24) + ); } #[test] @@ -458,7 +479,7 @@ mod tests { let addr_a = &MaybeRelocatable::from((7, 5)); let addr_b = &MaybeRelocatable::RelocatableValue(relocatable!(7, 10)); let error = addr_a.add(addr_b); - assert_eq!(error, Err(VirtualMachineError::RelocatableAdd)); + assert_matches!(error, Err(VirtualMachineError::RelocatableAdd)); } #[test] @@ -466,9 +487,12 @@ mod tests { let addr_a = &MaybeRelocatable::from((7, 7)); let addr_b = &MaybeRelocatable::from(Felt::new(10)); let added_addr = addr_a.add(addr_b); - assert_eq!( - Ok(MaybeRelocatable::RelocatableValue(relocatable!(7, 17))), - added_addr + assert_matches!( + added_addr, + Ok(MaybeRelocatable::RelocatableValue(Relocatable { + segment_index: 7, + offset: 17 + })) ); } @@ -477,9 +501,12 @@ mod tests { let addr_a = &MaybeRelocatable::from(Felt::new(10_i32)); let addr_b = &MaybeRelocatable::RelocatableValue(relocatable!(7, 7)); let added_addr = addr_a.add(addr_b); - assert_eq!( - Ok(MaybeRelocatable::RelocatableValue(relocatable!(7, 17))), - added_addr + assert_matches!( + added_addr, + Ok(MaybeRelocatable::RelocatableValue(Relocatable { + segment_index: 7, + offset: 17 + })) ); } @@ -491,9 +518,12 @@ mod tests { 16 )); let added_addr = addr_a.add(addr_b); - assert_eq!( - Ok(MaybeRelocatable::RelocatableValue(relocatable!(7, 14))), - added_addr + assert_matches!( + added_addr, + Ok(MaybeRelocatable::RelocatableValue(Relocatable { + segment_index: 7, + offset: 14 + })) ); } @@ -501,11 +531,11 @@ mod tests { fn add_int_rel_int_offset_exceeded() { let addr = MaybeRelocatable::from((0, 0)); let error = addr.add(&MaybeRelocatable::from(felt_str!("18446744073709551616"))); - assert_eq!( + assert_matches!( error, - Err(VirtualMachineError::OffsetExceeded(felt_str!( + Err(VirtualMachineError::OffsetExceeded(x)) if x == felt_str!( "18446744073709551616" - ))) + ) ); } @@ -517,11 +547,11 @@ mod tests { segment_index: 0, }; let error = addr.add(&MaybeRelocatable::RelocatableValue(relocatable)); - assert_eq!( + assert_matches!( error, - Err(VirtualMachineError::OffsetExceeded(felt_str!( + Err(VirtualMachineError::OffsetExceeded(x)) if x == felt_str!( "18446744073709551616" - ))) + ) ); } @@ -530,7 +560,10 @@ mod tests { let addr_a = &MaybeRelocatable::from(Felt::new(7)); let addr_b = &MaybeRelocatable::from(Felt::new(5)); let sub_addr = addr_a.sub(addr_b); - assert_eq!(Ok(MaybeRelocatable::from(Felt::new(2))), sub_addr); + assert_matches!( + sub_addr, + Ok(MaybeRelocatable::Int(num)) if num == Felt::new(2) + ); } #[test] @@ -538,7 +571,10 @@ mod tests { let addr_a = &MaybeRelocatable::from((7, 17)); let addr_b = &MaybeRelocatable::from((7, 7)); let sub_addr = addr_a.sub(addr_b); - assert_eq!(Ok(MaybeRelocatable::from(Felt::new(10))), sub_addr); + assert_matches!( + sub_addr, + Ok(MaybeRelocatable::Int(num)) if num == Felt::new(10) + ); } #[test] @@ -546,7 +582,7 @@ mod tests { let addr_a = &MaybeRelocatable::from((7, 17)); let addr_b = &MaybeRelocatable::from((8, 7)); let error = addr_a.sub(addr_b); - assert_eq!(error, Err(VirtualMachineError::DiffIndexSub)); + assert_matches!(error, Err(VirtualMachineError::DiffIndexSub)); assert_eq!( error.unwrap_err().to_string(), "Can only subtract two relocatable values of the same segment" @@ -558,14 +594,15 @@ mod tests { let addr_a = &MaybeRelocatable::from((7, 17)); let addr_b = &MaybeRelocatable::from(Felt::new(5_i32)); let addr_c = addr_a.sub(addr_b); - assert_eq!(addr_c, Ok(MaybeRelocatable::from((7, 12)))); + assert_matches!(addr_c, Ok(x) if x == MaybeRelocatable::from((7, 12))); } #[test] fn sub_rel_to_int_error() { - let a = &MaybeRelocatable::from(Felt::new(7_i32)); - let b = &MaybeRelocatable::from((7, 10)); - assert_eq!(Err(VirtualMachineError::NotImplemented), a.sub(b)); + assert_matches!( + &MaybeRelocatable::from(Felt::new(7_i32)).sub(&MaybeRelocatable::from((7, 10))), + Err::(VirtualMachineError::NotImplemented) + ); } #[test] @@ -581,7 +618,7 @@ mod tests { fn divmod_bad_type() { let value = &MaybeRelocatable::from(Felt::new(10)); let div = &MaybeRelocatable::from((2, 7)); - assert_eq!(value.divmod(div), Err(VirtualMachineError::NotImplemented)); + assert_matches!(value.divmod(div), Err(VirtualMachineError::NotImplemented)); } #[test] @@ -640,23 +677,23 @@ mod tests { #[test] fn relocatable_add_int() { - assert_eq!( - Ok(relocatable!(1, 6)), - relocatable!(1, 2).add_int(&Felt::new(4)) + assert_matches!( + relocatable!(1, 2).add_int(&Felt::new(4)), + Ok::(x) if x == relocatable!(1, 6) ); - assert_eq!( - Ok(relocatable!(3, 2)), - relocatable!(3, 2).add_int(&Felt::zero()) + assert_matches!( + relocatable!(3, 2).add_int(&Felt::zero()), + Ok::(x) if x == relocatable!(3, 2) ); } #[test] fn relocatable_add_int_mod_offset_exceeded_error() { - assert_eq!( - Err(VirtualMachineError::OffsetExceeded( - Felt::new(usize::MAX) + 1_usize - )), - relocatable!(0, 0).add_int(&(Felt::new(usize::MAX) + 1_usize)) + assert_matches!( + relocatable!(0, 0).add_int(&(Felt::new(usize::MAX) + 1_usize)), + Err::(VirtualMachineError::OffsetExceeded( + x + )) if x == Felt::new(usize::MAX) + 1_usize ); } @@ -689,11 +726,13 @@ mod tests { #[test] fn relocatable_sub_rel_test() { let reloc = relocatable!(7, 6); - - assert_eq!(Ok(1), reloc.sub(&relocatable!(7, 5))); - assert_eq!( - Err(VirtualMachineError::CantSubOffset(6, 9)), - reloc.sub(&relocatable!(7, 9)) + assert_matches!( + reloc.sub(&relocatable!(7, 5)), + Ok::(1) + ); + assert_matches!( + reloc.sub(&relocatable!(7, 9)), + Err::(VirtualMachineError::CantSubOffset(6, 9)) ); } @@ -701,64 +740,65 @@ mod tests { fn sub_rel_different_indexes() { let a = relocatable!(7, 6); let b = relocatable!(8, 6); - - assert_eq!(Err(VirtualMachineError::DiffIndexSub), a.sub(&b)); + assert_matches!( + a.sub(&b), + Err::(VirtualMachineError::DiffIndexSub) + ); } #[test] fn add_maybe_mod_ok() { - assert_eq!( - Ok(relocatable!(1, 2)), - relocatable!(1, 0).add_maybe(&mayberelocatable!(2)) + assert_matches!( + relocatable!(1, 0).add_maybe(&mayberelocatable!(2)), + Ok::(x) if x == relocatable!(1, 2) ); - assert_eq!( - Ok(relocatable!(0, 129)), - relocatable!(0, 29).add_maybe(&mayberelocatable!(100)) + assert_matches!( + relocatable!(0, 29).add_maybe(&mayberelocatable!(100)), + Ok::(x) if x == relocatable!(0, 129) ); - assert_eq!( - Ok(relocatable!(2, 116)), - relocatable!(2, 12).add_maybe(&mayberelocatable!(104)) + assert_matches!( + relocatable!(2, 12).add_maybe(&mayberelocatable!(104)), + Ok::(x) if x == relocatable!(2, 116) ); - - assert_eq!( - Ok(relocatable!(1, 0)), - relocatable!(1, 0).add_maybe(&mayberelocatable!(0)) + assert_matches!( + relocatable!(1, 0).add_maybe(&mayberelocatable!(0)), + Ok::(x) if x == relocatable!(1, 0) ); - assert_eq!( - Ok(relocatable!(1, 73)), - relocatable!(1, 2).add_maybe(&mayberelocatable!(71)) + assert_matches!( + relocatable!(1, 2).add_maybe(&mayberelocatable!(71)), + Ok::(x) if x == relocatable!(1, 73) ); } #[test] fn add_maybe_mod_add_two_relocatable_error() { - assert_eq!( - Err(VirtualMachineError::RelocatableAdd), - relocatable!(1, 0).add_maybe(&mayberelocatable!(1, 2)) + assert_matches!( + relocatable!(1, 0).add_maybe(&mayberelocatable!(1, 2)), + Err::(VirtualMachineError::RelocatableAdd) ); } #[test] fn add_maybe_mod_offset_exceeded_error() { - assert_eq!( - Err(VirtualMachineError::OffsetExceeded( - Felt::new(usize::MAX) + 1_usize - )), - relocatable!(1, 0).add_maybe(&mayberelocatable!(usize::MAX as i128 + 1),) + assert_matches!( + relocatable!(1, 0).add_maybe(&mayberelocatable!(usize::MAX as i128 + 1)), + Err::(VirtualMachineError::OffsetExceeded( + x + )) if x == Felt::new(usize::MAX) + 1_usize ); } #[test] fn get_relocatable_test() { - assert_eq!( - Ok(relocatable!(1, 2)), - mayberelocatable!(1, 2).get_relocatable() + assert_matches!( + mayberelocatable!(1, 2).get_relocatable(), + Ok::(x) if x == relocatable!(1, 2) ); - assert_eq!( - Err(VirtualMachineError::ExpectedRelocatable(mayberelocatable!( - 3 - ))), - mayberelocatable!(3).get_relocatable() + assert_matches!( + mayberelocatable!(3).get_relocatable(), + Err::(VirtualMachineError::ExpectedRelocatable( + x + )) if x == mayberelocatable!(3) ) } diff --git a/src/utils.rs b/src/utils.rs index 6796ddbd4a..35b2005a98 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,8 +4,8 @@ use crate::types::relocatable::Relocatable; macro_rules! relocatable { ($val1 : expr, $val2 : expr) => { Relocatable { - segment_index: ($val1), - offset: ($val2), + segment_index: $val1, + offset: $val2, } }; } @@ -398,7 +398,7 @@ pub mod test_utils { macro_rules! check_dictionary { ( $exec_scopes: expr, $tracker_num:expr, $( ($key:expr, $val:expr )),* ) => { $( - assert_eq!( + assert_matches::assert_matches!( $exec_scopes .get_dict_manager() .unwrap() @@ -407,9 +407,9 @@ pub mod test_utils { .get_mut(&$tracker_num) .unwrap() .get_value(&MaybeRelocatable::from($key)), - Ok(&MaybeRelocatable::from($val)) - ); - )* + Ok(x) if x == &MaybeRelocatable::from($val) + )); + * }; } pub(crate) use check_dictionary; @@ -671,7 +671,7 @@ mod test { let hint_code = "memory[ap] = segments.add()"; let mut vm = vm!(); add_segments!(vm, 1); - assert_eq!(run_hint!(vm, HashMap::new(), hint_code), Ok(())); + assert_matches::assert_matches!(run_hint!(vm, HashMap::new(), hint_code), Ok(())); //A segment is added assert_eq!(vm.segments.num_segments, 2); } @@ -803,9 +803,9 @@ mod test { dict_manager.trackers.insert(2, tracker); let mut exec_scopes = ExecutionScopes::new(); dict_manager!(exec_scopes, 2); - assert_eq!( + assert_matches::assert_matches!( exec_scopes.get_dict_manager(), - Ok(Rc::new(RefCell::new(dict_manager))) + Ok(x) if x == Rc::new(RefCell::new(dict_manager)) ); } @@ -820,9 +820,9 @@ mod test { dict_manager.trackers.insert(2, tracker); let mut exec_scopes = ExecutionScopes::new(); dict_manager_default!(exec_scopes, 2, 17); - assert_eq!( + assert_matches::assert_matches!( exec_scopes.get_dict_manager(), - Ok(Rc::new(RefCell::new(dict_manager))) + Ok(x) if x == Rc::new(RefCell::new(dict_manager)) ); } diff --git a/src/vm/context/run_context.rs b/src/vm/context/run_context.rs index 91b2a78934..ef6955814b 100644 --- a/src/vm/context/run_context.rs +++ b/src/vm/context/run_context.rs @@ -110,6 +110,7 @@ mod tests { use crate::types::instruction::{ApUpdate, FpUpdate, Opcode, PcUpdate, Res}; use crate::utils::test_utils::mayberelocatable; use crate::vm::errors::memory_errors::MemoryError; + use assert_matches::assert_matches; use felt::Felt; #[test] @@ -134,9 +135,9 @@ mod tests { ap: 5, fp: 6, }; - assert_eq!( - Ok(relocatable!(1, 6)), - run_context.compute_dst_addr(&instruction) + assert_matches!( + run_context.compute_dst_addr(&instruction), + Ok::(x) if x == relocatable!(1, 6) ); } @@ -162,9 +163,10 @@ mod tests { ap: 5, fp: 6, }; - assert_eq!( - Ok(relocatable!(1, 7)), - run_context.compute_dst_addr(&instruction) + + assert_matches!( + run_context.compute_dst_addr(&instruction), + Ok::(x) if x == relocatable!(1, 7) ); } @@ -190,9 +192,9 @@ mod tests { ap: 5, fp: 6, }; - assert_eq!( - Ok(relocatable!(1, 7)), - run_context.compute_op0_addr(&instruction) + assert_matches!( + run_context.compute_op0_addr(&instruction), + Ok::(x) if x == relocatable!(1, 7) ); } @@ -218,9 +220,9 @@ mod tests { ap: 5, fp: 6, }; - assert_eq!( - Ok(relocatable!(1, 8)), - run_context.compute_op0_addr(&instruction) + assert_matches!( + run_context.compute_op0_addr(&instruction), + Ok::(x) if x == relocatable!(1, 8) ); } @@ -246,9 +248,9 @@ mod tests { ap: 5, fp: 6, }; - assert_eq!( - Ok(relocatable!(1, 9)), - run_context.compute_op1_addr(&instruction, None) + assert_matches!( + run_context.compute_op1_addr(&instruction, None), + Ok::(x) if x == relocatable!(1, 9) ); } @@ -274,9 +276,9 @@ mod tests { ap: 5, fp: 6, }; - assert_eq!( - Ok(relocatable!(1, 8)), - run_context.compute_op1_addr(&instruction, None) + assert_matches!( + run_context.compute_op1_addr(&instruction, None), + Ok::(x) if x == relocatable!(1, 8) ); } @@ -302,9 +304,9 @@ mod tests { ap: 5, fp: 6, }; - assert_eq!( - Ok(relocatable!(0, 5)), - run_context.compute_op1_addr(&instruction, None) + assert_matches!( + run_context.compute_op1_addr(&instruction, None), + Ok::(x) if x == relocatable!(0, 5) ); } @@ -332,7 +334,7 @@ mod tests { }; let error = run_context.compute_op1_addr(&instruction, None); - assert_eq!(error, Err(VirtualMachineError::ImmShouldBe1)); + assert_matches!(error, Err(VirtualMachineError::ImmShouldBe1)); assert_eq!( error.unwrap_err().to_string(), "In immediate mode, off2 should be 1" @@ -363,9 +365,9 @@ mod tests { }; let op0 = mayberelocatable!(1, 7); - assert_eq!( - Ok(relocatable!(1, 8)), - run_context.compute_op1_addr(&instruction, Some(&op0)) + assert_matches!( + run_context.compute_op1_addr(&instruction, Some(&op0)), + Ok::(x) if x == relocatable!(1, 8) ); } @@ -393,11 +395,11 @@ mod tests { }; let op0 = MaybeRelocatable::from(Felt::new(7)); - assert_eq!( - Err(VirtualMachineError::MemoryError( + assert_matches!( + run_context.compute_op1_addr(&instruction, Some(&op0)), + Err::(VirtualMachineError::MemoryError( MemoryError::AddressNotRelocatable - )), - run_context.compute_op1_addr(&instruction, Some(&op0)) + )) ); } @@ -425,7 +427,7 @@ mod tests { }; let error = run_context.compute_op1_addr(&instruction, None); - assert_eq!(error, Err(VirtualMachineError::UnknownOp0)); + assert_matches!(error, Err(VirtualMachineError::UnknownOp0)); assert_eq!( error.unwrap_err().to_string(), "op0 must be known in double dereference" diff --git a/src/vm/decoding/decoder.rs b/src/vm/decoding/decoder.rs index 1f7188ac2f..106e7e7a0f 100644 --- a/src/vm/decoding/decoder.rs +++ b/src/vm/decoding/decoder.rs @@ -146,11 +146,12 @@ fn decode_offset(offset: i64) -> isize { #[cfg(test)] mod decoder_test { use super::*; + use assert_matches::assert_matches; #[test] fn invalid_op1_reg() { let error = decode_instruction(0x294F800080008000, None); - assert_eq!(error, Err(VirtualMachineError::InvalidOp1Reg(3))); + assert_matches!(error, Err(VirtualMachineError::InvalidOp1Reg(3))); assert_eq!( error.unwrap_err().to_string(), "Invalid op1_register value: 3" @@ -160,34 +161,34 @@ mod decoder_test { #[test] fn invalid_pc_update() { let error = decode_instruction(0x29A8800080008000, None); - assert_eq!(error, Err(VirtualMachineError::InvalidPcUpdate(3))); + assert_matches!(error, Err(VirtualMachineError::InvalidPcUpdate(3))); assert_eq!(error.unwrap_err().to_string(), "Invalid pc_update value: 3") } #[test] fn invalid_res_logic() { let error = decode_instruction(0x2968800080008000, None); - assert_eq!(error, Err(VirtualMachineError::InvalidRes(3))); + assert_matches!(error, Err(VirtualMachineError::InvalidRes(3))); assert_eq!(error.unwrap_err().to_string(), "Invalid res value: 3") } #[test] fn invalid_opcode() { let error = decode_instruction(0x3948800080008000, None); - assert_eq!(error, Err(VirtualMachineError::InvalidOpcode(3))); + assert_matches!(error, Err(VirtualMachineError::InvalidOpcode(3))); assert_eq!(error.unwrap_err().to_string(), "Invalid opcode value: 3") } #[test] fn invalid_ap_update() { let error = decode_instruction(0x2D48800080008000, None); - assert_eq!(error, Err(VirtualMachineError::InvalidApUpdate(3))); + assert_matches!(error, Err(VirtualMachineError::InvalidApUpdate(3))); assert_eq!(error.unwrap_err().to_string(), "Invalid ap_update value: 3") } #[test] fn decode_no_immediate_given() { - assert_eq!( + assert_matches!( decode_instruction(0x14A7800080008000, None), Err(VirtualMachineError::NoImm) ); @@ -201,14 +202,14 @@ mod decoder_test { // 0 0 0 1 0 1 0 0 1 0 1 0 0 1 1 1 // 0001 0100 1010 0111 = 0x14A7; offx = 0 let inst = decode_instruction(0x14A7800080008000, Some(&Felt::new(7))).unwrap(); - assert!(matches!(inst.dst_register, Register::FP)); - assert!(matches!(inst.op0_register, Register::FP)); - assert!(matches!(inst.op1_addr, Op1Addr::Imm)); - assert!(matches!(inst.res, Res::Add)); - assert!(matches!(inst.pc_update, PcUpdate::Jump)); - assert!(matches!(inst.ap_update, ApUpdate::Add)); - assert!(matches!(inst.opcode, Opcode::Call)); - assert!(matches!(inst.fp_update, FpUpdate::APPlus2)); + assert_matches!(inst.dst_register, Register::FP); + assert_matches!(inst.op0_register, Register::FP); + assert_matches!(inst.op1_addr, Op1Addr::Imm); + assert_matches!(inst.res, Res::Add); + assert_matches!(inst.pc_update, PcUpdate::Jump); + assert_matches!(inst.ap_update, ApUpdate::Add); + assert_matches!(inst.opcode, Opcode::Call); + assert_matches!(inst.fp_update, FpUpdate::APPlus2); } #[test] @@ -219,14 +220,14 @@ mod decoder_test { // 0 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0 // 0010 1001 0100 1000 = 0x2948; offx = 0 let inst = decode_instruction(0x2948800080008000, None).unwrap(); - assert!(matches!(inst.dst_register, Register::AP)); - assert!(matches!(inst.op0_register, Register::AP)); - assert!(matches!(inst.op1_addr, Op1Addr::FP)); - assert!(matches!(inst.res, Res::Mul)); - assert!(matches!(inst.pc_update, PcUpdate::JumpRel)); - assert!(matches!(inst.ap_update, ApUpdate::Add1)); - assert!(matches!(inst.opcode, Opcode::Ret)); - assert!(matches!(inst.fp_update, FpUpdate::Dst)); + assert_matches!(inst.dst_register, Register::AP); + assert_matches!(inst.op0_register, Register::AP); + assert_matches!(inst.op1_addr, Op1Addr::FP); + assert_matches!(inst.res, Res::Mul); + assert_matches!(inst.pc_update, PcUpdate::JumpRel); + assert_matches!(inst.ap_update, ApUpdate::Add1); + assert_matches!(inst.opcode, Opcode::Ret); + assert_matches!(inst.fp_update, FpUpdate::Dst); } #[test] @@ -237,14 +238,14 @@ mod decoder_test { // 0 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 // 0100 1010 0101 0000 = 0x4A50; offx = 0 let inst = decode_instruction(0x4A50800080008000, None).unwrap(); - assert!(matches!(inst.dst_register, Register::AP)); - assert!(matches!(inst.op0_register, Register::AP)); - assert!(matches!(inst.op1_addr, Op1Addr::AP)); - assert!(matches!(inst.res, Res::Mul)); - assert!(matches!(inst.pc_update, PcUpdate::Jnz)); - assert!(matches!(inst.ap_update, ApUpdate::Add1)); - assert!(matches!(inst.opcode, Opcode::AssertEq)); - assert!(matches!(inst.fp_update, FpUpdate::Regular)); + assert_matches!(inst.dst_register, Register::AP); + assert_matches!(inst.op0_register, Register::AP); + assert_matches!(inst.op1_addr, Op1Addr::AP); + assert_matches!(inst.res, Res::Mul); + assert_matches!(inst.pc_update, PcUpdate::Jnz); + assert_matches!(inst.ap_update, ApUpdate::Add1); + assert_matches!(inst.opcode, Opcode::AssertEq); + assert_matches!(inst.fp_update, FpUpdate::Regular); } #[test] @@ -255,14 +256,14 @@ mod decoder_test { // 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 // 0100 0010 0000 0000 = 0x4200; offx = 0 let inst = decode_instruction(0x4200800080008000, None).unwrap(); - assert!(matches!(inst.dst_register, Register::AP)); - assert!(matches!(inst.op0_register, Register::AP)); - assert!(matches!(inst.op1_addr, Op1Addr::Op0)); - assert!(matches!(inst.res, Res::Unconstrained)); - assert!(matches!(inst.pc_update, PcUpdate::Jnz)); - assert!(matches!(inst.ap_update, ApUpdate::Regular)); - assert!(matches!(inst.opcode, Opcode::AssertEq)); - assert!(matches!(inst.fp_update, FpUpdate::Regular)); + assert_matches!(inst.dst_register, Register::AP); + assert_matches!(inst.op0_register, Register::AP); + assert_matches!(inst.op1_addr, Op1Addr::Op0); + assert_matches!(inst.res, Res::Unconstrained); + assert_matches!(inst.pc_update, PcUpdate::Jnz); + assert_matches!(inst.ap_update, ApUpdate::Regular); + assert_matches!(inst.opcode, Opcode::AssertEq); + assert_matches!(inst.fp_update, FpUpdate::Regular); } #[test] @@ -273,14 +274,14 @@ mod decoder_test { // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 // 0000 0000 0000 0000 = 0x0000; offx = 0 let inst = decode_instruction(0x0000800080008000, None).unwrap(); - assert!(matches!(inst.dst_register, Register::AP)); - assert!(matches!(inst.op0_register, Register::AP)); - assert!(matches!(inst.op1_addr, Op1Addr::Op0)); - assert!(matches!(inst.res, Res::Op1)); - assert!(matches!(inst.pc_update, PcUpdate::Regular)); - assert!(matches!(inst.ap_update, ApUpdate::Regular)); - assert!(matches!(inst.opcode, Opcode::NOp)); - assert!(matches!(inst.fp_update, FpUpdate::Regular)); + assert_matches!(inst.dst_register, Register::AP); + assert_matches!(inst.op0_register, Register::AP); + assert_matches!(inst.op1_addr, Op1Addr::Op0); + assert_matches!(inst.res, Res::Op1); + assert_matches!(inst.pc_update, PcUpdate::Regular); + assert_matches!(inst.ap_update, ApUpdate::Regular); + assert_matches!(inst.opcode, Opcode::NOp); + assert_matches!(inst.fp_update, FpUpdate::Regular); } #[test] diff --git a/src/vm/errors/hint_errors.rs b/src/vm/errors/hint_errors.rs index 2bcdfe7f7c..e5e02dcb3b 100644 --- a/src/vm/errors/hint_errors.rs +++ b/src/vm/errors/hint_errors.rs @@ -6,7 +6,7 @@ use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use super::{exec_scope_errors::ExecScopeError, vm_errors::VirtualMachineError}; -#[derive(Debug, PartialEq, Error)] +#[derive(Debug, Error)] pub enum HintError { #[error("HintProcessor failed retrieve the compiled data necessary for hint execution")] WrongHintData, diff --git a/src/vm/errors/vm_errors.rs b/src/vm/errors/vm_errors.rs index a25bb93fbf..a0a76a6efd 100644 --- a/src/vm/errors/vm_errors.rs +++ b/src/vm/errors/vm_errors.rs @@ -7,9 +7,10 @@ use crate::{ }; use felt::Felt; use num_bigint::{BigInt, BigUint}; +use std::error::Error; use thiserror::Error; -#[derive(Debug, PartialEq, Error)] +#[derive(Debug, Error)] pub enum VirtualMachineError { #[error("Instruction should be an int")] InvalidInstructionEncoding, @@ -143,4 +144,6 @@ pub enum VirtualMachineError { NegBuiltinBase, #[error("Security Error: Invalid Memory Value: temporary address not relocated: {0}")] InvalidMemoryValueTemporaryAddress(Relocatable), + #[error(transparent)] + Other(Box), } diff --git a/src/vm/errors/vm_exception.rs b/src/vm/errors/vm_exception.rs index 4849d8410d..7d56819252 100644 --- a/src/vm/errors/vm_exception.rs +++ b/src/vm/errors/vm_exception.rs @@ -18,7 +18,7 @@ use crate::{ }; use super::vm_errors::VirtualMachineError; -#[derive(Debug, PartialEq, Error)] +#[derive(Debug, Error)] pub struct VmException { pub pc: usize, pub inst_location: Option, @@ -275,6 +275,7 @@ impl Location { } #[cfg(test)] mod test { + use assert_matches::assert_matches; use std::collections::HashMap; use std::path::Path; @@ -307,16 +308,15 @@ mod test { let program = program!(instruction_locations = Some(HashMap::from([(pc, instruction_location)])),); let runner = cairo_runner!(program); - let vm_excep = VmException { - pc, - inst_location: Some(location), - inner_exc: VirtualMachineError::NoImm, - error_attr_value: None, - traceback: None, - }; - assert_eq!( + assert_matches!( VmException::from_vm_error(&runner, &vm!(), VirtualMachineError::NoImm,), - vm_excep + VmException { + pc: x, + inst_location: Some(y), + inner_exc: VirtualMachineError::NoImm, + error_attr_value: None, + traceback: None, + } if x == pc && y == location ) } diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index 25530acd7f..e5add1dd87 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -885,64 +885,6 @@ mod tests { ); } - #[test] - fn deduce_memory_cell_ec_op_for_preset_memory_m_over_scalar_limit() { - let memory = memory![ - ( - (3, 0), - ( - "2962412995502985605007699495352191122971573493113767820301112397466445942584", - 10 - ) - ), - ( - (3, 1), - ( - "214950771763870898744428659242275426967582168179217139798831865603966154129", - 10 - ) - ), - ( - (3, 2), - ( - "874739451078007766457464989774322083649278607533249481151382481072868806602", - 10 - ) - ), - ( - (3, 3), - ( - "152666792071518830868575557812948353041420400780739481342941381225525861407", - 10 - ) - ), - //Scalar Limit + 1 - ( - (3, 4), - ( - "3618502788666131213697322783095070105623107215331596699973092056135872020482", - 10 - ) - ), - ( - (3, 5), - ( - "2778063437308421278851140253538604815869848682781135193774472480292420096757", - 10 - ) - ) - ]; - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); - - let _error = builtin.deduce_memory_cell(&Relocatable::from((3, 6)), &memory); - /*assert_eq!( - error, - Err(RunnerError::EcOpBuiltinScalarLimit( - builtin.ec_op_builtin.scalar_limit - )) - );*/ - } - #[test] fn get_memory_segment_addresses() { let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 39f844ac1b..1c3e87096d 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -459,6 +459,7 @@ mod tests { utils::test_utils::*, vm::vm_core::VirtualMachine, }; + use assert_matches::assert_matches; #[test] fn get_memory_accesses_missing_segment_used_sizes() { @@ -1020,7 +1021,7 @@ mod tests { let builtin = BuiltinRunner::Output(OutputBuiltinRunner::new(true)); let vm = vm!(); - assert_eq!(builtin.run_security_checks(&vm), Ok(())); + assert_matches!(builtin.run_security_checks(&vm), Ok(())); } #[test] @@ -1031,7 +1032,7 @@ mod tests { )); let vm = vm!(); // Unused builtin shouldn't fail security checks - assert_eq!(builtin.run_security_checks(&vm), Ok(()),); + assert_matches!(builtin.run_security_checks(&vm), Ok(())); } #[test] @@ -1043,9 +1044,9 @@ mod tests { }); let vm = vm!(); - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::NegBuiltinBase), + Err(VirtualMachineError::NegBuiltinBase) ); } @@ -1059,7 +1060,7 @@ mod tests { vm.memory.data = vec![vec![]]; - assert_eq!(builtin.run_security_checks(&vm), Ok(())); + assert_matches!(builtin.run_security_checks(&vm), Ok(())); } #[test] @@ -1078,9 +1079,11 @@ mod tests { mayberelocatable!(0, 4).into(), ]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCellsWithOffsets("bitwise", vec![0],).into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCellsWithOffsets("bitwise", x) + )) if x == vec![0] ); } @@ -1104,9 +1107,11 @@ mod tests { mayberelocatable!(0, 5).into(), ]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCells("bitwise").into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCells("bitwise") + )) ); } @@ -1123,10 +1128,11 @@ mod tests { mayberelocatable!(0, 4).into(), mayberelocatable!(0, 5).into(), ]]; - - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCellsWithOffsets("hash", vec![0],).into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCellsWithOffsets("hash", x) + )) if x == vec![0] ); } @@ -1140,9 +1146,11 @@ mod tests { vm.memory.data = vec![vec![mayberelocatable!(0, 0).into()]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCells("hash").into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCells("hash") + )) ); } @@ -1163,9 +1171,11 @@ mod tests { mayberelocatable!(22).into(), ]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCells("range_check").into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCells("range_check") + )) ); } @@ -1177,9 +1187,11 @@ mod tests { vm.memory.data = vec![vec![None, mayberelocatable!(0).into()]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCells("range_check").into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCells("range_check") + )) ); } @@ -1193,7 +1205,7 @@ mod tests { vm.memory.data = vec![vec![None, None, None]]; - assert_eq!(builtin.run_security_checks(&vm), Ok(()),); + assert_matches!(builtin.run_security_checks(&vm), Ok(())); } #[test] @@ -1212,7 +1224,7 @@ mod tests { mayberelocatable!(0, 4).into(), ]]; - assert_eq!(builtin.run_security_checks(&vm), Ok(())); + assert_matches!(builtin.run_security_checks(&vm), Ok(())); } #[test] @@ -1225,7 +1237,7 @@ mod tests { // The values stored in memory are not relevant for this test vm.memory.data = vec![vec![]]; - assert_eq!(builtin.run_security_checks(&vm), Ok(()),); + assert_matches!(builtin.run_security_checks(&vm), Ok(())); } #[test] @@ -1238,9 +1250,11 @@ mod tests { // The values stored in memory are not relevant for this test vm.memory.data = vec![vec![mayberelocatable!(0).into()]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCells("ec_op").into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCells("ec_op") + )) ); } @@ -1258,9 +1272,11 @@ mod tests { mayberelocatable!(0).into(), ]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCells("ec_op").into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCells("ec_op") + )) ); } @@ -1280,9 +1296,11 @@ mod tests { mayberelocatable!(0, 6).into(), ]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCellsWithOffsets("ec_op", vec![0],).into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCellsWithOffsets("ec_op", x) + )) if x == vec![0] ); } @@ -1309,9 +1327,11 @@ mod tests { mayberelocatable!(11).into(), ]]; - assert_eq!( + assert_matches!( builtin.run_security_checks(&vm), - Err(MemoryError::MissingMemoryCellsWithOffsets("ec_op", vec![7]).into()), + Err(VirtualMachineError::MemoryError( + MemoryError::MissingMemoryCellsWithOffsets("ec_op", x) + )) if x == vec![7] ); } diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index ef10765ae6..d70a485e19 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -1230,6 +1230,7 @@ mod tests { utils::test_utils::*, vm::{trace::trace_entry::TraceEntry, vm_memory::memory::Memory}, }; + use assert_matches::assert_matches; use felt::felt_str; use num_traits::One; use std::{ @@ -1245,7 +1246,7 @@ mod tests { let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); - assert_eq!(cairo_runner.check_memory_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_memory_usage(&vm), Ok(())); } #[test] @@ -1263,7 +1264,7 @@ mod tests { ("output".to_string(), builtin_runner) }]; vm.segments.segment_used_sizes = Some(vec![4, 12]); - assert_eq!( + assert_matches!( cairo_runner.check_memory_usage(&vm), Err(VirtualMachineError::MemoryError( MemoryError::InsufficientAllocatedCells @@ -1905,7 +1906,7 @@ mod tests { assert_eq!(end, Relocatable::from((3, 0))); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -1986,7 +1987,7 @@ mod tests { let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -2092,7 +2093,7 @@ mod tests { let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -2226,7 +2227,7 @@ mod tests { let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -2455,7 +2456,7 @@ mod tests { cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -2590,7 +2591,7 @@ mod tests { cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -2764,7 +2765,7 @@ mod tests { cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -2847,7 +2848,7 @@ mod tests { .expect("Couldn't initialize the VM."); let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); @@ -2928,13 +2929,13 @@ mod tests { cairo_runner.initialize_vm(&mut vm).unwrap(); // Full takes 10 steps. - assert_eq!( + assert_matches!( cairo_runner.run_for_steps(8, &mut vm, &mut hint_processor), Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_for_steps(8, &mut vm, &mut hint_processor), - Err(VirtualMachineError::EndOfProgram(8 - 2)) + Err(VirtualMachineError::EndOfProgram(x)) if x == 8 - 2 ); } @@ -2994,17 +2995,17 @@ mod tests { cairo_runner.initialize_vm(&mut vm).unwrap(); // Full takes 10 steps. - assert_eq!( + assert_matches!( cairo_runner.run_until_steps(8, &mut vm, &mut hint_processor), Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_until_steps(10, &mut vm, &mut hint_processor), Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_until_steps(11, &mut vm, &mut hint_processor), - Err(VirtualMachineError::EndOfProgram(1)), + Err(VirtualMachineError::EndOfProgram(1)) ); } @@ -3066,53 +3067,53 @@ mod tests { cairo_runner.initialize_vm(&mut vm).unwrap(); // Full takes 10 steps. - assert_eq!( + assert_matches!( cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), Ok(()) ); assert_eq!(vm.current_step, 1); - assert_eq!( + assert_matches!( cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), Ok(()) ); assert_eq!(vm.current_step, 2); - assert_eq!( + assert_matches!( cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), Ok(()) ); assert_eq!(vm.current_step, 4); - assert_eq!( + assert_matches!( cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), Ok(()) ); assert_eq!(vm.current_step, 8); - assert_eq!( + assert_matches!( cairo_runner.run_for_steps(1, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); - assert_eq!( + assert_matches!( cairo_runner.run_until_next_power_of_2(&mut vm, &mut hint_processor), - Err(VirtualMachineError::EndOfProgram(6)), + Err(VirtualMachineError::EndOfProgram(6)) ); assert_eq!(vm.current_step, 10); } @@ -3226,7 +3227,7 @@ mod tests { let vm = vm!(); cairo_runner.layout.diluted_pool_instance_def = None; - assert_eq!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); } /// Test that check_diluted_check_usage() works without builtin runners. @@ -3239,7 +3240,7 @@ mod tests { vm.current_step = 10000; vm.builtin_runners = vec![]; - assert_eq!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); } /// Test that check_diluted_check_usage() fails when there aren't enough @@ -3253,9 +3254,11 @@ mod tests { vm.current_step = 100; vm.builtin_runners = vec![]; - assert_eq!( + assert_matches!( cairo_runner.check_diluted_check_usage(&vm), - Err(MemoryError::InsufficientAllocatedCells.into()), + Err(VirtualMachineError::MemoryError( + MemoryError::InsufficientAllocatedCells + )) ); } @@ -3273,7 +3276,7 @@ mod tests { "bitwise".to_string(), BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(), )]; - assert_eq!(cairo_runner.check_diluted_check_usage(&vm), Ok(()),); + assert_matches!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); } #[test] @@ -3285,9 +3288,11 @@ mod tests { let mut vm = vm!(); cairo_runner.run_ended = true; - assert_eq!( + assert_matches!( cairo_runner.end_run(true, false, &mut vm, &mut hint_processor), - Err(RunnerError::RunAlreadyFinished.into()), + Err(VirtualMachineError::RunnerError( + RunnerError::RunAlreadyFinished + )) ); } @@ -3300,16 +3305,16 @@ mod tests { let mut vm = vm!(); vm.accessed_addresses = Some(Vec::new()); - assert_eq!( + assert_matches!( cairo_runner.end_run(true, false, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); cairo_runner.run_ended = false; cairo_runner.relocated_memory.clear(); - assert_eq!( + assert_matches!( cairo_runner.end_run(true, true, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); assert!(!cairo_runner.run_ended); } @@ -3330,9 +3335,9 @@ mod tests { cairo_runner .run_until_pc(end, &mut vm, &mut hint_processor) .expect("Call to `CairoRunner::run_until_pc()` failed."); - assert_eq!( + assert_matches!( cairo_runner.end_run(false, false, &mut vm, &mut hint_processor), - Ok(()), + Ok(()) ); } @@ -3568,9 +3573,11 @@ mod tests { let cairo_runner = cairo_runner!(program); let vm = vm!(); - assert_eq!( + assert_matches!( cairo_runner.get_perm_range_check_limits(&vm), - Err(TraceError::TraceNotEnabled.into()), + Err(VirtualMachineError::TracerError( + TraceError::TraceNotEnabled + )) ); } @@ -3584,7 +3591,7 @@ mod tests { let mut vm = vm!(); vm.trace = Some(vec![]); - assert_eq!(cairo_runner.get_perm_range_check_limits(&vm), Ok(None)); + assert_matches!(cairo_runner.get_perm_range_check_limits(&vm), Ok(None)); } /// Test that get_perm_range_check_limits() works correctly when there are @@ -3619,9 +3626,9 @@ mod tests { Some(Felt::new(0x8FFF_8000_0750u64).into()), ]]; - assert_eq!( + assert_matches!( cairo_runner.get_perm_range_check_limits(&vm), - Ok(Some((-31440, 16383))), + Ok(Some((-31440, 16383))) ); } @@ -3645,9 +3652,9 @@ mod tests { RangeCheckBuiltinRunner::new(12, 5, true).into(), )]; - assert_eq!( + assert_matches!( cairo_runner.get_perm_range_check_limits(&vm), - Ok(Some((-31440, 1328))), + Ok(Some((-31440, 1328))) ); } @@ -3661,7 +3668,7 @@ mod tests { let mut vm = vm!(); vm.trace = Some(vec![]); - assert_eq!(cairo_runner.check_range_check_usage(&vm), Ok(())); + assert_matches!(cairo_runner.check_range_check_usage(&vm), Ok(())); } /// Test that check_range_check_usage() returns successfully when all the @@ -3681,7 +3688,7 @@ mod tests { fp: (0, 0).into(), }]); - assert_eq!(cairo_runner.check_range_check_usage(&vm), Ok(()),); + assert_matches!(cairo_runner.check_range_check_usage(&vm), Ok(())); } /// Test that check_range_check_usage() returns an error if there are @@ -3703,9 +3710,11 @@ mod tests { fp: (0, 0).into(), }]); - assert_eq!( + assert_matches!( cairo_runner.check_range_check_usage(&vm), - Err(MemoryError::InsufficientAllocatedCells.into()), + Err(VirtualMachineError::MemoryError( + MemoryError::InsufficientAllocatedCells + )) ); } @@ -3745,7 +3754,7 @@ mod tests { vm.trace = Some(vec![]); cairo_runner.layout.diluted_pool_instance_def = None; - assert_eq!(cairo_runner.check_used_cells(&vm), Ok(())); + assert_matches!(cairo_runner.check_used_cells(&vm), Ok(())); } #[test] @@ -3765,7 +3774,7 @@ mod tests { fp: (0, 0).into(), }]); - assert_eq!( + assert_matches!( cairo_runner.check_used_cells(&vm), Err(VirtualMachineError::MemoryError( MemoryError::InsufficientAllocatedCells @@ -3790,7 +3799,7 @@ mod tests { vm.segments.segment_used_sizes = Some(vec![4, 12]); vm.trace = Some(vec![]); - assert_eq!( + assert_matches!( cairo_runner.check_used_cells(&vm), Err(VirtualMachineError::MemoryError( MemoryError::InsufficientAllocatedCells @@ -3806,7 +3815,7 @@ mod tests { vm.segments.segment_used_sizes = Some(vec![4]); vm.trace = Some(vec![]); - assert_eq!( + assert_matches!( cairo_runner.check_used_cells(&vm), Err(VirtualMachineError::MemoryError( MemoryError::InsufficientAllocatedCells @@ -4312,18 +4321,19 @@ mod tests { vm.accessed_addresses = Some(Vec::new()); cairo_runner.initialize_builtins(&mut vm).unwrap(); cairo_runner.initialize_segments(&mut vm, None); - - let error = cairo_runner.run_from_entrypoint( - main_entrypoint, - &[ - &mayberelocatable!(2).into(), - &MaybeRelocatable::from((2, 0)).into(), - ], //range_check_ptr - true, - &mut vm, - &mut hint_processor, + assert_matches!( + cairo_runner.run_from_entrypoint( + main_entrypoint, + &[ + &mayberelocatable!(2).into(), + &MaybeRelocatable::from((2, 0)).into() + ], //range_check_ptr + true, + &mut vm, + &mut hint_processor, + ), + Ok(()) ); - assert!(error.is_ok()); let mut new_cairo_runner = cairo_runner!(program); let mut new_vm = vm!(true); //this true expression dictates that the trace is enabled @@ -4340,17 +4350,19 @@ mod tests { .pc .unwrap(); - let result = new_cairo_runner.run_from_entrypoint( - fib_entrypoint, - &[ - &mayberelocatable!(2).into(), - &MaybeRelocatable::from((2, 0)).into(), - ], - true, - &mut new_vm, - &mut hint_processor, + assert_matches!( + new_cairo_runner.run_from_entrypoint( + fib_entrypoint, + &[ + &mayberelocatable!(2).into(), + &MaybeRelocatable::from((2, 0)).into() + ], + true, + &mut new_vm, + &mut hint_processor, + ), + Ok(()) ); - assert!(result.is_ok()); } #[test] diff --git a/src/vm/security.rs b/src/vm/security.rs index 26585dcc90..f5faa1ed1b 100644 --- a/src/vm/security.rs +++ b/src/vm/security.rs @@ -80,6 +80,7 @@ mod test { use crate::vm::errors::memory_errors::MemoryError; use crate::vm::vm_memory::memory::Memory; use crate::{relocatable, types::program::Program, utils::test_utils::*}; + use assert_matches::assert_matches; use felt::Felt; use num_traits::Zero; @@ -90,9 +91,9 @@ mod test { let runner = cairo_runner!(program); let mut vm = vm!(); - assert_eq!( + assert_matches!( verify_secure_runner(&runner, true, &mut vm), - Err(RunnerError::NoProgBase.into()), + Err(VirtualMachineError::RunnerError(RunnerError::NoProgBase)) ); } @@ -105,7 +106,7 @@ mod test { runner.initialize(&mut vm).unwrap(); vm.segments.compute_effective_sizes(&vm.memory); - assert_eq!(verify_secure_runner(&runner, true, &mut vm), Ok(())); + assert_matches!(verify_secure_runner(&runner, true, &mut vm), Ok(())); } #[test] @@ -120,7 +121,7 @@ mod test { vm.memory = memory![((0, 0), 100)]; vm.segments.segment_used_sizes = Some(vec![1]); - assert_eq!( + assert_matches!( verify_secure_runner(&runner, true, &mut vm), Err(VirtualMachineError::OutOfBoundsProgramSegmentAccess) ); @@ -138,7 +139,7 @@ mod test { vm.memory.data = vec![vec![], vec![], vec![Some(mayberelocatable!(1))]]; vm.segments.segment_used_sizes = Some(vec![0, 0, 0, 0]); - assert_eq!( + assert_matches!( verify_secure_runner(&runner, true, &mut vm), Err(VirtualMachineError::OutOfBoundsBuiltinSegmentAccess) ); @@ -160,7 +161,7 @@ mod test { vm.memory.data = vec![vec![], vec![], vec![Some(mayberelocatable!(1))]]; vm.segments.segment_used_sizes = Some(vec![0, 0, 1, 0]); - assert_eq!(verify_secure_runner(&runner, true, &mut vm), Ok(())); + assert_matches!(verify_secure_runner(&runner, true, &mut vm), Ok(())); } #[test] @@ -188,7 +189,7 @@ mod test { ]]; vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); - assert_eq!(verify_secure_runner(&runner, true, &mut vm), Ok(())); + assert_matches!(verify_secure_runner(&runner, true, &mut vm), Ok(())); } #[test] @@ -217,7 +218,7 @@ mod test { vm.memory.temp_data = vec![vec![Some(relocatable!(1, 2).into())]]; vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); - assert_eq!(verify_secure_runner(&runner, true, &mut vm), Ok(())); + assert_matches!(verify_secure_runner(&runner, true, &mut vm), Ok(())); } #[test] @@ -246,11 +247,11 @@ mod test { vm.memory.temp_data = vec![vec![Some(relocatable!(1, 2).into())]]; vm.segments.segment_used_sizes = Some(vec![5, 1, 2, 3, 4]); - assert_eq!( + assert_matches!( verify_secure_runner(&runner, true, &mut vm), Err(VirtualMachineError::InvalidMemoryValueTemporaryAddress( - relocatable!(-3, 2) - )) + x + )) if x == relocatable!(-3, 2) ); } } diff --git a/src/vm/trace/mod.rs b/src/vm/trace/mod.rs index 9c7b107970..822385bc49 100644 --- a/src/vm/trace/mod.rs +++ b/src/vm/trace/mod.rs @@ -53,6 +53,7 @@ pub fn get_perm_range_check_limits( mod test { use super::*; use crate::{utils::test_utils::*, vm::errors::memory_errors::MemoryError}; + use assert_matches::assert_matches; /// Test that get_perm_range_check_limits() works as intended with an empty /// trace. @@ -61,7 +62,7 @@ mod test { let trace = &[]; let memory = Memory::new(); - assert_eq!(get_perm_range_check_limits(trace, &memory), Ok(None)); + assert_matches!(get_perm_range_check_limits(trace, &memory), Ok(None)); } /// Test that get_perm_range_check_limits() works as intended with a single @@ -75,9 +76,9 @@ mod test { }]; let memory = memory![((0, 0), 0xFFFF_8000_0000_u64)]; - assert_eq!( + assert_matches!( get_perm_range_check_limits(trace, &memory), - Ok(Some((-32768, 32767))), + Ok(Some((-32768, 32767))) ); } @@ -108,9 +109,9 @@ mod test { ((0, 2), 0x8FFF_8000_0750u64) ]; - assert_eq!( + assert_matches!( get_perm_range_check_limits(trace, &memory), - Ok(Some((-31440, 16383))), + Ok(Some((-31440, 16383))) ); } } diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs index 818bd82249..224eae31a7 100644 --- a/src/vm/vm_core.rs +++ b/src/vm/vm_core.rs @@ -1043,6 +1043,7 @@ mod tests { }, }, }; + use assert_matches::assert_matches; use felt::felt_str; use std::{collections::HashSet, path::Path}; @@ -1076,7 +1077,7 @@ mod tests { #[test] fn get_instruction_encoding_unsuccesful() { let vm = vm!(); - assert_eq!( + assert_matches!( vm.get_instruction_encoding(), Err(VirtualMachineError::InvalidInstructionEncoding) ); @@ -1108,8 +1109,10 @@ mod tests { let mut vm = vm!(); run_context!(vm, 4, 5, 6); - - assert_eq!(Ok(()), vm.update_fp(&instruction, &operands)); + assert_matches!( + vm.update_fp(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.fp, 7) } @@ -1139,7 +1142,10 @@ mod tests { let mut vm = vm!(); - assert_eq!(Ok(()), vm.update_fp(&instruction, &operands)); + assert_matches!( + vm.update_fp(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.fp, 6) } @@ -1169,7 +1175,10 @@ mod tests { let mut vm = vm!(); - assert_eq!(Ok(()), vm.update_fp(&instruction, &operands)); + assert_matches!( + vm.update_fp(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.fp, 0) } @@ -1200,7 +1209,10 @@ mod tests { let mut vm = vm!(); run_context!(vm, 4, 5, 6); - assert_eq!(Ok(()), vm.update_fp(&instruction, &operands)); + assert_matches!( + vm.update_fp(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.fp, 11) } @@ -1233,7 +1245,10 @@ mod tests { vm.run_context.ap = 5; vm.run_context.fp = 6; - assert_eq!(Ok(()), vm.update_ap(&instruction, &operands)); + assert_matches!( + vm.update_ap(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.ap, 13); } @@ -1266,7 +1281,7 @@ mod tests { vm.run_context.ap = 5; vm.run_context.fp = 6; - assert_eq!( + assert_matches!( vm.update_ap(&instruction, &operands), Err(VirtualMachineError::UnconstrainedResAdd) ); @@ -1301,7 +1316,10 @@ mod tests { vm.run_context.ap = 5; vm.run_context.fp = 6; - assert_eq!(Ok(()), vm.update_ap(&instruction, &operands)); + assert_matches!( + vm.update_ap(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.ap, 6); } @@ -1334,7 +1352,10 @@ mod tests { vm.run_context.ap = 5; vm.run_context.fp = 6; - assert_eq!(Ok(()), vm.update_ap(&instruction, &operands)); + assert_matches!( + vm.update_ap(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.ap, 7); } @@ -1367,7 +1388,10 @@ mod tests { vm.run_context.ap = 5; vm.run_context.fp = 6; - assert_eq!(Ok(()), vm.update_ap(&instruction, &operands)); + assert_matches!( + vm.update_ap(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.ap, 5); } @@ -1397,7 +1421,10 @@ mod tests { let mut vm = vm!(); - assert_eq!(Ok(()), vm.update_pc(&instruction, &operands)); + assert_matches!( + vm.update_pc(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 1))); } @@ -1427,7 +1454,10 @@ mod tests { let mut vm = vm!(); - assert_eq!(Ok(()), vm.update_pc(&instruction, &operands)); + assert_matches!( + vm.update_pc(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 2))); } @@ -1457,7 +1487,10 @@ mod tests { let mut vm = vm!(); - assert_eq!(Ok(()), vm.update_pc(&instruction, &operands)); + assert_matches!( + vm.update_pc(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 8))); } @@ -1490,7 +1523,7 @@ mod tests { vm.run_context.ap = 5; vm.run_context.fp = 6; - assert_eq!( + assert_matches!( vm.update_pc(&instruction, &operands), Err(VirtualMachineError::UnconstrainedResJump) ); @@ -1523,7 +1556,10 @@ mod tests { let mut vm = vm!(); run_context!(vm, 1, 1, 1); - assert_eq!(Ok(()), vm.update_pc(&instruction, &operands)); + assert_matches!( + vm.update_pc(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 9))); } @@ -1553,7 +1589,7 @@ mod tests { let mut vm = vm!(); - assert_eq!( + assert_matches!( vm.update_pc(&instruction, &operands), Err(VirtualMachineError::UnconstrainedResJumpRel) ); @@ -1584,10 +1620,9 @@ mod tests { }; let mut vm = vm!(); - - assert_eq!( - Err(VirtualMachineError::PureValue), - vm.update_pc(&instruction, &operands) + assert_matches!( + vm.update_pc(&instruction, &operands), + Err::<(), VirtualMachineError>(VirtualMachineError::PureValue) ); } @@ -1617,7 +1652,10 @@ mod tests { let mut vm = vm!(); - assert_eq!(Ok(()), vm.update_pc(&instruction, &operands)); + assert_matches!( + vm.update_pc(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 1))); } @@ -1647,7 +1685,10 @@ mod tests { let mut vm = vm!(); - assert_eq!(Ok(()), vm.update_pc(&instruction, &operands)); + assert_matches!( + vm.update_pc(&instruction, &operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 10))); } @@ -1680,7 +1721,10 @@ mod tests { vm.run_context.ap = 5; vm.run_context.fp = 6; - assert_eq!(Ok(()), vm.update_registers(instruction, operands)); + assert_matches!( + vm.update_registers(instruction, operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 5))); assert_eq!(vm.run_context.ap, 5); assert_eq!(vm.run_context.fp, 6); @@ -1713,7 +1757,10 @@ mod tests { let mut vm = vm!(); run_context!(vm, 4, 5, 6); - assert_eq!(Ok(()), vm.update_registers(instruction, operands)); + assert_matches!( + vm.update_registers(instruction, operands), + Ok::<(), VirtualMachineError>(()) + ); assert_eq!(vm.run_context.pc, Relocatable::from((0, 12))); assert_eq!(vm.run_context.ap, 7); assert_eq!(vm.run_context.fp, 11); @@ -1722,21 +1769,27 @@ mod tests { #[test] fn is_zero_int_value() { let value = MaybeRelocatable::Int(Felt::new(1)); - assert_eq!(Ok(false), VirtualMachine::is_zero(&value)); + assert_matches!( + VirtualMachine::is_zero(&value), + Ok::(false) + ); } #[test] fn is_zero_relocatable_value() { let value = MaybeRelocatable::from((1, 2)); - assert_eq!(Ok(false), VirtualMachine::is_zero(&value)); + assert_matches!( + VirtualMachine::is_zero(&value), + Ok::(false) + ); } #[test] fn is_zero_relocatable_value_negative() { let value = MaybeRelocatable::from((1, 0)); - assert_eq!( - Err(VirtualMachineError::PureValue), - VirtualMachine::is_zero(&value) + assert_matches!( + VirtualMachine::is_zero(&value), + Err::(VirtualMachineError::PureValue) ); } @@ -1759,9 +1812,12 @@ mod tests { let vm = vm!(); - assert_eq!( - Ok((Some(MaybeRelocatable::from((0, 1))), None)), - vm.deduce_op0(&instruction, None, None) + assert_matches!( + vm.deduce_op0(&instruction, None, None), + Ok::<(Option, Option), VirtualMachineError>(( + Some(x), + None + )) if x == MaybeRelocatable::from((0, 1)) ); } @@ -1786,12 +1842,14 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(3)); let op1 = MaybeRelocatable::Int(Felt::new(2)); - assert_eq!( - Ok(( - Some(MaybeRelocatable::Int(Felt::new(1))), - Some(MaybeRelocatable::Int(Felt::new(3))) - )), - vm.deduce_op0(&instruction, Some(&dst), Some(&op1)) + + assert_matches!( + vm.deduce_op0(&instruction, Some(&dst), Some(&op1)), + Ok::<(Option, Option), VirtualMachineError>(( + x, + y + )) if x == Some(MaybeRelocatable::Int(Felt::new(1))) && + y == Some(MaybeRelocatable::Int(Felt::new(3))) ); } @@ -1814,7 +1872,12 @@ mod tests { let vm = vm!(); - assert_eq!(Ok((None, None)), vm.deduce_op0(&instruction, None, None)); + assert_matches!( + vm.deduce_op0(&instruction, None, None), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) + ); } #[test] @@ -1838,12 +1901,14 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(4)); let op1 = MaybeRelocatable::Int(Felt::new(2)); - assert_eq!( - Ok(( - Some(MaybeRelocatable::Int(Felt::new(2))), - Some(MaybeRelocatable::Int(Felt::new(4))) - )), - vm.deduce_op0(&instruction, Some(&dst), Some(&op1)) + + assert_matches!( + vm.deduce_op0(&instruction, Some(&dst), Some(&op1)), + Ok::<(Option, Option), VirtualMachineError>(( + Some(x), + Some(y) + )) if x == MaybeRelocatable::Int(Felt::new(2)) && + y == MaybeRelocatable::Int(Felt::new(4)) ); } @@ -1868,9 +1933,11 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(4)); let op1 = MaybeRelocatable::Int(Felt::new(0)); - assert_eq!( - Ok((None, None)), - vm.deduce_op0(&instruction, Some(&dst), Some(&op1)) + assert_matches!( + vm.deduce_op0(&instruction, Some(&dst), Some(&op1)), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) ); } @@ -1895,9 +1962,11 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(4)); let op1 = MaybeRelocatable::Int(Felt::new(0)); - assert_eq!( - Ok((None, None)), - vm.deduce_op0(&instruction, Some(&dst), Some(&op1)) + assert_matches!( + vm.deduce_op0(&instruction, Some(&dst), Some(&op1)), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) ); } @@ -1922,9 +1991,12 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(4)); let op1 = MaybeRelocatable::Int(Felt::new(0)); - assert_eq!( - Ok((None, None)), - vm.deduce_op0(&instruction, Some(&dst), Some(&op1)) + + assert_matches!( + vm.deduce_op0(&instruction, Some(&dst), Some(&op1)), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) ); } @@ -1947,7 +2019,12 @@ mod tests { let vm = vm!(); - assert_eq!(Ok((None, None)), vm.deduce_op1(&instruction, None, None)); + assert_matches!( + vm.deduce_op1(&instruction, None, None), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) + ); } #[test] @@ -1971,12 +2048,13 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(3)); let op0 = MaybeRelocatable::Int(Felt::new(2)); - assert_eq!( - Ok(( - Some(MaybeRelocatable::Int(Felt::new(1))), - Some(MaybeRelocatable::Int(Felt::new(3))) - )), - vm.deduce_op1(&instruction, Some(&dst), Some(op0)) + assert_matches!( + vm.deduce_op1(&instruction, Some(&dst), Some(op0)), + Ok::<(Option, Option), VirtualMachineError>(( + x, + y + )) if x == Some(MaybeRelocatable::Int(Felt::new(1))) && + y == Some(MaybeRelocatable::Int(Felt::new(3))) ); } @@ -1998,8 +2076,12 @@ mod tests { }; let vm = vm!(); - - assert_eq!(Ok((None, None)), vm.deduce_op1(&instruction, None, None)); + assert_matches!( + vm.deduce_op1(&instruction, None, None), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) + ); } #[test] @@ -2023,12 +2105,13 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(4)); let op0 = MaybeRelocatable::Int(Felt::new(2)); - assert_eq!( - Ok(( - Some(MaybeRelocatable::Int(Felt::new(2))), - Some(MaybeRelocatable::Int(Felt::new(4))) - )), - vm.deduce_op1(&instruction, Some(&dst), Some(op0)) + assert_matches!( + vm.deduce_op1(&instruction, Some(&dst), Some(op0)), + Ok::<(Option, Option), VirtualMachineError>(( + x, + y + )) if x == Some(MaybeRelocatable::Int(Felt::new(2))) && + y == Some(MaybeRelocatable::Int(Felt::new(4))) ); } @@ -2053,9 +2136,11 @@ mod tests { let dst = MaybeRelocatable::Int(Felt::new(4)); let op0 = MaybeRelocatable::Int(Felt::new(0)); - assert_eq!( - Ok((None, None)), - vm.deduce_op1(&instruction, Some(&dst), Some(op0)) + assert_matches!( + vm.deduce_op1(&instruction, Some(&dst), Some(op0)), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) ); } @@ -2079,9 +2164,11 @@ mod tests { let vm = vm!(); let op0 = MaybeRelocatable::Int(Felt::new(0)); - assert_eq!( - Ok((None, None)), - vm.deduce_op1(&instruction, None, Some(op0)) + assert_matches!( + vm.deduce_op1(&instruction, None, Some(op0)), + Ok::<(Option, Option), VirtualMachineError>(( + None, None + )) ); } @@ -2105,12 +2192,13 @@ mod tests { let vm = vm!(); let dst = MaybeRelocatable::Int(Felt::new(7)); - assert_eq!( - Ok(( - Some(MaybeRelocatable::Int(Felt::new(7))), - Some(MaybeRelocatable::Int(Felt::new(7))) - )), - vm.deduce_op1(&instruction, Some(&dst), None) + assert_matches!( + vm.deduce_op1(&instruction, Some(&dst), None), + Ok::<(Option, Option), VirtualMachineError>(( + x, + y + )) if x == Some(MaybeRelocatable::Int(Felt::new(7))) && + y == Some(MaybeRelocatable::Int(Felt::new(7))) ); } @@ -2135,9 +2223,11 @@ mod tests { let op1 = MaybeRelocatable::Int(Felt::new(7)); let op0 = MaybeRelocatable::Int(Felt::new(9)); - assert_eq!( - Ok(Some(MaybeRelocatable::Int(Felt::new(7)))), - vm.compute_res(&instruction, &op0, &op1) + assert_matches!( + vm.compute_res(&instruction, &op0, &op1), + Ok::, VirtualMachineError>(Some(MaybeRelocatable::Int( + x + ))) if x == Felt::new(7) ); } @@ -2162,9 +2252,11 @@ mod tests { let op1 = MaybeRelocatable::Int(Felt::new(7)); let op0 = MaybeRelocatable::Int(Felt::new(9)); - assert_eq!( - Ok(Some(MaybeRelocatable::Int(Felt::new(16)))), - vm.compute_res(&instruction, &op0, &op1) + assert_matches!( + vm.compute_res(&instruction, &op0, &op1), + Ok::, VirtualMachineError>(Some(MaybeRelocatable::Int( + x + ))) if x == Felt::new(16) ); } @@ -2189,9 +2281,11 @@ mod tests { let op1 = MaybeRelocatable::Int(Felt::new(7)); let op0 = MaybeRelocatable::Int(Felt::new(9)); - assert_eq!( - Ok(Some(MaybeRelocatable::Int(Felt::new(63)))), - vm.compute_res(&instruction, &op0, &op1) + assert_matches!( + vm.compute_res(&instruction, &op0, &op1), + Ok::, VirtualMachineError>(Some(MaybeRelocatable::Int( + x + ))) if x == Felt::new(63) ); } @@ -2216,9 +2310,9 @@ mod tests { let op1 = MaybeRelocatable::from((2, 3)); let op0 = MaybeRelocatable::from((2, 6)); - assert_eq!( - Err(VirtualMachineError::PureValue), - vm.compute_res(&instruction, &op0, &op1) + assert_matches!( + vm.compute_res(&instruction, &op0, &op1), + Err::, VirtualMachineError>(VirtualMachineError::PureValue) ); } @@ -2243,7 +2337,10 @@ mod tests { let op1 = MaybeRelocatable::Int(Felt::new(7)); let op0 = MaybeRelocatable::Int(Felt::new(9)); - assert_eq!(Ok(None), vm.compute_res(&instruction, &op0, &op1)); + assert_matches!( + vm.compute_res(&instruction, &op0, &op1), + Ok::, VirtualMachineError>(None) + ); } #[test] @@ -2486,7 +2583,7 @@ mod tests { assert!(operands == expected_operands); assert!(addresses == expected_addresses); let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_eq!( + assert_matches!( vm.step( &mut hint_processor, exec_scopes_ref!(), @@ -2520,7 +2617,7 @@ mod tests { vm.memory = memory!(((1, 0), 145944781867024385_i64)); let error = vm.compute_operands(&instruction).unwrap_err(); - assert_eq!(error, VirtualMachineError::NoDst); + assert_matches!(error, VirtualMachineError::NoDst); } #[test] @@ -2550,7 +2647,7 @@ mod tests { let vm = vm!(); let error = vm.opcode_assertions(&instruction, &operands); - assert_eq!(error, Err(VirtualMachineError::UnconstrainedResAssertEq)); + assert_matches!(error, Err(VirtualMachineError::UnconstrainedResAssertEq)); } #[test] @@ -2579,12 +2676,13 @@ mod tests { let vm = vm!(); - assert_eq!( + assert_matches!( vm.opcode_assertions(&instruction, &operands), Err(VirtualMachineError::DiffAssertValues( - MaybeRelocatable::Int(Felt::new(9_i32)), - MaybeRelocatable::Int(Felt::new(8_i32)) - )) + i, + j + )) if i == MaybeRelocatable::Int(Felt::new(9_i32)) && + j == MaybeRelocatable::Int(Felt::new(8_i32)) ); } @@ -2614,12 +2712,12 @@ mod tests { let vm = vm!(); - assert_eq!( + assert_matches!( vm.opcode_assertions(&instruction, &operands), Err(VirtualMachineError::DiffAssertValues( - MaybeRelocatable::from((1, 1)), - MaybeRelocatable::from((1, 2)) - )) + i, + j) + ) if i == MaybeRelocatable::from((1, 1)) && j == MaybeRelocatable::from((1, 2)) ); } @@ -2650,12 +2748,12 @@ mod tests { let mut vm = vm!(); vm.run_context.pc = relocatable!(0, 4); - assert_eq!( + assert_matches!( vm.opcode_assertions(&instruction, &operands), Err(VirtualMachineError::CantWriteReturnPc( - mayberelocatable!(9), - mayberelocatable!(0, 5), - )) + x, + y, + )) if x == mayberelocatable!(9) && y == mayberelocatable!(0, 5) ); } @@ -2685,12 +2783,12 @@ mod tests { let mut vm = vm!(); vm.run_context.fp = 6; - assert_eq!( + assert_matches!( vm.opcode_assertions(&instruction, &operands), Err(VirtualMachineError::CantWriteReturnFp( - mayberelocatable!(8), - mayberelocatable!(1, 6) - )) + x, + y + )) if x == mayberelocatable!(8) && y == mayberelocatable!(1, 6) ); } @@ -2725,7 +2823,7 @@ mod tests { ((1, 1), (3, 0)) ]; - assert_eq!( + assert_matches!( vm.step( &mut hint_processor, exec_scopes_ref!(), @@ -2805,7 +2903,7 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); //Run steps while vm.run_context.pc != final_pc { - assert_eq!( + assert_matches!( vm.step( &mut hint_processor, exec_scopes_ref!(), @@ -2901,7 +2999,7 @@ mod tests { assert_eq!(vm.run_context.pc, Relocatable::from((0, 0))); assert_eq!(vm.run_context.ap, 2); let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_eq!( + assert_matches!( vm.step( &mut hint_processor, exec_scopes_ref!(), @@ -2922,7 +3020,7 @@ mod tests { &MaybeRelocatable::Int(Felt::new(0x4)), ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_eq!( + assert_matches!( vm.step( &mut hint_processor, exec_scopes_ref!(), @@ -2944,7 +3042,7 @@ mod tests { ); let mut hint_processor = BuiltinHintProcessor::new_empty(); - assert_eq!( + assert_matches!( vm.step( &mut hint_processor, exec_scopes_ref!(), @@ -2969,7 +3067,7 @@ mod tests { #[test] fn deduce_memory_cell_no_pedersen_builtin() { let vm = vm!(); - assert_eq!(vm.deduce_memory_cell(&Relocatable::from((0, 0))), Ok(None)); + assert_matches!(vm.deduce_memory_cell(&Relocatable::from((0, 0))), Ok(None)); } #[test] @@ -2979,11 +3077,11 @@ mod tests { vm.builtin_runners .push((String::from("pedersen"), builtin.into())); vm.memory = memory![((0, 3), 32), ((0, 4), 72), ((0, 5), 0)]; - assert_eq!( + assert_matches!( vm.deduce_memory_cell(&Relocatable::from((0, 5))), - Ok(Some(MaybeRelocatable::from(felt::felt_str!( + Ok(i) if i == Some(MaybeRelocatable::from(felt::felt_str!( "3270867057177188607814717243084834301278723532952411121381966378910183338911" - )))) + ))) ); } @@ -3082,9 +3180,9 @@ mod tests { vm.builtin_runners .push((String::from("bitwise"), builtin.into())); vm.memory = memory![((0, 5), 10), ((0, 6), 12), ((0, 7), 0)]; - assert_eq!( + assert_matches!( vm.deduce_memory_cell(&Relocatable::from((0, 7))), - Ok(Some(MaybeRelocatable::from(Felt::new(8_i32)))) + Ok(i) if i == Some(MaybeRelocatable::from(Felt::new(8_i32))) ); } @@ -3202,12 +3300,11 @@ mod tests { ) ]; - let result = vm.deduce_memory_cell(&Relocatable::from((0, 6))); - assert_eq!( - result, - Ok(Some(MaybeRelocatable::from(felt_str!( + assert_matches!( + vm.deduce_memory_cell(&Relocatable::from((0, 6))), + Ok(i) if i == Some(MaybeRelocatable::from(felt_str!( "3598390311618116577316045819420613574162151407434885460365915347732568210029" - )))) + ))) ); } @@ -3272,7 +3369,7 @@ mod tests { ) ) ]; - assert_eq!(vm.verify_auto_deductions(), Ok(())); + assert_matches!(vm.verify_auto_deductions(), Ok(())); } #[test] @@ -3321,19 +3418,21 @@ mod tests { ) ]; let error = vm.verify_auto_deductions(); - assert_eq!( + assert_eq!(error.as_ref().unwrap_err().to_string(), "Inconsistent auto-deduction for builtin ec_op, expected 2739017437753868763038285897969098325279422804143820990343394856167768859289, got Some(Int(2778063437308421278851140253538604815869848682781135193774472480292420096757))"); + assert_matches!( error, Err(VirtualMachineError::InconsistentAutoDeduction( - String::from("ec_op"), - MaybeRelocatable::Int(felt_str!( - "2739017437753868763038285897969098325279422804143820990343394856167768859289" - )), - Some(MaybeRelocatable::Int(felt_str!( - "2778063437308421278851140253538604815869848682781135193774472480292420096757" - ))) - )) + x, + y, + z + )) if x == *String::from("ec_op") && + y == MaybeRelocatable::Int(felt_str!( + "2739017437753868763038285897969098325279422804143820990343394856167768859289" + )) && + z == Some(MaybeRelocatable::Int(felt_str!( + "2778063437308421278851140253538604815869848682781135193774472480292420096757" + ))) ); - assert_eq!(error.unwrap_err().to_string(), "Inconsistent auto-deduction for builtin ec_op, expected 2739017437753868763038285897969098325279422804143820990343394856167768859289, got Some(Int(2778063437308421278851140253538604815869848682781135193774472480292420096757))"); } #[test] @@ -3356,7 +3455,7 @@ mod tests { vm.builtin_runners .push((String::from("bitwise"), builtin.into())); vm.memory = memory![((2, 0), 12), ((2, 1), 10)]; - assert_eq!(vm.verify_auto_deductions(), Ok(())); + assert_matches!(vm.verify_auto_deductions(), Ok(())); } #[test] @@ -3378,11 +3477,11 @@ mod tests { let builtin: BuiltinRunner = builtin.into(); let mut vm = vm!(); vm.memory = memory![((2, 0), 12), ((2, 1), 10)]; - assert_eq!( + assert_matches!( vm.verify_auto_deductions_for_addr(&relocatable!(2, 0), &builtin), Ok(()) ); - assert_eq!( + assert_matches!( vm.verify_auto_deductions_for_addr(&relocatable!(2, 1), &builtin), Ok(()) ); @@ -3419,7 +3518,7 @@ mod tests { vm.builtin_runners .push((String::from("pedersen"), builtin.into())); vm.memory = memory![((3, 0), 32), ((3, 1), 72)]; - assert_eq!(vm.verify_auto_deductions(), Ok(())); + assert_matches!(vm.verify_auto_deductions(), Ok(())); } #[test] @@ -3440,10 +3539,7 @@ mod tests { fn get_return_values_fails_when_ap_is_0() { let mut vm = vm!(); vm.memory = memory![((1, 0), 1), ((1, 1), 2), ((1, 2), 3), ((1, 3), 4)]; - assert!(matches!( - vm.get_return_values(3), - Err(MemoryError::NumOutOfBounds) - )); + assert_matches!(vm.get_return_values(3), Err(MemoryError::NumOutOfBounds)); } /* @@ -3511,7 +3607,7 @@ mod tests { //Run Steps for _ in 0..6 { - assert_eq!( + assert_matches!( vm.step( &mut hint_processor, exec_scopes_ref!(), @@ -3718,7 +3814,7 @@ mod tests { let scopes = exec_scopes_ref!(); scopes.enter_scope(HashMap::new()); - assert_eq!( + assert_matches!( vm.end_run(scopes), Err(VirtualMachineError::MainScopeError( ExecScopeError::NoScopeError @@ -3751,7 +3847,7 @@ mod tests { fn decode_current_instruction_invalid_encoding() { let mut vm = vm!(); vm.memory = memory![((0, 0), ("112233445566778899", 16))]; - assert_eq!( + assert_matches!( vm.decode_current_instruction(), Err(VirtualMachineError::InvalidInstructionEncoding) ); @@ -3787,9 +3883,9 @@ mod tests { fn gen_arg_relocatable() { let mut vm = vm!(); - assert_eq!( + assert_matches!( vm.gen_arg(&mayberelocatable!(0, 0)), - Ok(mayberelocatable!(0, 0)), + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -3799,9 +3895,9 @@ mod tests { fn gen_arg_bigint() { let mut vm = vm!(); - assert_eq!( + assert_matches!( vm.gen_arg(&mayberelocatable!(1234)), - Ok(mayberelocatable!(1234)), + Ok(x) if x == mayberelocatable!(1234) ); } @@ -3813,7 +3909,7 @@ mod tests { let prime = felt_str!(felt::PRIME_STR[2..], 16); let prime_maybe = MaybeRelocatable::from(prime); - assert_eq!(vm.gen_arg(&prime_maybe), Ok(mayberelocatable!(0))); + assert_matches!(vm.gen_arg(&prime_maybe), Ok(x) if x == mayberelocatable!(0)); } /// Test that the call to .gen_arg() with a Vec writes its @@ -3822,7 +3918,7 @@ mod tests { fn gen_arg_vec() { let mut vm = vm!(); - assert_eq!( + assert_matches!( vm.gen_arg(&vec![ mayberelocatable!(0), mayberelocatable!(1), @@ -3833,7 +3929,7 @@ mod tests { mayberelocatable!(0, 2), mayberelocatable!(0, 3), ]), - Ok(mayberelocatable!(0, 0)), + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -3883,9 +3979,9 @@ mod tests { fn mark_as_accessed_run_not_finished() { let mut vm = vm!(); vm.accessed_addresses = Some(Vec::new()); - assert_eq!( + assert_matches!( vm.mark_address_range_as_accessed((0, 0).into(), 3), - Err(VirtualMachineError::RunNotFinished), + Err(VirtualMachineError::RunNotFinished) ); } @@ -3893,9 +3989,9 @@ mod tests { fn mark_as_accessed_missing_accessed_addresses() { let mut vm = vm!(); vm.accessed_addresses = None; - assert_eq!( + assert_matches!( vm.mark_address_range_as_accessed((0, 0).into(), 3), - Err(VirtualMachineError::RunNotFinished), + Err(VirtualMachineError::RunNotFinished) ); } diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index aacb76bb72..e6b8d1e50a 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -354,6 +354,7 @@ mod memory_tests { vm_memory::memory_segments::MemorySegmentManager, }, }; + use assert_matches::assert_matches; use felt::felt_str; use crate::vm::errors::memory_errors::MemoryError; @@ -546,10 +547,7 @@ mod memory_tests { 2, ) .unwrap(); - assert!(matches!( - mem.get(&MaybeRelocatable::from((1, 0))), - _val_clone - )); + assert_matches!(mem.get(&MaybeRelocatable::from((1, 0))), Ok(Some(inner)) if inner.clone().into_owned() == MaybeRelocatable::Int(Felt::new(5))); } #[test] @@ -719,11 +717,11 @@ mod memory_tests { &MaybeRelocatable::from((0, 10)), ) .unwrap(); - assert_eq!( + assert_matches!( memory.get_integer(&Relocatable::from((0, 0))), Err(VirtualMachineError::ExpectedInteger( - MaybeRelocatable::from((0, 0)) - )) + e + )) if e == MaybeRelocatable::from((0, 0)) ); } diff --git a/src/vm/vm_memory/memory_segments.rs b/src/vm/vm_memory/memory_segments.rs index ee025c93c9..296184619c 100644 --- a/src/vm/vm_memory/memory_segments.rs +++ b/src/vm/vm_memory/memory_segments.rs @@ -265,6 +265,7 @@ mod tests { use super::*; use crate::vm::vm_core::VirtualMachine; use crate::{relocatable, utils::test_utils::*}; + use assert_matches::assert_matches; use felt::Felt; use num_traits::Num; use std::vec; @@ -739,9 +740,9 @@ mod tests { let mut memory_segment_manager = MemorySegmentManager::new(); let mut vm = vm!(); - assert_eq!( + assert_matches!( memory_segment_manager.gen_arg(&mayberelocatable!(0, 0), &mut vm.memory), - Ok(mayberelocatable!(0, 0)), + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -752,9 +753,9 @@ mod tests { let mut memory_segment_manager = MemorySegmentManager::new(); let mut vm = vm!(); - assert_eq!( + assert_matches!( memory_segment_manager.gen_arg(&mayberelocatable!(1234), &mut vm.memory), - Ok(mayberelocatable!(1234)), + Ok(x) if x == mayberelocatable!(1234) ); } @@ -765,7 +766,7 @@ mod tests { let mut memory_segment_manager = MemorySegmentManager::new(); let mut vm = vm!(); - assert_eq!( + assert_matches!( memory_segment_manager.gen_arg( &vec![ mayberelocatable!(0), @@ -779,7 +780,7 @@ mod tests { ], &mut vm.memory, ), - Ok(mayberelocatable!(0, 0)), + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -790,7 +791,7 @@ mod tests { let mut memory_segment_manager = MemorySegmentManager::new(); let mut vm = vm!(); - assert_eq!( + assert_matches!( memory_segment_manager.gen_arg( &vec![ MaybeRelocatable::from((0, 0)), @@ -800,7 +801,7 @@ mod tests { ], &mut vm.memory, ), - Ok(mayberelocatable!(0, 0)), + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -811,9 +812,9 @@ mod tests { let mut memory_segment_manager = MemorySegmentManager::new(); let mut vm = vm!(); - assert_eq!( + assert_matches!( memory_segment_manager.gen_arg(&"", &mut vm.memory), - Err(VirtualMachineError::NotImplemented), + Err(VirtualMachineError::NotImplemented) ); } @@ -859,9 +860,9 @@ mod tests { let mut memory_segment_manager = MemorySegmentManager::new(); let mut vm = vm!(); - assert_eq!( + assert_matches!( memory_segment_manager.gen_cairo_arg(&mayberelocatable!(1234).into(), &mut vm.memory), - Ok(mayberelocatable!(1234)), + Ok(x) if x == mayberelocatable!(1234) ); } @@ -870,7 +871,7 @@ mod tests { let mut memory_segment_manager = MemorySegmentManager::new(); let mut vm = vm!(); - assert_eq!( + assert_matches!( memory_segment_manager.gen_cairo_arg( &vec![ mayberelocatable!(0), @@ -885,7 +886,7 @@ mod tests { .into(), &mut vm.memory, ), - Ok(mayberelocatable!(0, 0)), + Ok(x) if x == mayberelocatable!(0, 0) ); } @@ -908,9 +909,9 @@ mod tests { ]), ]); - assert_eq!( + assert_matches!( memory_segment_manager.gen_cairo_arg(&cairo_args, &mut vm.memory,), - Ok(mayberelocatable!(2, 0)), + Ok(x) if x == mayberelocatable!(2, 0) ); } } diff --git a/tests/bitwise_test.rs b/tests/bitwise_test.rs index 082696ef3a..c2b5cf14d6 100644 --- a/tests/bitwise_test.rs +++ b/tests/bitwise_test.rs @@ -6,6 +6,10 @@ use cairo_vm::{ vm_core::VirtualMachine, }, }; + +#[macro_use] +extern crate assert_matches; + use std::path::Path; #[test] fn bitwise_integration_test() { @@ -18,14 +22,12 @@ fn bitwise_integration_test() { let mut cairo_runner = CairoRunner::new(&program, "all", false).unwrap(); let mut vm = VirtualMachine::new(true); let end = cairo_runner.initialize(&mut vm).unwrap(); - assert!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor) == Ok(()), - "Execution failed" - ); - assert!( - cairo_runner.relocate(&mut vm,) == Ok(()), + assert_matches!( + cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), + Ok(()), "Execution failed" ); + assert_matches!(cairo_runner.relocate(&mut vm,), Ok(()), "Execution failed"); let python_vm_relocated_trace: Vec = vec![ RelocatedTraceEntry { diff --git a/tests/pedersen_test.rs b/tests/pedersen_test.rs index 060ac078e8..0392243d69 100644 --- a/tests/pedersen_test.rs +++ b/tests/pedersen_test.rs @@ -1,5 +1,6 @@ use std::path::Path; - +#[macro_use] +extern crate assert_matches; use cairo_vm::{ hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, types::program::Program, @@ -15,7 +16,7 @@ fn pedersen_integration_test() { let mut cairo_runner = CairoRunner::new(&program, "all", false).unwrap(); let mut vm = VirtualMachine::new(true); let end = cairo_runner.initialize(&mut vm).unwrap(); - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); diff --git a/tests/skip_instruction_test.rs b/tests/skip_instruction_test.rs index 870076f4cc..c8675e58a5 100644 --- a/tests/skip_instruction_test.rs +++ b/tests/skip_instruction_test.rs @@ -4,6 +4,8 @@ use cairo_vm::{ types::program::Program, vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine}, }; +#[macro_use] +extern crate assert_matches; #[cfg(feature = "skip_next_instruction_hint")] use std::path::Path; @@ -22,7 +24,7 @@ fn skip_next_instruction_test() { let mut cairo_runner = CairoRunner::new(&program, "all", false).unwrap(); let mut vm = VirtualMachine::new(false); let end = cairo_runner.initialize(&mut vm).unwrap(); - assert_eq!( + assert_matches!( cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), Ok(()) ); diff --git a/tests/struct_test.rs b/tests/struct_test.rs index 59529845c5..cd443c051f 100644 --- a/tests/struct_test.rs +++ b/tests/struct_test.rs @@ -9,6 +9,9 @@ use cairo_vm::{ vm::{runners::cairo_runner::CairoRunner, trace::trace_entry::RelocatedTraceEntry}, }; +#[macro_use] +extern crate assert_matches; + #[test] fn struct_integration_test() { let program = Program::from_file(Path::new("cairo_programs/struct.json"), Some("main")) @@ -18,8 +21,9 @@ fn struct_integration_test() { let mut vm = VirtualMachine::new(true); let end = cairo_runner.initialize(&mut vm).unwrap(); - assert!( - cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor) == Ok(()), + assert_matches!( + cairo_runner.run_until_pc(end, &mut vm, &mut hint_processor), + Ok(()), "Execution failed" ); assert!(cairo_runner.relocate(&mut vm) == Ok(()), "Execution failed");