gpt4 book ai didi

c++ - GTest - 不同类型的参数化测试

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:02:57 27 4
gpt4 key购买 nike

我想混合参数化测试和类型化测试。这是我的尝试:

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"));

Gtest 是否包含一些混合了 TYPED_TEST 和 TEST_P 的宏,或者上面的代码是实现我的目标的唯一方法?

最佳答案

据我所知,可能没有这样的宏,但有一种可能的方法,如下所示。


基本思想

让我们定义以下结构 a , bCase .这里a::strb::str是我们要测试的参数:

// 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;
}
};

测试所有类型组合的基本思想 XY单个测试逻辑中的字符串参数正在使用此结构 Case指定测试类型和参数如下:

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();
}

改进

现在我们的问题是如何改进类型和字符串参数的所有组合的构造。我已经回答了 almost same problem .在当前情况下,所有可能的 {X, Y} 对和 {a, b}由一维整数标记 0,1,2,3像这样:

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>

哪里2{a, b} 的大小.编写函数可以很简单地使用该算法进行所有可能的组合,如下所示。例如,Combinations_t<std::tuple<X, Y>, a, b>等于 std::tuple<Case<X,a>, Case<X,b>, Case<Y,a>, Case<Y,b>> 的类型:

Live DEMO

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;

应用此 post 的答案, 我们可以对这个元组 Combinations_t<...> 进行 strip 化进入类型列表,在这里我应用了 Nawaz 的简单类型。然后,最后,所有测试都可以按如下方式完成:

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();
}

关于c++ - GTest - 不同类型的参数化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56115790/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com