• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "gtest/hwext/gtest-ext.h"
18 #include "gtest/hwext/gtest-tag.h"
19 #include "unicode/unistr.h"
20 
21 #include "base/utils/utf_helper.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 namespace OHOS::Ace::UtfUtils {
26 class UtfHelperTestNg : public Test {
SetUp()27     void SetUp() override
28     {
29         index = 0;
30     }
31 
32 public:
33     size_t index;
34 };
35 
36 /**
37  * @tc.name: DecodeUTF16_001
38  * @tc.desc: Test utf_helper DecodeUTF16
39  * @tc.type: FUNC
40  */
41 HWTEST_F(UtfHelperTestNg, DecodeUTF16_001, TestSize.Level1)
42 {
43     uint16_t utf16[1] = { 0x0041 };
44     size_t len = sizeof(utf16) / sizeof(utf16[0]);
45     EXPECT_EQ(DecodeUTF16(utf16, len, &index), 0x0041);
46     EXPECT_EQ(index, 0);
47 }
48 
49 /**
50  * @tc.name: DecodeUTF16_002
51  * @tc.desc: Test utf_helper DecodeUTF16
52  * @tc.type: FUNC
53  */
54 HWTEST_F(UtfHelperTestNg, DecodeUTF16_002, TestSize.Level1)
55 {
56     uint16_t utf16[2] = { 0xD83D, 0xDE00 };
57     size_t len = sizeof(utf16) / sizeof(utf16[0]);
58     EXPECT_EQ(DecodeUTF16(utf16, len, &index), 0x1F600);
59     EXPECT_EQ(index, 1);
60 }
61 
62 /**
63  * @tc.name: DecodeUTF16_003
64  * @tc.desc: Test utf_helper DecodeUTF16
65  * @tc.type: FUNC
66  */
67 HWTEST_F(UtfHelperTestNg, DecodeUTF16_003, TestSize.Level1)
68 {
69     uint16_t utf16[3] = { 0xD83D, 0xDC00 - 1, 0xDE00 };
70     size_t len = sizeof(utf16) / sizeof(utf16[0]);
71     EXPECT_EQ(DecodeUTF16(utf16, len, &index), 0xD83D);
72     EXPECT_EQ(index, 0);
73 }
74 
75 /**
76  * @tc.name: DecodeUTF16_004
77  * @tc.desc: Test utf_helper DecodeUTF16
78  * @tc.type: FUNC
79  */
80 HWTEST_F(UtfHelperTestNg, DecodeUTF16_004, TestSize.Level1)
81 {
82     uint16_t utf16[1] = { 0xD83D };
83     size_t len = sizeof(utf16) / sizeof(utf16[0]);
84     EXPECT_EQ(DecodeUTF16(utf16, len, &index), 0xD83D);
85     EXPECT_EQ(index, 0);
86 }
87 
88 /**
89  * @tc.name: DecodeUTF16_005
90  * @tc.desc: Test utf_helper DecodeUTF16
91  * @tc.type: FUNC
92  */
93 HWTEST_F(UtfHelperTestNg, DecodeUTF16_005, TestSize.Level1)
94 {
95     uint16_t utf16[2] = { 0x0041, 0xDC00 };
96     size_t len = sizeof(utf16) / sizeof(utf16[0]);
97     EXPECT_EQ(DecodeUTF16(utf16, len, &index), 0x0041);
98     EXPECT_EQ(index, 0);
99 }
100 
101 /**
102  * @tc.name: HandleAndDecodeInvalidUTF16_001
103  * @tc.desc: Test utf_helper HandleAndDecodeInvalidUTF16
104  * @tc.type: FUNC
105  */
106 HWTEST_F(UtfHelperTestNg, HandleAndDecodeInvalidUTF16_001, TestSize.Level1)
107 {
108     uint16_t utf16[2] = { 0xDE00, 0xD83D };
109     size_t len = sizeof(utf16) / sizeof(utf16[0]);
110     EXPECT_EQ(HandleAndDecodeInvalidUTF16(utf16, len, &index), 0xFFFD);
111     EXPECT_EQ(index, 0);
112 }
113 
114 /**
115  * @tc.name: HandleAndDecodeInvalidUTF16_002
116  * @tc.desc: Test utf_helper HandleAndDecodeInvalidUTF16
117  * @tc.type: FUNC
118  */
119 HWTEST_F(UtfHelperTestNg, HandleAndDecodeInvalidUTF16_002, TestSize.Level1)
120 {
121     uint16_t utf16[1] = { 0x4100 };
122     size_t len = sizeof(utf16) / sizeof(utf16[0]);
123     EXPECT_EQ(HandleAndDecodeInvalidUTF16(utf16, len, &index), 0x4100);
124     EXPECT_EQ(index, 0);
125 }
126 
127 /**
128  * @tc.name: HandleAndDecodeInvalidUTF16_003
129  * @tc.desc: Test utf_helper HandleAndDecodeInvalidUTF16
130  * @tc.type: FUNC
131  */
132 HWTEST_F(UtfHelperTestNg, HandleAndDecodeInvalidUTF16_003, TestSize.Level1)
133 {
134     uint16_t utf16[1] = { 0xDBFF - 1 };
135     size_t len = sizeof(utf16) / sizeof(utf16[0]);
136     EXPECT_EQ(HandleAndDecodeInvalidUTF16(utf16, len, &index), 0xFFFD);
137     EXPECT_EQ(index, 0);
138 }
139 
140 /**
141  * @tc.name: HandleAndDecodeInvalidUTF16_004
142  * @tc.desc: Test utf_helper HandleAndDecodeInvalidUTF16
143  * @tc.type: FUNC
144  */
145 HWTEST_F(UtfHelperTestNg, HandleAndDecodeInvalidUTF16_004, TestSize.Level1)
146 {
147     uint16_t utf16[2] = { 0xDBFF - 1, 0xDBFF - 1 };
148     size_t len = sizeof(utf16) / sizeof(utf16[0]);
149     EXPECT_EQ(HandleAndDecodeInvalidUTF16(utf16, len, &index), 0xFFFD);
150     EXPECT_EQ(index, 0);
151 }
152 
153 /**
154  * @tc.name: HandleAndDecodeInvalidUTF16_005
155  * @tc.desc: Test utf_helper HandleAndDecodeInvalidUTF16
156  * @tc.type: FUNC
157  */
158 HWTEST_F(UtfHelperTestNg, HandleAndDecodeInvalidUTF16_005, TestSize.Level1)
159 {
160     uint16_t first = 0xDBFF - 1;
161     uint16_t second = 0xDC00 + 1;
162     uint16_t utf16[2] = { first, second };
163     size_t len = sizeof(utf16) / sizeof(utf16[0]);
164     uint32_t res = ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
165     EXPECT_EQ(HandleAndDecodeInvalidUTF16(utf16, len, &index), res);
166     EXPECT_EQ(index, 1);
167 }
168 
169 /**
170  * @tc.name: HandleInvalidUTF16_001
171  * @tc.desc: Test utf_helper HandleInvalidUTF16
172  * @tc.type: FUNC
173  */
174 HWTEST_F(UtfHelperTestNg, HandleInvalidUTF16_001, TestSize.Level1)
175 {
176     uint16_t* utf16 = nullptr;
177     HandleInvalidUTF16(utf16, 0, 0);
178     ASSERT_EQ(utf16, nullptr);
179 }
180 
181 /**
182  * @tc.name: RepalceUnpairedSurrogates_001
183  * @tc.desc: Test utf_helper RepalceUnpairedSurrogates
184  * @tc.type: FUNC
185  */
186 HWTEST_F(UtfHelperTestNg, RepalceUnpairedSurrogates_001, TestSize.Level1)
187 {
188     uint16_t utf16[1] = { 0xDC00 + 1 };
189     size_t len = sizeof(utf16) / sizeof(utf16[0]);
190     HandleInvalidUTF16(utf16, len, 0);
191     EXPECT_EQ(utf16[0], 0xFFFD);
192 }
193 
194 /**
195  * @tc.name: RepalceUnpairedSurrogates_002
196  * @tc.desc: Test utf_helper RepalceUnpairedSurrogates
197  * @tc.type: FUNC
198  */
199 HWTEST_F(UtfHelperTestNg, RepalceUnpairedSurrogates_002, TestSize.Level1)
200 {
201     uint16_t utf16[1] = { 0xD800 - 1 };
202     size_t len = sizeof(utf16) / sizeof(utf16[0]);
203     HandleInvalidUTF16(utf16, len, 0);
204     EXPECT_EQ(utf16[0], 0xD800 - 1);
205 }
206 
207 /**
208  * @tc.name: RepalceUnpairedSurrogates_003
209  * @tc.desc: Test utf_helper RepalceUnpairedSurrogates
210  * @tc.type: FUNC
211  */
212 HWTEST_F(UtfHelperTestNg, RepalceUnpairedSurrogates_003, TestSize.Level1)
213 {
214     uint16_t utf16[1] = { 0xDC00 - 1 };
215     size_t len = sizeof(utf16) / sizeof(utf16[0]);
216     HandleInvalidUTF16(utf16, len, 0);
217     EXPECT_EQ(utf16[0], 0xFFFD);
218 }
219 
220 /**
221  * @tc.name: RepalceUnpairedSurrogates_004
222  * @tc.desc: Test utf_helper RepalceUnpairedSurrogates
223  * @tc.type: FUNC
224  */
225 HWTEST_F(UtfHelperTestNg, RepalceUnpairedSurrogates_004, TestSize.Level1)
226 {
227     uint16_t utf16[2] = { 0xDC00 - 1, 0xDC00 - 1 };
228     size_t len = sizeof(utf16) / sizeof(utf16[0]);
229     HandleInvalidUTF16(utf16, len, 0);
230     EXPECT_EQ(utf16[0], 0xFFFD);
231     EXPECT_EQ(utf16[1], 0xFFFD);
232 }
233 
234 /**
235  * @tc.name: RepalceUnpairedSurrogates_005
236  * @tc.desc: Test utf_helper RepalceUnpairedSurrogates
237  * @tc.type: FUNC
238  */
239 HWTEST_F(UtfHelperTestNg, RepalceUnpairedSurrogates_005, TestSize.Level1)
240 {
241     uint16_t utf16[2] = { 0xDC00 - 1, 0xDC00 + 1 };
242     size_t len = sizeof(utf16) / sizeof(utf16[0]);
243     HandleInvalidUTF16(utf16, len, 0);
244     EXPECT_EQ(utf16[0], 0xDC00 - 1);
245     EXPECT_EQ(utf16[1], 0xDC00 + 1);
246 }
247 
248 /**
249  * @tc.name: UTF8Length_001
250  * @tc.desc: Test utf_helper UTF8Length
251  * @tc.type: FUNC
252  */
253 HWTEST_F(UtfHelperTestNg, UTF8Length_001, TestSize.Level1)
254 {
255     uint32_t codepoint = 0xfffff;
256     uint8_t utf8[3] = { 0xd8 + 1, 0xdc - 1, 0xd8 + 1 };
257     size_t len = sizeof(utf8) / sizeof(utf8[0]);
258     size_t indexOfUtf8 = 0;
259     EXPECT_EQ(EncodeUTF8(codepoint, utf8, len, indexOfUtf8 - 1), 4);
260 }
261 
262 /**
263  * @tc.name: UTF8Length_002
264  * @tc.desc: Test utf_helper UTF8Length
265  * @tc.type: FUNC
266  */
267 HWTEST_F(UtfHelperTestNg, UTF8Length_002, TestSize.Level1)
268 {
269     uint32_t codepoint = 0x7f + 1;
270     uint8_t utf8[1] = { 0xd8 + 1 };
271     size_t len = sizeof(utf8) / sizeof(utf8[0]);
272     size_t indexOfUtf8 = 0;
273     EXPECT_EQ(EncodeUTF8(codepoint, utf8, len, indexOfUtf8 - 1), 2);
274 }
275 
276 /**
277  * @tc.name: EncodeUTF8_001
278  * @tc.desc: Test utf_helper EncodeUTF8
279  * @tc.type: FUNC
280  */
281 HWTEST_F(UtfHelperTestNg, EncodeUTF8_001, TestSize.Level1)
282 {
283     uint32_t codepoint = 0xffff;
284     uint8_t utf8[2] = { 0xd8 + 1, 0xdc - 1 };
285     size_t len = sizeof(utf8) / sizeof(utf8[0]);
286     size_t indexOfUtf8 = 0;
287     EXPECT_EQ(EncodeUTF8(codepoint, utf8, len, indexOfUtf8), 0);
288 }
289 
290 /**
291  * @tc.name: EncodeUTF8_002
292  * @tc.desc: Test utf_helper EncodeUTF8
293  * @tc.type: FUNC
294  */
295 HWTEST_F(UtfHelperTestNg, EncodeUTF8_002, TestSize.Level1)
296 {
297     uint32_t codepoint = 0xfffff;
298     uint8_t utf8[3] = { 0xd8 + 1, 0xdc - 1, 0xd8 + 1 };
299     size_t len = sizeof(utf8) / sizeof(utf8[0]);
300     size_t indexOfUtf8 = 0;
301     EXPECT_EQ(EncodeUTF8(codepoint, utf8, len, indexOfUtf8 - 1), 4);
302 }
303 
304 /**
305  * @tc.name: Utf16ToUtf8Size001
306  * @tc.desc: Test utf_helper Utf16ToUtf8Size with nullptr
307  * @tc.type: FUNC
308  */
309 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size001, TestSize.Level1)
310 {
311     uint16_t* utf16 = nullptr;
312     EXPECT_EQ(Utf16ToUtf8Size(utf16, 0), 1);  // 代码初始值问题,正确应为0
313 }
314 
315 /**
316  * @tc.name: Utf16ToUtf8Size002
317  * @tc.desc: Test utf_helper Utf16ToUtf8Size wtih empty array
318  * @tc.type: FUNC
319  */
320 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size002, TestSize.Level1)
321 {
322     uint16_t utf16[] = {};
323     EXPECT_EQ(Utf16ToUtf8Size(utf16, 0), 1);
324 }
325 
326 /**
327  * @tc.name: Utf16ToUtf8Size003
328  * @tc.desc: Test utf_helper Utf16ToUtf8Size with zero and length 0
329  * @tc.type: FUNC
330  */
331 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size003, TestSize.Level1)
332 {
333     uint16_t utf16[] = { 0 };
334     EXPECT_EQ(Utf16ToUtf8Size(utf16, 0), 1);
335 }
336 
337 /**
338  * @tc.name: Utf16ToUtf8Size004
339  * @tc.desc: Test utf_helper Utf16ToUtf8Size with single zero 1
340  * @tc.type: FUNC
341  */
342 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size004, TestSize.Level1)
343 {
344     uint16_t utf16[] = { 0 };
345     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 1);
346 }
347 
348 /**
349  * @tc.name: Utf16ToUtf8Size005
350  * @tc.desc: Test utf_helper Utf16ToUtf8Size with single Ascii
351  * @tc.type: FUNC
352  */
353 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size005, TestSize.Level1)
354 {
355     uint16_t utf16[] = { 0x41 };
356     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 2);
357 }
358 
359 /**
360  * @tc.name: Utf16ToUtf8Size006
361  * @tc.desc: Test utf_helper Utf16ToUtf8Size with single two bytes
362  * @tc.type: FUNC
363  */
364 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size006, TestSize.Level1)
365 {
366     uint16_t utf16[] = { 0x80 };
367     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 3);
368 }
369 
370 /**
371  * @tc.name: Utf16ToUtf8Size007
372  * @tc.desc: Test utf_helper Utf16ToUtf8Size with single three bytes
373  * @tc.type: FUNC
374  */
375 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size007, TestSize.Level1)
376 {
377     uint16_t utf16[] = { 0x800 };
378     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 4);
379 }
380 
381 /**
382  * @tc.name: Utf16ToUtf8Size008
383  * @tc.desc: Test utf_helper Utf16ToUtf8Size with high surrogate min
384  * @tc.type: FUNC
385  */
386 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size008, TestSize.Level1)
387 {
388     uint16_t utf16[] = { 0xD800 };
389     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 4);
390 }
391 
392 /**
393  * @tc.name: Utf16ToUtf8Size009
394  * @tc.desc: Test utf_helper Utf16ToUtf8Size with num in range high surrogate min and low surrogate max
395  * @tc.type: FUNC
396  */
397 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size009, TestSize.Level1)
398 {
399     uint16_t utf16[] = { 0xDC00 };
400     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 4);
401 }
402 
403 /**
404  * @tc.name: Utf16ToUtf8Size010
405  * @tc.desc: Test utf_helper Utf16ToUtf8Size with low surrogate max
406  * @tc.type: FUNC
407  */
408 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size010, TestSize.Level1)
409 {
410     uint16_t utf16[] = { 0xDFFF };
411     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 4);
412 }
413 
414 /**
415  * @tc.name: Utf16ToUtf8Size011
416  * @tc.desc: Test utf_helper Utf16ToUtf8Size with valid surrogate pair
417  * @tc.type: FUNC
418  */
419 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size011, TestSize.Level1)
420 {
421     uint16_t utf16[] = { 0xD800, 0xDC00 };
422     EXPECT_EQ(Utf16ToUtf8Size(utf16, 2), 5);
423 }
424 
425 /**
426  * @tc.name: Utf16ToUtf8Size012
427  * @tc.desc: Test utf_helper Utf16ToUtf8Size with high surrogate followed by nonsurrogate
428  * @tc.type: FUNC
429  */
430 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size012, TestSize.Level1)
431 {
432     uint16_t utf16[] = { 0xD800, 0x1234 };
433     EXPECT_EQ(Utf16ToUtf8Size(utf16, 2), 7);
434 }
435 
436 /**
437  * @tc.name: Utf16ToUtf8Size013
438  * @tc.desc: Test utf_helper Utf16ToUtf8Size with high surrogate at end
439  * @tc.type: FUNC
440  */
441 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size013, TestSize.Level1)
442 {
443     uint16_t utf16[] = { 0xD800, 0xD800 };
444     EXPECT_EQ(Utf16ToUtf8Size(utf16, 2), 7);
445 }
446 
447 /**
448  * @tc.name: Utf16ToUtf8Size014
449  * @tc.desc: Test utf_helper Utf16ToUtf8Size with mixed characters with zero
450  * @tc.type: FUNC
451  */
452 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size014, TestSize.Level1)
453 {
454     uint16_t utf16[] = { 0x41, 0, 0x80, 0x800, 0xD800, 0xDC00 };
455     EXPECT_EQ(Utf16ToUtf8Size(utf16, 6), 11);
456 }
457 
458 /**
459  * @tc.name: Utf16ToUtf8Size015
460  * @tc.desc: Test utf_helper Utf16ToUtf8Size with multiple valid surrogate pairs
461  * @tc.type: FUNC
462  */
463 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size015, TestSize.Level1)
464 {
465     uint16_t utf16[] = { 0xD800, 0xDC00, 0xD801, 0xDC01 };
466     EXPECT_EQ(Utf16ToUtf8Size(utf16, 4), 9);
467 }
468 
469 /**
470  * @tc.name: Utf16ToUtf8Size016
471  * @tc.desc: Test utf_helper Utf16ToUtf8Size with max two bytes
472  * @tc.type: FUNC
473  */
474 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size016, TestSize.Level1)
475 {
476     uint16_t utf16[] = { 0x7FF };
477     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 3);
478 }
479 
480 /**
481  * @tc.name: Utf16ToUtf8Size017
482  * @tc.desc: Test utf_helper Utf16ToUtf8Size with min three bytes
483  * @tc.type: FUNC
484  */
485 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size017, TestSize.Level1)
486 {
487     uint16_t utf16[] = { 0x800 };
488     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 4);
489 }
490 
491 /**
492  * @tc.name: Utf16ToUtf8Size018
493  * @tc.desc: Test utf_helper Utf16ToUtf8Size with nonsurrogate three bytes
494  * @tc.type: FUNC
495  */
496 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size018, TestSize.Level1)
497 {
498     uint16_t utf16[] = { 0xD7FF };
499     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 4);
500 }
501 
502 /**
503  * @tc.name: Utf16ToUtf8Size019
504  * @tc.desc: Test utf_helper Utf16ToUtf8Size with low surrogate after
505  * @tc.type: FUNC
506  */
507 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size019, TestSize.Level1)
508 {
509     uint16_t utf16[] = { 0xE000 };
510     EXPECT_EQ(Utf16ToUtf8Size(utf16, 1), 4);
511 }
512 
513 /**
514  * @tc.name: Utf16ToUtf8Size020
515  * @tc.desc: Test utf_helper Utf16ToUtf8Size with multiple zeros
516  * @tc.type: FUNC
517  */
518 HWTEST_F(UtfHelperTestNg, Utf16ToUtf8Size020, TestSize.Level1)
519 {
520     uint16_t utf16[] = { 0, 0, 0 };
521     EXPECT_EQ(Utf16ToUtf8Size(utf16, 3), 1);
522 }
523 
524 /**
525  * @tc.name: ConvertRegionUtf16ToUtf8_001
526  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 with invalid input 1
527  * @tc.type: FUNC
528  */
529 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_001, TestSize.Level1)
530 {
531     uint8_t output[4] = { 0 };
532     size_t result = ConvertRegionUtf16ToUtf8(nullptr, output, 2, 4, 0);
533     EXPECT_EQ(result, 0);
534 }
535 
536 /**
537  * @tc.name: ConvertRegionUtf16ToUtf8_002
538  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 with invalid input 2
539  * @tc.type: FUNC
540  */
541 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_002, TestSize.Level1)
542 {
543     size_t result = ConvertRegionUtf16ToUtf8(reinterpret_cast<const uint16_t*>("A"), nullptr, 1, 4, 0);
544     EXPECT_EQ(result, 0);
545 }
546 
547 /**
548  * @tc.name: ConvertRegionUtf16ToUtf8_003
549  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 with invalid input 3
550  * @tc.type: FUNC
551  */
552 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_003, TestSize.Level1)
553 {
554     uint8_t output[4] = { 0 };
555     size_t result = ConvertRegionUtf16ToUtf8(reinterpret_cast<const uint16_t*>("A"), output, 1, 0, 0);
556     EXPECT_EQ(result, 0);
557 }
558 
559 /**
560  * @tc.name: ConvertRegionUtf16ToUtf8_004
561  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 with ASCII characters: 'a', 'b' and 'c'
562  * @tc.type: FUNC
563  */
564 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_004, TestSize.Level1)
565 {
566     const uint16_t input[] = { 0x0041, 0x0042, 0x0043 };
567     std::array<uint8_t, 3> output = { 0 };
568     size_t result = ConvertRegionUtf16ToUtf8(input, output.data(), 3, 3, 0);
569 
570     EXPECT_EQ(result, 3);
571     EXPECT_EQ(output[0], 0x41);
572     EXPECT_EQ(output[1], 0x42);
573     EXPECT_EQ(output[2], 0x43);
574 }
575 
576 /**
577  * @tc.name: ConvertRegionUtf16ToUtf8_005
578  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 with surrogate pair (smiling face)
579  * @tc.type: FUNC
580  */
581 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_005, TestSize.Level1)
582 {
583     const uint16_t input[] = { 0xD83D, 0xDE00 };
584     std::array<uint8_t, 4> output = { 0 };
585     size_t result = ConvertRegionUtf16ToUtf8(input, output.data(), 2, 4, 0);
586 
587     EXPECT_EQ(result, 4);
588     EXPECT_EQ(output[0], 0xF0);
589     EXPECT_EQ(output[1], 0x9F);
590     EXPECT_EQ(output[2], 0x98);
591     EXPECT_EQ(output[3], 0x80);
592 }
593 
594 /**
595  * @tc.name: ConvertRegionUtf16ToUtf8_006
596  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 start position
597  * @tc.type: FUNC
598  */
599 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_006, TestSize.Level1)
600 {
601     const uint16_t input[] = { 0x0041, 0xD83D, 0xDE00, 0x0042 };
602     std::array<uint8_t, 5> output = { 0 };
603     size_t result = ConvertRegionUtf16ToUtf8(input, output.data(), 3, 5, 1);
604 
605     EXPECT_EQ(result, 5);
606     EXPECT_EQ(output[0], 0xF0);
607     EXPECT_EQ(output[1], 0x9F);
608     EXPECT_EQ(output[2], 0x98);
609     EXPECT_EQ(output[3], 0x80);
610     EXPECT_EQ(output[4], 0x42);
611 }
612 
613 /**
614  * @tc.name: ConvertRegionUtf16ToUtf8_007
615  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 with empty region
616  * @tc.type: FUNC
617  */
618 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_007, TestSize.Level1)
619 {
620     const uint16_t input[] = { 0x0041 };
621     std::array<uint8_t, 1> output = { 0 };
622     size_t result = ConvertRegionUtf16ToUtf8(input, output.data(), 0, 1, 0);
623 
624     EXPECT_EQ(result, 0);
625 }
626 
627 /**
628  * @tc.name: ConvertRegionUtf16ToUtf8_008
629  * @tc.desc: Test utf_helper ConvertRegionUtf16ToUtf8 start beyond end
630  * @tc.type: FUNC
631  */
632 HWTEST_F(UtfHelperTestNg, ConvertRegionUtf16ToUtf8_008, TestSize.Level1)
633 {
634     const uint16_t input[] = { 0x0041 };
635     std::array<uint8_t, 1> output = { 0 };
636     size_t result = ConvertRegionUtf16ToUtf8(input, output.data(), 1, 0, 0);
637 
638     EXPECT_EQ(result, 0);
639 }
640 
641 /**
642  * @tc.name: IsIndexInPairedSurrogates001
643  * @tc.desc: Test utf_helper IsIndexInPairedSurrogates with empty string
644  * @tc.type: FUNC
645  */
646 HWTEST_F(UtfHelperTestNg, IsIndexInPairedSurrogates001, TestSize.Level1)
647 {
648     std::u16string empty;
649     EXPECT_FALSE(IsIndexInPairedSurrogates(0, empty));
650     EXPECT_FALSE(IsIndexInPairedSurrogates(-1, empty));
651     EXPECT_FALSE(IsIndexInPairedSurrogates(1, empty));
652 }
653 
654 /**
655  * @tc.name: IsIndexInPairedSurrogates002
656  * @tc.desc: Test utf_helper IsIndexInPairedSurrogates with index zero or outOfRange
657  * @tc.type: FUNC
658  */
659 HWTEST_F(UtfHelperTestNg, IsIndexInPairedSurrogates002, TestSize.Level1)
660 {
661     std::u16string str = {u'a', 0xD800, 0xDC00};
662     EXPECT_FALSE(IsIndexInPairedSurrogates(0, str));
663     EXPECT_FALSE(IsIndexInPairedSurrogates(3, str));
664 }
665 
666 /**
667  * @tc.name: IsIndexInPairedSurrogates003
668  * @tc.desc: Test utf_helper IsIndexInPairedSurrogates with valid surrogate pair
669  * @tc.type: FUNC
670  */
671 HWTEST_F(UtfHelperTestNg, IsIndexInPairedSurrogates003, TestSize.Level1)
672 {
673     std::u16string str1 = {0xD800, 0xDC00};
674     EXPECT_TRUE(IsIndexInPairedSurrogates(1, str1));
675 
676     std::u16string str2 = {0xDBFF, 0xDFFF};
677     EXPECT_TRUE(IsIndexInPairedSurrogates(1, str2));
678 }
679 
680 /**
681  * @tc.name: IsIndexInPairedSurrogates004
682  * @tc.desc: Test utf_helper IsIndexInPairedSurrogates with invalid surrogate pair
683  * @tc.type: FUNC
684  */
685 HWTEST_F(UtfHelperTestNg, IsIndexInPairedSurrogates004, TestSize.Level1)
686 {
687     std::u16string str1 = {0xD800, u'a'};
688     EXPECT_FALSE(IsIndexInPairedSurrogates(1, str1));
689 
690     std::u16string str2 = {u'a', 0xDC00};
691     EXPECT_FALSE(IsIndexInPairedSurrogates(1, str2));
692 
693     std::u16string str3 = {0xD800};
694     EXPECT_FALSE(IsIndexInPairedSurrogates(1, str3));
695 }
696 
697 /**
698  * @tc.name: IsIndexInPairedSurrogates005
699  * @tc.desc: Test utf_helper IsIndexInPairedSurrogates with multiple surrogate pairs
700  * @tc.type: FUNC
701  */
702 HWTEST_F(UtfHelperTestNg, IsIndexInPairedSurrogates005, TestSize.Level1)
703 {
704     std::u16string str = {0xD800, 0xDC00, 0xDBFF, 0xDFFF};
705 
706     EXPECT_TRUE(IsIndexInPairedSurrogates(1, str));
707     EXPECT_TRUE(IsIndexInPairedSurrogates(3, str));
708     EXPECT_FALSE(IsIndexInPairedSurrogates(2, str));
709 }
710 
711 /**
712  * @tc.name: IsIndexInPairedSurrogates006
713  * @tc.desc: Test utf_helper IsIndexInPairedSurrogates with mixed characters
714  * @tc.type: FUNC
715  */
716 HWTEST_F(UtfHelperTestNg, IsIndexInPairedSurrogates006, TestSize.Level1)
717 {
718     std::u16string str = {u'A', 0xD800, 0xDC00, u'B'};
719 
720     EXPECT_FALSE(IsIndexInPairedSurrogates(0, str));
721     EXPECT_FALSE(IsIndexInPairedSurrogates(1, str));
722     EXPECT_TRUE(IsIndexInPairedSurrogates(2, str));
723     EXPECT_FALSE(IsIndexInPairedSurrogates(3, str));
724 }
725 } // namespace OHOS::Ace::UtfUtils
726