[boost-test] question about using templates for tests
Hi,
I’m trying to test a template with different parameters and I can’t figure out how to do it for the following case.
My class is something like this:
template
On Saturday 19 July 2014 13:47:14 Damian Vicino wrote:
Hi, I’m trying to test a template with different parameters and I can’t figure out how to do it for the following case.
My class is something like this: template
class F> class C { T t; M m; F ; Public: … } So my test, should receive 3 template classes, first 2 normal classes and a third which receives 2 Template Parameters.
For now I’m just duplicating the tests for each implementation I write for F and they pass. Anyway, it is starting to be hard to maintain, a template solution would be great.
F is suppose to receive a priority_queue like parameter, something getting a value and a comparison operator. For example, this is one of the parameters I’m testing with: template
using priority_queue_vector = std::priority_queue , COMPARE_TYPE>; Is it possible to do this with current Boost.Test?
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.
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.
A simpler scenario in this case would be trying to test this:
TEST{
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
2. The other problem is that one of the Ts in my example is a "template
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: TEST{ 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” 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;
BOOST_AUTO_TEST_CASE_TEMPLATE(my_test, T, 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.
Thank you so much Andrey!
Just in case someone found this thread in the mailing list, this is a working example of the solution proposed in previous emails passing 3 types to evaluate:
using test_types=boost::mpl::list
participants (2)
-
Andrey Semashev
-
Damian Vicino