我实际上正在使用googletest框架。我有一个值参数化测试
std::tuple<int, double>
此int表示正多边形中的顶点数,double表示其半径。我可以用这样的结构表示这个:
struct RegularPolygon{
int NbVertices;
double Radius;
}
问题是我实际上是使用combine paramaters生成器创建测试:
INSTANTIATE_TEST_CASE_P(RegularPolygon, PolygonTestCase, testing::Combine(
testing::Range(3, 10),
testing::Range(1.0, 10.0)));
所以,如果我想切换到使用
RegularPolygon
struct作为我的参数,我必须硬编码两个范围的笛卡尔积。
除非有办法定义我自己的RegularPolygon Generator,它只将int映射到RegularPolygon :: NbVertices,将double映射到RegularPolygon :: Radius。
有办法吗?
如果没有,那么将元组转换为RegularPolygon实例的最佳做法是什么?
答案 0 :(得分:0)
我尝试为您的operator+
重载operator<
和RegularPolygon
:
struct RegularPolygon{
int NbVertices;
double Radius;
RegularPolygon(int a, double b) : NbVertices(a), Radius(b) {};
bool operator<(const RegularPolygon& o) { return (NbVertices < o.NbVertices && Radius < o.Radius); }
RegularPolygon operator+(const RegularPolygon& o) { return RegularPolygon(NbVertices + o.NbVertices, Radius + o.Radius);}
};
并使用:
RegularPolygon first(1, 1.0);
RegularPolygon end(10, 10.0);
RegularPolygon step(1, 1.0);
INSTANTIATE_TEST_CASE_P(RegularPolygonTest,
PolygonTestCase,
::testing::Range(::first, ::end, ::step));
答案 1 :(得分:0)
只需将此元组转换为测试夹具类中所需的类型(请参阅polygonToTest
成员变量):
class PolygonTestCase : public ::testing::TestWithParam<std::tuple<int, double>>
{
protected:
RegularPolygon polygonToTest{
std::get<0>(GetParam()),
std::get<1>(GetParam())
};
};
TEST_P(PolygonTestCase, SomeTest)
{
// have access here to polygonToTest.NbVertices and polygonToTest.Radius
}
INSTANTIATE_TEST_CASE_P(RegularPolygon, PolygonTestCase, testing::Combine(
testing::Range(3, 10),
testing::Range(1.0, 10.0)));