4

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?

Jo Ham
  • 149
  • 1
  • 11

2 Answers2

1

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)));
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
0

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));
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • 1
    That is a good solution, but not exactly what is stated in the question, since this doesn't provide tests for the Cartesian product of the polygon attributes – Jo Ham Jul 17 '17 at 16:28
  • @Kongol Since I don't know Cartesian product well so I just give you a **prototype**. You have to implement `operator<` and `operator+` your own way (in compliance with Cartesian product). – duong_dajgja Jul 18 '17 at 01:21