• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include <gtest/gtest.h>
17 #include <securec.h>
18 
19 #include "lnn_node_weight.h"
20 #include "lnn_net_ledger_mock.h"
21 #include "lnn_node_weight.c"
22 #include "lnn_service_mock.h"
23 #include "softbus_errcode.h"
24 
25 namespace OHOS {
26 using namespace testing;
27 using namespace testing::ext;
28 
29 constexpr char UDID1[] = "123456789AB";
30 constexpr char UDID2[] = "123456789ab";
31 constexpr int32_t WEIGHT = 10;
32 constexpr int32_t WEIGHT2 = 5;
33 constexpr int32_t WEIGHT3 = 5;
34 
35 class LNNNodeWeightTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase()43 void LNNNodeWeightTest::SetUpTestCase()
44 {
45 }
46 
TearDownTestCase()47 void LNNNodeWeightTest::TearDownTestCase()
48 {
49 }
50 
SetUp()51 void LNNNodeWeightTest::SetUp()
52 {
53 }
54 
TearDown()55 void LNNNodeWeightTest::TearDown()
56 {
57 }
58 
59 /*
60 * @tc.name: LNN_COMPARE_NODE_WEIGHT_TEST_001
61 * @tc.desc: test LnnCompareNodeWeight
62 * @tc.type: FUNC
63 * @tc.require: I5OMIK
64 */
65 HWTEST_F(LNNNodeWeightTest, LNN_COMPARE_NODE_WEIGHT_TEST_001, TestSize.Level1)
66 {
67     int32_t ret = LnnCompareNodeWeight(WEIGHT, UDID1, WEIGHT2, UDID2);
68     EXPECT_TRUE(ret == (WEIGHT - WEIGHT2));
69     ret = LnnCompareNodeWeight(WEIGHT, nullptr, WEIGHT, UDID2);
70     EXPECT_TRUE(ret == (WEIGHT - WEIGHT));
71     ret = LnnCompareNodeWeight(WEIGHT, UDID1, WEIGHT, nullptr);
72     EXPECT_TRUE(ret == (WEIGHT - WEIGHT));
73     ret = LnnCompareNodeWeight(WEIGHT2, UDID1, WEIGHT3, UDID2);
74     EXPECT_TRUE(ret < 0);
75 }
76 
77 /*
78 * @tc.name: LNN_GET_LOCAL_WEIGHT_TEST_001
79 * @tc.desc: test LnnGetLocalWeight
80 * @tc.type: FUNC
81 * @tc.require:
82 */
83 HWTEST_F(LNNNodeWeightTest, LNN_GET_LOCAL_WEIGHT_TEST_001, TestSize.Level1)
84 {
85     NiceMock<LnnServicetInterfaceMock> lnnServiceMock;
86     EXPECT_CALL(lnnServiceMock, SoftBusGenerateRandomArray).WillOnce(Return(SOFTBUS_ERR));
87     int32_t ret = LnnGetLocalWeight();
88     EXPECT_EQ(ret, SOFTBUS_OK);
89     unsigned char randStr = 20;
90     EXPECT_CALL(lnnServiceMock, SoftBusGenerateRandomArray)
91         .WillRepeatedly(DoAll(SetArgPointee<0>(randStr), Return(SOFTBUS_OK)));
92     NiceMock<LnnNetLedgertInterfaceMock> netLedgerMock;
93     EXPECT_CALL(netLedgerMock, LnnGetLocalNumInfo).WillOnce(Return(SOFTBUS_ERR));
94     ret = LnnGetLocalWeight();
95     EXPECT_EQ(ret, 78);
96     ret = LnnGetLocalWeight();
97     EXPECT_EQ(ret, 78);
98 }
99 } // namespace OHOS
100