• 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 } // namespace OHOS::Ace::UtfUtils
304