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

Parameterization of const generics? #300

Open
janbridley opened this issue Mar 7, 2025 · 0 comments
Open

Parameterization of const generics? #300

janbridley opened this issue Mar 7, 2025 · 0 comments

Comments

@janbridley
Copy link

Is there a clear-cut way to parameterize const generics? I see #207 mentions generic types, but I'm not sure if those solutions translate here - I haven't figured out a method by which rstest can pass const values. I have an example where I parameterize the dimension of a struct and would like to use the same test for multiple const generics

pub struct Sphere<const N: usize> {
    /// Radius of the sphere
    pub r: f64,
    pub center: [f64; N],
}

// ...

Currently, I parameterize tests with a declarative macro but I'm curious as to whether rstest provides a cleaner option:

    macro_rules! parameterize_dimension {
        ($test_body:ident, [$($dim:expr),*]) => {
            $(
                paste! {
                    #[test]
                    fn [< $test_body "_" $dim>]() {
                        const DIM: usize = $dim;
                        $test_body::<DIM>();
                    }
                }
            )*
        };
    }
    fn test_radius<const N: usize>() {
        let s = Sphere::<N>::from(1.0);
        assert_eq!(s.r, 1.0);
    }
    parameterize_vector_length!(test_radius, [0, 1, 2, 3, 4, 5]);
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

No branches or pull requests

1 participant