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

Conversation

Odomey
Copy link

@Odomey Odomey commented Dec 23, 2024

Description

This PR improves the test infrastructure by reducing code duplication and implementing proper lowering test functionality in examples_test.rs.

Changes

Code Duplication Reduction

  • Added TestConfig struct to encapsulate test configuration parameters:
    • auto_add_withdraw_gas: bool
    • available_gas: Option<usize>
    • expected_cost: Option<usize>
  • Created unified run_test_function to handle both regular and gas-enabled test cases
  • Consolidated duplicate test functions into a single implementation with configurable behavior

Lowering Test Implementation

  • Implemented proper lowering_test functionality to verify compiler behavior
  • Added comprehensive testing of function lowering for all modules
  • Improved error reporting for lowering failures
  • Added documentation explaining the test's purpose and functionality

Testing

  • All existing tests pass with the new structure
  • Added verification for function lowering across all modules
  • Maintained backward compatibility with existing test cases

Related Issues

  • Fixes unused test function warning
  • Reduces code maintenance burden through better structure

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 files reviewed, 2 unresolved discussions (waiting on @Odomey)


tests/examples_test.rs line 42 at r1 (raw file):

}

#[rstest]

you misunderstood the meaning of this test.
remove all changes related to it.

you may possibly leave the changes regarding the test config, and merge the two test-cases runners.

Code quote:

#[rstest]

tests/examples_test.rs line 50 at r1 (raw file):

    /// Expected gas cost
    expected_cost: Option<usize>,
}

consider adding specific constructors that are repeated between test cases.

Code quote:

    /// 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>,
}

@Odomey
Copy link
Author

Odomey commented Dec 23, 2024

@orizi Updated. Thanks for the answer

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 files reviewed, 3 unresolved discussions (waiting on @Odomey)


tests/examples_test.rs line 42 at r1 (raw file):

Previously, orizi wrote…

you misunderstood the meaning of this test.
remove all changes related to it.

you may possibly leave the changes regarding the test config, and merge the two test-cases runners.

you haven't reverted the change of the lowering test.

please review changes at https://reviewable.io/reviews/starkware-libs/cairo/6915#-


tests/examples_test.rs line 379 at r2 (raw file):

) {
    run_test_function(name, params, config, expected_result, example_dir_data);
}

Suggestion:

#[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_with_gas_test(
    #[case] name: &str,
    #[case] params: &[Felt252],
    #[case] config: TestConfig,
    #[case] expected_result: RunResultValue,
    example_dir_data: &ExampleDirData,
) {
    // inline and remove the called function.
}

@Odomey
Copy link
Author

Odomey commented Dec 23, 2024

@orizi updated

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please answer in the reviewable threads.

Reviewable status: 0 of 1 files reviewed, 3 unresolved discussions (waiting on @Odomey)

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 42 at r1 (raw file):

Previously, orizi wrote…

you haven't reverted the change of the lowering test.

please review changes at https://reviewable.io/reviews/starkware-libs/cairo/6915#-

/// 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,

        }

    }

}

// ... (остальной код остается без изменений до тестовых функций)

#[rstest]

#[case::fib("fib", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),

    RunResultValue::Success(vec![Felt252::from(21)]))]

// ... (остальные тест-кейсы без газа)

#[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::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] config: TestConfig,

    #[case] 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

    );

}

#[rstest]

fn lowering_test(example_dir_data: &ExampleDirData) {}

This is how i corrected it

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 50 at r1 (raw file):

Previously, orizi wrote…

consider adding specific constructors that are repeated between test cases.

Corrected

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 379 at r2 (raw file):

) {
    run_test_function(name, params, config, expected_result, example_dir_data);
}

Done as it seems to me

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r3, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Odomey)


tests/examples_test.rs line 42 at r1 (raw file):

Previously, Odomey (Odomey) wrote…

/// 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,

        }

    }

}

// ... (остальной код остается без изменений до тестовых функций)

#[rstest]

#[case::fib("fib", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(),

    RunResultValue::Success(vec![Felt252::from(21)]))]

// ... (остальные тест-кейсы без газа)

#[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::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] config: TestConfig,

    #[case] 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

    );

}

#[rstest]

fn lowering_test(example_dir_data: &ExampleDirData) {}

This is how i corrected it

you just move it to the bottom.
move it back up please to remove this snippet from being touched in this unrelated PR.

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 42 at r1 (raw file):

Previously, orizi wrote…

you just move it to the bottom.
move it back up please to remove this snippet from being touched in this unrelated PR.

Done

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @Odomey)


tests/examples_test.rs line 252 at r4 (raw file):

#[rstest]
#[case::fib("fib", &[1, 1, 7].map(Felt252::from), TestConfig::without_gas(), 

please make sure to not reorder the tests (as it makes it impossible to ascertain non were dropped).

additionally - prevent lines from exceeding 100 chars (as was before)

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 252 at r4 (raw file):

Previously, orizi wrote…

please make sure to not reorder the tests (as it makes it impossible to ascertain non were dropped).

additionally - prevent lines from exceeding 100 chars (as was before)

Done

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 0 of 1 files reviewed, 3 unresolved discussions (waiting on @Odomey)


tests/examples_test.rs line 230 at r5 (raw file):

#[case::fib(
    "fib_loop",
    &[1, 1, 7].map(Felt252::from), None, None,

this test seem to have dropped.

Code quote:

#[case::fib(
    "fib_loop",
    &[1, 1, 7].map(Felt252::from), None, None,

tests/examples_test.rs line 362 at r5 (raw file):

    &[1, 1, 7].map(Felt252::from),
    TestConfig::without_gas(),
    RunResultValue::Success(vec![Felt252::from(21)])

these does not look the proper args for these tests.

Code quote:

#[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)])

tests/examples_test.rs line 488 at r5 (raw file):

            // `r.low`
            Felt252::from(1 + 2 + 3 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18),
            // `r.high`

this seems very broken.

Code quote:

            // `r.low`
            Felt252::from(1 + 2 + 3 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18),
            // `r.high`

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 488 at r5 (raw file):

Previously, orizi wrote…

this seems very broken.

Resolved

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 362 at r5 (raw file):

Previously, orizi wrote…

these does not look the proper args for these tests.

Resolved

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 0 of 1 files reviewed, 2 unresolved discussions (waiting on @Odomey)


tests/examples_test.rs line 488 at r5 (raw file):

Previously, Odomey (Odomey) wrote…

Resolved

no it isnt.

make sure to run:
cargo t --profile=ci-dev -p tests and ./scripts/rust_fmt.sh.

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 230 at r5 (raw file):

Previously, orizi wrote…

this test seem to have dropped.

Done

@Odomey
Copy link
Author

Odomey commented Dec 24, 2024

tests/examples_test.rs line 488 at r5 (raw file):

Previously, orizi wrote…

no it isnt.

make sure to run:
cargo t --profile=ci-dev -p tests and ./scripts/rust_fmt.sh.

Done, as it seems to me((

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed all commit messages.
Reviewable status: 0 of 1 files reviewed, 4 unresolved discussions (waiting on @Odomey)


tests/examples_test.rs line 323 at r7 (raw file):

    TestConfig::without_gas(),
    RunResultValue::Success(vec![Felt252::from(21)])
)]

put it back in order.

Code quote:

#[case::fib_loop(
    "fib_loop",
    &[1, 1, 7].map(Felt252::from),
    TestConfig::without_gas(),
    RunResultValue::Success(vec![Felt252::from(21)])
)]

tests/examples_test.rs line 423 at r7 (raw file):

        ])
    );
}

please run again - this does not actually parse.

Code quote:

}

@Odomey
Copy link
Author

Odomey commented Dec 27, 2024

Reviewed all commit messages.
Reviewable status: 0 of 1 files reviewed, 4 unresolved discussions (waiting on @Odomey)

tests/examples_test.rs line 323 at r7 (raw file):

    TestConfig::without_gas(),
    RunResultValue::Success(vec![Felt252::from(21)])
)]

put it back in order.

Code quote:

#[case::fib_loop(
    "fib_loop",
    &[1, 1, 7].map(Felt252::from),
    TestConfig::without_gas(),
    RunResultValue::Success(vec![Felt252::from(21)])
)]

tests/examples_test.rs line 423 at r7 (raw file):

        ])
    );
}

please run again - this does not actually parse.

Code quote:

}

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants