1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 // --------------------<测试 参数化>-----------------------------------
17 #include "gtest/gtest.h"
18 #include <cmath>
19 #include "demo.h"
20 using namespace ::testing;
21
22 struct ParamList {
23 bool out;
24 int in;
25 };
26
27 class IsPrimeParamTest : public ::testing::TestWithParam<struct ParamList> {
28 };
29
30 // if n is prime num return true else return false
IsPrimeNum(int n)31 static bool IsPrimeNum(int n)
32 {
33 if (n < NUM2) {
34 return false;
35 }
36 int i;
37 for (i = NUM2; i <= sqrt(n); i++) {
38 if ((n % i) == 0) {
39 return false;
40 }
41 }
42 return true;
43 }
44
TEST_P(IsPrimeParamTest, IsPrime)45 TEST_P(IsPrimeParamTest, IsPrime)
46 {
47 bool out = GetParam().out;
48 int in = GetParam().in;
49 EXPECT_EQ(out, IsPrimeNum(in));
50 }
51
52 INSTANTIATE_TEST_CASE_P(IsPrimeParamTest,
53 IsPrimeParamTest,
54 Values(ParamList{false, NUM10}, ParamList{true, NUM3}, ParamList{false, NUM0}, ParamList{false, NG_NUM1}));
55