1 /*
2 * Copyright (c) 2025 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 "common_interfaces/objects/base_string.h"
17 #include "common_components/platform/string_hash.h"
18 #include "common_components/tests/test_helper.h"
19
20 using namespace common;
21
22 namespace common::test {
23 class BaseStringTest : public common::test::BaseTestWithScope {
24 };
25
HWTEST_F_L0(BaseStringTest,ComputeHashForData_TEST0)26 HWTEST_F_L0(BaseStringTest, ComputeHashForData_TEST0)
27 {
28 const uint8_t data[] = {'a', 'b', 'c'};
29 size_t size = sizeof(data) / sizeof(data[0]);
30 uint32_t hashSeed = 0;
31 uint32_t expectedHash = hashSeed;
32 for (uint32_t i = 0; i < size; ++i) {
33 expectedHash = (expectedHash << static_cast<uint32_t>(StringHash::HASH_SHIFT)) - expectedHash + data[i];
34 }
35
36 EXPECT_EQ(BaseString::ComputeHashForData(data, size, hashSeed), expectedHash);
37 }
38
HWTEST_F_L0(BaseStringTest,ComputeHashForData_TEST1)39 HWTEST_F_L0(BaseStringTest, ComputeHashForData_TEST1)
40 {
41 std::vector<uint8_t> largeData(1000, 'x');
42 size_t size = largeData.size();
43 uint32_t hashSeed = 0;
44
45 uint32_t result = BaseString::ComputeHashForData(largeData.data(), size, hashSeed);
46 EXPECT_NE(result, 0);
47 }
48
HWTEST_F_L0(BaseStringTest,ComputeHashcodeUtf8_TEST0)49 HWTEST_F_L0(BaseStringTest, ComputeHashcodeUtf8_TEST0)
50 {
51 const uint8_t utf8Data[] = u8"hello";
52 size_t utf8Len = sizeof(utf8Data) / sizeof(utf8Data[0]) - 1;
53
54 uint32_t expectedHash = 0;
55 for (uint32_t i = 0; i < utf8Len; ++i) {
56 expectedHash = (expectedHash << static_cast<uint32_t>(StringHash::HASH_SHIFT)) - expectedHash + utf8Data[i];
57 }
58
59 EXPECT_EQ(BaseString::ComputeHashcodeUtf8(utf8Data, utf8Len, true), expectedHash);
60 }
61
HWTEST_F_L0(BaseStringTest,ComputeHashcodeUtf8_TEST1)62 HWTEST_F_L0(BaseStringTest, ComputeHashcodeUtf8_TEST1)
63 {
64 const uint8_t utf8Data[] = u8"hello";
65 size_t utf8Len = sizeof(utf8Data) / sizeof(utf8Data[0]) - 1;
66
67 uint32_t expectedHash = 0;
68 for (uint32_t i = 0; i < utf8Len; ++i) {
69 expectedHash = (expectedHash << static_cast<uint32_t>(StringHash::HASH_SHIFT)) - expectedHash + utf8Data[i];
70 }
71
72 EXPECT_EQ(BaseString::ComputeHashcodeUtf8(utf8Data, utf8Len, false), expectedHash);
73 }
74
HWTEST_F_L0(BaseStringTest,IsASCIICharacter_TEST0)75 HWTEST_F_L0(BaseStringTest, IsASCIICharacter_TEST0)
76 {
77 const uint16_t num = 0;
78 bool result = BaseString::IsASCIICharacter(num);
79 ASSERT_FALSE(result);
80 }
81
HWTEST_F_L0(BaseStringTest,IsASCIICharacter_TEST1)82 HWTEST_F_L0(BaseStringTest, IsASCIICharacter_TEST1)
83 {
84 const uint16_t num = 0x7f;
85 bool result = BaseString::IsASCIICharacter(num);
86 ASSERT_TRUE(result);
87 }
88
HWTEST_F_L0(BaseStringTest,CanBeCompressed_TEST0)89 HWTEST_F_L0(BaseStringTest, CanBeCompressed_TEST0)
90 {
91 uint8_t data[] = {};
92 EXPECT_TRUE(BaseString::CanBeCompressed(data, 0));
93
94 uint8_t data1[] = {1, 1, 1, 1, 0};
95 ASSERT_FALSE(BaseString::CanBeCompressed(data1, 5));
96 ASSERT_TRUE(BaseString::CanBeCompressed(data1, 2));
97
98 uint8_t data2[] = {'a', 'b', 'c', 'd'};
99 ASSERT_TRUE(BaseString::CanBeCompressed(data2, 4));
100
101 uint8_t data3[] = {'a', '\0', 'c', 'd'};
102 ASSERT_FALSE(BaseString::CanBeCompressed(data3, 4));
103 }
104
HWTEST_F_L0(BaseStringTest,CanBeCompressed_TEST1)105 HWTEST_F_L0(BaseStringTest, CanBeCompressed_TEST1)
106 {
107 uint16_t data[] = {};
108 EXPECT_TRUE(BaseString::CanBeCompressed(data, 0));
109
110 uint16_t data1[] = {1, 1, 1, 1, 0};
111 ASSERT_FALSE(BaseString::CanBeCompressed(data1, 5));
112 ASSERT_TRUE(BaseString::CanBeCompressed(data1, 2));
113
114 uint16_t data2[] = {'a', 'b', 'c', 'd'};
115 ASSERT_TRUE(BaseString::CanBeCompressed(data2, 4));
116
117 uint16_t data3[] = {'a', '\0', 'c', 'd'};
118 ASSERT_FALSE(BaseString::CanBeCompressed(data3, 4));
119 }
120
HWTEST_F_L0(BaseStringTest,IndexOf_TEST0)121 HWTEST_F_L0(BaseStringTest, IndexOf_TEST0)
122 {
123 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
124 const uint8_t rhs[] = {'a', 'b', 'c'};
125 Span<const uint8_t> lhsSp(lhs, 5);
126 Span<const uint8_t> rhsSp(rhs, 3);
127
128 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), 0);
129 }
130
HWTEST_F_L0(BaseStringTest,IndexOf_TEST1)131 HWTEST_F_L0(BaseStringTest, IndexOf_TEST1)
132 {
133 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
134 const uint8_t rhs[] = {'c', 'd'};
135 Span<const uint8_t> lhsSp(lhs, 5);
136 Span<const uint8_t> rhsSp(rhs, 2);
137
138 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), 2);
139 }
140
HWTEST_F_L0(BaseStringTest,IndexOf_TEST2)141 HWTEST_F_L0(BaseStringTest, IndexOf_TEST2)
142 {
143 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
144 const uint8_t rhs[] = {'x', 'y', 'z'};
145 Span<const uint8_t> lhsSp(lhs, 5);
146 Span<const uint8_t> rhsSp(rhs, 3);
147
148 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), -1);
149 }
150
HWTEST_F_L0(BaseStringTest,IndexOf_TEST3)151 HWTEST_F_L0(BaseStringTest, IndexOf_TEST3)
152 {
153 const uint8_t lhs[] = {'a', 'b', 'a', 'b', 'c'};
154 const uint8_t rhs[] = {'a', 'b', 'c'};
155 Span<const uint8_t> lhsSp(lhs, 5);
156 Span<const uint8_t> rhsSp(rhs, 3);
157
158 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), 2);
159 }
160
HWTEST_F_L0(BaseStringTest,IndexOf_TEST4)161 HWTEST_F_L0(BaseStringTest, IndexOf_TEST4)
162 {
163 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
164 const uint8_t rhs[] = {'a', 'b', 'x'};
165 Span<const uint8_t> lhsSp(lhs, 5);
166 Span<const uint8_t> rhsSp(rhs, 3);
167
168 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), -1);
169 }
170
HWTEST_F_L0(BaseStringTest,CompareStringSpan_TEST0)171 HWTEST_F_L0(BaseStringTest, CompareStringSpan_TEST0)
172 {
173 const uint8_t lhs[] = {1, 2, 3};
174 const uint8_t rhs[] = {1, 2, 3};
175 Span<const uint8_t> lhsSp(lhs, 5);
176 Span<const uint8_t> rhsSp(rhs, 3);
177 EXPECT_EQ(CompareStringSpan(lhsSp, rhsSp, 3), 0);
178 }
179
HWTEST_F_L0(BaseStringTest,CompareStringSpan_TEST1)180 HWTEST_F_L0(BaseStringTest, CompareStringSpan_TEST1)
181 {
182 const uint8_t lhs[] = {1, 2, 4};
183 const uint8_t rhs[] = {1, 2, 3};
184 Span<const uint8_t> lhsSp(lhs, 5);
185 Span<const uint8_t> rhsSp(rhs, 3);
186 EXPECT_EQ(CompareStringSpan(lhsSp, rhsSp, 3), 1);
187 }
188
HWTEST_F_L0(BaseStringTest,IsSubStringAtSpan_TEST1)189 HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST1)
190 {
191 const uint8_t lhs[] = {'a', 'b', 'c'};
192 const uint8_t rhs[] = {'x', 'y'};
193 Span<const uint8_t> lhsSp(lhs, 3);
194 Span<const uint8_t> rhsSp(rhs, 2);
195 ASSERT_FALSE(IsSubStringAtSpan(lhsSp, rhsSp, 1));
196 }
197
HWTEST_F_L0(BaseStringTest,IsSubStringAtSpan_TEST2)198 HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST2)
199 {
200 const uint8_t lhs[] = {'a', 'b'};
201 const uint8_t rhs[] = {'b'};
202 Span<const uint8_t> lhsSp(lhs, 2);
203 Span<const uint8_t> rhsSp(rhs, 1);
204 ASSERT_TRUE(IsSubStringAtSpan(lhsSp, rhsSp, 1));
205 }
206
HWTEST_F_L0(BaseStringTest,IndexOf_TEST5)207 HWTEST_F_L0(BaseStringTest, IndexOf_TEST5)
208 {
209 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
210 const uint16_t rhs[] = {'a', 'b', 'c'};
211 Span<const uint8_t> lhsSp(lhs, 5);
212 Span<const uint16_t> rhsSp(rhs, 3);
213 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), 0);
214
215 const uint8_t lhs1[] = {'a', 'b', 'c', 'd', 'e'};
216 const uint16_t rhs1[] = {'c', 'd'};
217 Span<const uint8_t> lhsSp1(lhs1, 5);
218 Span<const uint16_t> rhsSp1(rhs1, 2);
219 EXPECT_EQ(BaseString::IndexOf(lhsSp1, rhsSp1, 0, 4), 2);
220
221 const uint8_t lhs2[] = {'a', 'b', 'c', 'd', 'e'};
222 const uint16_t rhs2[] = {'x', 'y', 'z'};
223 Span<const uint8_t> lhsSp2(lhs2, 5);
224 Span<const uint16_t> rhsSp2(rhs2, 3);
225 EXPECT_EQ(BaseString::IndexOf(lhsSp2, rhsSp2, 0, 4), -1);
226
227 const uint8_t lhs3[] = {'a', 'b', 'a', 'b', 'c'};
228 const uint16_t rhs3[] = {'a', 'b', 'c'};
229 Span<const uint8_t> lhsSp3(lhs3, 5);
230 Span<const uint16_t> rhsSp3(rhs3, 3);
231 EXPECT_EQ(BaseString::IndexOf(lhsSp3, rhsSp3, 0, 4), 2);
232
233 const uint8_t lhs4[] = {'a', 'b', 'c', 'd', 'e'};
234 const uint16_t rhs4[] = {'a', 'b', 'x'};
235 Span<const uint8_t> lhsSp4(lhs4, 5);
236 Span<const uint16_t> rhsSp4(rhs4, 3);
237 EXPECT_EQ(BaseString::IndexOf(lhsSp4, rhsSp4, 0, 4), -1);
238 }
239
HWTEST_F_L0(BaseStringTest,IndexOf_TEST6)240 HWTEST_F_L0(BaseStringTest, IndexOf_TEST6)
241 {
242 const uint16_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
243 const uint16_t rhs[] = {'a', 'b', 'c'};
244 Span<const uint16_t> lhsSp(lhs, 5);
245 Span<const uint16_t> rhsSp(rhs, 3);
246 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), 0);
247
248 const uint16_t lhs1[] = {'a', 'b', 'c', 'd', 'e'};
249 const uint16_t rhs1[] = {'c', 'd'};
250 Span<const uint16_t> lhsSp1(lhs1, 5);
251 Span<const uint16_t> rhsSp1(rhs1, 2);
252 EXPECT_EQ(BaseString::IndexOf(lhsSp1, rhsSp1, 0, 4), 2);
253
254 const uint16_t lhs2[] = {'a', 'b', 'c', 'd', 'e'};
255 const uint16_t rhs2[] = {'x', 'y', 'z'};
256 Span<const uint16_t> lhsSp2(lhs2, 5);
257 Span<const uint16_t> rhsSp2(rhs2, 3);
258 EXPECT_EQ(BaseString::IndexOf(lhsSp2, rhsSp2, 0, 4), -1);
259
260 const uint16_t lhs3[] = {'a', 'b', 'a', 'b', 'c'};
261 const uint16_t rhs3[] = {'a', 'b', 'c'};
262 Span<const uint16_t> lhsSp3(lhs3, 5);
263 Span<const uint16_t> rhsSp3(rhs3, 3);
264 EXPECT_EQ(BaseString::IndexOf(lhsSp3, rhsSp3, 0, 4), 2);
265
266 const uint16_t lhs4[] = {'a', 'b', 'c', 'd', 'e'};
267 const uint16_t rhs4[] = {'a', 'b', 'x'};
268 Span<const uint16_t> lhsSp4(lhs4, 5);
269 Span<const uint16_t> rhsSp4(rhs4, 3);
270 EXPECT_EQ(BaseString::IndexOf(lhsSp4, rhsSp4, 0, 4), -1);
271 }
272
HWTEST_F_L0(BaseStringTest,IndexOf_TEST7)273 HWTEST_F_L0(BaseStringTest, IndexOf_TEST7)
274 {
275 const uint16_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
276 const uint8_t rhs[] = {'a', 'b', 'c'};
277 Span<const uint16_t> lhsSp(lhs, 5);
278 Span<const uint8_t> rhsSp(rhs, 3);
279 EXPECT_EQ(BaseString::IndexOf(lhsSp, rhsSp, 0, 4), 0);
280
281 const uint16_t lhs1[] = {'a', 'b', 'c', 'd', 'e'};
282 const uint8_t rhs1[] = {'c', 'd'};
283 Span<const uint16_t> lhsSp1(lhs1, 5);
284 Span<const uint8_t> rhsSp1(rhs1, 2);
285 EXPECT_EQ(BaseString::IndexOf(lhsSp1, rhsSp1, 0, 4), 2);
286
287 const uint16_t lhs2[] = {'a', 'b', 'c', 'd', 'e'};
288 const uint8_t rhs2[] = {'x', 'y', 'z'};
289 Span<const uint16_t> lhsSp2(lhs2, 5);
290 Span<const uint8_t> rhsSp2(rhs2, 3);
291 EXPECT_EQ(BaseString::IndexOf(lhsSp2, rhsSp2, 0, 4), -1);
292
293 const uint16_t lhs3[] = {'a', 'b', 'a', 'b', 'c'};
294 const uint8_t rhs3[] = {'a', 'b', 'c'};
295 Span<const uint16_t> lhsSp3(lhs3, 5);
296 Span<const uint8_t> rhsSp3(rhs3, 3);
297 EXPECT_EQ(BaseString::IndexOf(lhsSp3, rhsSp3, 0, 4), 2);
298
299 const uint16_t lhs4[] = {'a', 'b', 'c', 'd', 'e'};
300 const uint8_t rhs4[] = {'a', 'b', 'x'};
301 Span<const uint16_t> lhsSp4(lhs4, 5);
302 Span<const uint8_t> rhsSp4(rhs4, 3);
303 EXPECT_EQ(BaseString::IndexOf(lhsSp4, rhsSp4, 0, 4), -1);
304 }
305
HWTEST_F_L0(BaseStringTest,IsUtf8EqualsUtf16_TEST0)306 HWTEST_F_L0(BaseStringTest, IsUtf8EqualsUtf16_TEST0)
307 {
308 const uint8_t utf8_01[] = {0xF0, 0xE0, 0xC0};
309 const uint16_t utf16_01[] = {'h', 'e', 'l', 'l', 'o'};
310 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_01, 3, utf16_01, 5));
311
312 const uint8_t utf8_02[] = {0xF0, 0x90, 0x80, 0x80};
313 const uint16_t utf16_02[] = {0xD801, 0xDC00};
314 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_02, 4, utf16_02, 2));
315
316 const uint8_t utf8_03[] = {0xF0, 0x90, 0x80, 0x80};
317 const uint16_t utf16_03[] = {0xD800, 0xDC01};
318 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_03, 4, utf16_03, 2));
319
320 const uint8_t utf8_04[] = {0xE0, 0xA0, 0x80};
321 const uint16_t utf16_04[] = {0x0801};
322 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_04, 3, utf16_04, 1));
323
324 const uint8_t utf8_05[] = {0xC3, 0xA9};
325 const uint16_t utf16_05[] = {0x00EA};
326 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_05, 2, utf16_05, 1));
327
328 const uint8_t utf8_06[] = {'A'};
329 const uint16_t utf16_06[] = {'B'};
330 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_06, 1, utf16_06, 1));
331
332 const uint8_t utf8_07[] = {0xF0, 0x90, 0x80, 0x80};
333 const uint16_t utf16_07[] = {0xD800};
334 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_07, 4, utf16_07, 1));
335
336 const uint8_t utf8_08[] = {0xF0, 0x8F, 0xBF, 0xBF};
337 const uint16_t utf16_08[] = {0xFFFE};
338 EXPECT_FALSE(BaseString::IsUtf8EqualsUtf16(utf8_08, 4, utf16_08, 1));
339 }
340
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST0)341 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST0)
342 {
343 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
344 const uint8_t rhs[] = {'c', 'd'};
345 Span<const uint8_t> lhsSp(lhs, 5);
346 Span<const uint8_t> rhsSp(rhs, 2);
347
348 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2);
349 }
350
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST1)351 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST1)
352 {
353 const uint8_t lhs[] = {'a', 'b', 'c', 'x', 'e'};
354 const uint8_t rhs[] = {'c', 'd'};
355 Span<const uint8_t> lhsSp(lhs, 5);
356 Span<const uint8_t> rhsSp(rhs, 2);
357
358 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), -1);
359 }
360
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST2)361 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST2)
362 {
363 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
364 const uint8_t rhs[] = {'c', 'x'};
365 Span<const uint8_t> lhsSp(lhs, 5);
366 Span<const uint8_t> rhsSp(rhs, 2);
367
368 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), -1);
369 }
370
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST3)371 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST3)
372 {
373 const uint8_t lhs[] = {'a', 'b', 'a', 'b', 'c'};
374 const uint8_t rhs[] = {'a', 'b'};
375 Span<const uint8_t> lhsSp(lhs, 5);
376 Span<const uint8_t> rhsSp(rhs, 2);
377
378 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2);
379 }
380
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST4)381 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST4)
382 {
383 const uint8_t lhs[] = {'a', 'b', 'c', 'd'};
384 const uint8_t rhs[] = {'x', 'y'};
385 Span<const uint8_t> lhsSp(lhs, 4);
386 Span<const uint8_t> rhsSp(rhs, 2);
387
388 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 3), -1);
389 }
390
HWTEST_F_L0(BaseStringTest,IsSubStringAtSpan_TEST3)391 HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST3)
392 {
393 const uint8_t lhs[] = {'a', 'b', 'c'};
394 const uint16_t rhs[] = {'x', 'y'};
395 Span<const uint8_t> lhsSp(lhs, 3);
396 Span<const uint16_t> rhsSp(rhs, 2);
397 ASSERT_FALSE(IsSubStringAtSpan(lhsSp, rhsSp, 1));
398
399 const uint8_t lhs1[] = {'a', 'b'};
400 const uint16_t rhs1[] = {'b'};
401 Span<const uint8_t> lhsSp1(lhs1, 2);
402 Span<const uint16_t> rhsSp1(rhs1, 1);
403 ASSERT_TRUE(IsSubStringAtSpan(lhsSp1, rhsSp1, 1));
404 }
405
HWTEST_F_L0(BaseStringTest,IsSubStringAtSpan_TEST4)406 HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST4)
407 {
408 const uint16_t lhs[] = {'a', 'b', 'c'};
409 const uint8_t rhs[] = {'x', 'y'};
410 Span<const uint16_t> lhsSp(lhs, 3);
411 Span<const uint8_t> rhsSp(rhs, 2);
412 ASSERT_FALSE(IsSubStringAtSpan(lhsSp, rhsSp, 1));
413
414 const uint16_t lhs1[] = {'a', 'b'};
415 const uint8_t rhs1[] = {'b'};
416 Span<const uint16_t> lhsSp1(lhs1, 2);
417 Span<const uint8_t> rhsSp1(rhs1, 1);
418 ASSERT_TRUE(IsSubStringAtSpan(lhsSp1, rhsSp1, 1));
419 }
420
HWTEST_F_L0(BaseStringTest,IsSubStringAtSpan_TEST5)421 HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST5)
422 {
423 const uint16_t lhs[] = {'a', 'b', 'c'};
424 const uint16_t rhs[] = {'x', 'y'};
425 Span<const uint16_t> lhsSp(lhs, 3);
426 Span<const uint16_t> rhsSp(rhs, 2);
427 ASSERT_FALSE(IsSubStringAtSpan(lhsSp, rhsSp, 1));
428
429 const uint16_t lhs1[] = {'a', 'b'};
430 const uint16_t rhs1[] = {'b'};
431 Span<const uint16_t> lhsSp1(lhs1, 2);
432 Span<const uint16_t> rhsSp1(rhs1, 1);
433 ASSERT_TRUE(IsSubStringAtSpan(lhsSp1, rhsSp1, 1));
434 }
435
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST5)436 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST5)
437 {
438 const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
439 const uint16_t rhs[] = {'c', 'd'};
440 Span<const uint8_t> lhsSp(lhs, 5);
441 Span<const uint16_t> rhsSp(rhs, 2);
442 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2);
443
444 const uint8_t lhs1[] = {'a', 'b', 'c', 'x', 'e'};
445 const uint16_t rhs1[] = {'c', 'd'};
446 Span<const uint8_t> lhsSp1(lhs1, 5);
447 Span<const uint16_t> rhsSp1(rhs1, 2);
448 EXPECT_EQ(BaseString::LastIndexOf(lhsSp1, rhsSp1, 4), -1);
449
450 const uint8_t lhs2[] = {'a', 'b', 'c', 'd', 'e'};
451 const uint16_t rhs2[] = {'c', 'x'};
452 Span<const uint8_t> lhsSp2(lhs2, 5);
453 Span<const uint16_t> rhsSp2(rhs2, 2);
454 EXPECT_EQ(BaseString::LastIndexOf(lhsSp2, rhsSp2, 4), -1);
455
456 const uint8_t lhs3[] = {'a', 'b', 'a', 'b', 'c'};
457 const uint16_t rhs3[] = {'a', 'b'};
458 Span<const uint8_t> lhsSp3(lhs3, 5);
459 Span<const uint16_t> rhsSp3(rhs3, 2);
460 EXPECT_EQ(BaseString::LastIndexOf(lhsSp3, rhsSp3, 4), 2);
461
462 const uint8_t lhs4[] = {'a', 'b', 'c', 'd'};
463 const uint16_t rhs4[] = {'x', 'y'};
464 Span<const uint8_t> lhsSp4(lhs4, 4);
465 Span<const uint16_t> rhsSp4(rhs4, 2);
466 EXPECT_EQ(BaseString::LastIndexOf(lhsSp4, rhsSp4, 3), -1);
467 }
468
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST6)469 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST6)
470 {
471 const uint16_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
472 const uint16_t rhs[] = {'c', 'd'};
473 Span<const uint16_t> lhsSp(lhs, 5);
474 Span<const uint16_t> rhsSp(rhs, 2);
475 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2);
476
477 const uint16_t lhs1[] = {'a', 'b', 'c', 'x', 'e'};
478 const uint16_t rhs1[] = {'c', 'd'};
479 Span<const uint16_t> lhsSp1(lhs1, 5);
480 Span<const uint16_t> rhsSp1(rhs1, 2);
481 EXPECT_EQ(BaseString::LastIndexOf(lhsSp1, rhsSp1, 4), -1);
482
483 const uint16_t lhs2[] = {'a', 'b', 'c', 'd', 'e'};
484 const uint16_t rhs2[] = {'c', 'x'};
485 Span<const uint16_t> lhsSp2(lhs2, 5);
486 Span<const uint16_t> rhsSp2(rhs2, 2);
487 EXPECT_EQ(BaseString::LastIndexOf(lhsSp2, rhsSp2, 4), -1);
488
489 const uint16_t lhs3[] = {'a', 'b', 'a', 'b', 'c'};
490 const uint16_t rhs3[] = {'a', 'b'};
491 Span<const uint16_t> lhsSp3(lhs3, 5);
492 Span<const uint16_t> rhsSp3(rhs3, 2);
493 EXPECT_EQ(BaseString::LastIndexOf(lhsSp3, rhsSp3, 4), 2);
494
495 const uint16_t lhs4[] = {'a', 'b', 'c', 'd'};
496 const uint16_t rhs4[] = {'x', 'y'};
497 Span<const uint16_t> lhsSp4(lhs4, 4);
498 Span<const uint16_t> rhsSp4(rhs4, 2);
499 EXPECT_EQ(BaseString::LastIndexOf(lhsSp4, rhsSp4, 3), -1);
500 }
501
HWTEST_F_L0(BaseStringTest,LastIndexOf_TEST7)502 HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST7)
503 {
504 const uint16_t lhs[] = {'a', 'b', 'c', 'd', 'e'};
505 const uint8_t rhs[] = {'c', 'd'};
506 Span<const uint16_t> lhsSp(lhs, 5);
507 Span<const uint8_t> rhsSp(rhs, 2);
508 EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2);
509
510 const uint16_t lhs1[] = {'a', 'b', 'c', 'x', 'e'};
511 const uint8_t rhs1[] = {'c', 'd'};
512 Span<const uint16_t> lhsSp1(lhs1, 5);
513 Span<const uint8_t> rhsSp1(rhs1, 2);
514 EXPECT_EQ(BaseString::LastIndexOf(lhsSp1, rhsSp1, 4), -1);
515
516 const uint16_t lhs2[] = {'a', 'b', 'c', 'd', 'e'};
517 const uint8_t rhs2[] = {'c', 'x'};
518 Span<const uint16_t> lhsSp2(lhs2, 5);
519 Span<const uint8_t> rhsSp2(rhs2, 2);
520 EXPECT_EQ(BaseString::LastIndexOf(lhsSp2, rhsSp2, 4), -1);
521
522 const uint16_t lhs3[] = {'a', 'b', 'a', 'b', 'c'};
523 const uint8_t rhs3[] = {'a', 'b'};
524 Span<const uint16_t> lhsSp3(lhs3, 5);
525 Span<const uint8_t> rhsSp3(rhs3, 2);
526 EXPECT_EQ(BaseString::LastIndexOf(lhsSp3, rhsSp3, 4), 2);
527
528 const uint16_t lhs4[] = {'a', 'b', 'c', 'd'};
529 const uint8_t rhs4[] = {'x', 'y'};
530 Span<const uint16_t> lhsSp4(lhs4, 4);
531 Span<const uint8_t> rhsSp4(rhs4, 2);
532 EXPECT_EQ(BaseString::LastIndexOf(lhsSp4, rhsSp4, 3), -1);
533 }
534 } // namespace common::test
535