Giter Site home page Giter Site logo

Comments (4)

la10736 avatar la10736 commented on July 30, 2024 3

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 .... 😢

from rstest.

la10736 avatar la10736 commented on July 30, 2024

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.

from rstest.

benediktsatalia avatar benediktsatalia commented on July 30, 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);
}

from rstest.

benediktsatalia avatar benediktsatalia commented on July 30, 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)]
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

from rstest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.