1 /*
2 * Copyright (c) 2020-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 "gfx_utils/vector.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 using namespace OHOS::Graphic;
23
24 namespace OHOS {
25 namespace {
26 const uint16_t FIRST_VALUE = 1;
27 const uint16_t SECOND_VALUE = 2;
28 const uint16_t THIRD_VALUE = 3;
29 } // namespace
30
31 class VectorTest : public testing::Test {
32 public:
33 VectorTest() = default;
34 ~VectorTest() = default;
SetUpTestCase(void)35 static void SetUpTestCase(void) {}
TearDownTestCase(void)36 static void TearDownTestCase(void) {}
37 void SetUp();
38 void TearDown();
39
40 Vector<uint16_t>* vector_ = nullptr;
41 };
42
SetUp()43 void VectorTest::SetUp()
44 {
45 if (vector_ == nullptr) {
46 vector_ = new Vector<uint16_t>();
47 }
48 }
49
TearDown()50 void VectorTest::TearDown()
51 {
52 if (vector_ != nullptr) {
53 delete vector_;
54 vector_ = nullptr;
55 }
56 }
57 /**
58 * @tc.name: VectorFront_001
59 * @tc.desc: Verify Front function, equal.
60 * @tc.type: FUNC
61 * @tc.require: AR000FCKJR
62 */
63 HWTEST_F(VectorTest, VectorFront_001, TestSize.Level0)
64 {
65 if (vector_ == nullptr) {
66 ADD_FAILURE();
67 return;
68 }
69 vector_->PushBack(FIRST_VALUE);
70 vector_->PushBack(SECOND_VALUE);
71 EXPECT_EQ(vector_->Front(), FIRST_VALUE);
72 }
73
74 /**
75 * @tc.name: VectorBack_001
76 * @tc.desc: Verify Back function, equal.
77 * @tc.type: FUNC
78 * @tc.require: AR000FCKJR
79 */
80 HWTEST_F(VectorTest, VectorBack_001, TestSize.Level0)
81 {
82 if (vector_ == nullptr) {
83 ADD_FAILURE();
84 return;
85 }
86 vector_->PushBack(FIRST_VALUE);
87 vector_->PushBack(SECOND_VALUE);
88 EXPECT_EQ(vector_->Back(), SECOND_VALUE);
89 }
90
91 /**
92 * @tc.name: VectorPushBack_001
93 * @tc.desc: Verify PushBack/PopBack function, equal.
94 * @tc.type: FUNC
95 * @tc.require: AR000FCKJR
96 */
97 HWTEST_F(VectorTest, VectorPushBack_001, TestSize.Level0)
98 {
99 uint16_t size = 0;
100 if (vector_ == nullptr) {
101 ADD_FAILURE();
102 return;
103 }
104 vector_->PushBack(FIRST_VALUE);
105 size++;
106 vector_->PushBack(SECOND_VALUE);
107 size++;
108 EXPECT_EQ(vector_->Size(), size);
109 EXPECT_EQ(vector_->Front(), FIRST_VALUE);
110 EXPECT_EQ(vector_->Back(), SECOND_VALUE);
111
112 vector_->PopBack();
113 size--;
114 EXPECT_EQ(vector_->Size(), size);
115 EXPECT_EQ(vector_->Front(), FIRST_VALUE);
116 EXPECT_EQ(vector_->Back(), FIRST_VALUE);
117 }
118
119 /**
120 * @tc.name: VectorClear_001
121 * @tc.desc: Verify IsEmpty/Clear/Size function, equal.
122 * @tc.type: FUNC
123 * @tc.require: AR000FCKJR
124 */
125 HWTEST_F(VectorTest, VectorClear_001, TestSize.Level0)
126 {
127 if (vector_ == nullptr) {
128 ADD_FAILURE();
129 return;
130 }
131 uint16_t size = 0;
132 EXPECT_TRUE(vector_->IsEmpty());
133
134 vector_->PushBack(FIRST_VALUE);
135 ++size;
136 vector_->PushBack(SECOND_VALUE);
137 ++size;
138 EXPECT_EQ(vector_->Size(), size);
139 EXPECT_FALSE(vector_->IsEmpty());
140
141 vector_->Clear();
142 EXPECT_TRUE(vector_->IsEmpty());
143 EXPECT_EQ(vector_->Size(), 0);
144 }
145
146 /**
147 * @tc.name: VectorBegin_001
148 * @tc.desc: Verify Begin/End function, equal.
149 * @tc.type: FUNC
150 * @tc.require: AR000FCKJR
151 */
152 HWTEST_F(VectorTest, VectorBegin_001, TestSize.Level0)
153 {
154 if (vector_ == nullptr) {
155 ADD_FAILURE();
156 return;
157 }
158 vector_->PushBack(FIRST_VALUE);
159 vector_->PushBack(SECOND_VALUE);
160 EXPECT_EQ(*(vector_->Begin()), FIRST_VALUE);
161 auto p = vector_->End();
162 EXPECT_EQ((--p)[0], SECOND_VALUE);
163 }
164
165 /**
166 * @tc.name: VectorSize_001
167 * @tc.desc: Verify Size function, equal.
168 * @tc.type: FUNC
169 * @tc.require: AR000FCKJR
170 */
171 HWTEST_F(VectorTest, VectorSize_001, TestSize.Level0)
172 {
173 if (vector_ == nullptr) {
174 ADD_FAILURE();
175 return;
176 }
177 uint16_t size = 0;
178
179 vector_->PushBack(FIRST_VALUE);
180 size++;
181 EXPECT_EQ(vector_->Size(), size);
182 vector_->Clear();
183 EXPECT_EQ(vector_->Size(), 0);
184 }
185
186 /**
187 * @tc.name: VectorCapacity_001
188 * @tc.desc: Verify Capacity/ReSize function, equal.
189 * @tc.type: FUNC
190 * @tc.require: AR000FCKJR
191 */
192 HWTEST_F(VectorTest, VectorCapacity_001, TestSize.Level0)
193 {
194 if (vector_ == nullptr) {
195 ADD_FAILURE();
196 return;
197 }
198 uint16_t capacity = 1;
199 uint16_t size = capacity;
200
201 vector_->PushBack(FIRST_VALUE);
202 EXPECT_EQ(vector_->Capacity(), capacity);
203 vector_->PushBack(SECOND_VALUE);
204 capacity <<= 1; // 1: capacity double
205 EXPECT_EQ(vector_->Capacity(), capacity);
206 vector_->PushBack(THIRD_VALUE);
207 capacity <<= 1; // 1: capacity double
208 EXPECT_EQ(vector_->Capacity(), capacity);
209
210 EXPECT_EQ(vector_->ReSize(size), size);
211 }
212
213 /**
214 * @tc.name: VectorErase_001
215 * @tc.desc: Verify Erase function, equal.
216 * @tc.type: FUNC
217 * @tc.require: AR000FCKJR
218 */
219 HWTEST_F(VectorTest, VectorErase_001, TestSize.Level0)
220 {
221 if (vector_ == nullptr) {
222 ADD_FAILURE();
223 return;
224 }
225 uint16_t idx = 0;
226
227 vector_->PushBack(FIRST_VALUE);
228 idx++;
229 vector_->PushBack(SECOND_VALUE);
230 vector_->PushBack(THIRD_VALUE);
231
232 vector_->Erase(idx); // remove SECOND_VALUE
233 EXPECT_EQ((*vector_)[idx], THIRD_VALUE);
234 EXPECT_EQ(vector_->Size(), ++idx);
235 }
236
237 /**
238 * @tc.name: VectorSwap_001
239 * @tc.desc: Verify Swap function, equal.
240 * @tc.type: FUNC
241 * @tc.require: AR000FCKJR
242 */
243 HWTEST_F(VectorTest, VectorSwap_001, TestSize.Level0)
244 {
245 if (vector_ == nullptr) {
246 ADD_FAILURE();
247 return;
248 }
249 vector_->PushBack(FIRST_VALUE);
250 Vector<uint16_t> copy;
251 copy = (*vector_);
252
253 vector_->PushBack(SECOND_VALUE);
254 copy.Swap(*vector_);
255
256 EXPECT_EQ(copy.Size(), SECOND_VALUE);
257 EXPECT_EQ(copy[0], FIRST_VALUE);
258 EXPECT_EQ(copy[1], SECOND_VALUE);
259 EXPECT_EQ(vector_->Size(), FIRST_VALUE);
260 EXPECT_EQ((*vector_)[0], FIRST_VALUE);
261 }
262 } // namespace OHOS