1 /*
2 * Copyright (c) 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 #include <thread>
17 #include <unistd.h>
18 #include <vector>
19 #include <gtest/gtest.h>
20 #include "hdf_log.h"
21 #include "hdf_slist.h"
22 #include "osal_mem.h"
23 #include "securec.h"
24
25 #define HDF_LOG_TAG osal_slist_test_cpp
26
27 namespace OHOS {
28 using namespace testing::ext;
29 using HdfSListOps = void (*)(struct HdfSList *list, struct HdfSListNode *link);
30
31 class HdfOsalSlistTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37 };
38
SetUpTestCase()39 void HdfOsalSlistTest::SetUpTestCase()
40 {
41 }
42
TearDownTestCase()43 void HdfOsalSlistTest::TearDownTestCase()
44 {
45 }
46
SetUp()47 void HdfOsalSlistTest::SetUp()
48 {
49 }
50
TearDown()51 void HdfOsalSlistTest::TearDown()
52 {
53 }
54
55 struct TestList {
56 struct HdfSListNode entry;
57 uint32_t data;
58 };
59
TestListDeleter(struct HdfSListNode * listNode)60 void TestListDeleter(struct HdfSListNode *listNode)
61 {
62 struct TestList *data = reinterpret_cast<struct TestList *>(listNode);
63 if (data != nullptr) {
64 data->data = 0;
65 OsalMemFree(data);
66 }
67 }
68
HdfTestSListSearchCompare(struct HdfSListNode * entry,uint32_t data)69 bool HdfTestSListSearchCompare(struct HdfSListNode *entry, uint32_t data)
70 {
71 struct TestList *node = reinterpret_cast<struct TestList *>(entry);
72 if (node->data == data) {
73 return true;
74 }
75 return false;
76 }
77
78 /*
79 * @tc.name: SlistInitTest001
80 * @tc.desc: Slist init
81 * @tc.type: FUNC
82 * @tc.require: AR000DT1TK
83 */
84 HWTEST_F(HdfOsalSlistTest, SlistInitTest001, TestSize.Level1)
85 {
86 struct HdfSList list;
87 HdfSListInit(&list);
88 EXPECT_EQ(NULL, list.root);
89 EXPECT_TRUE(HdfSListIsEmpty(&list));
90 }
91
HdfOsalSlistTestInit(struct HdfSList * list,int & totalCount,HdfSListOps ops)92 static void HdfOsalSlistTestInit(struct HdfSList *list, int &totalCount, HdfSListOps ops)
93 {
94 HdfSListInit(list);
95 uint32_t insertData[] = {3, 1, 4, 5};
96 totalCount = static_cast<int>(sizeof(insertData) / sizeof(insertData[0]));
97 for (int i = 0; i < totalCount; i++) {
98 struct TestList *testData = reinterpret_cast<struct TestList *>(OsalMemAlloc(sizeof(struct TestList)));
99 ASSERT_NE(testData, nullptr);
100 testData->data = insertData[i];
101 ops(list, &testData->entry);
102 }
103 EXPECT_EQ(totalCount, HdfSListCount(list));
104 }
105
106 /*
107 * @tc.name: SlistCountTest001
108 * @tc.desc: Slist init
109 * @tc.type: FUNC
110 * @tc.require: AR000DT1TK
111 */
112 HWTEST_F(HdfOsalSlistTest, SlistCountTest001, TestSize.Level1)
113 {
114 struct HdfSList list;
115 int totalCount = 0;
116 HdfOsalSlistTestInit(&list, totalCount, HdfSListAdd);
117 HdfSListFlush(&list, TestListDeleter);
118 EXPECT_EQ(0, HdfSListCount(&list));
119 }
120
121 /*
122 * @tc.name: SlistSearchTest001
123 * @tc.desc: Slist init
124 * @tc.type: FUNC
125 * @tc.require: AR000DT1TK
126 */
127 HWTEST_F(HdfOsalSlistTest, SlistSearchTest001, TestSize.Level1)
128 {
129 struct HdfSList list;
130 int totalCount = 0;
131 HdfOsalSlistTestInit(&list, totalCount, HdfSListAdd);
132 struct HdfSListNode *resultNode = HdfSListSearch(&list, 5, HdfTestSListSearchCompare);
133 EXPECT_TRUE(resultNode != nullptr);
134 EXPECT_EQ(5, static_cast<int>((reinterpret_cast<struct TestList *>(resultNode))->data));
135 HdfSListFlush(&list, TestListDeleter);
136 EXPECT_EQ(0, HdfSListCount(&list));
137 }
138
139 /*
140 * @tc.name: SlistGetTest001
141 * @tc.desc: Slist init
142 * @tc.type: FUNC
143 * @tc.require: AR000DT1TK
144 */
145 HWTEST_F(HdfOsalSlistTest, SlistGetTest001, TestSize.Level1)
146 {
147 struct HdfSList list;
148 int totalCount = 0;
149 HdfOsalSlistTestInit(&list, totalCount, HdfSListAdd);
150 struct HdfSListNode *resultNode = HdfSListGetLast(&list);
151 EXPECT_TRUE(resultNode != nullptr);
152 EXPECT_EQ(3, static_cast<int>((reinterpret_cast<struct TestList *>(resultNode))->data));
153 HdfSListFlush(&list, TestListDeleter);
154 EXPECT_EQ(0, HdfSListCount(&list));
155
156 HdfOsalSlistTestInit(&list, totalCount, HdfSListAddTail);
157 resultNode = HdfSListGetLast(&list);
158 EXPECT_TRUE(resultNode != nullptr);
159 EXPECT_EQ(5, static_cast<int>((reinterpret_cast<struct TestList *>(resultNode))->data));
160 HdfSListFlush(&list, TestListDeleter);
161 EXPECT_EQ(0, HdfSListCount(&list));
162 }
163 } // namespace OHOS
164