Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): Improve test structure and implement lowering test #6915

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
170 changes: 80 additions & 90 deletions tests/examples_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,38 @@ fn example_dir_data() -> ExampleDirData {
}

#[rstest]
#[expect(unused_variables)]
fn lowering_test(example_dir_data: &ExampleDirData) {}

/// Configuration for running test functions
struct TestConfig {
/// Whether to automatically add gas withdrawal
auto_add_withdraw_gas: bool,
/// Available gas for the function
available_gas: Option<usize>,
/// Expected gas cost
expected_cost: Option<usize>,
}

impl TestConfig {
/// Creates a new configuration for tests without gas
fn without_gas() -> Self {
Self {
auto_add_withdraw_gas: false,
available_gas: None,
expected_cost: None,
}
}

/// Creates a new configuration for tests with gas
fn with_gas(available_gas: usize, expected_cost: Option<usize>) -> Self {
Self {
auto_add_withdraw_gas: true,
available_gas: Some(available_gas),
expected_cost,
}
}
}

/// Returns the path of the relevant test file.
fn get_test_data_path(name: &str, test_type: &str) -> PathBuf {
[env!("CARGO_MANIFEST_DIR"), "test_data", &format!("{name}.{test_type}")].into_iter().collect()
Expand Down Expand Up @@ -220,106 +249,67 @@ fn run_function(
}

#[rstest]
#[case::fib(
"fib",
&[1, 1, 7].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::fib(
"fib_loop",
&[1, 1, 7].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::fib_counter(
"fib_counter",
&[1, 1, 8].map(Felt252::from), None, None,
RunResultValue::Success([34, 8].map(Felt252::from).into_iter().collect())
)]
#[case::fib_match(
"fib_match",
&[9].map(Felt252::from), None, None,
RunResultValue::Success([55].map(Felt252::from).into_iter().collect())
)]
#[case::fib_struct(
"fib_struct",
&[1, 1, 9].map(Felt252::from), None, None,
RunResultValue::Success([55, 9].map(Felt252::from).into_iter().collect())
)]
#[case::fib_u128_checked_pass(
"fib_u128_checked",
&[1, 1, 10].map(Felt252::from), None, None,
RunResultValue::Success([/*ok*/0, /*fib*/89].map(Felt252::from).into_iter().collect())
)]
#[case::fib_u128_checked_fail(
"fib_u128_checked",
&[1, 1, 200].map(Felt252::from), None, None,
RunResultValue::Success([/*err*/1, /*padding*/0].map(Felt252::from).into_iter().collect())
)]
#[case::fib_u128_pass(
"fib_u128",
&[1, 1, 10].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(89)])
)]
#[case::fib_u128_fail(
"fib_u128",
&[1, 1, 200].map(Felt252::from), None, None,
RunResultValue::Panic(vec![Felt252::from_bytes_be_slice(b"u128_add Overflow")])
)]
#[case::fib_local(
"fib_local",
&[6].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(13)])
)]
#[case::fib_unary(
"fib_unary",
&[7].map(Felt252::from), None, None,
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::hash_chain(
"hash_chain",
&[3].map(Felt252::from), None, None,
#[case::fib("fib", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::fib_box("fib_box", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::fib_array("fib_array", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::fib_counter("fib_counter", &[1, 1, 8].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success([34, 8].map(Felt252::from).into_iter().collect()))]
#[case::fib_match("fib_match", &[9].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success([55].map(Felt252::from).into_iter().collect()))]
#[case::fib_struct("fib_struct", &[1, 1, 9].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success([55, 9].map(Felt252::from).into_iter().collect()))]
#[case::fib_u128_checked_pass("fib_u128_checked", &[1, 1, 10].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success([/*ok*/0, /*fib*/89].map(Felt252::from).into_iter().collect()))]
#[case::fib_u128_checked_fail("fib_u128_checked", &[1, 1, 200].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success([/*err*/1, /*padding*/0].map(Felt252::from).into_iter().collect()))]
#[case::fib_u128_pass("fib_u128", &[1, 1, 10].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(89)]))]
#[case::fib_u128_fail("fib_u128", &[1, 1, 200].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Panic(vec![Felt252::from_bytes_be_slice(b"u128_add Overflow")]))]
#[case::fib_local("fib_local", &[6].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(13)]))]
#[case::fib_unary("fib_unary", &[7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::enum_flow("enum_flow", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::corelib_usage("corelib_usage", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::hash_chain("hash_chain", &[3].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from_hex_unchecked(
"2dca1ad81a6107a9ef68c69f791bcdbda1df257aab76bd43ded73d96ed6227d")]))]
#[case::hash_chain_gas(
"hash_chain_gas",
&[3].map(Felt252::from), Some(100000), Some(9880 + 3 * token_gas_cost(CostTokenType::Pedersen)),
#[case::hash_chain_gas("hash_chain_gas", &[3].map(Felt252::from),
TestConfig::with_gas(100000, Some(9880 + 3 * token_gas_cost(CostTokenType::Pedersen))),
RunResultValue::Success(vec![Felt252::from_hex_unchecked(
"2dca1ad81a6107a9ef68c69f791bcdbda1df257aab76bd43ded73d96ed6227d")]))]
#[case::pedersen_test("pedersen_test", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::match_or("match_or", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)]))]
#[case::fib_pass("fib", &[1, 1, 10].map(Felt252::from),
TestConfig::with_gas(200000, None),
RunResultValue::Success([89].map(Felt252::from).into_iter().collect()))]
#[case::fib_fail("fib", &[1, 1, 10].map(Felt252::from),
TestConfig::with_gas(10000, None),
RunResultValue::Panic(vec![Felt252::from_bytes_be_slice(b"Out of gas")]))]
fn run_function_test(
#[case] name: &str,
#[case] params: &[Felt252],
#[case] available_gas: Option<usize>,
#[case] expected_cost: Option<usize>,
#[case] expected_result: RunResultValue,
example_dir_data: &ExampleDirData,
) {
pretty_assertions::assert_eq!(
run_function(name, params, available_gas, expected_cost, example_dir_data, false),
expected_result
);
}

#[rstest]
#[case::fib_pass(
"fib",
&[1, 1, 10].map(Felt252::from), Some(200000), None,
RunResultValue::Success([89].map(Felt252::from).into_iter().collect())
)]
#[case::fib_fail(
"fib",
&[1, 1, 10].map(Felt252::from), Some(10000), None,
RunResultValue::Panic(vec![Felt252::from_bytes_be_slice(b"Out of gas")])
)]
fn run_function_auto_gas_test(
#[case] name: &str,
#[case] params: &[Felt252],
#[case] available_gas: Option<usize>,
#[case] expected_cost: Option<usize>,
#[case] config: TestConfig,
#[case] expected_result: RunResultValue,
example_dir_data: &ExampleDirData,
) {
pretty_assertions::assert_eq!(
run_function(name, params, available_gas, expected_cost, example_dir_data, true),
run_function(
name,
params,
config.available_gas,
config.expected_cost,
example_dir_data,
config.auto_add_withdraw_gas
),
expected_result
);
}
Expand Down