C++ 在google测试夹具中使用引用而不是指针?
代码如下:C++ 在google测试夹具中使用引用而不是指针?,c++,testing,googletest,C++,Testing,Googletest,代码如下: #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 in
#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));
}
我应该如何相应地修改夹具?对于这个特定示例,实际上不需要使用指针。让
Sqtest
成员的类型为int
,您就完成了:
#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));
}
#包括
整数平方(整数x)
{
返回x*x;
}
类Sqtest:public::testing::Test
{
受保护的:
虚拟无效设置()覆盖
{
a=1;
b=2;
c=3;
}
INTA、b、c;
};
测试F(Sqtest,posnos)
{
除等式(1,sq(a))外;
除等式(4,sq(b))外;
除等式(9,sq(c))外;
}
无需修改。但是你为什么要用指针呢?我不知道,我用的是SO的模板。我也尝试不使用指针,但测试未能编译。那么有没有办法避免使用指针呢?你能详细解释一下吗?只是不要用它们。这比使用它们更容易。你能不能不使用指针就发布一个答案?
#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));
}