class Prime {
public:
bool IsPrime(int n) {
if (n <= 1) return false;
​
for (int i = 2; i * i <= n; i++) {
// n is divisible by an integer other than 1 and itself.
if ((n % i) == 0) return false;
}
​
return true;
}
};
​
TEST(IsPrimeTest, Negative) {
Prime prime;
EXPECT_FALSE(prime.IsPrime(-5));
EXPECT_FALSE(prime.IsPrime(-1));
}
​
TEST(IsPrimeTest, Trivial) {
Prime prime;
EXPECT_FALSE(prime.IsPrime(0));
EXPECT_FALSE(prime.IsPrime(1));
EXPECT_TRUE(prime.IsPrime(2));
EXPECT_TRUE(prime.IsPrime(3));
}

class PrimeTest : public ::testing::Test {
protected:
Prime prime;
};
​
TEST_F(PrimeTest, Negative) {
EXPECT_FALSE(prime.IsPrime(-5));
EXPECT_FALSE(prime.IsPrime(-1));
}
​
TEST_F(PrimeTest, Trivial) {
EXPECT_FALSE(prime.IsPrime(0));
EXPECT_FALSE(prime.IsPrime(1));
EXPECT_TRUE(prime.IsPrime(2));
EXPECT_TRUE(prime.IsPrime(3));
}

class PrimeTest : public ::testing::Test {
protected:
Prime prime;
};
​
TEST_F(PrimeTest, Negative) {
auto vec = std::vector<int>{-5, -1, 0, 1};
for (auto v : vec) {
EXPECT_FALSE(prime.IsPrime(v));
}
}
​
TEST_F(PrimeTest, Trivial) {
auto vec2 = std::vector<int>{2, 3, 5, 7};
for_each(vec2.begin(), vec2.end(), [&](int a) {
EXPECT_TRUE(prime.IsPrime(a));
}
);
}

// 例如第一个用例有个参数写错了
TEST_F(PrimeTest, Negative) {
auto vec = std::vector<int>{-5, -1, 0, 2};
for (auto v : vec) {
EXPECT_FALSE(prime.IsPrime(v));
}
}

[ RUN      ] PrimeTest.Negative
Actual: true
Expected: false
[  FAILED  ] PrimeTest.Negative (1 ms)

[ RUN      ] PrimeTest.Negative
Actual: true
Expected: false
[  FAILED  ] PrimeTest.Negative (1 ms)

class PrimeTest : public ::testing::TestWithParam<int> {
protected:
Prime prime;
};
​
TEST_P(PrimeTest, ReturnsFalseForNonPrimes) {
int n = GetParam();
EXPECT_FALSE(this->prime.IsPrime(n));
}
​
INSTANTIATE_TEST_CASE_P(myParmTest,    // Instance name
PrimeTest,     // Test case name
testing::Values(-5,0,1,4));  // Type list
​

• myParmTest/PrimeTest.ReturnsFalseForNonPrimes/3, where GetParam() = 5 (1 ms)

[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from myParmTest/PrimeTest
[ RUN      ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/0
[       OK ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/0 (0 ms)
[ RUN      ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/1
[       OK ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/1 (0 ms)
[ RUN      ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/2
[       OK ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/2 (0 ms)
[ RUN      ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/3
Actual: true
Expected: false
[  FAILED  ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/3, where GetParam() = 5 (1 ms)
​

class PrimeTest : public ::testing::TestWithParam<std::pair<int, bool>>{
protected:
Prime prime;
};
​
TEST_P(PrimeTest, ReturnsFalseForNonPrimes) {
auto parm = GetParam();
ASSERT_EQ(this->prime.IsPrime(parm.first), parm.second);
}
​
INSTANTIATE_TEST_CASE_P(myParmTest,
PrimeTest,
testing::Values(std::make_pair(-5, false),
std::make_pair(-5, false),
std::make_pair(0, false),
std::make_pair(1, false),
std::make_pair(4, false),
std::make_pair(2, true),
std::make_pair(3, true),
std::make_pair(5, true)
));

[ RUN      ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/3
this->prime.IsPrime(parm.first)
Which is: true
parm.second
Which is: false
[  FAILED  ] myParmTest/PrimeTest.ReturnsFalseForNonPrimes/3, where GetParam() = (11, false) (1 ms)