Skip to content

Commit

Permalink
fix: cairo1-run array argument's length can not be less than 2 (#1737)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Pedro Fontana <[email protected]>
Co-authored-by: Mario Rugiero <[email protected]>
  • Loading branch information
4 people committed May 9, 2024
1 parent dd6c311 commit f47f760
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
44 changes: 29 additions & 15 deletions cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,36 @@ fn process_args(value: &str) -> Result<FuncArgs, String> {
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()))
Expand Down Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions cairo_programs/cairo-1-programs/with_input/array_length.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use array::ArrayTrait;

fn main(array_a: Array<u32>, array_b: Array<u32>) -> u32 {
array_a.len() + array_b.len()
}

0 comments on commit f47f760

Please sign in to comment.