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

[question] Test with different specializations of a generic type? #207

Open
Seanmatthews opened this issue Aug 23, 2023 · 4 comments
Open

Comments

@Seanmatthews
Copy link

Is it possible to test with different specializations of a generic type? For example, how can I do something like this:

struct MyStruct<T, const U: usize, V: MyEnumType> { ... }

impl<...> MyStruct<...> {
    const u: usize = U;
    
    fn do_something() -> usize { U + 1 } 
}

#[rstest]
#[case(f64, 5, MyEnumType::ONE)]
fn test_do_something<#[case] T, #[case] const U: usize, #[case] V: MyEnumType>() {
    let a = MyStruct<T, U, V>{};
    assert!(U + 1 == a.do_something());
}

The documentation mentions generics, but I don't see any examples of dealing with generics.

@la10736
Copy link
Owner

la10736 commented Aug 24, 2023

Yes you can but you you should just use values of different types. In your example you cannot build MyStruct without set all its inner values that constrains the generic types. I'll try to fill your example but ...

  1. MyEnumType should be a trait and not an enum... I remove it as generic type
  2. const generic argument are not supported yet
use rstest::rstest;

struct MyStruct<T, const U: usize> {
    a: [T; U],
}

impl<T, const U: usize> MyStruct<T, U> {
    const u: usize = U;

    fn do_something(&self) -> usize {
        U + 1
    }
}

#[rstest]
#[case(1_f64)]
fn test_do_something<T: Copy>(#[case] t: T) {
    let a = MyStruct { a: [t; 5] };
    assert!(6 == a.do_something());
}

If you can provide to me some more concrete examples I'll happy to help you. I can think to introduce something to define a const argument but I would like some examples were that is really necessary.

@benediktsatalia
Copy link

benediktsatalia commented Feb 6, 2024

I have the following use case of specifying generics explicitly:

I have a trait that defines a constant epsilon value on the type level:

pub trait ConstEps {
    fn eps() -> f64;
}

then I have a function that does some calculation and uses a generic epsilon value for numerical precision, for example:

fn almost_equal<E: ConstEps>(left: f64, right: f64) -> bool {
    (right-left).abs() < E::eps()
}

I can have now many different epsilon types like:

struct Eps1eMinus7;
impl ConstEps for Eps1eMinus7 {
    fn eps() -> f64 {
        1e-7
    }
}

struct Eps1eMinus8;
impl ConstEps for Eps1eMinus8 {
    fn eps() -> f64 {
        1e-8
    }
}

struct Eps1eMinus9;
impl ConstEps for Eps1eMinus9 {
    fn eps() -> f64 {
        1e-9
    }
}
...

Now I want to test this function with various different epsilons (and left and right values). I don't know how I could do this at the moment without writing separate tests for each epsilon.

Any ideas? Note that the given example function almost_equal is of course just a simple example, my real use case has much more complex numerical calculations that use the generic epsilon value.

Edit: I found a solution with a slight hack, if there would be native support it could be much nicer I think:

use rstest::rstest;
use std::marker::PhantomData;

#[rstest]
#[case(PhantomData::<Eps1eMinus7>, 0.50000001f64, 0.5, true)]
#[case(PhantomData::<Eps1eMinus9>, 0.50000001f64, 0.5, false)]
fn test_almost_equal<E: ConstEps>(
    #[case] _e: PhantomData<E>,
    #[case] left: f64,
    #[case] right: f64,
    #[case] expect: bool,
) {
    let result = almost_equal::<E>(left, right);
    assert_eq!(result, expect);
}

@la10736
Copy link
Owner

la10736 commented Feb 6, 2024

That's exactly what I were pasting right now 😄

Yesterday I saw something in divan crate documentation that ring me a bell... So maybe I can introduce the following syntax:

#[rstest]
#[case(Eps1eMinus7, 0.50000001f64, 0.5, true)]
#[case(Eps1eMinus9, 0.50000001f64, 0.5, false)]
fn test_almost_equal<#[case] E: ConstEps>(
    #[case] left: f64,
    #[case] right: f64,
    #[case] expect: bool,
) {
    let result = almost_equal::<E>(left, right);
    assert_eq!(result, expect);
}

The sad news is that I've no time to implement it .... 😢

@benediktsatalia
Copy link

benediktsatalia commented Feb 6, 2024

Thank you for the quick answer! That would be nice to have this syntax, although I understand it is not high priority (the PhantomData version works well enough).

Btw. there is an interesting edge case if one uses _ as variable name the macro fails:

If I use

#[rstest]
#[case(PhantomData::<Eps1eMinus7>, 0.50000001f64, 0.5, true)]
#[case(PhantomData::<Eps1eMinus9>, 0.50000001f64, 0.5, false)]
fn test_almost_equal<E: ConstEps>(
    #[case] _: PhantomData<E>,
    #[case] left: f64,
    #[case] right: f64,
    #[case] expect: bool,
) {
    let result = almost_equal::<E>(left, right);
    assert_eq!(result, expect);
}

compilation fails with:

error: Wrong case signature: should match the given parameters list.
   --> src/investigate_rstest.rs:51:16
    |
 51 |         #[case(PhantomData::<Eps1eMinus7>, 0.50000001f64, 0.5, true)]
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: Wrong case signature: should match the given parameters list.
   --> src/investigate_rstest.rs:52:16
    |
 52 |         #[case(PhantomData::<Eps1eMinus9>, 0.50000001f64, 0.5, false)]
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

3 participants