diff --git a/crates/starknet_os/src/tests/aliases.rs b/crates/starknet_os/src/tests/aliases.rs index d470ea23a21..6d20fcd88e0 100644 --- a/crates/starknet_os/src/tests/aliases.rs +++ b/crates/starknet_os/src/tests/aliases.rs @@ -1,3 +1,4 @@ +use core::panic; use std::collections::{BTreeSet, HashMap, HashSet}; use apollo_starknet_os_program::test_programs::ALIASES_TEST_BYTES; @@ -32,7 +33,6 @@ use crate::test_utils::utils::{ }; // TODO(Nimrod): Move this next to the stateful compression hints implementation. -// TODO(Amos): This test is incomplete. Add the rest of the test cases and remove this todo. const DEFAULT_CLASS_HASH: u128 = 7777; @@ -317,7 +317,132 @@ fn allocate_aliases_for_keys_and_replace( } #[rstest] -#[case(HashMap::new(), HashMap::new(), HashMap::new(), HashMap::new(), HashMap::new())] +#[case::non_allocation_of_address_lt_16_from_empty_storage( + HashMap::from([ + ( + 15, + HashMap::from([(5534, 1), (98435, 1), (99999, 1)]) + ), + ( + 16, + HashMap::from([(11, 1), (127, 1), (128, 1), (129, 1), (225, 1), (7659, 1)]) + ), + ( + 7659, + HashMap::from([(12, 0), (200, 1), (300, 1), (1111, 1)]) + ), + ( + 99999, + HashMap::from([(225, 1)]) + ) + ]), + HashMap::new(), + HashMap::new(), + HashMap::new(), + HashMap::from([(0, 136), (128, 128), (129, 129), (200, 132), (225, 130), (300, 133), (1111, 134), (7659, 131), (99999, 135)]) +)] +#[case::non_allocation_of_address_lt_16_from_non_empty_storage( + HashMap::from([ + ( + 9, + HashMap::from([(5534, 1), (98435, 1), (99999, 1)]) + ), + ( + 44, + HashMap::from([(11, 1), (129, 1), (225, 1), (7659, 1)]) + ), + ( + 400, + HashMap::from([(225, 1), (400, 1), (700, 1), (701, 1), (1111, 1)]) + ), + ]), + HashMap::new(), + HashMap::new(), + HashMap::from([(0, 135), (129, 128), (225, 129), (7659, 130), (200, 131), (300, 132), (1111, 133), (99999, 134)]), + HashMap::from([(0, 138), (129, 128), (225, 129), (400, 135), (700, 136), (701, 137), (1111, 133), (7659, 130)] + ) +)] +#[case::non_allocation_with_only_trivial_updates( + HashMap::from([ + ( + 11, + HashMap::from([(5534, 1), (98435, 1), (99999, 1)]) + ), + ( + 44, + HashMap::from([(11, 0), (129, 1), (225, 0), (400, 1), (7659, 1)]) + ), + ( + 400, + HashMap::from([(225, 0), (406, 0), (700, 1), (701, 1), (1111, 1)]) + ), + ( + 598, + HashMap::from([(2255, 0), (7008, 0)]) // Trivial update. + ) + ]), + HashMap::new(), + HashMap::new(), + HashMap::new(), + HashMap::from([(0, 134), (129, 128), (400, 129), (700, 131), (701, 132), (1111, 133), (7659, 130)] + ) +)] +#[case::allocation_with_only_nonce_change( + HashMap::new(), + HashMap::from([ + (13, 1), + (58, 1), + (11111, DEFAULT_CLASS_HASH), // Gets a new nonce. + (222222, 1), + (3333333, 1), + (3333336, DEFAULT_CLASS_HASH), // Nothing changed. + ]), + HashMap::from([(11111, 1)]), + HashMap::new(), + HashMap::from([(0, 131), (11111, 128), (222222, 129), (3333333, 130)] + ) +)] +#[case::non_allocation_with_trivial_class_hash_update( + HashMap::new(), + HashMap::from([(24, 1), (5000, 1), (6666, 1), (9999, 1), (11111, DEFAULT_CLASS_HASH), + ]), + HashMap::new(), + HashMap::from([(0, 133), (5000, 128), (11111, 129), (222222, 130), (3333333, 131), (87777, 132)]), + HashMap::from([(0, 135), (5000, 128), (6666, 133), (9999, 134)] + ) +)] +#[case::allocation_with_partially_trivial_updates( + HashMap::from([ + ( + 1, + HashMap::from([(777, 1), (8888, 1), (9999, 1)]) + ), + ( + 100, + HashMap::from([(200, 1), (777, 1), (888, 0)]) + ), + ( + 600, + HashMap::from([(2000, 1), (3000, 1)]) + ), + ( + 800, + HashMap::from([(700, 1), (701, 1)]) + ), + ( + 3000, + HashMap::from([(600, 1), (2000, 1)]) + ), + ( + 10000, + HashMap::from([(34567, 0), (435, 0)]) + ) + ]), + HashMap::from([(200, 1), (500, 1), (700, 1), (800, DEFAULT_CLASS_HASH)]), + HashMap::from([(700, 1), (10000, 0)]), + HashMap::new(), + HashMap::from([(0, 137), (200, 128), (500, 130), (600, 133), (700, 134), (701, 135), (777, 129), (800, 136), (2000, 131), (3000, 132)]) +)] fn test_allocate_addresses_for_state_diff_and_replace( #[case] storage_updates: HashMap>, #[case] address_to_class_hash: HashMap, @@ -381,13 +506,22 @@ fn test_allocate_addresses_for_state_diff_and_replace( EndpointArg::Value(ValueArg::Single(n_contracts.into())), EndpointArg::Pointer(PointerArg::Array(flat_contract_state_changes)), ]; - let expected_explicit_return_values = vec![]; // COMPLETE! let storage_view = initial_alias_storage .into_iter() .map(|(key, value)| ((*ALIAS_CONTRACT_ADDRESS, key.into()), value.into())) .collect(); let state_reader = DictStateReader { storage_view, ..Default::default() }; - run_cairo_0_entrypoint( + let expected_aliases_storage_flat_length = expected_alias_storage.len() * DICT_ACCESS_SIZE; + let expected_explicit_return_values = vec![ + EndpointArg::Pointer(PointerArg::Array(vec![ + MaybeRelocatable::Int(Felt::ZERO); + expected_aliases_storage_flat_length + ])), + EndpointArg::Pointer(PointerArg::Array(vec![])), + EndpointArg::Pointer(PointerArg::Array(vec![])), + ]; + + let (_, explicit_return_values) = run_cairo_0_entrypoint( entrypoint, &explicit_args, &implicit_args, @@ -396,5 +530,27 @@ fn test_allocate_addresses_for_state_diff_and_replace( &program, &runner_config, &expected_explicit_return_values, - ); + ) + .unwrap(); + + // TODO(Nimrod): Complete this test to also compare the other return values. + if let [ + EndpointArg::Pointer(PointerArg::Array(aliases_storage_updates)), + EndpointArg::Pointer(PointerArg::Array(_)), + EndpointArg::Pointer(PointerArg::Array(_)), + ] = explicit_return_values.as_slice() + { + let aliases_storage_updates_as_felts: Vec = + aliases_storage_updates.iter().map(|f| f.get_int().unwrap()).collect(); + let actual_alias_storage = parse_squashed_cairo_dict(&aliases_storage_updates_as_felts); + let expected_alias_storage: HashMap = expected_alias_storage + .into_iter() + .map(|(key, value)| (key.into(), value.into())) + .collect(); + assert_eq!(actual_alias_storage, expected_alias_storage); + } else { + panic!( + "The return value doesn't match the given format.\n Got: {explicit_return_values:?}" + ); + } }