diff --git a/Cargo.lock b/Cargo.lock index 45148736aa..60e1dc5ad9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1321,6 +1321,7 @@ dependencies = [ name = "cairo1-run" version = "0.1.0" dependencies = [ + "assert_matches", "bincode", "cairo-lang-compiler 2.1.0 (git+https://github.com/starkware-libs/cairo)", "cairo-lang-runner", @@ -1330,6 +1331,7 @@ dependencies = [ "cairo-vm 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)", "clap", "itertools 0.11.0", + "rstest", "thiserror", ] diff --git a/cairo1-run/Cargo.toml b/cairo1-run/Cargo.toml index a874154a72..f022a8f145 100644 --- a/cairo1-run/Cargo.toml +++ b/cairo1-run/Cargo.toml @@ -15,4 +15,6 @@ cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo" } itertools = "0.11.0" clap = { version = "4.3.10", features = ["derive"] } thiserror = { version = "1.0.40" } -bincode.workspace = true \ No newline at end of file +bincode.workspace = true +assert_matches = "1.5.0" +rstest = "0.17.0" \ No newline at end of file diff --git a/cairo1-run/Makefile b/cairo1-run/Makefile new file mode 100644 index 0000000000..1a17ef22cb --- /dev/null +++ b/cairo1-run/Makefile @@ -0,0 +1,2 @@ +make test: + cargo test \ No newline at end of file diff --git a/cairo1-run/cairo1-programs/factorial.cairo b/cairo1-run/cairo1-programs/factorial.cairo new file mode 100644 index 0000000000..937aca7bf5 --- /dev/null +++ b/cairo1-run/cairo1-programs/factorial.cairo @@ -0,0 +1,14 @@ +use core::felt252; + +fn main() -> felt252 { + let n = 10; + let result = factorial(n); + result +} + +fn factorial(n: felt252) -> felt252 { + match n { + 0 => 1, + _ => n * factorial(n - 1), + } +} diff --git a/cairo1-run/fibonacci.cairo b/cairo1-run/cairo1-programs/fibonacci.cairo similarity index 100% rename from cairo1-run/fibonacci.cairo rename to cairo1-run/cairo1-programs/fibonacci.cairo diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 68f5db563f..53e94a1b5a 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -37,13 +37,6 @@ use std::path::PathBuf; use std::{collections::HashMap, io, path::Path}; use thiserror::Error; -#[cfg(feature = "with_mimalloc")] -use mimalloc::MiMalloc; - -#[cfg(feature = "with_mimalloc")] -#[global_allocator] -static ALLOC: MiMalloc = MiMalloc; - #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct Args { @@ -51,20 +44,10 @@ struct Args { filename: PathBuf, #[clap(long = "trace_file", value_parser)] trace_file: Option, - #[structopt(long = "print_output")] - print_output: bool, - #[structopt(long = "entrypoint", default_value = "main")] - entrypoint: String, #[structopt(long = "memory_file")] memory_file: Option, #[clap(long = "layout", default_value = "plain", value_parser=validate_layout)] layout: String, - #[structopt(long = "proof_mode")] - proof_mode: bool, - #[structopt(long = "secure_run")] - secure_run: Option, - #[clap(long = "air_public_input")] - air_public_input: Option, } fn validate_layout(value: &str) -> Result { @@ -245,8 +228,6 @@ fn run(args: impl Iterator) -> Result<(), Error> { cairo_run::write_encoded_memory(&runner.relocated_memory, &mut memory_writer)?; memory_writer.flush().unwrap(); } - println!(); - println!("Cairo1 program ran successfully"); Ok(()) } @@ -277,3 +258,25 @@ fn main() -> Result<(), Error> { other => other, } } + +#[cfg(test)] +mod tests { + #![allow(clippy::too_many_arguments)] + use super::*; + use assert_matches::assert_matches; + use rstest::rstest; + + #[rstest] + #[case(["cairo1-run", "cairo1-programs/fibonacci.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null"].as_slice())] + fn test_run_fibonacci_ok(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Ok(())); + } + + #[rstest] + #[case(["cairo1-run", "cairo1-programs/factorial.cairo", "--trace_file", "/dev/null", "--memory_file", "/dev/null"].as_slice())] + fn test_run_factorial_ok(#[case] args: &[&str]) { + let args = args.iter().cloned().map(String::from); + assert_matches!(run(args), Ok(())); + } +}