Question

Here is the code:

#include <gtest/gtest.h>

double sq(const double x) {
    return x*x;
}


class Sqtest : public ::testing::Test {
    protected:
        virtual void SetUp() {
            a = new int(1);
            b = new int(2);
            c = new int(3);
        }
        virtual void TearDown() {
            delete a;
            delete b;
            delete c;
        }
        int *a, *b, *c;
};

TEST_F (Sqtest, posnos) {
    EXPECT_EQ(1, sq(*a));
    EXPECT_EQ(4, sq(*b));
    EXPECT_EQ(9, sq(*c));
}

This is all nice, but what if in the function Sq I use reference, i.e.

double sq(const double& x) {
    return x*x;
}

How should I modify the fixture accordingly?

Was it helpful?

Solution

There's no really need to use pointer for this particular example. Let Sqtest members be of type int and you're done:

#include <gtest/gtest.h>

int sq(int x)
{
    return x * x;
}


class Sqtest : public ::testing::Test
{
protected:
    virtual void SetUp() override
    {
        a = 1;
        b = 2;
        c = 3;
    }

    int a, b, c;
};

TEST_F(Sqtest, posnos)
{
    EXPECT_EQ(1, sq(a));
    EXPECT_EQ(4, sq(b));
    EXPECT_EQ(9, sq(c));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top