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 "pasteboard_linked_list.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 using namespace testing::ext;
23 
24 class PasteboardLinkedListTest : public testing::Test {
25 public:
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
28     void SetUp();
29     void TearDown();
30 };
31 
SetUpTestCase(void)32 void PasteboardLinkedListTest::SetUpTestCase(void)
33 {
34 }
35 
TearDownTestCase(void)36 void PasteboardLinkedListTest::TearDownTestCase(void)
37 {
38 }
39 
SetUp(void)40 void PasteboardLinkedListTest::SetUp(void)
41 {
42 }
43 
TearDown(void)44 void PasteboardLinkedListTest::TearDown(void)
45 {
46 }
47 
48 /**
49  * @tc.name: TestInsert001
50  * @tc.desc: Test InsertFront
51  * @tc.type: FUNC
52  */
53 HWTEST_F(PasteboardLinkedListTest, TestInsert001, TestSize.Level0)
54 {
55     LinkedList<int32_t> lst;
56     lst.InsertFront(0);
57     lst.InsertFront(1);
58     lst.InsertFront(2);
59 
60     std::vector<int32_t> vec;
__anon5926d87b0102(int32_t value) 61     lst.ForEach([&vec](int32_t value) {
62         vec.push_back(value);
63     });
64 
65     ASSERT_EQ(vec.size(), 3);
66     EXPECT_EQ(vec[0], 2);
67     EXPECT_EQ(vec[1], 1);
68     EXPECT_EQ(vec[2], 0);
69 }
70 
71 /**
72  * @tc.name: TestInsert002
73  * @tc.desc: Test InsertTail
74  * @tc.type: FUNC
75  */
76 HWTEST_F(PasteboardLinkedListTest, TestInsert002, TestSize.Level0)
77 {
78     LinkedList<int32_t> lst;
79     lst.InsertTail(0);
80     lst.InsertTail(1);
81     lst.InsertTail(2);
82 
83     std::vector<int32_t> vec;
__anon5926d87b0202(int32_t value) 84     lst.ForEach([&vec](int32_t value) {
85         vec.push_back(value);
86     });
87 
88     ASSERT_EQ(vec.size(), 3);
89     EXPECT_EQ(vec[0], 0);
90     EXPECT_EQ(vec[1], 1);
91     EXPECT_EQ(vec[2], 2);
92 }
93 
94 /**
95  * @tc.name: TestInsert003
96  * @tc.desc: Test InsertFront & InsertTail
97  * @tc.type: FUNC
98  */
99 HWTEST_F(PasteboardLinkedListTest, TestInsert003, TestSize.Level0)
100 {
101     LinkedList<int32_t> lst;
102     lst.InsertTail(0);
103     lst.InsertFront(1);
104     lst.InsertTail(2);
105 
106     std::vector<int32_t> vec;
__anon5926d87b0302(int32_t value) 107     lst.ForEach([&vec](int32_t value) {
108         vec.push_back(value);
109     });
110 
111     ASSERT_EQ(vec.size(), 3);
112     EXPECT_EQ(vec[0], 1);
113     EXPECT_EQ(vec[1], 0);
114     EXPECT_EQ(vec[2], 2);
115 }
116 
117 /**
118  * @tc.name: TestFindExist001
119  * @tc.desc: Test FindExist
120  * @tc.type: FUNC
121  */
122 HWTEST_F(PasteboardLinkedListTest, TestFindExist001, TestSize.Level0)
123 {
124     LinkedList<int32_t> lst;
125     lst.InsertTail(0);
126     EXPECT_TRUE(lst.FindExist(0));
127     EXPECT_FALSE(lst.FindExist(1));
128     EXPECT_FALSE(lst.FindExist(2));
__anon5926d87b0402(int32_t value) 129     EXPECT_FALSE(lst.FindExist([](int32_t value) {
130         return value > 0;
131     }));
132 
133     lst.InsertFront(1);
134     EXPECT_TRUE(lst.FindExist(0));
135     EXPECT_TRUE(lst.FindExist(1));
136     EXPECT_FALSE(lst.FindExist(2));
__anon5926d87b0502(int32_t value) 137     EXPECT_TRUE(lst.FindExist([](int32_t value) {
138         return value > 0;
139     }));
__anon5926d87b0602(int32_t value) 140     EXPECT_FALSE(lst.FindExist([](int32_t value) {
141         return value > 1;
142     }));
143 
144     lst.InsertTail(2);
145     EXPECT_TRUE(lst.FindExist(0));
146     EXPECT_TRUE(lst.FindExist(1));
147     EXPECT_TRUE(lst.FindExist(2));
__anon5926d87b0702(int32_t value) 148     EXPECT_TRUE(lst.FindExist([](int32_t value) {
149         return value > 1;
150     }));
151 }
152 
153 /**
154  * @tc.name: TestRemoveIf001
155  * @tc.desc: Test RemoveIf
156  * @tc.type: FUNC
157  */
158 HWTEST_F(PasteboardLinkedListTest, TestRemoveIf001, TestSize.Level0)
159 {
160     LinkedList<int32_t> lst;
161     lst.InsertTail(0);
162     lst.InsertFront(1);
163     lst.InsertTail(2);
164     lst.InsertFront(2);
165     EXPECT_TRUE(lst.FindExist(0));
166     EXPECT_TRUE(lst.FindExist(1));
167     EXPECT_TRUE(lst.FindExist(2));
168 
__anon5926d87b0802(int32_t value) 169     lst.RemoveIf([](int32_t value) {
170         return value == 0;
171     });
172     EXPECT_FALSE(lst.FindExist(0));
173     EXPECT_TRUE(lst.FindExist(1));
174     EXPECT_TRUE(lst.FindExist(2));
175 
176     lst.InsertTail(0);
__anon5926d87b0902(int32_t value) 177     lst.RemoveIf([](int32_t value) {
178         return value > 0;
179     });
180     EXPECT_TRUE(lst.FindExist(0));
181     EXPECT_FALSE(lst.FindExist(1));
182     EXPECT_FALSE(lst.FindExist(2));
183 }
184 
185 } // namespace MiscServices
186 } // namespace OHOS
187 
188