From f47f760d26be0e021bd37be3fa482807eda8e515 Mon Sep 17 00:00:00 2001 From: catusax <11882183+catusax@users.noreply.github.com> Date: Thu, 9 May 2024 10:57:35 +0800 Subject: [PATCH] fix: cairo1-run array argument's length can not be less than 2 (#1737) * fix: cairo1-run array argument's length can not be less than 2 * update changelog * update changelog * test: add test case for array args --------- Co-authored-by: fmoletta <99273364+fmoletta@users.noreply.github.com> Co-authored-by: Pedro Fontana Co-authored-by: Mario Rugiero --- CHANGELOG.md | 3 +- cairo1-run/src/main.rs | 44 ++++++++++++------- .../with_input/array_length.cairo | 5 +++ 3 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 cairo_programs/cairo-1-programs/with_input/array_length.cairo diff --git a/CHANGELOG.md b/CHANGELOG.md index a07ce1a7a5..e4e2f10302 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* fix: add support for arrays shorter than 2 as arguments for cairo1-run [#1737](https://github.com/lambdaclass/cairo-vm/pull/1737) + * bugfix: Fix BuiltinRunner::final_stack for SegmentArena[#1747](https://github.com/lambdaclass/cairo-vm/pull/1747) * feat: unify `arbitrary`, `hooks`, `print` and `skip_next_instruction_hint` features as a single `test_utils` feature [#1755](https://github.com/lambdaclass/cairo-vm/pull/1755) @@ -16,7 +18,6 @@ #### [1.0.0-rc2] - 2024-05-02 - * `cairo1-run` CLI: Allow loading arguments from file[#1739](https://github.com/lambdaclass/cairo-vm/pull/1739) * BREAKING: Remove unused `CairoRunner` field `original_steps`[#1742](https://github.com/lambdaclass/cairo-vm/pull/1742) diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index e31619b624..cd9d01b5ff 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -72,24 +72,36 @@ fn process_args(value: &str) -> Result { while let Some(value) = input.next() { // First argument in an array if value.starts_with('[') { - let mut array_arg = - vec![Felt252::from_dec_str(value.strip_prefix('[').unwrap()).unwrap()]; - // Process following args in array - let mut array_end = false; - while !array_end { - if let Some(value) = input.next() { - // Last arg in array - if value.ends_with(']') { - array_arg - .push(Felt252::from_dec_str(value.strip_suffix(']').unwrap()).unwrap()); - array_end = true; - } else { - array_arg.push(Felt252::from_dec_str(value).unwrap()) + if value.ends_with(']') { + if value.len() == 2 { + args.push(FuncArg::Array(Vec::new())); + } else { + args.push(FuncArg::Array(vec![Felt252::from_dec_str( + value.strip_prefix('[').unwrap().strip_suffix(']').unwrap(), + ) + .unwrap()])); + } + } else { + let mut array_arg = + vec![Felt252::from_dec_str(value.strip_prefix('[').unwrap()).unwrap()]; + // Process following args in array + let mut array_end = false; + while !array_end { + if let Some(value) = input.next() { + // Last arg in array + if value.ends_with(']') { + array_arg.push( + Felt252::from_dec_str(value.strip_suffix(']').unwrap()).unwrap(), + ); + array_end = true; + } else { + array_arg.push(Felt252::from_dec_str(value).unwrap()) + } } } + // Finalize array + args.push(FuncArg::Array(array_arg)) } - // Finalize array - args.push(FuncArg::Array(array_arg)) } else { // Single argument args.push(FuncArg::Single(Felt252::from_dec_str(value).unwrap())) @@ -286,6 +298,8 @@ mod tests { #[case("null_ret.cairo", "null", None)] #[case("with_input/tensor.cairo", "1", Some("[2 2] [1 2 3 4]"))] #[case("with_input/array_input_sum.cairo", "12", Some("2 [1 2 3 4] 0 [9 8]"))] + #[case("with_input/array_length.cairo", "5", Some("[1 2 3 4] [1]"))] + #[case("with_input/array_length.cairo", "4", Some("[1 2 3 4] []"))] #[case("with_input/branching.cairo", "0", Some("17"))] #[case("with_input/branching.cairo", "1", Some("0"))] #[case("dictionaries.cairo", "1024", None)] diff --git a/cairo_programs/cairo-1-programs/with_input/array_length.cairo b/cairo_programs/cairo-1-programs/with_input/array_length.cairo new file mode 100644 index 0000000000..40558cb829 --- /dev/null +++ b/cairo_programs/cairo-1-programs/with_input/array_length.cairo @@ -0,0 +1,5 @@ +use array::ArrayTrait; + +fn main(array_a: Array, array_b: Array) -> u32 { + array_a.len() + array_b.len() +}