1 /*
2 * Copyright (c) 2022 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 "softbus_bitmap.h"
20 #include "softbus_common.h"
21 #include "softbus_def.h"
22 #include "softbus_errcode.h"
23
24 #define BITNUM 32
25 #define TEST_POS 1
26 #define TEST_BIT 2
27
28 using namespace std;
29 using namespace testing::ext;
30
31 namespace OHOS {
32 class CommonCoreBitMapTest : public testing::Test {
33 public:
CommonCoreBitMapTest()34 CommonCoreBitMapTest() {}
~CommonCoreBitMapTest()35 ~CommonCoreBitMapTest() {}
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
SetUp()38 void SetUp() override {}
TearDown()39 void TearDown() override {}
40 };
41
SetUpTestCase(void)42 void CommonCoreBitMapTest::SetUpTestCase(void) {}
TearDownTestCase(void)43 void CommonCoreBitMapTest::TearDownTestCase(void) {}
44
45 /**
46 * @tc.name: CommonBitMapTest001
47 * @tc.desc: core common bit map test
48 * @tc.type: FUNC
49 * @tc.require:
50 */
51 HWTEST_F(CommonCoreBitMapTest, CommonBitMapTest001, TestSize.Level0)
52 {
53 uint8_t pos = BITNUM + 1;
54 uint32_t bitmap = 0;
55
56 SoftbusBitmapSet(nullptr, pos);
57 EXPECT_EQ(SOFTBUS_OK, bitmap);
58
59 SoftbusBitmapClr(nullptr, pos);
60 EXPECT_EQ(SOFTBUS_OK, bitmap);
61
62 bool result = SoftbusIsBitmapSet(nullptr, pos);
63 EXPECT_EQ(false, result);
64
65 bitmap = OSD_CAPABILITY_BITMAP;
66 SoftbusBitmapSet(&bitmap, pos);
67 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
68
69 bitmap = OSD_CAPABILITY_BITMAP;
70 SoftbusBitmapClr(&bitmap, pos);
71 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
72
73 bitmap = OSD_CAPABILITY_BITMAP;
74 result = SoftbusIsBitmapSet(&bitmap, pos);
75 EXPECT_EQ(false, result);
76
77 pos = TEST_POS;
78 bitmap = OSD_CAPABILITY_BITMAP;
79 SoftbusBitmapSet(&bitmap, pos);
80 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
81
82 bitmap = OSD_CAPABILITY_BITMAP;
83 SoftbusBitmapSet(&bitmap, pos);
84 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
85 }
86
87 /**
88 * @tc.name: CommonBitMapTest002
89 * @tc.desc: core common bit map test
90 * @tc.type: FUNC
91 * @tc.require:
92 */
93 HWTEST_F(CommonCoreBitMapTest, CommonBitMapTest002, TestSize.Level0)
94 {
95 uint32_t bitmap = 0;
96 uint8_t start = BITNUM + 1;
97 uint8_t nums = 0;
98 SoftbusBitmapSetBits(nullptr, start, nums);
99 EXPECT_EQ(SOFTBUS_OK, bitmap);
100
101 SoftbusBitmapClrBits(nullptr, start, nums);
102 EXPECT_EQ(SOFTBUS_OK, bitmap);
103
104 bitmap = OSD_CAPABILITY_BITMAP;
105 SoftbusBitmapSetBits(&bitmap, start, nums);
106 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
107
108 bitmap = OSD_CAPABILITY_BITMAP;
109 SoftbusBitmapClrBits(&bitmap, start, nums);
110 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
111
112 start = TEST_POS;
113 nums = BITNUM;
114 bitmap = OSD_CAPABILITY_BITMAP;
115 SoftbusBitmapSetBits(&bitmap, start, nums);
116 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
117
118 bitmap = OSD_CAPABILITY_BITMAP;
119 SoftbusBitmapClrBits(&bitmap, start, nums);
120 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
121
122 nums = 0;
123 bitmap = OSD_CAPABILITY_BITMAP;
124 SoftbusBitmapSetBits(&bitmap, start, nums);
125 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
126
127 bitmap = OSD_CAPABILITY_BITMAP;
128 SoftbusBitmapClrBits(&bitmap, start, nums);
129 EXPECT_EQ(OSD_CAPABILITY_BITMAP, bitmap);
130
131 nums = TEST_POS;
132 bitmap = OSD_CAPABILITY_BITMAP;
133 SoftbusBitmapSetBits(&bitmap, start, nums);
134 EXPECT_NE(OSD_CAPABILITY_BITMAP, bitmap);
135
136 bitmap = OSD_CAPABILITY_BITMAP;
137 SoftbusBitmapClrBits(&bitmap, start, nums);
138 EXPECT_NE(OSD_CAPABILITY_BITMAP, bitmap);
139 }
140
141 /**
142 * @tc.name: CommonBitMapTest003
143 * @tc.desc: core common bit map test
144 * @tc.type: FUNC
145 * @tc.require:
146 */
147 HWTEST_F(CommonCoreBitMapTest, CommonBitMapTest003, TestSize.Level0)
148 {
149 uint32_t bitmap = TEST_BIT;
150 uint8_t ret = SoftbusLowBitGet(bitmap);
151 EXPECT_EQ(TEST_BIT, ret);
152
153 ret = SoftbusHighBitGet(bitmap);
154 EXPECT_EQ(TEST_BIT, ret);
155 }
156 } // namespace OHOS