Boost logo

Boost :

Subject: Re: [boost] [test] Test cases parametrized by both type and value?
From: Raffi Enficiaud (raffi.enficiaud_at_[hidden])
Date: 2018-01-05 19:16:31


Le 05.01.18 à 12:39, Andrzej Krzemienski via Boost a écrit :
> Hi,
> Can anyone tell me if Boost.Test can solve the problem that I describe
> below.
>
> I have a function template inspect:
>
> ```
> template <typename T>
> bool inspect(T const& v);
> ```
>
> I want to test if it returns `true` for the following values:
>
> 1 (type int)
> 10 (type int)
> 1.0 (type double)
> "Y" (type const char *)
>
> Basically, different values of different types, but sometimes different
> values have the same type. I would like to write one test or "test
> template" with the following contents:
>
> ```
> BOOST_TEST((inspect(v)));
> ```
>
> Where `v` is name of the currently tested value. (But `v`s will have
> different types). I can emulate this behavior using template test cases and
> artificial types:
>
> ```
> struct int_1 {
> static int value() { return 1; }
> };
> struct int_10 {
> static int value() { return 10; }
> };
> struct double_1 {
> static double value() { return 1.0; }
> };
> struct cstr_Y {
> static const char * value() { return "Y"; }
> };
>
> typedef boost::mpl::list<int_1, int_10, double_1, cstr_Y> test_types;
>
> BOOST_AUTO_TEST_CASE_TEMPLATE( my_test, T, test_types )
> {
> BOOST_TEST(inspect(T::value()));
> }
> ```
>
> (See working example in WandBox:
> https://wandbox.org/permlink/KcJqT8KvL3zIqAxL)
>
> But maybe there exists a direct solution in Boost.Test? Where I would just
> pass a list of values: `{1, 10, 1.0, "Y"}`
>
> It is easy to deduce types from values:
>
> ```
> template <typename... T>
> mpl::liss<T...> Values(T... values);
>
> using test_types = decltype(Values(1, 10, 1.0, "Y"));
> ```
>
> Does something like this exist, or ois it a reasonable extension?

As Steven said, it was considered during the initial developments from
Gennadiy but nobody expressed the need, so we did not go further that
direction.

I recently extended the inference of the types from
BOOST_AUTO_TEST_CASE_TEMPLATE to consider tuples instead of an mpl::list
(trac https://svn.boost.org/trac10/ticket/12092) but this is only
considering types. Adding the value carried by the tuple should not be
too difficult, and I think would address your feature request.

OTOH, providing an compile time iterator interface to datasets for
polymorphic collections is a nice feature. Will consider it again :) but
there are other things I would like to push forward first.

Raffi


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk