1 /*
2 * Copyright (c) 2024 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
18 #include "softbus_adapter_mem.h"
19 #include "softbus_common.h"
20 #include "softbus_def.h"
21 #include "softbus_errcode.h"
22 #include "softbus_queue.h"
23
24 #define INVALID_NUM (-1)
25 #define IS_POWER_OF_2_NUM 4
26 #define NOT_POWER_OF_2_NUM 5
27 #define QUEUE_SIZE_MAX 8193
28
29 using namespace std;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 class CommonCoreQueueTest : public testing::Test {
34 public:
CommonCoreQueueTest()35 CommonCoreQueueTest() { }
~CommonCoreQueueTest()36 ~CommonCoreQueueTest() { }
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
SetUp()39 void SetUp() override { }
TearDown()40 void TearDown() override { }
41 };
42
SetUpTestCase(void)43 void CommonCoreQueueTest::SetUpTestCase(void) { }
TearDownTestCase(void)44 void CommonCoreQueueTest::TearDownTestCase(void) { }
45
46 /**
47 * @tc.name: QueueInitTest001
48 * @tc.desc: core common QueueInit invalid param test
49 * @tc.type: FUNC
50 * @tc.require:
51 */
52 HWTEST_F(CommonCoreQueueTest, QueueInitTest001, TestSize.Level1)
53 {
54 LockFreeQueue queue;
55 int32_t ret = QueueInit(nullptr, IS_POWER_OF_2_NUM);
56 EXPECT_EQ(ret, QUEUE_INVAL);
57 ret = QueueInit(&queue, INVALID_NUM);
58 EXPECT_EQ(ret, QUEUE_INVAL);
59 ret = QueueInit(&queue, NOT_POWER_OF_2_NUM);
60 EXPECT_EQ(ret, QUEUE_INVAL);
61 }
62
63 /**
64 * @tc.name: QueueSizeCalcTest001
65 * @tc.desc: core common QueueSizeCalc invalid param test
66 * @tc.type: FUNC
67 * @tc.require:
68 */
69 HWTEST_F(CommonCoreQueueTest, QueueSizeCalcTest001, TestSize.Level1)
70 {
71 uint32_t queueSize;
72 int32_t ret = QueueSizeCalc(IS_POWER_OF_2_NUM, nullptr);
73 EXPECT_EQ(ret, QUEUE_INVAL);
74 ret = QueueSizeCalc(QUEUE_SIZE_MAX + 1, &queueSize);
75 EXPECT_EQ(ret, QUEUE_INVAL);
76 }
77
78 /**
79 * @tc.name: QueueCountGetTest001
80 * @tc.desc: core common QueueCountGet invalid param test
81 * @tc.type: FUNC
82 * @tc.require:
83 */
84 HWTEST_F(CommonCoreQueueTest, QueueCountGetTest001, TestSize.Level1)
85 {
86 LockFreeQueue queue;
87 uint32_t count;
88 int32_t ret = QueueCountGet(nullptr, &count);
89 EXPECT_EQ(ret, QUEUE_INVAL);
90 ret = QueueCountGet(&queue, nullptr);
91 EXPECT_EQ(ret, QUEUE_INVAL);
92 }
93
94 /**
95 * @tc.name: CreateQueueTest001
96 * @tc.desc: core common CreateQueueTest invalid param test
97 * @tc.type: FUNC
98 * @tc.require:
99 */
100 HWTEST_F(CommonCoreQueueTest, CreateQueueTest001, TestSize.Level1)
101 {
102 LockFreeQueue *queue = CreateQueue(INVALID_NUM);
103 EXPECT_EQ(queue, nullptr);
104 queue = CreateQueue(NOT_POWER_OF_2_NUM);
105 EXPECT_EQ(queue, nullptr);
106 SoftBusFree(queue);
107 }
108
109 /**
110 * @tc.name: CreateQueueTest002
111 * @tc.desc: core common CreateQueue success test
112 * @tc.type: FUNC
113 * @tc.require:
114 */
115 HWTEST_F(CommonCoreQueueTest, CreateQueueTest002, TestSize.Level1)
116 {
117 uint32_t count;
118 LockFreeQueue *queue = CreateQueue(IS_POWER_OF_2_NUM);
119 EXPECT_NE(queue, nullptr);
120 int32_t ret = QueueCountGet(queue, &count);
121 EXPECT_EQ(ret, 0);
122 SoftBusFree(queue);
123 }
124 } // namespace OHOS