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
116 changes: 85 additions & 31 deletions tests/examples_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,56 @@ fn example_dir_data() -> ExampleDirData {
(db.into(), crate_ids)
}

#[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,
}
}
}

/// Runs test function with the given configuration
fn run_test_function(
name: &str,
params: &[Felt252],
config: TestConfig,
expected_result: RunResultValue,
example_dir_data: &ExampleDirData,
) {
pretty_assertions::assert_eq!(
run_function(
name,
params,
config.available_gas,
config.expected_cost,
example_dir_data,
config.auto_add_withdraw_gas
),
expected_result
);
}

/// Returns the path of the relevant test file.
fn get_test_data_path(name: &str, test_type: &str) -> PathBuf {
Expand Down Expand Up @@ -222,106 +269,113 @@ fn run_function(
#[rstest]
#[case::fib(
"fib",
&[1, 1, 7].map(Felt252::from), None, None,
&[1, 1, 7].map(Felt252::from),
TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::fib(
"fib_loop",
&[1, 1, 7].map(Felt252::from), None, None,
&[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), None, None,
&[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), None, None,
&[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), None, None,
&[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), None, None,
&[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), None, None,
&[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), None, None,
&[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), None, None,
&[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), None, None,
&[6].map(Felt252::from),
TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(13)])
)]
#[case::fib_unary(
"fib_unary",
&[7].map(Felt252::from), None, None,
&[7].map(Felt252::from),
TestConfig::without_gas(),
RunResultValue::Success(vec![Felt252::from(21)])
)]
#[case::hash_chain(
"hash_chain",
&[3].map(Felt252::from), None, None,
&[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)),
&[3].map(Felt252::from),
TestConfig::with_gas(100000, Some(9880 + 3 * token_gas_cost(CostTokenType::Pedersen))),
RunResultValue::Success(vec![Felt252::from_hex_unchecked(
"2dca1ad81a6107a9ef68c69f791bcdbda1df257aab76bd43ded73d96ed6227d")]))]
fn run_function_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, false),
expected_result
);
run_test_function(name, params, config, expected_result, example_dir_data);
}

#[rstest]
#[case::fib_pass(
"fib",
&[1, 1, 10].map(Felt252::from), Some(200000), None,
&[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), Some(10000), None,
&[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_auto_gas_test(
fn run_function_with_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),
expected_result
);
run_test_function(name, params, config, expected_result, example_dir_data);
}

#[rstest]
Expand Down