libcester icon indicating copy to clipboard operation
libcester copied to clipboard

Tests using c++ templates

Open MarcelInTO opened this issue 4 years ago • 1 comments

It would be useful to be able to define a test that uses C++ templates to test template functions. For example, if I have a function like this:

template <typename T>
inline bool isSubnormal(const T & v)
{
    return std::isfinite(v) && !std::isnormal(v) && v != T(0.0);
}

Currently, my tests look like this:

CESTER_TEST(isSubnormalFloatWithSubnormalInput, inst,
    float p1 = std::numeric_limits<float>::min() / float(2.0);
    cester_assert_true(isSubnormal(p1));
)
CESTER_TEST(isSubnormalDoubleWithSubnormalInput, inst,
    double p1 = std::numeric_limits<double>::min() / double(2.0);
    cester_assert_true(isSubnormal(p1));
)

It would be great to do something like:

CESTER_TEMPLATE_TEST(isSubnormalWithSubnormalInput, inst, T, (float, double, long double),
    T p1 = std::numeric_limits<T>::min() / T(2.0);
    cester_assert_true(isSubnormal(p1));
)

Basically - add parameters specifying the template variable and the list of types to instantiate.

It would have to generate names for each of the tests that include an indication what T was for that test.

I have not really thought through the ideal syntax, because if you have more than one template variable, my suggested syntax is not very practical, but you get the idea.....

MarcelInTO avatar Mar 10 '21 20:03 MarcelInTO

Great, I totally get the idea. I'll implement this + other possible generators in an upcoming release.

On Wed, Mar 10, 2021 at 9:42 PM MarcelInTO [email protected] wrote:

It would be useful to be able to define a test that uses C++ templates to test template functions. For example, if I have a function like this:

template inline bool isSubnormal(const T & v) { return std::isfinite(v) && !std::isnormal(v) && v != T(0.0); }

Currently, my tests look like this:

CESTER_TEST(isSubnormalFloatWithSubnormalInput, inst, float p1 = std::numeric_limits::min() / float(2.0); cester_assert_true(isSubnormal(p1)); ) CESTER_TEST(isSubnormalDoubleWithSubnormalInput, inst, double p1 = std::numeric_limits::min() / double(2.0); cester_assert_true(isSubnormal(p1)); )

It would be great to do something like:

CESTER_TEMPLATE_TEST(isSubnormalWithSubnormalInput, inst, T, (float, double, long double), T p1 = std::numeric_limits::min() / T(2.0); cester_assert_true(isSubnormal(p1)); )

Basically - add parameters specifying the template variable and the list of types to instantiate.

It would have to generate names for each of the tests that include an indication what T was for that test.

I have not really thought through the ideal syntax, because if you have more than one template variable, my suggested syntax is not very practical, but you get the idea.....

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/exoticlibraries/libcester/issues/48, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADRQVG7SQUSAVVGZVL6SPM3TC7DQZANCNFSM4Y623Q6A .

Thecarisma avatar Mar 10 '21 20:03 Thecarisma