Reputation: 148
I am actually using googletest framework. I have a value parameterized test with
std::tuple<int, double>
This int represents the number of vertices in a regular polygon and the double represents its radius. I could represent this using a struct like this :
struct RegularPolygon{
int NbVertices;
double Radius;
}
The problem is that I actually create tests using the combine paramaters generator :
INSTANTIATE_TEST_CASE_P(RegularPolygon, PolygonTestCase, testing::Combine(
testing::Range(3, 10),
testing::Range(1.0, 10.0)));
So if I want to switch to the use of the
RegularPolygon
struct as my parameter, I have to hard code the cartesian product of the two ranges.
Unless there is a way to define my own RegularPolygon Generator that would only map the int to RegularPolygon::NbVertices and the double to RegularPolygon::Radius.
Is there a way to do so?
If there isn't, what would be the best practice to convert the tuple to a RegularPolygon instance?
Upvotes: 4
Views: 3588
Reputation: 24347
Just translate this tuple to your desired type in your test-fixture class (see polygonToTest
member variable):
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)));
Upvotes: 1
Reputation: 4276
I tried overloading operator+
and operator<
for your 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);}
};
and use:
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));
Upvotes: 0