• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "ecmascript/ecma_string-inl.h"
17 #include "ecmascript/object_factory.h"
18 #include "ecmascript/tests/test_helper.h"
19 
20 using namespace panda::ecmascript;
21 
22 namespace panda::test {
23 class LineEcmaStringTest : public testing::Test {
24 public:
SetUpTestCase()25     static void SetUpTestCase()
26     {
27         GTEST_LOG_(INFO) << "SetUpTestCase";
28     }
29 
TearDownTestCase()30     static void TearDownTestCase()
31     {
32         GTEST_LOG_(INFO) << "TearDownCase";
33     }
34 
SetUp()35     void SetUp() override
36     {
37         TestHelper::CreateEcmaVMWithScope(ecmaVMPtr, thread, scope);
38     }
39 
TearDown()40     void TearDown() override
41     {
42         TestHelper::DestroyEcmaVMWithScope(ecmaVMPtr, scope);
43     }
44 
45     EcmaVM *ecmaVMPtr {nullptr};
46     ecmascript::EcmaHandleScope *scope {nullptr};
47     JSThread *thread {nullptr};
48 };
49 
50 /*
51  * @tc.name: ComputeSizeUtf8
52  * @tc.desc: Check whether the value returned through calling ComputeSizeUtf8 function is within expectations.
53  * @tc.type: FUNC
54  * @tc.require:
55  */
HWTEST_F_L0(LineEcmaStringTest,ComputeSizeUtf8)56 HWTEST_F_L0(LineEcmaStringTest, ComputeSizeUtf8)
57 {
58     uint32_t scale = 3333;
59     for (uint32_t i = EcmaString::MAX_STRING_LENGTH - 1; i > scale; i = i - scale) {
60         uint32_t length = i;
61         EXPECT_EQ(LineEcmaString::ComputeSizeUtf8(length), length + LineEcmaString::SIZE);
62     }
63 }
64 
65 /*
66  * @tc.name: ComputeSizeUtf16
67  * @tc.desc: Check whether the value returned through calling ComputeSizeUtf16 function is within expectations.
68  * @tc.type: FUNC
69  * @tc.require:
70  */
HWTEST_F_L0(LineEcmaStringTest,ComputeSizeUtf16)71 HWTEST_F_L0(LineEcmaStringTest, ComputeSizeUtf16)
72 {
73     uint32_t scale = 3333;
74     for (uint32_t i = EcmaString::MAX_STRING_LENGTH - 1; i > scale; i = i - scale) {
75         uint32_t length = i;
76         EXPECT_EQ(LineEcmaString::ComputeSizeUtf16(length), 2 * length + LineEcmaString::SIZE);
77     }
78 }
79 
80 class EcmaStringAccessorTest : public testing::Test {
81 public:
SetUpTestCase()82     static void SetUpTestCase()
83     {
84         GTEST_LOG_(INFO) << "SetUpTestCase";
85     }
86 
TearDownTestCase()87     static void TearDownTestCase()
88     {
89         GTEST_LOG_(INFO) << "TearDownCase";
90     }
91 
SetUp()92     void SetUp() override
93     {
94         TestHelper::CreateEcmaVMWithScope(ecmaVMPtr, thread, scope);
95     }
96 
TearDown()97     void TearDown() override
98     {
99         TestHelper::DestroyEcmaVMWithScope(ecmaVMPtr, scope);
100     }
101 
102     EcmaVM *ecmaVMPtr {nullptr};
103     ecmascript::EcmaHandleScope *scope {nullptr};
104     JSThread *thread {nullptr};
105 };
106 
107 /*
108  * @tc.name: CreateLineString
109  * @tc.desc: Check whether the EcmaString created through calling CreateLineString function is within expectations.
110  * @tc.type: FUNC
111  * @tc.require:
112  */
HWTEST_F_L0(EcmaStringAccessorTest,CreateLineString)113 HWTEST_F_L0(EcmaStringAccessorTest, CreateLineString)
114 {
115     // CreateLineString( , true, ).
116     size_t sizeAllocComp = 5;
117     JSHandle<EcmaString> handleEcmaStrAllocComp(thread,
118         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeAllocComp, true));
119     for (uint32_t i = 0; i < sizeAllocComp; i++) {
120         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocComp).Get(i), 0U);
121     }
122     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocComp).GetLength(), sizeAllocComp);
123     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrAllocComp).IsUtf8());
124     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrAllocComp).IsUtf16());
125 
126     // CreateLineString( , false, ).
127     size_t sizeAllocNotComp = 5;
128     JSHandle<EcmaString> handleEcmaStrAllocNotComp(thread,
129         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeAllocNotComp, false));
130     for (uint32_t i = 0; i < sizeAllocNotComp; i++) {
131         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocNotComp).Get(i), 0U);
132     }
133     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocNotComp).GetLength(), sizeAllocNotComp);
134     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrAllocNotComp).IsUtf8());
135     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrAllocNotComp).IsUtf16());
136 }
137 
138 /*
139  * @tc.name: CreateEmptyString
140  * @tc.desc: Check whether the EcmaString created through calling CreateEmptyString function is within expectations.
141  * @tc.type: FUNC
142  * @tc.require:
143  */
HWTEST_F_L0(EcmaStringAccessorTest,CreateEmptyString)144 HWTEST_F_L0(EcmaStringAccessorTest, CreateEmptyString)
145 {
146     JSHandle<EcmaString> handleEcmaStrEmpty(thread, EcmaStringAccessor::CreateEmptyString(ecmaVMPtr));
147     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrEmpty).GetLength(), 0U);
148     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrEmpty).IsUtf8());
149     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrEmpty).IsUtf16());
150 }
151 
152 /*
153  * @tc.name: CreateFromUtf8
154  * @tc.desc: Check whether the EcmaString created through calling CreateFromUtf8 function is within expectations.
155  * @tc.type: FUNC
156  * @tc.require:
157  */
HWTEST_F_L0(EcmaStringAccessorTest,CreateFromUtf8)158 HWTEST_F_L0(EcmaStringAccessorTest, CreateFromUtf8)
159 {
160     uint8_t arrayU8[] = {"xyz123!@#"};
161     size_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
162     JSHandle<EcmaString> handleEcmaStrU8(thread,
163         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8[0], lengthEcmaStrU8, true));
164     for (uint32_t i = 0; i < lengthEcmaStrU8; i++) {
165         EXPECT_EQ(arrayU8[i], EcmaStringAccessor(handleEcmaStrU8).Get(i));
166     }
167     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU8).GetLength(), lengthEcmaStrU8);
168     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrU8).IsUtf8());
169     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrU8).IsUtf16());
170 }
171 
172 /*
173  * @tc.name: CreateFromUtf16
174  * @tc.desc: Check whether the EcmaString created through calling CreateFromUtf16 function is within expectations.
175  * @tc.type: FUNC
176  * @tc.require:
177  */
HWTEST_F_L0(EcmaStringAccessorTest,CreateFromUtf16)178 HWTEST_F_L0(EcmaStringAccessorTest, CreateFromUtf16)
179 {
180     // CreateFromUtf16( , , , true).
181     uint16_t arrayU16Comp[] = {1, 23, 45, 67, 127};
182     size_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]);
183     JSHandle<EcmaString> handleEcmaStrU16Comp(thread,
184         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16Comp[0], lengthEcmaStrU16Comp, true));
185     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16Comp).GetLength(), lengthEcmaStrU16Comp);
186     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrU16Comp).IsUtf8());
187     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrU16Comp).IsUtf16());
188 
189     // CreateFromUtf16( , , , false).
190     uint16_t arrayU16NotComp[] = {127, 33, 128, 12, 256, 11100, 65535};
191     size_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]);
192     JSHandle<EcmaString> handleEcmaStrU16NotComp(thread,
193         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, false));
194     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16NotComp).GetLength(), lengthEcmaStrU16NotComp);
195     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrU16NotComp).IsUtf8());
196     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrU16NotComp).IsUtf16());
197 }
198 
199 /*
200  * @tc.name: Concat_001
201  * @tc.desc: Check whether the EcmaString returned through calling Concat function between EcmaString made by
202  * CreateFromUtf8() and EcmaString made by CreateFromUtf8() is within expectations.
203  * @tc.type: FUNC
204  * @tc.require:
205  */
HWTEST_F_L0(EcmaStringAccessorTest,Concat_001)206 HWTEST_F_L0(EcmaStringAccessorTest, Concat_001)
207 {
208     // Concat(). EcmaString made by CreateFromUtf8() and EcmaString made by CreateFromUtf8().
209     uint8_t arrayFrontU8[] = {"abcdef"};
210     uint8_t arrayBackU8[] = {"ABCDEF"};
211     uint32_t lengthEcmaStrFrontU8 = sizeof(arrayFrontU8) - 1;
212     uint32_t lengthEcmaStrBackU8 = sizeof(arrayBackU8) - 1;
213     JSHandle<EcmaString> handleEcmaStrFrontU8(thread,
214         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayFrontU8[0], lengthEcmaStrFrontU8, true));
215     JSHandle<EcmaString> handleEcmaStrBackU8(thread,
216         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayBackU8[0], lengthEcmaStrBackU8, true));
217     JSHandle<EcmaString> handleEcmaStrConcatU8(thread,
218         EcmaStringAccessor::Concat(ecmaVMPtr, handleEcmaStrFrontU8, handleEcmaStrBackU8));
219     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrConcatU8).IsUtf8());
220     for (uint32_t i = 0; i < lengthEcmaStrFrontU8; i++) {
221         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU8).Get(i), arrayFrontU8[i]);
222     }
223     for (uint32_t i = 0; i < lengthEcmaStrBackU8; i++) {
224         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU8).Get(i + lengthEcmaStrFrontU8), arrayBackU8[i]);
225     }
226     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU8).GetLength(), lengthEcmaStrFrontU8 + lengthEcmaStrBackU8);
227 }
228 
229 /*
230  * @tc.name: Concat_002
231  * @tc.desc: Check whether the EcmaString returned through calling Concat function between EcmaString made by
232  * CreateFromUtf16( , , , false) and EcmaString made by CreateFromUtf16( , , , false) is within expectations.
233  * @tc.type: FUNC
234  * @tc.require:
235  */
HWTEST_F_L0(EcmaStringAccessorTest,Concat_002)236 HWTEST_F_L0(EcmaStringAccessorTest, Concat_002)
237 {
238     // Concat(). EcmaString made by CreateFromUtf16( , , , false) and EcmaString made by CreateFromUtf16( , , , false).
239     uint16_t arrayFrontU16NotComp[] = {128, 129, 256, 11100, 65535, 100};
240     uint16_t arrayBackU16NotComp[] = {88, 768, 1, 270, 345, 333};
241     uint32_t lengthEcmaStrFrontU16NotComp = sizeof(arrayFrontU16NotComp) / sizeof(arrayFrontU16NotComp[0]);
242     uint32_t lengthEcmaStrBackU16NotComp = sizeof(arrayBackU16NotComp) / sizeof(arrayBackU16NotComp[0]);
243     JSHandle<EcmaString> handleEcmaStrFrontU16NotComp(thread,
244         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayFrontU16NotComp[0], lengthEcmaStrFrontU16NotComp, false));
245     JSHandle<EcmaString> handleEcmaStrBackU16NotComp(thread,
246         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayBackU16NotComp[0], lengthEcmaStrBackU16NotComp, false));
247     JSHandle<EcmaString> handleEcmaStrConcatU16NotComp(thread,
248         EcmaStringAccessor::Concat(ecmaVMPtr, handleEcmaStrFrontU16NotComp, handleEcmaStrBackU16NotComp));
249     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrConcatU16NotComp).IsUtf16());
250     for (uint32_t i = 0; i < lengthEcmaStrFrontU16NotComp; i++) {
251         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU16NotComp).Get(i), arrayFrontU16NotComp[i]);
252     }
253     for (uint32_t i = 0; i < lengthEcmaStrBackU16NotComp; i++) {
254         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU16NotComp).Get(i + lengthEcmaStrFrontU16NotComp),
255             arrayBackU16NotComp[i]);
256     }
257     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU16NotComp).GetLength(),
258         lengthEcmaStrFrontU16NotComp + lengthEcmaStrBackU16NotComp);
259 }
260 
261 /*
262  * @tc.name: Concat_003
263  * @tc.desc: Check whether the EcmaString returned through calling Concat function between EcmaString made by
264  * CreateFromUtf8() and EcmaString made by CreateFromUtf16( , , , false) is within expectations.
265  * @tc.type: FUNC
266  * @tc.require:
267  */
HWTEST_F_L0(EcmaStringAccessorTest,Concat_003)268 HWTEST_F_L0(EcmaStringAccessorTest, Concat_003)
269 {
270     // Concat(). EcmaString made by CreateFromUtf8() and EcmaString made by CreateFromUtf16( , , , false).
271     uint8_t arrayFrontU8[] = {"abcdef"};
272     uint16_t arrayBackU16NotComp[] = {88, 768, 1, 270, 345, 333};
273     uint32_t lengthEcmaStrFrontU8 = sizeof(arrayFrontU8) - 1;
274     uint32_t lengthEcmaStrBackU16NotComp = sizeof(arrayBackU16NotComp) / sizeof(arrayBackU16NotComp[0]);
275     JSHandle<EcmaString> handleEcmaStrFrontU8(thread,
276         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayFrontU8[0], lengthEcmaStrFrontU8, true));
277     JSHandle<EcmaString> handleEcmaStrBackU16NotComp(thread,
278         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayBackU16NotComp[0], lengthEcmaStrBackU16NotComp, false));
279     JSHandle<EcmaString> handleEcmaStrConcatU8U16NotComp(thread,
280         EcmaStringAccessor::Concat(ecmaVMPtr, handleEcmaStrFrontU8, handleEcmaStrBackU16NotComp));
281     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrConcatU8U16NotComp).IsUtf16());
282     for (uint32_t i = 0; i < lengthEcmaStrFrontU8; i++) {
283         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU8U16NotComp).Get(i), arrayFrontU8[i]);
284     }
285     for (uint32_t i = 0; i < lengthEcmaStrBackU16NotComp; i++) {
286         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU8U16NotComp).Get(i + lengthEcmaStrFrontU8),
287             arrayBackU16NotComp[i]);
288     }
289     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrConcatU8U16NotComp).GetLength(),
290         lengthEcmaStrFrontU8 + lengthEcmaStrBackU16NotComp);
291 }
292 
293 /*
294  * @tc.name: FastSubString_001
295  * @tc.desc: Check whether the EcmaString returned through calling FastSubString function from EcmaString made by
296  * CreateFromUtf8() is within expectations.
297  * @tc.type: FUNC
298  * @tc.require:
299  */
HWTEST_F_L0(EcmaStringAccessorTest,FastSubString_001)300 HWTEST_F_L0(EcmaStringAccessorTest, FastSubString_001)
301 {
302     // FastSubString(). From EcmaString made by CreateFromUtf8().
303     uint8_t arrayU8[6] = {3, 7, 19, 54, 99};
304     uint32_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
305     JSHandle<EcmaString> handleEcmaStrU8(thread,
306         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8[0], lengthEcmaStrU8, true));
307     uint32_t indexStartSubU8 = 2;
308     uint32_t lengthSubU8 = 2;
309     JSHandle<EcmaString> handleEcmaStrSubU8(thread,
310         EcmaStringAccessor::FastSubString(ecmaVMPtr, handleEcmaStrU8, indexStartSubU8, lengthSubU8));
311     for (uint32_t i = 0; i < lengthSubU8; i++) {
312         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrSubU8).Get(i),
313             EcmaStringAccessor(handleEcmaStrU8).Get(i + indexStartSubU8));
314     }
315     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrSubU8).GetLength(), lengthSubU8);
316 }
317 
318 /*
319  * @tc.name: FastSubString_002
320  * @tc.desc: Check whether the EcmaString returned through calling FastSubString function from EcmaString made by
321  * CreateFromUtf16( , , , true) is within expectations.
322  * @tc.type: FUNC
323  * @tc.require:
324  */
HWTEST_F_L0(EcmaStringAccessorTest,FastSubString_002)325 HWTEST_F_L0(EcmaStringAccessorTest, FastSubString_002)
326 {
327     // FastSubString(). From EcmaString made by CreateFromUtf16( , , , true).
328     uint16_t arrayU16Comp[] = {1, 12, 34, 56, 127};
329     uint32_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]);
330     JSHandle<EcmaString> handleEcmaStrU16Comp(thread,
331         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16Comp[0], lengthEcmaStrU16Comp, true));
332     uint32_t indexStartSubU16Comp = 0;
333     uint32_t lengthSubU16Comp = 2;
334     JSHandle<EcmaString> handleEcmaStrSubU16Comp(thread,
335         EcmaStringAccessor::FastSubString(ecmaVMPtr, handleEcmaStrU16Comp, indexStartSubU16Comp, lengthSubU16Comp));
336     for (uint32_t i = 0; i < lengthSubU16Comp; i++) {
337         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrSubU16Comp).Get(i),
338             EcmaStringAccessor(handleEcmaStrU16Comp).Get(i + indexStartSubU16Comp));
339     }
340     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrSubU16Comp).GetLength(), lengthSubU16Comp);
341 }
342 
343 /*
344  * @tc.name: FastSubString_003
345  * @tc.desc: Check whether the EcmaString returned through calling FastSubString function from EcmaString made by
346  * CreateFromUtf16( , , , false) is within expectations.
347  * @tc.type: FUNC
348  * @tc.require:
349  */
HWTEST_F_L0(EcmaStringAccessorTest,FastSubString_003)350 HWTEST_F_L0(EcmaStringAccessorTest, FastSubString_003)
351 {
352     // FastSubString(). From EcmaString made by CreateFromUtf16( , , , false).
353     uint16_t arrayU16NotComp[] = {19, 54, 256, 11100, 65535};
354     uint32_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]);
355     JSHandle<EcmaString> handleEcmaStrU16NotComp(thread,
356         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, false));
357     uint32_t indexStartSubU16NotComp = 0;
358     uint32_t lengthSubU16NotComp = 2;
359     JSHandle<EcmaString> handleEcmaStrSubU16NotComp(thread, EcmaStringAccessor::FastSubString(
360         ecmaVMPtr, handleEcmaStrU16NotComp, indexStartSubU16NotComp, lengthSubU16NotComp));
361     for (uint32_t i = 0; i < lengthSubU16NotComp; i++) {
362         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrSubU16NotComp).Get(i),
363             EcmaStringAccessor(handleEcmaStrU16NotComp).Get(i + indexStartSubU16NotComp));
364     }
365     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrSubU16NotComp).GetLength(), lengthSubU16NotComp);
366 }
367 
368 /*
369  * @tc.name: FastSubString_004
370  * @tc.desc: Check whether the EcmaString returned through calling FastSubString function from EcmaString
371  * @tc.type: FUNC
372  * @tc.require:
373  */
HWTEST_F_L0(EcmaStringAccessorTest,FastSubString_004)374 HWTEST_F_L0(EcmaStringAccessorTest, FastSubString_004)
375 {
376     ObjectFactory* factory = ecmaVMPtr->GetFactory();
377     {
378         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("整数integer");
379         JSHandle<EcmaString> tmpString = factory->NewFromASCII("integer");
380         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
381         EcmaString *res = EcmaStringAccessor::FastSubString(ecmaVMPtr, sourceString, 2, 7);
382         EXPECT_TRUE(EcmaStringAccessor(res).IsUtf8());
383         EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(res, *tmpString));
384     }
385     {
386         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("整数integer");
387         JSHandle<EcmaString> tmpString = factory->NewFromUtf8("整数");
388         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
389         EcmaString *res = EcmaStringAccessor::FastSubString(ecmaVMPtr, sourceString, 0, 2);
390         EXPECT_TRUE(EcmaStringAccessor(res).IsUtf16());
391         EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(res, *tmpString));
392     }
393     {
394         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("整数integer");
395         JSHandle<EcmaString> tmpString = factory->NewFromUtf8("数intege");
396         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
397         EcmaString *res = EcmaStringAccessor::FastSubString(ecmaVMPtr, sourceString, 1, 7);
398         EXPECT_TRUE(EcmaStringAccessor(res).IsUtf16());
399         EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(res, *tmpString));
400     }
401     {
402         JSHandle<EcmaString> sourceString = factory->NewFromASCII("integer123");
403         JSHandle<EcmaString> tmpString = factory->NewFromASCII("integer");
404         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf8());
405         EcmaString *res = EcmaStringAccessor::FastSubString(ecmaVMPtr, sourceString, 0, 7);
406         EXPECT_TRUE(EcmaStringAccessor(res).IsUtf8());
407         EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(res, *tmpString));
408     }
409 }
410 
411 /*
412  * @tc.name: GetUtf8Length
413  * @tc.desc: Check whether the value returned through calling GetUtf8Length function is within expectations.
414  * @tc.type: FUNC
415  * @tc.require:
416  */
HWTEST_F_L0(EcmaStringAccessorTest,GetUtf8Length)417 HWTEST_F_L0(EcmaStringAccessorTest, GetUtf8Length)
418 {
419     uint8_t arrayU8[6] = {3, 7, 19, 54, 99};
420     uint16_t arrayU16Comp[] = {1, 12, 34, 56, 127};
421     uint16_t arrayU16NotComp[] = {19, 54, 256, 11100, 65535};
422     uint32_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
423     uint32_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]);
424     uint32_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]);
425     JSHandle<EcmaString> handleEcmaStrU8(thread,
426         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8[0], lengthEcmaStrU8, true));
427     JSHandle<EcmaString> handleEcmaStrU16Comp(thread,
428         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16Comp[0], lengthEcmaStrU16Comp, true));
429     JSHandle<EcmaString> handleEcmaStrU16NotComp(thread,
430         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, false));
431     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU8).GetUtf8Length(), lengthEcmaStrU8 + 1);
432     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16Comp).GetUtf8Length(), lengthEcmaStrU16Comp + 1);
433     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16NotComp).GetUtf8Length(), 2 * lengthEcmaStrU16NotComp + 1);
434 }
435 
436 /*
437  * @tc.name: ObjectSize
438  * @tc.desc: Check whether the value returned through calling ObjectSize function is within expectations.
439  * @tc.type: FUNC
440  * @tc.require:
441  */
HWTEST_F_L0(EcmaStringAccessorTest,ObjectSize)442 HWTEST_F_L0(EcmaStringAccessorTest, ObjectSize)
443 {
444     JSHandle<EcmaString> handleEcmaStrEmpty(thread, EcmaStringAccessor::CreateEmptyString(ecmaVMPtr));
445     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrEmpty).ObjectSize(), EcmaString::SIZE + 0);
446 
447     size_t lengthEcmaStrAllocComp = 5;
448     JSHandle<EcmaString> handleEcmaStrAllocComp(thread,
449         EcmaStringAccessor::CreateLineString(ecmaVMPtr, lengthEcmaStrAllocComp, true));
450     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocComp).ObjectSize(),
451         EcmaString::SIZE + sizeof(uint8_t) * lengthEcmaStrAllocComp);
452 
453     size_t lengthEcmaStrAllocNotComp = 5;
454     JSHandle<EcmaString> handleEcmaStrAllocNotComp(thread,
455         EcmaStringAccessor::CreateLineString(ecmaVMPtr, lengthEcmaStrAllocNotComp, false));
456     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocNotComp).ObjectSize(),
457         EcmaString::SIZE + sizeof(uint16_t) * lengthEcmaStrAllocNotComp);
458 
459     uint8_t arrayU8[] = {"abcde"};
460     size_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
461     JSHandle<EcmaString> handleEcmaStrU8(thread,
462         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8[0], lengthEcmaStrU8, true));
463     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU8).ObjectSize(), EcmaString::SIZE + sizeof(uint8_t) * lengthEcmaStrU8);
464 
465     // ObjectSize(). EcmaString made by CreateFromUtf16( , , , true).
466     uint16_t arrayU16Comp[] = {1, 23, 45, 67, 127};
467     size_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]);
468     JSHandle<EcmaString> handleEcmaStrU16Comp(thread,
469         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16Comp[0], lengthEcmaStrU16Comp, true));
470     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16Comp).ObjectSize(),
471         EcmaString::SIZE + sizeof(uint8_t) * lengthEcmaStrU16Comp);
472 
473     // ObjectSize(). EcmaString made by CreateFromUtf16( , , , false).
474     uint16_t arrayU16NotComp[] = {127, 128, 256, 11100, 65535};
475     size_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]);
476     JSHandle<EcmaString> handleEcmaStrU16NotComp(thread,
477         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, false));
478     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16NotComp).ObjectSize(),
479         EcmaString::SIZE + sizeof(uint16_t) * lengthEcmaStrU16NotComp);
480 }
481 
482 /*
483  * @tc.name: SetInternString
484  * @tc.desc: Call SetInternString function, check whether the bool returned through calling IsInternString function
485  * is within expectations.
486  * @tc.type: FUNC
487  * @tc.require:
488  */
HWTEST_F_L0(EcmaStringAccessorTest,SetInternString)489 HWTEST_F_L0(EcmaStringAccessorTest, SetInternString)
490 {
491     uint8_t arrayU8[] = {"abc"};
492     uint32_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
493     JSHandle<EcmaString> handleEcmaStrU8(thread,
494         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8[0], lengthEcmaStrU8, true));
495     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrU8).IsInternString());
496     EcmaStringAccessor(handleEcmaStrU8).SetInternString();
497     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrU8).IsInternString());
498 
499     uint16_t arrayU16Comp[] = {97, 98, 99};
500     uint32_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]);
501     JSHandle<EcmaString> handleEcmaStrU16Comp(thread,
502         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16Comp[0], lengthEcmaStrU16Comp, true));
503     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrU16Comp).IsInternString());
504     EcmaStringAccessor(handleEcmaStrU16Comp).SetInternString();
505     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrU16Comp).IsInternString());
506 
507     uint16_t arrayU16NotComp[] = {97, 98, 99};
508     uint32_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]);
509     JSHandle<EcmaString> handleEcmaStrU16NotComp(thread,
510         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, true));
511     EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrU16NotComp).IsInternString());
512     EcmaStringAccessor(handleEcmaStrU16NotComp).SetInternString();
513     EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrU16NotComp).IsInternString());
514 }
515 
516 /*
517  * @tc.name: GetDataUtf8
518  * @tc.desc: Check whether the pointer returned through calling GetDataUtf8 function is within expectations.
519  * @tc.type: FUNC
520  * @tc.require:
521  */
HWTEST_F_L0(EcmaStringAccessorTest,GetDataUtf8)522 HWTEST_F_L0(EcmaStringAccessorTest, GetDataUtf8)
523 {
524     // From EcmaString made by CreateFromUtf8().
525     uint8_t arrayU8[] = {"abcde"};
526     uint32_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
527     JSHandle<EcmaString> handleEcmaStrU8(thread,
528         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8[0], lengthEcmaStrU8, true));
529     for (uint32_t i = 0; i < lengthEcmaStrU8; i++) {
530         EXPECT_EQ(*(EcmaStringAccessor(handleEcmaStrU8).GetDataUtf8() + i), arrayU8[i]);
531     }
532 
533     // From EcmaString made by CreateFromUtf16( , , , true).
534     uint16_t arrayU16Comp[] = {3, 1, 34, 123, 127, 111, 42, 3, 20, 10};
535     uint32_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]);
536     JSHandle<EcmaString> handleEcmaStrU16Comp(thread,
537         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16Comp[0], lengthEcmaStrU16Comp, true));
538     for (uint32_t i = 0; i < sizeof(arrayU16Comp) / arrayU16Comp[0]; i++) {
539         EXPECT_EQ(*(EcmaStringAccessor(handleEcmaStrU16Comp).GetDataUtf8() + i), arrayU16Comp[i]);
540     }
541 }
542 
543 /*
544  * @tc.name: GetDataUtf16
545  * @tc.desc: Check whether the pointer returned through calling GetDataUtf16 function is within expectations.
546  * @tc.type: FUNC
547  * @tc.require:
548  */
HWTEST_F_L0(EcmaStringAccessorTest,GetDataUtf16)549 HWTEST_F_L0(EcmaStringAccessorTest, GetDataUtf16)
550 {
551     // From EcmaString made by CreateFromUtf16( , , , false).
552     uint16_t arrayU16NotComp[] = {67, 777, 1999, 1, 45, 66, 23456, 65535, 127, 333};
553     uint32_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]);
554     JSHandle<EcmaString> handleEcmaStrU16NotComp(thread,
555         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, false));
556     for (uint32_t i = 0; i < lengthEcmaStrU16NotComp; i++) {
557         EXPECT_EQ(*(EcmaStringAccessor(handleEcmaStrU16NotComp).GetDataUtf16() + i), arrayU16NotComp[i]);
558     }
559 }
560 
561 /*
562  * @tc.name: WriteToFlatUtf8
563  * @tc.desc: Check whether the returned value and the changed array through a source EcmaString's calling
564  * WriteToFlatUtf8 function are within expectations.
565  * @tc.type: FUNC
566  * @tc.require:
567  */
HWTEST_F_L0(EcmaStringAccessorTest,WriteToFlatUtf8)568 HWTEST_F_L0(EcmaStringAccessorTest, WriteToFlatUtf8)
569 {
570     // WriteToFlatUtf8(). From EcmaString made by CreateFromUtf8().
571     uint8_t arrayU8CopyFrom[6] = {1, 12, 34, 56, 127};
572     uint32_t lengthEcmaStrU8CopyFrom = sizeof(arrayU8CopyFrom) - 1;
573     JSHandle<EcmaString> handleEcmaStrU8CopyFrom(thread,
574         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8CopyFrom[0], lengthEcmaStrU8CopyFrom, true));
575     const size_t lengthArrayU8Target = 6;
576     uint8_t arrayU8CopyTo[lengthArrayU8Target];
577 
578     size_t lengthReturnU8 = EcmaStringAccessor(handleEcmaStrU8CopyFrom)
579         .WriteToFlatUtf8(&arrayU8CopyTo[0], lengthArrayU8Target);
580 
581     EXPECT_EQ(lengthReturnU8, lengthArrayU8Target);
582     for (uint32_t i = 0; i < lengthReturnU8 - 1; i++) {
583         EXPECT_EQ(arrayU8CopyTo[i], arrayU8CopyFrom[i]);
584     }
585     EXPECT_EQ(arrayU8CopyTo[lengthReturnU8 - 1], 0);
586 
587     // CopyDataUtf8(). From EcmaString made by CreateFromUtf16( , , , true).
588     uint16_t arrayU16CompCopyFrom[] = {1, 12, 34, 56, 127};
589     uint32_t lengthEcmaStrU16CompCopyFrom = sizeof(arrayU16CompCopyFrom) / sizeof(arrayU16CompCopyFrom[0]);
590     JSHandle<EcmaString> handleEcmaStrU16CompCopyFrom(thread,
591         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompCopyFrom[0], lengthEcmaStrU16CompCopyFrom, true));
592     const size_t lengthArrayU16Target = 6;
593     uint8_t arrayU8CompCopyTo[lengthArrayU16Target];
594 
595     size_t lengthReturnU16Comp = EcmaStringAccessor(handleEcmaStrU16CompCopyFrom)
596         .WriteToFlatUtf8(&arrayU8CompCopyTo[0], lengthArrayU16Target);
597 
598     EXPECT_EQ(lengthReturnU16Comp, lengthArrayU16Target);
599     for (uint32_t i = 0; i < lengthReturnU16Comp - 1; i++) {
600         EXPECT_EQ(arrayU8CompCopyTo[i], arrayU16CompCopyFrom[i]);
601     }
602     EXPECT_EQ(arrayU8CompCopyTo[lengthReturnU16Comp - 1], 0U);
603 }
604 
605 /*
606  * @tc.name: WriteToFlatUtf16
607  * @tc.desc: Check whether the returned value and the changed array through a source EcmaString's calling
608  * WriteToFlatUtf16 function are within expectations.
609  * @tc.type: FUNC
610  * @tc.require:
611  */
HWTEST_F_L0(EcmaStringAccessorTest,WriteToFlatUtf16)612 HWTEST_F_L0(EcmaStringAccessorTest, WriteToFlatUtf16)
613 {
614     // WriteToFlatUtf16(). From EcmaString made by CreateFromUtf16( , , , false).
615     uint16_t arrayU16NotCompCopyFrom[10] = {67, 777, 1999, 1, 45, 66, 23456, 65535, 127, 333};
616     uint32_t lengthEcmaStrU16NotCompCopyFrom = sizeof(arrayU16NotCompCopyFrom) / sizeof(arrayU16NotCompCopyFrom[0]);
617     JSHandle<EcmaString> handleEcmaStrU16NotCompCopyFrom(thread, EcmaStringAccessor::CreateFromUtf16(
618         ecmaVMPtr, &arrayU16NotCompCopyFrom[0], lengthEcmaStrU16NotCompCopyFrom, false));
619     const size_t lengthArrayU16Target = 13;
620     uint16_t arrayU16NotCompCopyTo[lengthArrayU16Target];
621     uint8_t defaultOneByteValueOfArrayU16NotCompCopyTo = 244;
622     int checkResUtf16 = memset_s(&arrayU16NotCompCopyTo[0], sizeof(uint16_t) * lengthArrayU16Target,
623         defaultOneByteValueOfArrayU16NotCompCopyTo, sizeof(uint16_t) * lengthArrayU16Target);
624     EXPECT_TRUE(checkResUtf16 == 0);
625 
626     size_t lengthReturnU16NotComp = EcmaStringAccessor(handleEcmaStrU16NotCompCopyFrom)
627         .WriteToFlatUtf16(&arrayU16NotCompCopyTo[0], lengthArrayU16Target);
628 
629     EXPECT_EQ(lengthReturnU16NotComp, lengthEcmaStrU16NotCompCopyFrom);
630     for (uint32_t i = 0; i < lengthReturnU16NotComp; i++) {
631         EXPECT_EQ(arrayU16NotCompCopyTo[i], EcmaStringAccessor(handleEcmaStrU16NotCompCopyFrom).Get(i));
632     }
633     for (uint32_t i = lengthReturnU16NotComp; i < lengthArrayU16Target; i++) {
634         EXPECT_EQ(arrayU16NotCompCopyTo[i], ((uint16_t)defaultOneByteValueOfArrayU16NotCompCopyTo) * (1 + (1 << 8)));
635     }
636 }
637 
638 /*
639  * @tc.name: ReadData_001
640  * @tc.desc: Check whether the target EcmaString made by CreateLineString( , true, ) changed through calling ReadData
641  * function with a source EcmaString made by CreateFromUtf8() is within expectations.
642  * @tc.type: FUNC
643  * @tc.require:
644  */
HWTEST_F_L0(EcmaStringAccessorTest,ReadData_001)645 HWTEST_F_L0(EcmaStringAccessorTest, ReadData_001)
646 {
647     // ReadData(). From EcmaString made by CreateFromUtf8() to EcmaString made by CreateLineString( , true, ).
648     uint8_t arrayU8WriteFrom[6] = {1, 12, 34, 56, 127};
649     uint32_t lengthEcmaStrU8WriteFrom = sizeof(arrayU8WriteFrom) - 1;
650     JSHandle<EcmaString> handleEcmaStrU8WriteFrom(thread,
651         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8WriteFrom[0], lengthEcmaStrU8WriteFrom, true));
652     size_t sizeEcmaStrU8WriteTo = 5;
653     JSHandle<EcmaString> handleEcmaStrAllocTrueWriteTo(thread,
654         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeEcmaStrU8WriteTo, true));
655     uint32_t indexStartWriteFromArrayU8 = 2;
656     uint32_t lengthWriteFromArrayU8 = 2;
657     EcmaStringAccessor::ReadData(*handleEcmaStrAllocTrueWriteTo, *handleEcmaStrU8WriteFrom, indexStartWriteFromArrayU8,
658         sizeEcmaStrU8WriteTo, lengthWriteFromArrayU8);
659     for (uint32_t i = 0; i < lengthWriteFromArrayU8; i++) {
660         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocTrueWriteTo).Get(i + indexStartWriteFromArrayU8),
661             EcmaStringAccessor(handleEcmaStrU8WriteFrom).Get(i));
662     }
663 }
664 
665 /*
666  * @tc.name: ReadData_002
667  * @tc.desc: Check whether the target EcmaString made by CreateLineString( , false, ) changed through calling
668  * ReadData function with a source EcmaString made by CreateFromUtf16( , , , false) is within expectations.
669  * @tc.type: FUNC
670  * @tc.require:
671  */
HWTEST_F_L0(EcmaStringAccessorTest,ReadData_002)672 HWTEST_F_L0(EcmaStringAccessorTest, ReadData_002)
673 {
674     /* ReadData(). From EcmaString made by CreateFromUtf16( , , , false) to EcmaStringU16 made by
675      * CreateLineString( , false, ).
676      */
677     uint16_t arrayU16WriteFrom[10] = {67, 777, 1999, 1, 45, 66, 23456, 65535, 127, 333};
678     uint32_t lengthEcmaStrU16WriteFrom = sizeof(arrayU16WriteFrom) / sizeof(arrayU16WriteFrom[0]);
679     JSHandle<EcmaString> handleEcmaStrU16WriteFrom(thread,
680         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16WriteFrom[0], lengthEcmaStrU16WriteFrom, false));
681     size_t sizeEcmaStrU16WriteTo = 10;
682     JSHandle<EcmaString> handleEcmaStrU16WriteTo(thread,
683         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeEcmaStrU16WriteTo, false));
684     uint32_t indexStartWriteFromArrayU16 = 3;
685     uint32_t numBytesWriteFromArrayU16 = 2 * 3;
686     EcmaStringAccessor::ReadData(*handleEcmaStrU16WriteTo, *handleEcmaStrU16WriteFrom, indexStartWriteFromArrayU16,
687         sizeEcmaStrU16WriteTo, numBytesWriteFromArrayU16);
688     for (uint32_t i = 0; i < (numBytesWriteFromArrayU16 / 2); i++) {
689         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16WriteTo).Get(i + indexStartWriteFromArrayU16),
690             EcmaStringAccessor(handleEcmaStrU16WriteFrom).Get(i));
691     }
692 }
693 
694 /*
695  * @tc.name: ReadData_003
696  * @tc.desc: Check whether the target EcmaString made by CreateLineString( , false, ) changed through calling
697  * ReadData function with a source EcmaString made by CreateFromUtf8() is within expectations.
698  * @tc.type: FUNC
699  * @tc.require:
700  */
HWTEST_F_L0(EcmaStringAccessorTest,ReadData_003)701 HWTEST_F_L0(EcmaStringAccessorTest, ReadData_003)
702 {
703     // ReadData(). From EcmaString made by CreateFromUtf8() to EcmaString made by CreateLineString( , false, ).
704     uint8_t arrayU8WriteFrom[6] = {1, 12, 34, 56, 127};
705     uint32_t lengthEcmaStrU8WriteFrom = sizeof(arrayU8WriteFrom) - 1;
706     JSHandle<EcmaString> handleEcmaStrU8WriteFrom(thread,
707         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8WriteFrom[0], lengthEcmaStrU8WriteFrom, true));
708     size_t sizeEcmaStrU16WriteTo = 10;
709     JSHandle<EcmaString> handleEcmaStrU16WriteTo(thread,
710         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeEcmaStrU16WriteTo, false));
711     uint32_t indexStartWriteFromU8ToU16 = 1;
712     uint32_t numBytesWriteFromU8ToU16 = 4;
713     EcmaStringAccessor::ReadData(*handleEcmaStrU16WriteTo, *handleEcmaStrU8WriteFrom,
714         indexStartWriteFromU8ToU16, sizeEcmaStrU16WriteTo,
715         numBytesWriteFromU8ToU16);
716     for (uint32_t i = 0; i < numBytesWriteFromU8ToU16; i++) {
717         EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16WriteTo).Get(i + indexStartWriteFromU8ToU16),
718             EcmaStringAccessor(handleEcmaStrU8WriteFrom).Get(i));
719     }
720 }
721 
722 /*
723  * @tc.name: Set_001
724  * @tc.desc: Check whether the target EcmaString made by CreateLineString( , true, ) changed through calling Set
725  * function from a source char is within expectations.
726  * @tc.type: FUNC
727  * @tc.require:
728  */
HWTEST_F_L0(EcmaStringAccessorTest,Set_001)729 HWTEST_F_L0(EcmaStringAccessorTest, Set_001)
730 {
731     // Set(). From char to EcmaString made by CreateLineString( , true, ).
732     char u8Write = 'a';
733     size_t sizeEcmaStrU8WriteTo = 5;
734     JSHandle<EcmaString> handleEcmaStrAllocTrueWriteTo(thread,
735         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeEcmaStrU8WriteTo, true));
736     uint32_t indexAtWriteFromU8 = 4;
737     EcmaStringAccessor(handleEcmaStrAllocTrueWriteTo).Set(indexAtWriteFromU8, u8Write);
738     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocTrueWriteTo).Get(indexAtWriteFromU8), u8Write);
739 }
740 
741 /*
742  * @tc.name: Set_002
743  * @tc.desc: Check whether the target EcmaString made by CreateLineString( , false, ) changed through calling
744  * Set function with a source char is within expectations.
745  * @tc.type: FUNC
746  * @tc.require:
747  */
HWTEST_F_L0(EcmaStringAccessorTest,Set_002)748 HWTEST_F_L0(EcmaStringAccessorTest, Set_002)
749 {
750     // Set(). From char to EcmaString made by CreateLineString( , false, ).
751     size_t sizeEcmaStrU16WriteTo = 10;
752     JSHandle<EcmaString> handleEcmaStrU16WriteTo(thread,
753         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeEcmaStrU16WriteTo, false));
754     char u8Write = 'a';
755     uint32_t indexAt = 4;
756     EcmaStringAccessor(handleEcmaStrU16WriteTo).Set(indexAt, u8Write);
757     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16WriteTo).Get(indexAt), u8Write);
758 }
759 
760 /*
761  * @tc.name: GetHashcode_001
762  * @tc.desc: Check whether the value returned through an EcmaString made by CreateFromUtf8() calling GetHashcode
763  * function is within expectations.
764  * @tc.type: FUNC
765  * @tc.require:
766  */
HWTEST_F_L0(EcmaStringAccessorTest,GetHashcode_001)767 HWTEST_F_L0(EcmaStringAccessorTest, GetHashcode_001)
768 {
769     // GetHashcode(). EcmaString made by CreateFromUtf8().
770     uint8_t arrayU8[] = {"abc"};
771     uint32_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
772     JSHandle<EcmaString> handleEcmaStrU8(thread,
773         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8[0], lengthEcmaStrU8, true));
774     uint32_t hashExpect = 0;
775     for (uint32_t i = 0; i < lengthEcmaStrU8; i++) {
776         hashExpect = hashExpect * 31 + arrayU8[i];
777     }
778     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU8).GetHashcode(), hashExpect);
779 }
780 
781 /*
782  * @tc.name: GetHashcode_002
783  * @tc.desc: Check whether the value returned through an EcmaString made by CreateFromUtf16( , , , true) calling
784  * GetHashcode function is within expectations.
785  * @tc.type: FUNC
786  * @tc.require:
787  */
HWTEST_F_L0(EcmaStringAccessorTest,GetHashcode_002)788 HWTEST_F_L0(EcmaStringAccessorTest, GetHashcode_002)
789 {
790     // GetHashcode(). EcmaString made by CreateFromUtf16( , , , true).
791     uint16_t arrayU16Comp[] = {45, 92, 78, 24};
792     uint32_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]);
793     JSHandle<EcmaString> handleEcmaStrU16Comp(thread,
794         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16Comp[0], lengthEcmaStrU16Comp, true));
795     uint32_t hashExpect = 0;
796     for (uint32_t i = 0; i < lengthEcmaStrU16Comp; i++) {
797         hashExpect = hashExpect * 31 + arrayU16Comp[i];
798     }
799     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16Comp).GetHashcode(), hashExpect);
800 }
801 
802 /*
803  * @tc.name: GetHashcode_003
804  * @tc.desc: Check whether the value returned through an EcmaString made by CreateFromUtf16( , , , false) calling
805  * GetHashcode function is within expectations.
806  * @tc.type: FUNC
807  * @tc.require:
808  */
HWTEST_F_L0(EcmaStringAccessorTest,GetHashcode_003)809 HWTEST_F_L0(EcmaStringAccessorTest, GetHashcode_003)
810 {
811     // GetHashcode(). EcmaString made by CreateFromUtf16( , , , false).
812     uint16_t arrayU16NotComp[] = {199, 1, 256, 65535, 777};
813     uint32_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]);
814     JSHandle<EcmaString> handleEcmaStrU16NotComp(thread,
815         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, false));
816     uint32_t hashExpect = 0;
817     for (uint32_t i = 0; i < lengthEcmaStrU16NotComp; i++) {
818         hashExpect = hashExpect * 31 + arrayU16NotComp[i];
819     }
820     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrU16NotComp).GetHashcode(), hashExpect);
821 }
822 
823 /*
824  * @tc.name: GetHashcode_004
825  * @tc.desc: Check whether the value returned through an EcmaString made by CreateEmptyString() calling GetHashcode
826  * function is within expectations.
827  * @tc.type: FUNC
828  * @tc.require:
829  */
HWTEST_F_L0(EcmaStringAccessorTest,GetHashcode_004)830 HWTEST_F_L0(EcmaStringAccessorTest, GetHashcode_004)
831 {
832     // GetHashcode(). EcmaString made by CreateEmptyString().
833     JSHandle<EcmaString> handleEcmaStrEmpty(thread, EcmaStringAccessor::CreateEmptyString(ecmaVMPtr));
834     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrEmpty).GetHashcode(), 0U);
835 }
836 
837 /*
838  * @tc.name: GetHashcode_005
839  * @tc.desc: Check whether the value returned through an EcmaString made by CreateLineString(, true/false, ) calling
840  * GetHashcode function is within expectations.
841  * @tc.type: FUNC
842  * @tc.require:
843  */
HWTEST_F_L0(EcmaStringAccessorTest,GetHashcode_005)844 HWTEST_F_L0(EcmaStringAccessorTest, GetHashcode_005)
845 {
846     // GetHashcode(). EcmaString made by CreateLineString().
847     size_t sizeAlloc = 5;
848     JSHandle<EcmaString> handleEcmaStrAllocComp(thread,
849         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeAlloc, true));
850     JSHandle<EcmaString> handleEcmaStrAllocNotComp(thread,
851         EcmaStringAccessor::CreateLineString(ecmaVMPtr, sizeAlloc, false));
852     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocComp).GetHashcode(), 0U);
853     EXPECT_EQ(EcmaStringAccessor(handleEcmaStrAllocNotComp).GetHashcode(), 0U);
854 }
855 
856 /*
857  * @tc.name: ComputeHashcodeUtf8
858  * @tc.desc: Check whether the value returned through calling ComputeHashcodeUtf8 function with an Array(uint8_t) is
859  * within expectations.
860  * @tc.type: FUNC
861  * @tc.require:
862  */
HWTEST_F_L0(EcmaStringAccessorTest,ComputeHashcodeUtf8)863 HWTEST_F_L0(EcmaStringAccessorTest, ComputeHashcodeUtf8)
864 {
865     uint8_t arrayU8[] = {"abc"};
866     uint32_t lengthEcmaStrU8 = sizeof(arrayU8) - 1;
867     uint32_t hashExpect = 0;
868     for (uint32_t i = 0; i < lengthEcmaStrU8; i++) {
869         hashExpect = hashExpect * 31 + arrayU8[i];
870     }
871     EXPECT_EQ(EcmaStringAccessor::ComputeHashcodeUtf8(&arrayU8[0], lengthEcmaStrU8, true), hashExpect);
872 }
873 
874 /*
875  * @tc.name: ComputeHashcodeUtf16
876  * @tc.desc: Check whether the value returned through calling ComputeHashcodeUtf16 function with an Array(uint16_t) is
877  * within expectations.
878  * @tc.type: FUNC
879  * @tc.require:
880  */
HWTEST_F_L0(EcmaStringAccessorTest,ComputeHashcodeUtf16)881 HWTEST_F_L0(EcmaStringAccessorTest, ComputeHashcodeUtf16)
882 {
883     uint16_t arrayU16[] = {199, 1, 256, 65535, 777};
884     uint32_t lengthEcmaStrU16 = sizeof(arrayU16) / sizeof(arrayU16[0]);
885     uint32_t hashExpect = 0;
886     for (uint32_t i = 0; i < lengthEcmaStrU16; i++) {
887         hashExpect = hashExpect * 31 + arrayU16[i];
888     }
889     EXPECT_EQ(EcmaStringAccessor::ComputeHashcodeUtf16(&arrayU16[0], lengthEcmaStrU16), hashExpect);
890 }
891 
892 /*
893  * @tc.name: IndexOf_001
894  * @tc.desc: Check whether the value returned through a source EcmaString made by CreateFromUtf8() calling IndexOf
895  * function with a target EcmaString made by CreateFromUtf8() is within expectations.
896  * @tc.type: FUNC
897  * @tc.require:
898  */
HWTEST_F_L0(EcmaStringAccessorTest,IndexOf_001)899 HWTEST_F_L0(EcmaStringAccessorTest, IndexOf_001)
900 {
901     // IndexOf(). Find EcmaString made by CreateFromUtf8() From EcmaString made by CreateFromUtf8().
902     uint8_t arrayU8From[7] = {23, 25, 1, 3, 39, 80};
903     uint8_t arrayU8Target[4] = {1, 3, 39};
904     uint32_t lengthEcmaStrU8From = sizeof(arrayU8From) - 1;
905     uint32_t lengthEcmaStrU8Target = sizeof(arrayU8Target) - 1;
906     JSHandle<EcmaString> handleEcmaStr(thread,
907         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8From[0], lengthEcmaStrU8From, true));
908     JSHandle<EcmaString> handleEcmaStr1(thread,
909         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8Target[0], lengthEcmaStrU8Target, true));
910     int32_t posStart = 0;
911     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr, handleEcmaStr1, posStart), 2);
912     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr1, handleEcmaStr, posStart), -1);
913     posStart = -1;
914     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr, handleEcmaStr1, posStart), 2);
915     posStart = 1;
916     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr, handleEcmaStr1, posStart), 2);
917     posStart = 2;
918     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr, handleEcmaStr1, posStart), 2);
919     posStart = 3;
920     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr, handleEcmaStr1, posStart), -1);
921 }
922 
923 /*
924  * @tc.name: IndexOf_002
925  * @tc.desc: Check whether the value returned through a source EcmaString made by CreateFromUtf16( , , , false) calling
926  * IndexOf function with a target EcmaString made by CreateFromUtf8() is within expectations.
927  * @tc.type: FUNC
928  * @tc.require:
929  */
HWTEST_F_L0(EcmaStringAccessorTest,IndexOf_002)930 HWTEST_F_L0(EcmaStringAccessorTest, IndexOf_002)
931 {
932     // IndexOf(). Find EcmaString made by CreateFromUtf8() From EcmaString made by CreateFromUtf16( , , , false).
933     uint8_t arrayU8Target[4] = {1, 3, 39};
934     uint16_t arrayU16NotCompFromNo1[] = {67, 65535, 127, 777, 1453, 44, 1, 3, 39, 80, 333};
935     uint32_t lengthEcmaStrU8Target = sizeof(arrayU8Target) - 1;
936     uint32_t lengthEcmaStrU16NotCompFromNo1 = sizeof(arrayU16NotCompFromNo1) / sizeof(arrayU16NotCompFromNo1[0]);
937     JSHandle<EcmaString> handleEcmaStr(thread,
938         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8Target[0], lengthEcmaStrU8Target, true));
939     JSHandle<EcmaString> handleEcmaStr1(thread, EcmaStringAccessor::CreateFromUtf16(
940         ecmaVMPtr, &arrayU16NotCompFromNo1[0], lengthEcmaStrU16NotCompFromNo1, false));
941     int32_t posStart = 0;
942     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr1, handleEcmaStr, posStart), 6);
943     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr, handleEcmaStr1, posStart), -1);
944     posStart = -1;
945     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr1, handleEcmaStr, posStart), 6);
946     posStart = 1;
947     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr1, handleEcmaStr, posStart), 6);
948     posStart = 6;
949     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr1, handleEcmaStr, posStart), 6);
950     posStart = 7;
951     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr, handleEcmaStr1, handleEcmaStr, posStart), -1);
952 }
953 
954 /*
955  * @tc.name: IndexOf_003
956  * @tc.desc: Check whether the value returned through a source EcmaString made by CreateFromUtf16( , , , false) calling
957  * IndexOf function with a target EcmaString made by CreateFromUtf16( , , , false) is within expectations.
958  * @tc.type: FUNC
959  * @tc.require:
960  */
HWTEST_F_L0(EcmaStringAccessorTest,IndexOf_003)961 HWTEST_F_L0(EcmaStringAccessorTest, IndexOf_003)
962 {
963     /* IndexOf(). Find EcmaString made by CreateFromUtf16( , , , false) From EcmaString made by
964      * CreateFromUtf16( , , , false).
965      */
966     uint16_t arrayU16NotCompTarget[] = {1453, 44};
967     uint16_t arrayU16NotCompFrom[] = {67, 65535, 127, 777, 1453, 44, 1, 3, 39, 80, 333};
968     uint32_t lengthEcmaStrU16NotCompTarget = sizeof(arrayU16NotCompTarget) / sizeof(arrayU16NotCompTarget[0]);
969     uint32_t lengthEcmaStrU16NotCompFrom = sizeof(arrayU16NotCompFrom) / sizeof(arrayU16NotCompFrom[0]);
970     JSHandle<EcmaString> handleEcmaStrU16NotCompTarget(thread, EcmaStringAccessor::CreateFromUtf16(
971         ecmaVMPtr, &arrayU16NotCompTarget[0], lengthEcmaStrU16NotCompTarget, false));
972     JSHandle<EcmaString> handleEcmaStrU16NotCompFrom(thread,
973         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompFrom[0], lengthEcmaStrU16NotCompFrom, false));
974     int32_t posStart = 0;
975     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
976         handleEcmaStrU16NotCompFrom, handleEcmaStrU16NotCompTarget, posStart), 4);
977     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
978         handleEcmaStrU16NotCompTarget, handleEcmaStrU16NotCompFrom, posStart), -1);
979     posStart = -1;
980     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
981         handleEcmaStrU16NotCompFrom, handleEcmaStrU16NotCompTarget, posStart), 4);
982     posStart = 1;
983     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
984         handleEcmaStrU16NotCompFrom, handleEcmaStrU16NotCompTarget, posStart), 4);
985     posStart = 4;
986     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
987         handleEcmaStrU16NotCompFrom, handleEcmaStrU16NotCompTarget, posStart), 4);
988     posStart = 5;
989     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
990         handleEcmaStrU16NotCompFrom, handleEcmaStrU16NotCompTarget, posStart), -1);
991 }
992 
993 /*
994  * @tc.name: IndexOf_004
995  * @tc.desc: Check whether the value returned through a source EcmaString made by CreateFromUtf8() calling IndexOf
996  * function with a target EcmaString made by CreateFromUtf16() is within expectations.
997  * @tc.type: FUNC
998  * @tc.require:
999  */
HWTEST_F_L0(EcmaStringAccessorTest,IndexOf_004)1000 HWTEST_F_L0(EcmaStringAccessorTest, IndexOf_004)
1001 {
1002     // IndexOf(). Find EcmaString made by CreateFromUtf16() From EcmaString made by CreateFromUtf8().
1003     uint16_t ecmaStrU16NotCompTarget[] = {3, 39, 80};
1004     uint8_t arrayU8From[7] = {23, 25, 1, 3, 39, 80};
1005     uint32_t lengthEcmaStrU16NotCompTarget = sizeof(ecmaStrU16NotCompTarget) / sizeof(ecmaStrU16NotCompTarget[0]);
1006     uint32_t lengthEcmaStrU8From = sizeof(arrayU8From) - 1;
1007     JSHandle<EcmaString> handleEcmaStrU16NotCompTarget(thread, EcmaStringAccessor::CreateFromUtf16(
1008         ecmaVMPtr, &ecmaStrU16NotCompTarget[0], lengthEcmaStrU16NotCompTarget, true));
1009     JSHandle<EcmaString> handleEcmaStrU8From(thread,
1010         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8From[0], lengthEcmaStrU8From, true));
1011     int32_t posStart = 0;
1012     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
1013         handleEcmaStrU8From, handleEcmaStrU16NotCompTarget, posStart), 3);
1014     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
1015         handleEcmaStrU16NotCompTarget, handleEcmaStrU8From, posStart), -1);
1016     posStart = -1;
1017     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
1018         handleEcmaStrU8From, handleEcmaStrU16NotCompTarget, posStart), 3);
1019     posStart = 1;
1020     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
1021         handleEcmaStrU8From, handleEcmaStrU16NotCompTarget, posStart), 3);
1022     posStart = 3;
1023     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
1024         handleEcmaStrU8From, handleEcmaStrU16NotCompTarget, posStart), 3);
1025     posStart = 4;
1026     EXPECT_EQ(EcmaStringAccessor::IndexOf(ecmaVMPtr,
1027         handleEcmaStrU8From, handleEcmaStrU16NotCompTarget, posStart), -1);
1028 }
1029 
1030 /*
1031  * @tc.name: Compare_001
1032  * @tc.desc: Check whether the value returned through calling Compare function between EcmaStrings made by
1033  * CreateFromUtf8() is within expectations.
1034  * @tc.type: FUNC
1035  * @tc.require:
1036  */
HWTEST_F_L0(EcmaStringAccessorTest,Compare_001)1037 HWTEST_F_L0(EcmaStringAccessorTest, Compare_001)
1038 {
1039     // Compare(). Between EcmaStrings made by CreateFromUtf8().
1040     uint8_t arrayU8No1[3] = {1, 23};
1041     uint8_t arrayU8No2[4] = {1, 23, 49};
1042     uint8_t arrayU8No3[6] = {1, 23, 45, 97, 127};
1043     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1044     uint32_t lengthEcmaStrU8No2 = sizeof(arrayU8No2) - 1;
1045     uint32_t lengthEcmaStrU8No3 = sizeof(arrayU8No3) - 1;
1046     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1047         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1048     JSHandle<EcmaString> handleEcmaStrU8No2(thread,
1049         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No2[0], lengthEcmaStrU8No2, true));
1050     JSHandle<EcmaString> handleEcmaStrU8No3(thread,
1051         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No3[0], lengthEcmaStrU8No3, true));
1052     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No1, handleEcmaStrU8No2), -1);
1053     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No2, handleEcmaStrU8No1), 1);
1054     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No2, handleEcmaStrU8No3), 49 - 45);
1055     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No3, handleEcmaStrU8No2), 45 - 49);
1056 }
1057 
1058 /*
1059  * @tc.name: Compare_002
1060  * @tc.desc: Check whether the value returned through calling Compare function between EcmaStrings made by
1061  * CreateFromUtf16( , , , true) is within expectations.
1062  * @tc.type: FUNC
1063  * @tc.require:
1064  */
HWTEST_F_L0(EcmaStringAccessorTest,Compare_002)1065 HWTEST_F_L0(EcmaStringAccessorTest, Compare_002)
1066 {
1067     // Compare(). Between EcmaStrings made by CreateFromUtf16( , , , true).
1068     uint16_t arrayU16CompNo1[] = {1, 23};
1069     uint16_t arrayU16CompNo2[] = {1, 23, 49};
1070     uint16_t arrayU16CompNo3[] = {1, 23, 45, 97, 127};
1071     uint32_t lengthEcmaStrU16CompNo1 = sizeof(arrayU16CompNo1) / sizeof(arrayU16CompNo1[0]);
1072     uint32_t lengthEcmaStrU16CompNo2 = sizeof(arrayU16CompNo2) / sizeof(arrayU16CompNo2[0]);
1073     uint32_t lengthEcmaStrU16CompNo3 = sizeof(arrayU16CompNo3) / sizeof(arrayU16CompNo3[0]);
1074     JSHandle<EcmaString> handleEcmaStrU16CompNo1(thread,
1075         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1, true));
1076     JSHandle<EcmaString> handleEcmaStrU16CompNo2(thread,
1077         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo2[0], lengthEcmaStrU16CompNo2, true));
1078     JSHandle<EcmaString> handleEcmaStrU16CompNo3(thread,
1079         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo3[0], lengthEcmaStrU16CompNo3, true));
1080     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo1, handleEcmaStrU16CompNo2), -1);
1081     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo2, handleEcmaStrU16CompNo1), 1);
1082     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo2, handleEcmaStrU16CompNo3), 49 - 45);
1083     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo3, handleEcmaStrU16CompNo2), 45 - 49);
1084 }
1085 
1086 /*
1087  * @tc.name: Compare_003
1088  * @tc.desc: Check whether the value returned through calling Compare function between EcmaString made by
1089  * CreateFromUtf8() and EcmaString made by CreateFromUtf16( , , , true) made by CreateFromUtf16( , , , true) is within
1090  * expectations.
1091  * @tc.type: FUNC
1092  * @tc.require:
1093  */
HWTEST_F_L0(EcmaStringAccessorTest,Compare_003)1094 HWTEST_F_L0(EcmaStringAccessorTest, Compare_003)
1095 {
1096     // Compare(). EcmaString made by CreateFromUtf8() and EcmaString made by CreateFromUtf16( , , , true).
1097     uint8_t arrayU8No1[3] = {1, 23};
1098     uint8_t arrayU8No2[4] = {1, 23, 49};
1099     uint16_t arrayU16CompNo1[] = {1, 23};
1100     uint16_t arrayU16CompNo2[] = {1, 23, 49};
1101     uint16_t arrayU16CompNo3[] = {1, 23, 45, 97, 127};
1102     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1103     uint32_t lengthEcmaStrU8No2 = sizeof(arrayU8No2) - 1;
1104     uint32_t lengthEcmaStrU16CompNo1 = sizeof(arrayU16CompNo1) / sizeof(arrayU16CompNo1[0]);
1105     uint32_t lengthEcmaStrU16CompNo2 = sizeof(arrayU16CompNo2) / sizeof(arrayU16CompNo2[0]);
1106     uint32_t lengthEcmaStrU16CompNo3 = sizeof(arrayU16CompNo3) / sizeof(arrayU16CompNo3[0]);
1107     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1108         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1109     JSHandle<EcmaString> handleEcmaStrU8No2(thread,
1110         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No2[0], lengthEcmaStrU8No2, true));
1111     JSHandle<EcmaString> handleEcmaStrU16CompNo1(thread,
1112         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1, true));
1113     JSHandle<EcmaString> handleEcmaStrU16CompNo2(thread,
1114         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo2[0], lengthEcmaStrU16CompNo2, true));
1115     JSHandle<EcmaString> handleEcmaStrU16CompNo3(thread,
1116         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo3[0], lengthEcmaStrU16CompNo3, true));
1117     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No1, handleEcmaStrU16CompNo1), 0);
1118     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo1, handleEcmaStrU8No1), 0);
1119     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No1, handleEcmaStrU16CompNo2), -1);
1120     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo2, handleEcmaStrU8No1), 1);
1121     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No2, handleEcmaStrU16CompNo3), 49 - 45);
1122     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo3, handleEcmaStrU8No2), 45 - 49);
1123 }
1124 
1125 /*
1126  * @tc.name: Compare_004
1127  * @tc.desc: Check whether the value returned through calling Compare function between EcmaStrings made by
1128  * CreateFromUtf16( , , , false) is within expectations.
1129  * @tc.type: FUNC
1130  * @tc.require:
1131  */
HWTEST_F_L0(EcmaStringAccessorTest,Compare_004)1132 HWTEST_F_L0(EcmaStringAccessorTest, Compare_004)
1133 {
1134     // Compare(). Between EcmaStrings made by CreateFromUtf16( , , , false).
1135     uint16_t arrayU16NotCompNo1[] = {1, 23};
1136     uint16_t arrayU16NotCompNo2[] = {1, 23, 49};
1137     uint16_t arrayU16NotCompNo3[] = {1, 23, 456, 6789, 65535, 127};
1138     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1139     uint32_t lengthEcmaStrU16NotCompNo2 = sizeof(arrayU16NotCompNo2) / sizeof(arrayU16NotCompNo2[0]);
1140     uint32_t lengthEcmaStrU16NotCompNo3 = sizeof(arrayU16NotCompNo3) / sizeof(arrayU16NotCompNo3[0]);
1141     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1142         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, true));
1143     JSHandle<EcmaString> handleEcmaStrU16NotCompNo2(thread,
1144         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo2[0], lengthEcmaStrU16NotCompNo2, true));
1145     JSHandle<EcmaString> handleEcmaStrU16NotCompNo3(thread,
1146         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo3[0], lengthEcmaStrU16NotCompNo3, false));
1147     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo1, handleEcmaStrU16NotCompNo2), -1);
1148     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo2, handleEcmaStrU16NotCompNo1), 1);
1149     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo2, handleEcmaStrU16NotCompNo3), 49 - 456);
1150     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo3, handleEcmaStrU16NotCompNo2), 456 - 49);
1151 }
1152 
1153 /*
1154  * @tc.name: Compare_005
1155  * @tc.desc: Check whether the value returned through calling Compare function between EcmaString made by
1156  * CreateFromUtf8() and EcmaString made by CreateFromUtf16( , , , false) is within expectations.
1157  * @tc.type: FUNC
1158  * @tc.require:
1159  */
HWTEST_F_L0(EcmaStringAccessorTest,Compare_005)1160 HWTEST_F_L0(EcmaStringAccessorTest, Compare_005)
1161 {
1162     // Compare(). EcmaString made by CreateFromUtf8() and EcmaString made by CreateFromUtf16( , , , false).
1163     uint8_t arrayU8No1[3] = {1, 23};
1164     uint8_t arrayU8No2[4] = {1, 23, 49};
1165     uint16_t arrayU16NotCompNo1[] = {1, 23};
1166     uint16_t arrayU16NotCompNo2[] = {1, 23, 49};
1167     uint16_t arrayU16NotCompNo3[] = {1, 23, 456, 6789, 65535, 127};
1168     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1169     uint32_t lengthEcmaStrU8No2 = sizeof(arrayU8No2) - 1;
1170     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1171     uint32_t lengthEcmaStrU16NotCompNo2 = sizeof(arrayU16NotCompNo2) / sizeof(arrayU16NotCompNo2[0]);
1172     uint32_t lengthEcmaStrU16NotCompNo3 = sizeof(arrayU16NotCompNo3) / sizeof(arrayU16NotCompNo3[0]);
1173     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1174         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1175     JSHandle<EcmaString> handleEcmaStrU8No2(thread,
1176         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No2[0], lengthEcmaStrU8No2, true));
1177     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1178         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, true));
1179     JSHandle<EcmaString> handleEcmaStrU16NotCompNo2(thread,
1180         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo2[0], lengthEcmaStrU16NotCompNo2, true));
1181     JSHandle<EcmaString> handleEcmaStrU16NotCompNo3(thread,
1182         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo3[0], lengthEcmaStrU16NotCompNo3, false));
1183     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No1, handleEcmaStrU16NotCompNo1), 0);
1184     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo1, handleEcmaStrU8No1), 0);
1185     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No1, handleEcmaStrU16NotCompNo2), -1);
1186     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo2, handleEcmaStrU8No1), 1);
1187     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU8No2, handleEcmaStrU16NotCompNo3), 49 - 456);
1188     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo3, handleEcmaStrU8No2), 456 - 49);
1189 }
1190 
1191 /*
1192  * @tc.name: Compare_006
1193  * @tc.desc: Check whether the value returned through calling Compare function between EcmaString made by
1194  * CreateFromUtf16( , , , true) and EcmaString made by CreateFromUtf16( , , , false) is within expectations.
1195  * @tc.type: FUNC
1196  * @tc.require:
1197  */
HWTEST_F_L0(EcmaStringAccessorTest,Compare_006)1198 HWTEST_F_L0(EcmaStringAccessorTest, Compare_006)
1199 {
1200     // Compare(). EcmaString made by CreateFromUtf16( , , , true) and EcmaString made by CreateFromUtf16( , , , false).
1201     uint16_t arrayU16CompNo1[] = {1, 23};
1202     uint16_t arrayU16CompNo2[] = {1, 23, 49};
1203     uint16_t arrayU16NotCompNo1[] = {1, 23};
1204     uint16_t arrayU16NotCompNo2[] = {1, 23, 49};
1205     uint16_t arrayU16NotCompNo3[] = {1, 23, 456, 6789, 65535, 127};
1206     uint32_t lengthEcmaStrU16CompNo1 = sizeof(arrayU16CompNo1) / sizeof(arrayU16CompNo1[0]);
1207     uint32_t lengthEcmaStrU16CompNo2 = sizeof(arrayU16CompNo2) / sizeof(arrayU16CompNo2[0]);
1208     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1209     uint32_t lengthEcmaStrU16NotCompNo2 = sizeof(arrayU16NotCompNo2) / sizeof(arrayU16NotCompNo2[0]);
1210     uint32_t lengthEcmaStrU16NotCompNo3 = sizeof(arrayU16NotCompNo3) / sizeof(arrayU16NotCompNo3[0]);
1211     JSHandle<EcmaString> handleEcmaStrU16CompNo1(thread,
1212         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1, true));
1213     JSHandle<EcmaString> handleEcmaStrU16CompNo2(thread,
1214         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo2[0], lengthEcmaStrU16CompNo2, true));
1215     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1216         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, true));
1217     JSHandle<EcmaString> handleEcmaStrU16NotCompNo2(thread,
1218         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo2[0], lengthEcmaStrU16NotCompNo2, true));
1219     JSHandle<EcmaString> handleEcmaStrU16NotCompNo3(thread,
1220         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo3[0], lengthEcmaStrU16NotCompNo3, false));
1221     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo1, handleEcmaStrU16NotCompNo1), 0);
1222     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo1, handleEcmaStrU16CompNo1), 0);
1223     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo1, handleEcmaStrU16NotCompNo2), -1);
1224     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo2, handleEcmaStrU16CompNo1), 1);
1225     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16CompNo2, handleEcmaStrU16NotCompNo3), 49 - 456);
1226     EXPECT_EQ(EcmaStringAccessor::Compare(ecmaVMPtr, handleEcmaStrU16NotCompNo3, handleEcmaStrU16CompNo2), 456 - 49);
1227 }
1228 
1229 /*
1230  * @tc.name: StringsAreEqual_001
1231  * @tc.desc: Check whether the bool returned through calling StringsAreEqual function with two EcmaStrings made by
1232  * CreateFromUtf8() is within expectations.
1233  * @tc.type: FUNC
1234  * @tc.require:
1235  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqual_001)1236 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqual_001)
1237 {
1238     // StringsAreEqual().
1239     uint8_t arrayU8No1[4] = {45, 92, 78};
1240     uint8_t arrayU8No2[4] = {45, 92, 78};
1241     uint8_t arrayU8No3[5] = {45, 92, 78, 1};
1242     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1243     uint32_t lengthEcmaStrU8No2 = sizeof(arrayU8No2) - 1;
1244     uint32_t lengthEcmaStrU8No3 = sizeof(arrayU8No3) - 1;
1245     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1246         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1247     JSHandle<EcmaString> handleEcmaStrU8No2(thread,
1248         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No2[0], lengthEcmaStrU8No2, true));
1249     JSHandle<EcmaString> handleEcmaStrU8No3(thread,
1250         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No3[0], lengthEcmaStrU8No3, true));
1251     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU8No1, *handleEcmaStrU8No2));
1252     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU8No1, *handleEcmaStrU8No3));
1253     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU8No3, *handleEcmaStrU8No1));
1254 }
1255 
1256 /*
1257  * @tc.name: StringsAreEqual_002
1258  * @tc.desc: Check whether the bool returned through calling StringsAreEqual function with a EcmaString made by
1259  * CreateFromUtf8() and a EcmaString made by CreateFromUtf16(, , , true) is within expectations.
1260  * @tc.type: FUNC
1261  * @tc.require:
1262  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqual_002)1263 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqual_002)
1264 {
1265     // StringsAreEqual().
1266     uint8_t arrayU8No1[4] = {45, 92, 78};
1267     uint16_t arrayU16CompNo2[] = {45, 92, 78};
1268     uint16_t arrayU16CompNo3[] = {45, 92, 78, 1};
1269     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1270     uint32_t lengthEcmaStrU16CompNo2 = sizeof(arrayU16CompNo2) / sizeof(arrayU16CompNo2[0]);
1271     uint32_t lengthEcmaStrU16CompNo3 = sizeof(arrayU16CompNo3) / sizeof(arrayU16CompNo3[0]);
1272     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1273         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1274     JSHandle<EcmaString> handleEcmaStrU16CompNo2(thread,
1275         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo2[0], lengthEcmaStrU16CompNo2, true));
1276     JSHandle<EcmaString> handleEcmaStrU16CompNo3(thread,
1277         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo3[0], lengthEcmaStrU16CompNo3, true));
1278     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU8No1, *handleEcmaStrU16CompNo2));
1279     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU8No1, *handleEcmaStrU16CompNo3));
1280 }
1281 
1282 /*
1283  * @tc.name: StringsAreEqual_003
1284  * @tc.desc: Check whether the bool returned through calling StringsAreEqual function with two EcmaStrings made by
1285  * CreateFromUtf16(, , , true) is within expectations.
1286  * @tc.type: FUNC
1287  * @tc.require:
1288  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqual_003)1289 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqual_003)
1290 {
1291     // StringsAreEqual().
1292     uint16_t arrayU16CompNo1[] = {45, 92, 78};
1293     uint16_t arrayU16CompNo2[] = {45, 92, 78};
1294     uint16_t arrayU16CompNo3[] = {45, 92, 78, 1};
1295     uint32_t lengthEcmaStrU16CompNo1 = sizeof(arrayU16CompNo1) / sizeof(arrayU16CompNo1[0]);
1296     uint32_t lengthEcmaStrU16CompNo2 = sizeof(arrayU16CompNo2) / sizeof(arrayU16CompNo2[0]);
1297     uint32_t lengthEcmaStrU16CompNo3 = sizeof(arrayU16CompNo3) / sizeof(arrayU16CompNo3[0]);
1298     JSHandle<EcmaString> handleEcmaStrU16CompNo1(thread,
1299         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1, true));
1300     JSHandle<EcmaString> handleEcmaStrU16CompNo2(thread,
1301         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo2[0], lengthEcmaStrU16CompNo2, true));
1302     JSHandle<EcmaString> handleEcmaStrU16CompNo3(thread,
1303         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo3[0], lengthEcmaStrU16CompNo3, true));
1304     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16CompNo1, *handleEcmaStrU16CompNo2));
1305     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16CompNo1, *handleEcmaStrU16CompNo3));
1306     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16CompNo3, *handleEcmaStrU16CompNo1));
1307 }
1308 
1309 /*
1310  * @tc.name: StringsAreEqual_004
1311  * @tc.desc: Check whether the bool returned through calling StringsAreEqual function with a EcmaString made by
1312  * CreateFromUtf8() and a EcmaString made by CreateFromUtf16(, , , false) is within expectations.
1313  * @tc.type: FUNC
1314  * @tc.require:
1315  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqual_004)1316 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqual_004)
1317 {
1318     // StringsAreEqual().
1319     uint8_t arrayU8No1[4] = {45, 92, 78};
1320     uint16_t arrayU16NotCompNo1[] = {45, 92, 78};
1321     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1322     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1323     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1324         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1325     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1326         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, true));
1327     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU8No1, *handleEcmaStrU16NotCompNo1));
1328     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16NotCompNo1, *handleEcmaStrU8No1));
1329 }
1330 
1331 /*
1332  * @tc.name: StringsAreEqual_005
1333  * @tc.desc: Check whether the bool returned through calling StringsAreEqual function with a EcmaString made by
1334  * CreateFromUtf16(, , , true) and a EcmaString made by CreateFromUtf16(, , , false) is within expectations.
1335  * @tc.type: FUNC
1336  * @tc.require:
1337  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqual_005)1338 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqual_005)
1339 {
1340     // StringsAreEqual().
1341     uint16_t arrayU16CompNo1[] = {45, 92, 78};
1342     uint16_t arrayU16NotCompNo1[] = {45, 92, 78};
1343     uint32_t lengthEcmaStrU16CompNo1 = sizeof(arrayU16CompNo1) / sizeof(arrayU16CompNo1[0]);
1344     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1345     JSHandle<EcmaString> handleEcmaStrU16CompNo1(thread,
1346         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1, true));
1347     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1348         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, true));
1349     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16CompNo1, *handleEcmaStrU16NotCompNo1));
1350     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16NotCompNo1, *handleEcmaStrU16CompNo1));
1351 }
1352 
1353 /*
1354  * @tc.name: StringsAreEqual_006
1355  * @tc.desc: Check whether the bool returned through calling StringsAreEqual function with two EcmaStrings made by
1356  * CreateFromUtf16(, , , false) is within expectations.
1357  * @tc.type: FUNC
1358  * @tc.require:
1359  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqual_006)1360 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqual_006)
1361 {
1362     // StringsAreEqual().
1363     uint16_t arrayU16NotCompNo1[] = {234, 345, 127, 2345, 65535, 5};
1364     uint16_t arrayU16NotCompNo2[] = {234, 345, 127, 2345, 65535, 5};
1365     uint16_t arrayU16NotCompNo3[] = {1, 234, 345, 127, 2345, 65535, 5};
1366     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1367     uint32_t lengthEcmaStrU16NotCompNo2 = sizeof(arrayU16NotCompNo2) / sizeof(arrayU16NotCompNo2[0]);
1368     uint32_t lengthEcmaStrU16NotCompNo3 = sizeof(arrayU16NotCompNo3) / sizeof(arrayU16NotCompNo3[0]);
1369     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1370         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, false));
1371     JSHandle<EcmaString> handleEcmaStrU16NotCompNo2(thread,
1372         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo2[0], lengthEcmaStrU16NotCompNo2, false));
1373     JSHandle<EcmaString> handleEcmaStrU16NotCompNo3(thread,
1374         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo3[0], lengthEcmaStrU16NotCompNo3, false));
1375     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16NotCompNo1, *handleEcmaStrU16NotCompNo2));
1376     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16NotCompNo1, *handleEcmaStrU16NotCompNo3));
1377     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqual(*handleEcmaStrU16NotCompNo3, *handleEcmaStrU16NotCompNo1));
1378 }
1379 
1380 /*
1381  * @tc.name: StringsAreEqualUtf8_001
1382  * @tc.desc: Check whether the bool returned through calling StringsAreEqualUtf8 function with an EcmaString made by
1383  * CreateFromUtf8() and an Array(uint8_t) is within expectations.
1384  * @tc.type: FUNC
1385  * @tc.require:
1386  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqualUtf8_001)1387 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqualUtf8_001)
1388 {
1389     // StringsAreEqualUtf8(). EcmaString made by CreateFromUtf8(), Array:U8.
1390     uint8_t arrayU8No1[4] = {45, 92, 78};
1391     uint8_t arrayU8No2[5] = {45, 92, 78, 24};
1392     uint8_t arrayU8No3[3] = {45, 92};
1393     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1394     uint32_t lengthEcmaStrU8No2 = sizeof(arrayU8No2) - 1;
1395     uint32_t lengthEcmaStrU8No3 = sizeof(arrayU8No3) - 1;
1396     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1397         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1398     JSHandle<EcmaString> handleEcmaStrU8No2(thread,
1399         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No2[0], lengthEcmaStrU8No2, true));
1400     JSHandle<EcmaString> handleEcmaStrU8No3(thread,
1401         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No3[0], lengthEcmaStrU8No3, true));
1402     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqualUtf8(
1403         *handleEcmaStrU8No1, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1404     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1405         *handleEcmaStrU8No1, &arrayU8No1[0], lengthEcmaStrU8No1, false));
1406     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1407         *handleEcmaStrU8No2, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1408     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1409         *handleEcmaStrU8No3, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1410 }
1411 
1412 /*
1413  * @tc.name: StringsAreEqualUtf8_002
1414  * @tc.desc: Check whether the bool returned through calling StringsAreEqualUtf8 function with an EcmaString made by
1415  * CreateFromUtf16( , , , true) and an Array(uint8_t) is within expectations.
1416  * @tc.type: FUNC
1417  * @tc.require:
1418  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqualUtf8_002)1419 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqualUtf8_002)
1420 {
1421     // StringsAreEqualUtf8(). EcmaString made by CreateFromUtf16( , , , true), Array:U8.
1422     uint8_t arrayU8No1[4] = {45, 92, 78};
1423     uint16_t arrayU16CompNo1[] = {45, 92, 78};
1424     uint16_t arrayU16CompNo2[] = {45, 92, 78, 24};
1425     uint16_t arrayU16CompNo3[] = {45, 92};
1426     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1427     uint32_t lengthEcmaStrU16CompNo1 = sizeof(arrayU16CompNo1) / sizeof(arrayU16CompNo1[0]);
1428     uint32_t lengthEcmaStrU16CompNo2 = sizeof(arrayU16CompNo2) / sizeof(arrayU16CompNo2[0]);
1429     uint32_t lengthEcmaStrU16CompNo3 = sizeof(arrayU16CompNo3) / sizeof(arrayU16CompNo3[0]);
1430     JSHandle<EcmaString> handleEcmaStrU16CompNo1(thread,
1431         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1, true));
1432     JSHandle<EcmaString> handleEcmaStrU16CompNo2(thread,
1433         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo2[0], lengthEcmaStrU16CompNo2, true));
1434     JSHandle<EcmaString> handleEcmaStrU16CompNo3(thread,
1435         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo3[0], lengthEcmaStrU16CompNo3, true));
1436     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqualUtf8(
1437         *handleEcmaStrU16CompNo1, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1438     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1439         *handleEcmaStrU16CompNo1, &arrayU8No1[0], lengthEcmaStrU8No1, false));
1440     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1441         *handleEcmaStrU16CompNo2, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1442     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1443         *handleEcmaStrU16CompNo3, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1444 }
1445 
1446 /*
1447  * @tc.name: StringsAreEqualUtf8_003
1448  * @tc.desc: Check whether the bool returned through calling StringsAreEqualUtf8 function with an EcmaString made by
1449  * CreateFromUtf16( , , , false) and an Array(uint8_t) is within expectations.
1450  * @tc.type: FUNC
1451  * @tc.require:
1452  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqualUtf8_003)1453 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqualUtf8_003)
1454 {
1455     // StringsAreEqualUtf8(). EcmaString made by CreateFromUtf16( , , , false), Array:U8.
1456     uint8_t arrayU8No1[4] = {45, 92, 78};
1457     uint16_t arrayU16NotCompNo1[] = {45, 92, 78};
1458     uint16_t arrayU16NotCompNo2[] = {45, 92, 78, 24};
1459     uint16_t arrayU16NotCompNo3[] = {45, 92};
1460     uint16_t arrayU16NotCompNo4[] = {25645, 25692, 25678};
1461     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1462     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1463     uint32_t lengthEcmaStrU16NotCompNo2 = sizeof(arrayU16NotCompNo2) / sizeof(arrayU16NotCompNo2[0]);
1464     uint32_t lengthEcmaStrU16NotCompNo3 = sizeof(arrayU16NotCompNo3) / sizeof(arrayU16NotCompNo3[0]);
1465     uint32_t lengthEcmaStrU16NotCompNo4 = sizeof(arrayU16NotCompNo4) / sizeof(arrayU16NotCompNo4[0]);
1466     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1467         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, true));
1468     JSHandle<EcmaString> handleEcmaStrU16NotCompNo2(thread,
1469         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo2[0], lengthEcmaStrU16NotCompNo2, true));
1470     JSHandle<EcmaString> handleEcmaStrU16NotCompNo3(thread,
1471         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo3[0], lengthEcmaStrU16NotCompNo3, true));
1472     JSHandle<EcmaString> handleEcmaStrU16NotCompNo4(thread,
1473         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo4[0], lengthEcmaStrU16NotCompNo4, false));
1474     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1475         *handleEcmaStrU16NotCompNo1, &arrayU8No1[0], lengthEcmaStrU8No1, false));
1476     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqualUtf8(
1477         *handleEcmaStrU16NotCompNo1, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1478     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1479         *handleEcmaStrU16NotCompNo2, &arrayU8No1[0], lengthEcmaStrU8No1, false));
1480     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1481         *handleEcmaStrU16NotCompNo3, &arrayU8No1[0], lengthEcmaStrU8No1, false));
1482     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf8(
1483         *handleEcmaStrU16NotCompNo4, &arrayU8No1[0], lengthEcmaStrU8No1, false));
1484 }
1485 
1486 /*
1487  * @tc.name: StringsAreEqualUtf16_001
1488  * @tc.desc: Check whether the bool returned through calling StringsAreEqualUtf16 function with an EcmaString made by
1489  * CreateFromUtf8() and an Array(uint16_t) is within expectations.
1490  * @tc.type: FUNC
1491  * @tc.require:
1492  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqualUtf16_001)1493 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqualUtf16_001)
1494 {
1495     // StringsAreEqualUtf16(). EcmaString made by CreateFromUtf8, Array:U16(1-127).
1496     uint8_t arrayU8No1[4] = {45, 92, 78};
1497     uint8_t arrayU8No2[5] = {45, 92, 78, 24};
1498     uint8_t arrayU8No3[3] = {45, 92};
1499     uint16_t arrayU16NotCompNo1[] = {45, 92, 78};
1500     uint32_t lengthEcmaStrU8No1 = sizeof(arrayU8No1) - 1;
1501     uint32_t lengthEcmaStrU8No2 = sizeof(arrayU8No2) - 1;
1502     uint32_t lengthEcmaStrU8No3 = sizeof(arrayU8No3) - 1;
1503     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1504     JSHandle<EcmaString> handleEcmaStrU8No1(thread,
1505         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No1[0], lengthEcmaStrU8No1, true));
1506     JSHandle<EcmaString> handleEcmaStrU8No2(thread,
1507         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No2[0], lengthEcmaStrU8No2, true));
1508     JSHandle<EcmaString> handleEcmaStrU8No3(thread,
1509         EcmaStringAccessor::CreateFromUtf8(ecmaVMPtr, &arrayU8No3[0], lengthEcmaStrU8No3, true));
1510     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqualUtf16(
1511         *handleEcmaStrU8No1, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1));
1512     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(
1513         *handleEcmaStrU8No2, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1));
1514     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(
1515         *handleEcmaStrU8No3, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1));
1516 }
1517 
1518 /*
1519  * @tc.name: StringsAreEqualUtf16_002
1520  * @tc.desc: Check whether the bool returned through calling StringsAreEqualUtf16 function with an EcmaString made by
1521  * CreateFromUtf16( , , , true) and an Array(uint16_t) is within expectations.
1522  * @tc.type: FUNC
1523  * @tc.require:
1524  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqualUtf16_002)1525 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqualUtf16_002)
1526 {
1527     // StringsAreEqualUtf16(). EcmaString made by CreateFromUtf16( , , , true), Array:U16(1-127).
1528     uint16_t arrayU16CompNo1[] = {45, 92, 78};
1529     uint16_t arrayU16CompNo2[] = {45, 92, 78, 24};
1530     uint16_t arrayU16CompNo3[] = {45, 92};
1531     uint16_t arrayU16CompNo4[] = {25645, 25692, 25678}; // 25645 % 256 == 45...
1532     uint32_t lengthEcmaStrU16CompNo1 = sizeof(arrayU16CompNo1) / sizeof(arrayU16CompNo1[0]);
1533     uint32_t lengthEcmaStrU16CompNo2 = sizeof(arrayU16CompNo2) / sizeof(arrayU16CompNo2[0]);
1534     uint32_t lengthEcmaStrU16CompNo3 = sizeof(arrayU16CompNo3) / sizeof(arrayU16CompNo3[0]);
1535     uint32_t lengthEcmaStrU16CompNo4 = sizeof(arrayU16CompNo4) / sizeof(arrayU16CompNo4[0]);
1536     JSHandle<EcmaString> handleEcmaStrU16CompNo1(thread,
1537         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1, true));
1538     JSHandle<EcmaString> handleEcmaStrU16CompNo2(thread,
1539         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo2[0], lengthEcmaStrU16CompNo2, true));
1540     JSHandle<EcmaString> handleEcmaStrU16CompNo3(thread,
1541         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo3[0], lengthEcmaStrU16CompNo3, true));
1542     JSHandle<EcmaString> handleEcmaStrU16CompNo4(thread,
1543         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16CompNo4[0], lengthEcmaStrU16CompNo4, true));
1544     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqualUtf16(
1545         *handleEcmaStrU16CompNo1, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1));
1546     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(
1547         *handleEcmaStrU16CompNo2, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1));
1548     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(
1549         *handleEcmaStrU16CompNo3, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1));
1550     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqualUtf16(
1551         *handleEcmaStrU16CompNo4, &arrayU16CompNo1[0], lengthEcmaStrU16CompNo1));
1552 }
1553 
1554 /*
1555  * @tc.name: StringsAreEqualUtf16_003
1556  * @tc.desc: Check whether the bool returned through calling StringsAreEqualUtf16 function with an EcmaString made by
1557  * CreateFromUtf16( , , , false) and an Array(uint16_t) is within expectations.
1558  * @tc.type: FUNC
1559  * @tc.require:
1560  */
HWTEST_F_L0(EcmaStringAccessorTest,StringsAreEqualUtf16_003)1561 HWTEST_F_L0(EcmaStringAccessorTest, StringsAreEqualUtf16_003)
1562 {
1563     // StringsAreEqualUtf16(). EcmaString made by CreateFromUtf16( , , , false), Array:U16(0-65535).
1564     uint16_t arrayU16NotCompNo1[] = {25645, 25692, 25678};
1565     uint16_t arrayU16NotCompNo2[] = {25645, 25692, 78}; // 25645 % 256 == 45...
1566     uint16_t arrayU16NotCompNo3[] = {25645, 25692, 25678, 65535};
1567     uint16_t arrayU16NotCompNo4[] = {25645, 25692};
1568     uint32_t lengthEcmaStrU16NotCompNo1 = sizeof(arrayU16NotCompNo1) / sizeof(arrayU16NotCompNo1[0]);
1569     uint32_t lengthEcmaStrU16NotCompNo2 = sizeof(arrayU16NotCompNo2) / sizeof(arrayU16NotCompNo2[0]);
1570     uint32_t lengthEcmaStrU16NotCompNo3 = sizeof(arrayU16NotCompNo3) / sizeof(arrayU16NotCompNo3[0]);
1571     uint32_t lengthEcmaStrU16NotCompNo4 = sizeof(arrayU16NotCompNo4) / sizeof(arrayU16NotCompNo4[0]);
1572     JSHandle<EcmaString> handleEcmaStrU16NotCompNo1(thread,
1573         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo1[0], lengthEcmaStrU16NotCompNo1, false));
1574     JSHandle<EcmaString> handleEcmaStrU16NotCompNo2(thread,
1575         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo2[0], lengthEcmaStrU16NotCompNo2, false));
1576     JSHandle<EcmaString> handleEcmaStrU16NotCompNo3(thread,
1577         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo3[0], lengthEcmaStrU16NotCompNo3, false));
1578     JSHandle<EcmaString> handleEcmaStrU16NotCompNo4(thread,
1579         EcmaStringAccessor::CreateFromUtf16(ecmaVMPtr, &arrayU16NotCompNo4[0], lengthEcmaStrU16NotCompNo4, false));
1580     EXPECT_TRUE(EcmaStringAccessor::StringsAreEqualUtf16(*handleEcmaStrU16NotCompNo1, &arrayU16NotCompNo1[0],
1581         lengthEcmaStrU16NotCompNo1));
1582     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(*handleEcmaStrU16NotCompNo1, &arrayU16NotCompNo2[0],
1583         lengthEcmaStrU16NotCompNo2));
1584     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(*handleEcmaStrU16NotCompNo2, &arrayU16NotCompNo1[0],
1585         lengthEcmaStrU16NotCompNo1));
1586     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(*handleEcmaStrU16NotCompNo3, &arrayU16NotCompNo1[0],
1587         lengthEcmaStrU16NotCompNo1));
1588     EXPECT_FALSE(EcmaStringAccessor::StringsAreEqualUtf16(*handleEcmaStrU16NotCompNo4, &arrayU16NotCompNo1[0],
1589         lengthEcmaStrU16NotCompNo1));
1590 }
1591 
1592 /*
1593  * @tc.name: EqualToSplicedString
1594  * @tc.desc: Tests whether the source string is equal to the concatenated string.
1595  * is within expectations.
1596  * @tc.type: FUNC
1597  * @tc.require:
1598  */
HWTEST_F_L0(EcmaStringAccessorTest,EqualToSplicedString)1599 HWTEST_F_L0(EcmaStringAccessorTest, EqualToSplicedString)
1600 {
1601     ObjectFactory* factory = ecmaVMPtr->GetFactory();
1602     {
1603         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Start开始");
1604         JSHandle<EcmaString> firstString = factory->NewFromASCII("Start");
1605         JSHandle<EcmaString> secondString = factory->NewFromUtf8("开始");
1606         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
1607         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf8());
1608         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf16());
1609         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1610         EXPECT_TRUE(result);
1611     }
1612 
1613     {
1614         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Start开始");
1615         JSHandle<EcmaString> firstString = factory->NewFromASCII("Start");
1616         JSHandle<EcmaString> secondString = factory->NewFromASCII("start");
1617         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
1618         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf8());
1619         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf8());
1620         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1621         EXPECT_TRUE(!result);
1622     }
1623 
1624     {
1625         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Start开始");
1626         JSHandle<EcmaString> firstString = factory->NewFromUtf8("Start开");
1627         JSHandle<EcmaString> secondString = factory->NewFromUtf8("始");
1628         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
1629         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf16());
1630         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf16());
1631         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1632         EXPECT_TRUE(result);
1633     }
1634 
1635     {
1636         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Startstart");
1637         JSHandle<EcmaString> firstString = factory->NewFromASCII("Start");
1638         JSHandle<EcmaString> secondString = factory->NewFromASCII("start");
1639         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf8());
1640         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf8());
1641         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf8());
1642         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1643         EXPECT_TRUE(result);
1644     }
1645 
1646     {
1647         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Startstart");
1648         JSHandle<EcmaString> firstString = factory->NewFromASCII("Start");
1649         JSHandle<EcmaString> secondString = factory->NewFromUtf8("开始");
1650         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf8());
1651         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf8());
1652         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf16());
1653         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1654         EXPECT_TRUE(!result);
1655     }
1656 
1657     {
1658         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Startstat");
1659         JSHandle<EcmaString> firstString = factory->NewFromASCII("Start");
1660         JSHandle<EcmaString> secondString = factory->NewFromASCII("start");
1661         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf8());
1662         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf8());
1663         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf8());
1664         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1665         EXPECT_TRUE(!result);
1666     }
1667 
1668     {
1669         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Start开始");
1670         JSHandle<EcmaString> firstString = factory->NewFromUtf8("Stat开");
1671         JSHandle<EcmaString> secondString = factory->NewFromUtf8("始");
1672         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
1673         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf16());
1674         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf16());
1675         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1676         EXPECT_TRUE(!result);
1677     }
1678 
1679     {
1680         JSHandle<EcmaString> sourceString = factory->NewFromUtf8("Start开始");
1681         JSHandle<EcmaString> firstString = factory->NewFromASCII("Stat");
1682         JSHandle<EcmaString> secondString = factory->NewFromUtf8("开始");
1683         EXPECT_TRUE(EcmaStringAccessor(sourceString).IsUtf16());
1684         EXPECT_TRUE(EcmaStringAccessor(firstString).IsUtf8());
1685         EXPECT_TRUE(EcmaStringAccessor(secondString).IsUtf16());
1686         bool result = EcmaStringAccessor(sourceString).EqualToSplicedString(*firstString, *secondString);
1687         EXPECT_TRUE(!result);
1688     }
1689 }
1690 
1691 /*
1692  * @tc.name: CanBeCompressed
1693  * @tc.desc: Check whether the bool returned through calling CanBeCompressed function is within expectations.
1694  * @tc.type: FUNC
1695  * @tc.require:
1696  */
HWTEST_F_L0(EcmaStringAccessorTest,CanBeCompressed)1697 HWTEST_F_L0(EcmaStringAccessorTest, CanBeCompressed)
1698 {
1699     uint8_t arrayU8[] = {12, 34, 77, 127, 99, 1};
1700     uint16_t arrayU16Comp[] = {1, 4, 37, 91, 127, 1};
1701     uint16_t arrayU16NotComp[] = {72, 43, 337, 961, 1317, 65535};
1702     EXPECT_TRUE(EcmaStringAccessor::CanBeCompressed(arrayU8, sizeof(arrayU8) / sizeof(arrayU8[0])));
1703     EXPECT_TRUE(EcmaStringAccessor::CanBeCompressed(arrayU16Comp, sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0])));
1704     EXPECT_FALSE(EcmaStringAccessor::CanBeCompressed(arrayU16NotComp, sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0])));
1705 }
1706 
1707 /*
1708  * @tc.name: TryToLower
1709  * @tc.desc: Check whether the EcmaString created through calling TryToLower function is within expectations.
1710  * is within expectations.
1711  * @tc.type: FUNC
1712  * @tc.require:
1713  */
HWTEST_F_L0(EcmaStringAccessorTest,TryToLower)1714 HWTEST_F_L0(EcmaStringAccessorTest, TryToLower)
1715 {
1716     ObjectFactory* factory = ecmaVMPtr->GetFactory();
1717     JSHandle<EcmaString> lowerStr = factory->NewFromASCII("aaabbbcccddd");
1718     JSHandle<EcmaString> upperStr = factory->NewFromASCII("AAABBBCCCDDD");
1719     JSHandle<EcmaString> testStr1 = factory->NewFromASCII("aaaBBBcccDDD");
1720     JSHandle<EcmaString> testStr2 = factory->NewFromASCII("AAAbbbcccDDD");
1721     {
1722         JSHandle<EcmaString> lowerEcmaString(thread, EcmaStringAccessor::TryToLower(ecmaVMPtr, lowerStr));
1723         EXPECT_TRUE(JSTaggedValue::SameValue(lowerStr.GetTaggedValue(), lowerEcmaString.GetTaggedValue()));
1724         EXPECT_EQ(EcmaStringAccessor(lowerStr).GetLength(), EcmaStringAccessor(lowerEcmaString).GetLength());
1725         EXPECT_TRUE(EcmaStringAccessor(lowerEcmaString).IsUtf8());
1726         EXPECT_FALSE(EcmaStringAccessor(lowerEcmaString).IsUtf16());
1727     }
1728 
1729     {
1730         JSHandle<EcmaString> lowerEcmaString(thread, EcmaStringAccessor::TryToLower(ecmaVMPtr, upperStr));
1731         EXPECT_TRUE(JSTaggedValue::SameValue(lowerStr.GetTaggedValue(), lowerEcmaString.GetTaggedValue()));
1732         EXPECT_EQ(EcmaStringAccessor(lowerStr).GetLength(), EcmaStringAccessor(lowerEcmaString).GetLength());
1733         EXPECT_TRUE(EcmaStringAccessor(lowerEcmaString).IsUtf8());
1734         EXPECT_FALSE(EcmaStringAccessor(lowerEcmaString).IsUtf16());
1735     }
1736 
1737     {
1738         JSHandle<EcmaString> testEcmaString(thread, EcmaStringAccessor::TryToLower(ecmaVMPtr, testStr1));
1739         EXPECT_TRUE(JSTaggedValue::SameValue(lowerStr.GetTaggedValue(), testEcmaString.GetTaggedValue()));
1740         EXPECT_EQ(EcmaStringAccessor(lowerStr).GetLength(), EcmaStringAccessor(testEcmaString).GetLength());
1741         EXPECT_TRUE(EcmaStringAccessor(testEcmaString).IsUtf8());
1742         EXPECT_FALSE(EcmaStringAccessor(testEcmaString).IsUtf16());
1743     }
1744 
1745     {
1746         JSHandle<EcmaString> testEcmaString(thread, EcmaStringAccessor::TryToLower(ecmaVMPtr, testStr2));
1747         EXPECT_TRUE(JSTaggedValue::SameValue(lowerStr.GetTaggedValue(), testEcmaString.GetTaggedValue()));
1748         EXPECT_EQ(EcmaStringAccessor(lowerStr).GetLength(), EcmaStringAccessor(testEcmaString).GetLength());
1749         EXPECT_TRUE(EcmaStringAccessor(testEcmaString).IsUtf8());
1750         EXPECT_FALSE(EcmaStringAccessor(testEcmaString).IsUtf16());
1751     }
1752 }
1753 }  // namespace panda::test
1754