Reputation: 1511
I would like to mix parametrized test with typed test. Here is my attempt:
struct X {};
struct Y {};
template <typename T>
struct MyTestFixture: public ::testing::Test
{
T t;
};
template <typename T, typename Param>
struct MyTestFixtureWithParam : public MyTestFixture<T>,
public ::testing::WithParamInterface<Param>
{
};
using MyTestFixtureWithXandString = MyTestFixtureWithParam<X, std::string>;
TEST_P(MyTestFixtureWithXandString, Test1)
{
}
INSTANTIATE_TEST_CASE_P(Name, MyTestFixtureWithXandString,
::testing::Values("a", "b"));
using MyTestFixtureWithYandString = MyTestFixtureWithParam<Y, std::string>;
TEST_P(MyTestFixtureWithYandString, Test1)
{
}
INSTANTIATE_TEST_CASE_P(Name, MyTestFixtureWithYandString,
::testing::Values("a", "b"));
Does Gtest contain some macro which mixes TYPED_TEST and TEST_P or the above code is the only way to achieve my goal ?
Upvotes: 5
Views: 4132
Reputation: 2880
There might be no such a macro as far as I know but there is a possible way to do it as follows.
Basic Idea
Let us define the following structs a
, b
and Case
.
Here a::str
and b::str
are the parameters which we want to test:
// types
struct X {};
struct Y {};
// "parameters"
struct a { static constexpr char str[] = "a"; };
struct b { static constexpr char str[] = "b"; };
template<class T, class P>
struct Case
{
using type = T;
static std::string GetParam()
{
return P::str;
}
};
The basic idea of testing all combinations of your types X
and Y
and string parameters within a single test logic is using this struct Case
to specify the test type and parameter as follows:
using TestTypes = ::testing::Types<Case<X, a>, Case<X, b>, Case<Y, a>, Case<Y, b>>;
template <class T>
class MyTestFixture: public ::testing::Test {};
TYPED_TEST_CASE(MyTestFixture, TestTypes);
TYPED_TEST(MyTestFixture, Test12)
{
// X or Y
TypeParam::type t;
// "a" or "b"
const std::string str = TypeParam::GetParam();
}
Improvement
Now our problem is how we can improve the construction of the all combinations of types and string parameters.
I have answered to the almost same problem.
In the current case, all possible pairs of {X, Y}
and {a, b}
are labeled by one dimensional integers 0,1,2,3
like this:
0 -> (0/2, 0%2) = (0,0) -> Case<X, a>
1 -> (1/2, 1%2) = (0,1) -> Case<X, b>
2 -> (2/2, 2%2) = (1,0) -> Case<Y, a>
3 -> (3/2, 3%2) = (1,1) -> Case<Y, b>
where 2
is the size of {a, b}
.
It is straightforward to write the function which makes all possible combinations with this algorithm as follows.
For instance, Combinations_t<std::tuple<X, Y>, a, b>
is equal to the type of std::tuple<Case<X,a>, Case<X,b>, Case<Y,a>, Case<Y,b>>
:
template<class TupleType, class TupleParam, std::size_t I>
struct make_case
{
static constexpr std::size_t N = std::tuple_size<TupleParam>::value;
using type = Case<typename std::tuple_element<I / N, TupleType >::type,
typename std::tuple_element<I % N, TupleParam>::type>;
};
template <class T1, class T2, class Is>
struct make_combinations;
template <class TupleType, class TupleParam, std::size_t... Is>
struct make_combinations<TupleType, TupleParam, std::index_sequence<Is...>>
{
using tuples = std::tuple<typename make_case<TupleType, TupleParam, Is>::type...>;
};
template<class TupleTypes, class... Params>
using Combinations_t = typename make_combinations
<TupleTypes,
std::tuple<Params...>,
std::make_index_sequence<(std::tuple_size<TupleTypes>::value)*(sizeof...(Params))>>
::tuples;
Applying answers of this post, we can stripe this tuple Combinations_t<...>
into a list of types and here I apply Nawaz's simple one.
Then, finally, all tests can be done as follows:
template <class T>
class MyTestFixture : public ::testing::Test {};
template<class T>
struct Test;
template<class ...T>
struct Test<std::tuple<T...>>
{
using Types = ::testing::Types<T...>;
};
using TestTypes = Test<Combinations_t<std::tuple<X, Y>, a, b>>::Types;
TYPED_TEST_CASE(MyTestFixture, TestTypes);
TYPED_TEST(MyTestFixture, Test12)
{
// X or Y
TypeParam::type t;
// "a" or "b"
const std::string str = TypeParam::GetParam();
}
Upvotes: 2