-
Notifications
You must be signed in to change notification settings - Fork 43
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
Comments
Yes you can but you you should just use values of different types. In your example you cannot build
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 |
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 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);
} |
That's exactly what I were pasting right now 😄 Yesterday I saw something in #[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 .... 😢 |
Thank you for the quick answer! That would be nice to have this syntax, although I understand it is not high priority (the Btw. there is an interesting edge case if one uses 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:
|
Is it possible to test with different specializations of a generic type? For example, how can I do something like this:
The documentation mentions generics, but I don't see any examples of dealing with generics.
The text was updated successfully, but these errors were encountered: