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