Boost logo

Boost :

Subject: Re: [boost] [boost-test] question about using templates for tests
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2014-07-19 17:33:12

On Saturday 19 July 2014 15:38:23 Damian Vicino wrote:
> I read it this example in the documentation
> > You can create test templates like this:
> >
> > typedef mpl::vector< T1, T2, T3 > types;
> >
> > BOOST_AUTO_TEST_CASE_TEMPLATE(my_test, T, types)
> > {
> >
> > // Here T will be one of the 'types'
> >
> > }
> >
> > In your case you can create an mpl::vector of specializations of your
> > class C you want to test.
> If I understood wheel the documentation that will run 3 different tests with
> T1, T2 and T3 as the T of the test.
> Maybe my previous problem is easier to explain if I break it in 2 different
> questions:
> 1. How can I pass more than one T to the test, in my case I need 3 Ts in the
> test which each has different values.

As I suggested, you can specify a type sequence of your type specializations,
which will have all 3 types specified.

> A simpler scenario in this case would be trying to test this:
> T1 a;
> T2 b;
> BOOST_CHECK(a+b == b+a);
> }
> And suppose I want to run the test with 4 combinations of parameters:
> T1=int, T2=float
> T1=float, T2=int
> T1=int, T2=int
> T1=float, T2=float

As far as I know, Boost.Test only works with a single type sequence. You'll
have to do permutations yourself, either by manually listing all combinations
you want to test or by doing some metaprogramming.

> 2. The other problem is that one of the Ts in my example is a
> "template<class, class> class” and I can’t find the way to make it even
> compile using that kind of type in the example from the documentation.

Again, this is a matter of the type sequence element. For example:

  struct A1, A2, B1, B2;
  template< typename, typename >
  struct C1, C2;

  typedef mpl::vector<
    mpl::vector< A1, B1, mpl::quote2< C1 > >,
    mpl::vector< A2, B2, mpl::quote2< C2 > >
> types;

    // Here T is an mpl::vector of 3 elements

Depending on your case, you could implement the test in a separate function
(say, my_test_impl) and call it from the test case with every possible
permutation of elements of T in the test case. This way you won't need to
manually list the permutations.

Boost list run by bdawes at, gregod at, cpdaniel at, john at