• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/ecma_string-inl.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/object_factory.h"
19 #include "ecmascript/regexp/regexp_executor.h"
20 #include "ecmascript/regexp/regexp_parser.h"
21 #include "ecmascript/tests/test_helper.h"
22 
23 namespace panda::test {
24 using namespace panda::ecmascript;
25 using RegExpGlobalResult = ecmascript::builtins::RegExpGlobalResult;
26 
27 class RegExpTest : public testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase()
30     {
31         GTEST_LOG_(INFO) << "SetUpTestCase";
32     }
33 
TearDownTestCase()34     static void TearDownTestCase()
35     {
36         GTEST_LOG_(INFO) << "TearDownCase";
37     }
38 
SetUp()39     void SetUp() override
40     {
41         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
42         chunk_ = thread->GetEcmaVM()->GetChunk();
43         regExpCachedChunk_ = new RegExpCachedChunk(thread);
44     }
45 
TearDown()46     void TearDown() override
47     {
48         delete regExpCachedChunk_;
49         TestHelper::DestroyEcmaVMWithScope(instance, scope);
50     }
51 
IsValidAlphaEscapeInAtom(char s) const52     bool IsValidAlphaEscapeInAtom(char s) const
53     {
54         switch (s) {
55             // Assertion [U] :: \b
56             case 'b':
57             // Assertion [U] :: \B
58             case 'B':
59             // ControlEscape :: one of f n r t v
60             case 'f':
61             case 'n':
62             case 'r':
63             case 't':
64             case 'v':
65             // CharacterClassEscape :: one of d D s S w W
66             case 'd':
67             case 'D':
68             case 's':
69             case 'S':
70             case 'w':
71             case 'W':
72                 return true;
73             default:
74                 return false;
75         }
76     }
77 
IsValidAlphaEscapeInClass(char s) const78     bool IsValidAlphaEscapeInClass(char s) const
79     {
80         switch (s) {
81             // ClassEscape[U] :: b
82             case 'b':
83             // ControlEscape :: one of f n r t v
84             case 'f':
85             case 'n':
86             case 'r':
87             case 't':
88             case 'v':
89             // CharacterClassEscape :: one of d D s S w W
90             case 'd':
91             case 'D':
92             case 's':
93             case 'S':
94             case 'w':
95             case 'W':
96                 return true;
97             default:
98                 return false;
99         }
100     }
101 
GetSubString(JSHandle<RegExpGlobalResult> regExpGlobalResult,JSHandle<EcmaString> inputStr,uint32_t index)102     JSHandle<EcmaString> GetSubString(JSHandle<RegExpGlobalResult>regExpGlobalResult,
103                                       JSHandle<EcmaString> inputStr, uint32_t index)
104     {
105         uint32_t startIndex = regExpGlobalResult->GetStartOfCaptureIndex(index).GetInt();
106         uint32_t len = regExpGlobalResult->GetEndOfCaptureIndex(index).GetInt() - startIndex;
107         return JSHandle<EcmaString>(thread, EcmaStringAccessor::FastSubString(
108             thread->GetEcmaVM(), inputStr, startIndex, len));
109     }
110 
111 protected:
112     EcmaVM *instance {nullptr};
113     EcmaHandleScope *scope {nullptr};
114     JSThread *thread {nullptr};
115     Chunk *chunk_ {nullptr};
116     RegExpCachedChunk *regExpCachedChunk_ {nullptr};
117 };
118 
HWTEST_F_L0(RegExpTest,ParseError1)119 HWTEST_F_L0(RegExpTest, ParseError1)
120 {
121     RegExpParser parser = RegExpParser(thread, chunk_);
122     CString source("0{2,1}");
123     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
124     parser.Parse();
125     bool parseResult = parser.IsError();
126     ASSERT_TRUE(parseResult);
127 }
128 
HWTEST_F_L0(RegExpTest,ParseError2)129 HWTEST_F_L0(RegExpTest, ParseError2)
130 {
131     RegExpParser parser = RegExpParser(thread, chunk_);
132     CString source("^[z-a]$");
133     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
134     parser.Parse();
135     bool parseResult = parser.IsError();
136     ASSERT_TRUE(parseResult);
137 }
138 
HWTEST_F_L0(RegExpTest,ParseError3)139 HWTEST_F_L0(RegExpTest, ParseError3)
140 {
141     RegExpParser parser = RegExpParser(thread, chunk_);
142     CString source("\\");
143     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
144     parser.Parse();
145     bool parseResult = parser.IsError();
146     ASSERT_TRUE(parseResult);
147 }
148 
HWTEST_F_L0(RegExpTest,ParseError4)149 HWTEST_F_L0(RegExpTest, ParseError4)
150 {
151     RegExpParser parser = RegExpParser(thread, chunk_);
152     CString source("a**");
153     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
154     parser.Parse();
155     bool parseResult = parser.IsError();
156     ASSERT_TRUE(parseResult);
157 }
158 
HWTEST_F_L0(RegExpTest,ParseError5)159 HWTEST_F_L0(RegExpTest, ParseError5)
160 {
161     RegExpParser parser = RegExpParser(thread, chunk_);
162     CString source("a***");
163     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
164     parser.Parse();
165     bool parseResult = parser.IsError();
166     ASSERT_TRUE(parseResult);
167 }
168 
HWTEST_F_L0(RegExpTest,ParseError6)169 HWTEST_F_L0(RegExpTest, ParseError6)
170 {
171     RegExpParser parser = RegExpParser(thread, chunk_);
172     CString source("a**");
173     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
174     parser.Parse();
175     bool parseResult = parser.IsError();
176     ASSERT_TRUE(parseResult);
177 }
178 
HWTEST_F_L0(RegExpTest,ParseError7)179 HWTEST_F_L0(RegExpTest, ParseError7)
180 {
181     RegExpParser parser = RegExpParser(thread, chunk_);
182     CString source("a++");
183     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
184     parser.Parse();
185     bool parseResult = parser.IsError();
186     ASSERT_TRUE(parseResult);
187 }
188 
HWTEST_F_L0(RegExpTest,ParseError8)189 HWTEST_F_L0(RegExpTest, ParseError8)
190 {
191     RegExpParser parser = RegExpParser(thread, chunk_);
192     CString source("a+++");
193     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
194     parser.Parse();
195     bool parseResult = parser.IsError();
196     ASSERT_TRUE(parseResult);
197 }
198 
HWTEST_F_L0(RegExpTest,ParseError9)199 HWTEST_F_L0(RegExpTest, ParseError9)
200 {
201     RegExpParser parser = RegExpParser(thread, chunk_);
202     CString source("a???");
203     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
204     parser.Parse();
205     bool parseResult = parser.IsError();
206     ASSERT_TRUE(parseResult);
207 }
208 
HWTEST_F_L0(RegExpTest,ParseError10)209 HWTEST_F_L0(RegExpTest, ParseError10)
210 {
211     RegExpParser parser = RegExpParser(thread, chunk_);
212     CString source("a????");
213     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
214     parser.Parse();
215     bool parseResult = parser.IsError();
216     ASSERT_TRUE(parseResult);
217 }
218 
HWTEST_F_L0(RegExpTest,ParseError11)219 HWTEST_F_L0(RegExpTest, ParseError11)
220 {
221     RegExpParser parser = RegExpParser(thread, chunk_);
222     CString source("*a");
223     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
224     parser.Parse();
225     bool parseResult = parser.IsError();
226     ASSERT_TRUE(parseResult);
227 }
228 
HWTEST_F_L0(RegExpTest,ParseError12)229 HWTEST_F_L0(RegExpTest, ParseError12)
230 {
231     RegExpParser parser = RegExpParser(thread, chunk_);
232     CString source("**a");
233     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
234     parser.Parse();
235     bool parseResult = parser.IsError();
236     ASSERT_TRUE(parseResult);
237 }
238 
HWTEST_F_L0(RegExpTest,ParseError13)239 HWTEST_F_L0(RegExpTest, ParseError13)
240 {
241     RegExpParser parser = RegExpParser(thread, chunk_);
242     CString source("+a");
243     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
244     parser.Parse();
245     bool parseResult = parser.IsError();
246     ASSERT_TRUE(parseResult);
247 }
248 
HWTEST_F_L0(RegExpTest,ParseError14)249 HWTEST_F_L0(RegExpTest, ParseError14)
250 {
251     RegExpParser parser = RegExpParser(thread, chunk_);
252     CString source("++a");
253     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
254     parser.Parse();
255     bool parseResult = parser.IsError();
256     ASSERT_TRUE(parseResult);
257 }
258 
HWTEST_F_L0(RegExpTest,ParseError15)259 HWTEST_F_L0(RegExpTest, ParseError15)
260 {
261     RegExpParser parser = RegExpParser(thread, chunk_);
262     CString source("?a");
263     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
264     parser.Parse();
265     bool parseResult = parser.IsError();
266     ASSERT_TRUE(parseResult);
267 }
268 
HWTEST_F_L0(RegExpTest,ParseError16)269 HWTEST_F_L0(RegExpTest, ParseError16)
270 {
271     RegExpParser parser = RegExpParser(thread, chunk_);
272     CString source("??a");
273     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
274     parser.Parse();
275     bool parseResult = parser.IsError();
276     ASSERT_TRUE(parseResult);
277 }
278 
HWTEST_F_L0(RegExpTest,ParseError17)279 HWTEST_F_L0(RegExpTest, ParseError17)
280 {
281     RegExpParser parser = RegExpParser(thread, chunk_);
282     CString source("x{1}{1,}");
283     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
284     parser.Parse();
285     bool parseResult = parser.IsError();
286     ASSERT_TRUE(parseResult);
287 }
288 
HWTEST_F_L0(RegExpTest,ParseError18)289 HWTEST_F_L0(RegExpTest, ParseError18)
290 {
291     RegExpParser parser = RegExpParser(thread, chunk_);
292     CString source("x{1,2}{1}");
293     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
294     parser.Parse();
295     bool parseResult = parser.IsError();
296     ASSERT_TRUE(parseResult);
297 }
298 
HWTEST_F_L0(RegExpTest,ParseError19)299 HWTEST_F_L0(RegExpTest, ParseError19)
300 {
301     RegExpParser parser = RegExpParser(thread, chunk_);
302     CString source("x{1,}{1}");
303     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
304     parser.Parse();
305     bool parseResult = parser.IsError();
306     ASSERT_TRUE(parseResult);
307 }
308 
HWTEST_F_L0(RegExpTest,ParseError20)309 HWTEST_F_L0(RegExpTest, ParseError20)
310 {
311     RegExpParser parser = RegExpParser(thread, chunk_);
312     CString source("x{0,1}{1,}");
313     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
314     parser.Parse();
315     bool parseResult = parser.IsError();
316     ASSERT_TRUE(parseResult);
317 }
318 
HWTEST_F_L0(RegExpTest,ParseError21)319 HWTEST_F_L0(RegExpTest, ParseError21)
320 {
321     RegExpParser parser = RegExpParser(thread, chunk_);
322     CString source("[b-ac-e]");
323     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
324     parser.Parse();
325     bool parseResult = parser.IsError();
326     ASSERT_TRUE(parseResult);
327 }
328 
HWTEST_F_L0(RegExpTest,ParseError22)329 HWTEST_F_L0(RegExpTest, ParseError22)
330 {
331     RegExpParser parser = RegExpParser(thread, chunk_);
332     CString source("[\\10b-G]");
333     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
334     parser.Parse();
335     bool parseResult = parser.IsError();
336     ASSERT_TRUE(parseResult);
337 }
338 
HWTEST_F_L0(RegExpTest,ParseError23)339 HWTEST_F_L0(RegExpTest, ParseError23)
340 {
341     RegExpParser parser = RegExpParser(thread, chunk_);
342     CString source("[\\0b-G]");
343     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
344     parser.Parse();
345     bool parseResult = parser.IsError();
346     ASSERT_TRUE(parseResult);
347 }
348 
HWTEST_F_L0(RegExpTest,ParseError24)349 HWTEST_F_L0(RegExpTest, ParseError24)
350 {
351     RegExpParser parser = RegExpParser(thread, chunk_);
352     CString source("(");
353     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
354     parser.Parse();
355     bool parseResult = parser.IsError();
356     ASSERT_TRUE(parseResult);
357 }
358 
HWTEST_F_L0(RegExpTest,ParseError25)359 HWTEST_F_L0(RegExpTest, ParseError25)
360 {
361     RegExpParser parser = RegExpParser(thread, chunk_);
362     CString source(")");
363     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
364     parser.Parse();
365     bool parseResult = parser.IsError();
366     ASSERT_TRUE(parseResult);
367 }
368 
HWTEST_F_L0(RegExpTest,ParseError26)369 HWTEST_F_L0(RegExpTest, ParseError26)
370 {
371     RegExpParser parser = RegExpParser(thread, chunk_);
372     CString source("{");
373     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
374     parser.Parse();
375     bool parseResult = parser.IsError();
376     ASSERT_TRUE(parseResult);
377 }
378 
HWTEST_F_L0(RegExpTest,ParseError27)379 HWTEST_F_L0(RegExpTest, ParseError27)
380 {
381     RegExpParser parser = RegExpParser(thread, chunk_);
382     CString source("}");
383     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
384     parser.Parse();
385     bool parseResult = parser.IsError();
386     ASSERT_TRUE(parseResult);
387 }
388 
HWTEST_F_L0(RegExpTest,ParseError28)389 HWTEST_F_L0(RegExpTest, ParseError28)
390 {
391     RegExpParser parser = RegExpParser(thread, chunk_);
392     CString source("[");
393     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
394     parser.Parse();
395     bool parseResult = parser.IsError();
396     ASSERT_TRUE(parseResult);
397 }
398 
HWTEST_F_L0(RegExpTest,ParseError29)399 HWTEST_F_L0(RegExpTest, ParseError29)
400 {
401     RegExpParser parser = RegExpParser(thread, chunk_);
402     CString source("]");
403     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
404     parser.Parse();
405     bool parseResult = parser.IsError();
406     ASSERT_TRUE(parseResult);
407 }
408 
HWTEST_F_L0(RegExpTest,ParseError30)409 HWTEST_F_L0(RegExpTest, ParseError30)
410 {
411     RegExpParser parser = RegExpParser(thread, chunk_);
412     CString source("\\c");
413     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
414     parser.Parse();
415     bool parseResult = parser.IsError();
416     ASSERT_TRUE(parseResult);
417 }
418 
HWTEST_F_L0(RegExpTest,ParseError31)419 HWTEST_F_L0(RegExpTest, ParseError31)
420 {
421     RegExpParser parser = RegExpParser(thread, chunk_);
422     CString source("\\c\024");
423     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
424     parser.Parse();
425     bool parseResult = parser.IsError();
426     ASSERT_TRUE(parseResult);
427 }
428 
HWTEST_F_L0(RegExpTest,ParseError32)429 HWTEST_F_L0(RegExpTest, ParseError32)
430 {
431     RegExpParser parser = RegExpParser(thread, chunk_);
432     CString source("[\\c]");
433     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
434     parser.Parse();
435     bool parseResult = parser.IsError();
436     ASSERT_TRUE(parseResult);
437 }
438 
HWTEST_F_L0(RegExpTest,ParseError33)439 HWTEST_F_L0(RegExpTest, ParseError33)
440 {
441     RegExpParser parser = RegExpParser(thread, chunk_);
442     CString source("[\\c\024]");
443     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
444     parser.Parse();
445     bool parseResult = parser.IsError();
446     ASSERT_TRUE(parseResult);
447 }
448 
HWTEST_F_L0(RegExpTest,ParseError34)449 HWTEST_F_L0(RegExpTest, ParseError34)
450 {
451     RegExpParser parser = RegExpParser(thread, chunk_);
452     CString source("[\\d-a]");
453     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
454     parser.Parse();
455     bool parseResult = parser.IsError();
456     ASSERT_TRUE(parseResult);
457 }
458 
HWTEST_F_L0(RegExpTest,ParseError35)459 HWTEST_F_L0(RegExpTest, ParseError35)
460 {
461     RegExpParser parser = RegExpParser(thread, chunk_);
462     CString source("[\\s-a]");
463     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
464     parser.Parse();
465     bool parseResult = parser.IsError();
466     ASSERT_TRUE(parseResult);
467 }
468 
HWTEST_F_L0(RegExpTest,ParseError36)469 HWTEST_F_L0(RegExpTest, ParseError36)
470 {
471     RegExpParser parser = RegExpParser(thread, chunk_);
472     CString source("[\\s-\\w]");
473     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
474     parser.Parse();
475     bool parseResult = parser.IsError();
476     ASSERT_TRUE(parseResult);
477 }
478 
HWTEST_F_L0(RegExpTest,ParseError37)479 HWTEST_F_L0(RegExpTest, ParseError37)
480 {
481     RegExpParser parser = RegExpParser(thread, chunk_);
482     CString source("[a-\\w]");
483     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
484     parser.Parse();
485     bool parseResult = parser.IsError();
486     ASSERT_TRUE(parseResult);
487 }
488 
HWTEST_F_L0(RegExpTest,ParseError38)489 HWTEST_F_L0(RegExpTest, ParseError38)
490 {
491     RegExpParser parser = RegExpParser(thread, chunk_);
492     CString source("\\{");
493     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
494     parser.Parse();
495     bool parseResult = parser.IsError();
496     ASSERT_FALSE(parseResult);
497 }
498 
HWTEST_F_L0(RegExpTest,ParseError39)499 HWTEST_F_L0(RegExpTest, ParseError39)
500 {
501     RegExpParser parser = RegExpParser(thread, chunk_);
502     CString source("\\/");
503     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
504     parser.Parse();
505     bool parseResult = parser.IsError();
506     ASSERT_FALSE(parseResult);
507 }
508 
HWTEST_F_L0(RegExpTest,ParseError40)509 HWTEST_F_L0(RegExpTest, ParseError40)
510 {
511     for (char cu = 0x41; cu <= 0x5a; ++cu) {
512         if (!IsValidAlphaEscapeInAtom(cu)) {
513             CString source("\\");
514             source += CString(&cu, 1);
515             RegExpParser parser = RegExpParser(thread, chunk_);
516             parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
517             parser.Parse();
518             bool parseResult = parser.IsError();
519             ASSERT_TRUE(parseResult);
520         }
521     }
522     for (char cu = 0x61; cu <= 0x7a; ++cu) {
523         if (!IsValidAlphaEscapeInAtom(cu)) {
524             CString source("\\");
525             source += CString(&cu, 1);
526             RegExpParser parser = RegExpParser(thread, chunk_);
527             parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
528             parser.Parse();
529             bool parseResult = parser.IsError();
530             ASSERT_TRUE(parseResult);
531         }
532     }
533     for (char cu = 0x41; cu <= 0x5a; ++cu) {
534         CString source("[\\");
535         if (!IsValidAlphaEscapeInAtom(cu)) {
536             source += CString(&cu, 1);
537             source += CString("]");
538             RegExpParser parser = RegExpParser(thread, chunk_);
539             parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
540             parser.Parse();
541             bool parseResult = parser.IsError();
542             ASSERT_TRUE(parseResult);
543         }
544     }
545     for (char cu = 0x61; cu <= 0x7a; ++cu) {
546         CString source("[\\");
547         if (!IsValidAlphaEscapeInAtom(cu)) {
548             source += CString(&cu, 1);
549             source += CString("]");
550             RegExpParser parser = RegExpParser(thread, chunk_);
551             parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
552             parser.Parse();
553             bool parseResult = parser.IsError();
554             ASSERT_TRUE(parseResult);
555         }
556     }
557 }
558 
HWTEST_F_L0(RegExpTest,ParseError44)559 HWTEST_F_L0(RegExpTest, ParseError44)
560 {
561     RegExpParser parser = RegExpParser(thread, chunk_);
562     CString source("\\1");
563     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
564     parser.Parse();
565     bool parseResult = parser.IsError();
566     ASSERT_TRUE(parseResult);
567 }
568 
HWTEST_F_L0(RegExpTest,ParseError45)569 HWTEST_F_L0(RegExpTest, ParseError45)
570 {
571     RegExpParser parser = RegExpParser(thread, chunk_);
572     CString source("[\\1]");
573     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
574     parser.Parse();
575     bool parseResult = parser.IsError();
576     ASSERT_TRUE(parseResult);
577 }
578 
HWTEST_F_L0(RegExpTest,ParseError46)579 HWTEST_F_L0(RegExpTest, ParseError46)
580 {
581     RegExpParser parser = RegExpParser(thread, chunk_);
582     CString source("\\00");
583     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
584     parser.Parse();
585     bool parseResult = parser.IsError();
586     ASSERT_TRUE(parseResult);
587 }
588 
HWTEST_F_L0(RegExpTest,ParseError47)589 HWTEST_F_L0(RegExpTest, ParseError47)
590 {
591     RegExpParser parser = RegExpParser(thread, chunk_);
592     CString source("[\\00]");
593     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
594     parser.Parse();
595     bool parseResult = parser.IsError();
596     ASSERT_TRUE(parseResult);
597 }
598 
HWTEST_F_L0(RegExpTest,ParseError48)599 HWTEST_F_L0(RegExpTest, ParseError48)
600 {
601     RegExpParser parser = RegExpParser(thread, chunk_);
602     CString source("(+");
603     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
604     parser.Parse();
605     bool parseResult = parser.IsError();
606     ASSERT_TRUE(parseResult);
607 }
608 
HWTEST_F_L0(RegExpTest,ParseError49)609 HWTEST_F_L0(RegExpTest, ParseError49)
610 {
611     RegExpParser parser = RegExpParser(thread, chunk_);
612     CString source("/\\p{Line_Break=Glue}/u");
613     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
614     parser.Parse();
615     bool parseResult = parser.IsError();
616     ASSERT_TRUE(parseResult);
617 }
618 
HWTEST_F_L0(RegExpTest,ParseError50)619 HWTEST_F_L0(RegExpTest, ParseError50)
620 {
621     RegExpParser parser = RegExpParser(thread, chunk_);
622     CString source("/\\p{lb=AL}/u");
623     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
624     parser.Parse();
625     bool parseResult = parser.IsError();
626     ASSERT_TRUE(parseResult);
627 }
628 
HWTEST_F_L0(RegExpTest,ParseNoError1)629 HWTEST_F_L0(RegExpTest, ParseNoError1)
630 {
631     RegExpParser parser = RegExpParser(thread, chunk_);
632     CString source("a{10,2147483648}"); // 2^31
633     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
634     parser.Parse();
635     bool parseResult = parser.IsError();
636     ASSERT_FALSE(parseResult);
637 }
638 
HWTEST_F_L0(RegExpTest,ParseNoError2)639 HWTEST_F_L0(RegExpTest, ParseNoError2)
640 {
641     RegExpParser parser = RegExpParser(thread, chunk_);
642     CString source("a{10,4294967306}"); // 2^32+10
643     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
644     parser.Parse();
645     bool parseResult = parser.IsError();
646     ASSERT_FALSE(parseResult);
647 }
648 
HWTEST_F_L0(RegExpTest,ParseNoError3)649 HWTEST_F_L0(RegExpTest, ParseNoError3)
650 {
651     RegExpParser parser = RegExpParser(thread, chunk_);
652     CString source("[\\⥚]");
653     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 1);
654     parser.Parse();
655     bool parseResult = parser.IsError();
656     ASSERT_FALSE(parseResult);
657 }
658 
HWTEST_F_L0(RegExpTest,ParseNoError4)659 HWTEST_F_L0(RegExpTest, ParseNoError4)
660 {
661     RegExpParser parser = RegExpParser(thread, chunk_);
662     CString source("[\\⊲|\\⇐]");
663     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 1);
664     parser.Parse();
665     bool parseResult = parser.IsError();
666     ASSERT_FALSE(parseResult);
667 }
668 
HWTEST_F_L0(RegExpTest,ParseNoError5)669 HWTEST_F_L0(RegExpTest, ParseNoError5)
670 {
671     RegExpParser parser = RegExpParser(thread, chunk_);
672     CString source("/\\p{General_Category=Letter}/u");
673     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 1);
674     parser.Parse();
675     bool parseResult = parser.IsError();
676     ASSERT_FALSE(parseResult);
677 }
678 
HWTEST_F_L0(RegExpTest,ParseNoError6)679 HWTEST_F_L0(RegExpTest, ParseNoError6)
680 {
681     RegExpParser parser = RegExpParser(thread, chunk_);
682     CString source("/\\p{gc=L}/u");
683     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 1);
684     parser.Parse();
685     bool parseResult = parser.IsError();
686     ASSERT_FALSE(parseResult);
687 }
688 
HWTEST_F_L0(RegExpTest,ParseAndExec1)689 HWTEST_F_L0(RegExpTest, ParseAndExec1)
690 {
691     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
692     RegExpParser parser = RegExpParser(thread, chunk_);
693     CString source("ab");
694     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
695     parser.Parse();
696     bool parseResult = parser.IsError();
697     ASSERT_FALSE(parseResult);
698 
699     RegExpExecutor executor(regExpCachedChunk_);
700     CString input("abc");
701     bool ret =
702         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
703     ASSERT_TRUE(ret);
704 
705     executor.GetResult(thread);
706     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
707     JSHandle<EcmaString> inputStr = factory->NewFromASCII("abc");
708     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
709     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
710     JSHandle<EcmaString> str = factory->NewFromASCII("ab");
711     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
712 }
713 
HWTEST_F_L0(RegExpTest,ParseAndExec2)714 HWTEST_F_L0(RegExpTest, ParseAndExec2)
715 {
716     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
717     RegExpParser parser = RegExpParser(thread, chunk_);
718     CString source("(((ab)|(cd)|(de))|((ef)|(gh)|(jk)))");
719     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
720     parser.Parse();
721     bool parseResult = parser.IsError();
722     ASSERT_FALSE(parseResult);
723 
724     RegExpExecutor executor(regExpCachedChunk_);
725     CString input("cabd");
726     bool ret =
727         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
728     ASSERT_TRUE(ret);
729 
730     executor.GetResult(thread);
731     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
732     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 10U);
733     JSHandle<EcmaString> inputStr = factory->NewFromASCII("cabd");
734     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
735     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
736     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
737     JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
738     JSHandle<EcmaString> str = factory->NewFromASCII("ab");
739     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str) == 0);
740     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str) == 0);
741     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str) == 0);
742     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str) == 0);
743     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(4).GetInt(), -1);
744     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(5).GetInt(), -1);
745     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(6).GetInt(), -1);
746     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(7).GetInt(), -1);
747     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(8).GetInt(), -1);
748     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(9).GetInt(), -1);
749 }
750 
HWTEST_F_L0(RegExpTest,ParseAndExec3)751 HWTEST_F_L0(RegExpTest, ParseAndExec3)
752 {
753     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
754     RegExpParser parser = RegExpParser(thread, chunk_);
755     CString source("(aa|aabaac|ba|b|c)*");
756     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
757     parser.Parse();
758     bool parseResult = parser.IsError();
759     ASSERT_FALSE(parseResult);
760 
761     RegExpExecutor executor(regExpCachedChunk_);
762     CString input("aabaac");
763     bool ret =
764         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
765     ASSERT_TRUE(ret);
766 
767     executor.GetResult(thread);
768     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
769     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
770     JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabaac");
771     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
772     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
773     JSHandle<EcmaString> str1 = factory->NewFromASCII("aaba");
774     JSHandle<EcmaString> str2 = factory->NewFromASCII("ba");
775     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str1) == 0);
776     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str2) == 0);
777 }
778 
HWTEST_F_L0(RegExpTest,ParseAndExec4)779 HWTEST_F_L0(RegExpTest, ParseAndExec4)
780 {
781     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
782     RegExpParser parser = RegExpParser(thread, chunk_);
783     CString source("a*");
784     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
785     parser.Parse();
786     bool parseResult = parser.IsError();
787     ASSERT_FALSE(parseResult);
788 
789     RegExpExecutor executor(regExpCachedChunk_);
790     CString input("aabaac");
791     bool ret =
792         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
793     ASSERT_TRUE(ret);
794 
795     executor.GetResult(thread);
796     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
797     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
798     JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabaac");
799     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
800     JSHandle<EcmaString> str = factory->NewFromASCII("aa");
801     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
802 }
803 
HWTEST_F_L0(RegExpTest,ParseAndExec5)804 HWTEST_F_L0(RegExpTest, ParseAndExec5)
805 {
806     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
807     RegExpParser parser = RegExpParser(thread, chunk_);
808     CString source("a?");
809     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
810     parser.Parse();
811     bool parseResult = parser.IsError();
812     ASSERT_FALSE(parseResult);
813 
814     RegExpExecutor executor(regExpCachedChunk_);
815     CString input("b");
816     bool ret =
817         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
818     ASSERT_TRUE(ret);
819 
820     executor.GetResult(thread);
821     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
822     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
823     JSHandle<EcmaString> inputStr = factory->NewFromASCII("b");
824     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
825     JSHandle<EcmaString> str = factory->NewFromASCII("");
826     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
827 }
828 
HWTEST_F_L0(RegExpTest,ParseAndExec6)829 HWTEST_F_L0(RegExpTest, ParseAndExec6)
830 {
831     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
832     RegExpParser parser = RegExpParser(thread, chunk_);
833     CString source("(z)((a+)?(b+)?(c))*");
834     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
835     parser.Parse();
836     bool parseResult = parser.IsError();
837     ASSERT_FALSE(parseResult);
838 
839     RegExpExecutor executor(regExpCachedChunk_);
840     CString input("zaacbbbcac");
841     bool ret =
842         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
843     ASSERT_TRUE(ret);
844 
845     executor.GetResult(thread);
846     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
847     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 6U);
848     JSHandle<EcmaString> inputStr = factory->NewFromASCII("zaacbbbcac");
849     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
850     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
851     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
852     JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
853     JSHandle<EcmaString> resultStr5 = GetSubString(regExpGlobalResult, inputStr, 5);
854     JSHandle<EcmaString> str0 = factory->NewFromASCII("zaacbbbcac");
855     JSHandle<EcmaString> str1 = factory->NewFromASCII("z");
856     JSHandle<EcmaString> str2 = factory->NewFromASCII("ac");
857     JSHandle<EcmaString> str3 = factory->NewFromASCII("a");
858     JSHandle<EcmaString> str5 = factory->NewFromASCII("c");
859     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
860     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
861     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
862     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
863     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(4).GetInt(), -1);
864     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr5, str5) == 0);
865 }
866 
HWTEST_F_L0(RegExpTest,ParseAndExec7)867 HWTEST_F_L0(RegExpTest, ParseAndExec7)
868 {
869     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
870     RegExpParser parser = RegExpParser(thread, chunk_);
871     CString source("^abc");
872     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
873     parser.Parse();
874     bool parseResult = parser.IsError();
875     ASSERT_FALSE(parseResult);
876 
877     RegExpExecutor executor(regExpCachedChunk_);
878     CString input("ab\nabc");
879     bool ret =
880         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
881     ASSERT_TRUE(ret);
882 
883     executor.GetResult(thread);
884     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
885     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
886     JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab\nabc");
887     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
888     JSHandle<EcmaString> str = factory->NewFromASCII("abc");
889     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
890 }
891 
HWTEST_F_L0(RegExpTest,ParseAndExec8)892 HWTEST_F_L0(RegExpTest, ParseAndExec8)
893 {
894     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
895     RegExpParser parser = RegExpParser(thread, chunk_);
896     CString source("abc$");
897     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
898     parser.Parse();
899     bool parseResult = parser.IsError();
900     ASSERT_FALSE(parseResult);
901 
902     RegExpExecutor executor(regExpCachedChunk_);
903     CString input("ab\nabc");
904     bool ret =
905         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
906     ASSERT_TRUE(ret);
907 
908     executor.GetResult(thread);
909     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
910     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
911     JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab\nabc");
912     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
913     JSHandle<EcmaString> str = factory->NewFromASCII("abc");
914     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
915 }
916 
HWTEST_F_L0(RegExpTest,ParseAndExec9)917 HWTEST_F_L0(RegExpTest, ParseAndExec9)
918 {
919     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
920     RegExpParser parser = RegExpParser(thread, chunk_);
921     CString source("er\\B");
922     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
923     parser.Parse();
924     bool parseResult = parser.IsError();
925     ASSERT_FALSE(parseResult);
926 
927     RegExpExecutor executor(regExpCachedChunk_);
928     CString input("erv");
929     bool ret =
930         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
931     ASSERT_TRUE(ret);
932 
933     executor.GetResult(thread);
934     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
935     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
936     JSHandle<EcmaString> inputStr = factory->NewFromASCII("erv");
937     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
938     JSHandle<EcmaString> str = factory->NewFromASCII("er");
939     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
940 }
941 
HWTEST_F_L0(RegExpTest,ParseAndExec10)942 HWTEST_F_L0(RegExpTest, ParseAndExec10)
943 {
944     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
945     RegExpParser parser = RegExpParser(thread, chunk_);
946     CString source("d\\b");
947     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
948     parser.Parse();
949     bool parseResult = parser.IsError();
950     ASSERT_FALSE(parseResult);
951 
952     RegExpExecutor executor(regExpCachedChunk_);
953     CString input("bad good");
954     bool ret =
955         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
956     ASSERT_TRUE(ret);
957 
958     executor.GetResult(thread);
959     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
960     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
961     JSHandle<EcmaString> inputStr = factory->NewFromASCII("bad good");
962     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
963     JSHandle<EcmaString> str = factory->NewFromASCII("d");
964     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
965 }
966 
HWTEST_F_L0(RegExpTest,ParseAndExec11)967 HWTEST_F_L0(RegExpTest, ParseAndExec11)
968 {
969     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
970     RegExpParser parser = RegExpParser(thread, chunk_);
971     CString source(".");
972     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
973     parser.Parse();
974     bool parseResult = parser.IsError();
975     ASSERT_FALSE(parseResult);
976 
977     RegExpExecutor executor(regExpCachedChunk_);
978     CString input("\na");
979     bool ret =
980         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
981     ASSERT_TRUE(ret);
982 
983     executor.GetResult(thread);
984     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
985     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
986     JSHandle<EcmaString> inputStr = factory->NewFromASCII("\na");
987     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
988     JSHandle<EcmaString> str = factory->NewFromASCII("a");
989     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
990 }
991 
HWTEST_F_L0(RegExpTest,ParseAndExec12)992 HWTEST_F_L0(RegExpTest, ParseAndExec12)
993 {
994     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
995     RegExpParser parser = RegExpParser(thread, chunk_);
996     CString source(".");
997     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 8);
998     parser.Parse();
999     bool parseResult = parser.IsError();
1000     ASSERT_FALSE(parseResult);
1001 
1002     RegExpExecutor executor(regExpCachedChunk_);
1003     CString input("\n");
1004     bool ret =
1005         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1006     ASSERT_TRUE(ret);
1007 
1008     executor.GetResult(thread);
1009     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1010     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1011     JSHandle<EcmaString> inputStr = factory->NewFromASCII("\n");
1012     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1013     JSHandle<EcmaString> str = factory->NewFromASCII("\n");
1014     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1015 }
1016 
HWTEST_F_L0(RegExpTest,ParseAndExec13)1017 HWTEST_F_L0(RegExpTest, ParseAndExec13)
1018 {
1019     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1020     RegExpParser parser = RegExpParser(thread, chunk_);
1021     CString source("abc");
1022     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
1023     parser.Parse();
1024     bool parseResult = parser.IsError();
1025     ASSERT_FALSE(parseResult);
1026 
1027     RegExpExecutor executor(regExpCachedChunk_);
1028     CString input("\naabc");
1029     bool ret =
1030         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1031     ASSERT_TRUE(ret);
1032 
1033     executor.GetResult(thread);
1034     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1035     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1036     JSHandle<EcmaString> inputStr = factory->NewFromASCII("\naabc");
1037     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1038     JSHandle<EcmaString> str = factory->NewFromASCII("abc");
1039     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1040 }
1041 
HWTEST_F_L0(RegExpTest,ParseAndExec14)1042 HWTEST_F_L0(RegExpTest, ParseAndExec14)
1043 {
1044     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1045     RegExpParser parser = RegExpParser(thread, chunk_);
1046     CString source("abc");
1047     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
1048     parser.Parse();
1049     bool parseResult = parser.IsError();
1050     ASSERT_FALSE(parseResult);
1051 
1052     RegExpExecutor executor(regExpCachedChunk_);
1053     CString input("\nbbabc");
1054     bool ret =
1055         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1056     ASSERT_TRUE(ret);
1057 
1058     executor.GetResult(thread);
1059     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1060     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1061     JSHandle<EcmaString> inputStr = factory->NewFromASCII("\nbbabc");
1062     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1063     JSHandle<EcmaString> str = factory->NewFromASCII("abc");
1064     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1065 }
1066 
HWTEST_F_L0(RegExpTest,ParseAndExec15)1067 HWTEST_F_L0(RegExpTest, ParseAndExec15)
1068 {
1069     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1070     RegExpParser parser = RegExpParser(thread, chunk_);
1071     CString source("a(?=a)");
1072     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1073     parser.Parse();
1074     bool parseResult = parser.IsError();
1075     ASSERT_FALSE(parseResult);
1076 
1077     RegExpExecutor executor(regExpCachedChunk_);
1078     CString input("aabc");
1079     bool ret =
1080         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1081     ASSERT_TRUE(ret);
1082 
1083     executor.GetResult(thread);
1084     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1085     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1086     JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabc");
1087     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1088     JSHandle<EcmaString> str = factory->NewFromASCII("a");
1089     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1090 }
1091 
HWTEST_F_L0(RegExpTest,ParseAndExec16)1092 HWTEST_F_L0(RegExpTest, ParseAndExec16)
1093 {
1094     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1095     RegExpParser parser = RegExpParser(thread, chunk_);
1096     CString source("abc");
1097     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1098     parser.Parse();
1099     bool parseResult = parser.IsError();
1100     ASSERT_FALSE(parseResult);
1101 
1102     RegExpExecutor executor(regExpCachedChunk_);
1103     CString input("ABC");
1104     bool ret =
1105         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1106     ASSERT_TRUE(ret);
1107 
1108     executor.GetResult(thread);
1109     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1110     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1111     JSHandle<EcmaString> inputStr = factory->NewFromASCII("ABC");
1112     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1113     JSHandle<EcmaString> str = factory->NewFromASCII("ABC");
1114     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1115 }
1116 
HWTEST_F_L0(RegExpTest,ParseAndExec17)1117 HWTEST_F_L0(RegExpTest, ParseAndExec17)
1118 {
1119     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1120     RegExpParser parser = RegExpParser(thread, chunk_);
1121     CString source("a\\n");
1122     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1123     parser.Parse();
1124     bool parseResult = parser.IsError();
1125     ASSERT_FALSE(parseResult);
1126 
1127     RegExpExecutor executor(regExpCachedChunk_);
1128     CString input("a\n");
1129     bool ret =
1130         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1131     ASSERT_TRUE(ret);
1132 
1133     executor.GetResult(thread);
1134     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1135     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1136     JSHandle<EcmaString> inputStr = factory->NewFromASCII("a\n");
1137     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1138     JSHandle<EcmaString> str = factory->NewFromASCII("a\n");
1139     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1140 }
1141 
HWTEST_F_L0(RegExpTest,ParseAndExec18)1142 HWTEST_F_L0(RegExpTest, ParseAndExec18)
1143 {
1144     RegExpParser parser = RegExpParser(thread, chunk_);
1145     CString source("a(?=a)");
1146     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1147     parser.Parse();
1148     bool parseResult = parser.IsError();
1149     ASSERT_FALSE(parseResult);
1150 
1151     RegExpExecutor executor(regExpCachedChunk_);
1152     CString input("ababc");
1153     bool ret =
1154         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1155     ASSERT_FALSE(ret);
1156 }
1157 
HWTEST_F_L0(RegExpTest,ParseAndExec19)1158 HWTEST_F_L0(RegExpTest, ParseAndExec19)
1159 {
1160     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1161     RegExpParser parser = RegExpParser(thread, chunk_);
1162     CString source("a(?!a)");
1163     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1164     parser.Parse();
1165     bool parseResult = parser.IsError();
1166     ASSERT_FALSE(parseResult);
1167 
1168     RegExpExecutor executor(regExpCachedChunk_);
1169     CString input("ababc");
1170     bool ret =
1171         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1172     ASSERT_TRUE(ret);
1173 
1174     executor.GetResult(thread);
1175     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1176     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1177     JSHandle<EcmaString> inputStr = factory->NewFromASCII("ababc");
1178     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1179     JSHandle<EcmaString> str = factory->NewFromASCII("a");
1180     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1181 }
1182 
HWTEST_F_L0(RegExpTest,ParseAndExec20)1183 HWTEST_F_L0(RegExpTest, ParseAndExec20)
1184 {
1185     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1186     RegExpParser parser = RegExpParser(thread, chunk_);
1187     CString source("(?=(a+))");
1188     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1189     parser.Parse();
1190     bool parseResult = parser.IsError();
1191     ASSERT_FALSE(parseResult);
1192 
1193     RegExpExecutor executor(regExpCachedChunk_);
1194     CString input("baaabac");
1195     bool ret =
1196         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1197     ASSERT_TRUE(ret);
1198 
1199     executor.GetResult(thread);
1200     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1201     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1202     JSHandle<EcmaString> inputStr = factory->NewFromASCII("baaabac");
1203     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1204     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1205     JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1206     JSHandle<EcmaString> str1 = factory->NewFromASCII("aaa");
1207     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1208     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1209 }
1210 
HWTEST_F_L0(RegExpTest,ParseAndExec21)1211 HWTEST_F_L0(RegExpTest, ParseAndExec21)
1212 {
1213     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1214     RegExpParser parser = RegExpParser(thread, chunk_);
1215     CString source("a(?=a(?=b))");
1216     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1217     parser.Parse();
1218     bool parseResult = parser.IsError();
1219     ASSERT_FALSE(parseResult);
1220 
1221     RegExpExecutor executor(regExpCachedChunk_);
1222     CString input("caab");
1223     bool ret =
1224         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1225     ASSERT_TRUE(ret);
1226 
1227     executor.GetResult(thread);
1228     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1229     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1230     JSHandle<EcmaString> inputStr = factory->NewFromASCII("caab");
1231     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1232     JSHandle<EcmaString> str = factory->NewFromASCII("a");
1233     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1234 }
1235 
HWTEST_F_L0(RegExpTest,ParseAndExec22)1236 HWTEST_F_L0(RegExpTest, ParseAndExec22)
1237 {
1238     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1239     RegExpParser parser = RegExpParser(thread, chunk_);
1240     CString source(".+:");
1241     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1242     parser.Parse();
1243     bool parseResult = parser.IsError();
1244     ASSERT_FALSE(parseResult);
1245 
1246     RegExpExecutor executor(regExpCachedChunk_);
1247     CString input("aaaa:aa");
1248     bool ret =
1249         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1250     ASSERT_TRUE(ret);
1251 
1252     executor.GetResult(thread);
1253     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1254     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1255     JSHandle<EcmaString> inputStr = factory->NewFromASCII("aaaa:aa");
1256     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1257     JSHandle<EcmaString> str = factory->NewFromASCII("aaaa:");
1258     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1259 }
1260 
HWTEST_F_L0(RegExpTest,ParseAndExec23)1261 HWTEST_F_L0(RegExpTest, ParseAndExec23)
1262 {
1263     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1264     RegExpParser parser = RegExpParser(thread, chunk_);
1265     CString source("a(?<=a(?<!b))");
1266     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1267     parser.Parse();
1268     bool parseResult = parser.IsError();
1269     ASSERT_FALSE(parseResult);
1270 
1271     RegExpExecutor executor(regExpCachedChunk_);
1272     CString input("caab");
1273     bool ret =
1274         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1275     ASSERT_TRUE(ret);
1276 
1277     executor.GetResult(thread);
1278     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1279     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1280     JSHandle<EcmaString> inputStr = factory->NewFromASCII("caab");
1281     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1282     JSHandle<EcmaString> str = factory->NewFromASCII("a");
1283     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1284 }
1285 
HWTEST_F_L0(RegExpTest,ParseAndExec24)1286 HWTEST_F_L0(RegExpTest, ParseAndExec24)
1287 {
1288     RegExpParser parser = RegExpParser(thread, chunk_);
1289     CString source("a(?<=ab(?<!bc))");
1290     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1291     parser.Parse();
1292     bool parseResult = parser.IsError();
1293     ASSERT_FALSE(parseResult);
1294 
1295     RegExpExecutor executor(regExpCachedChunk_);
1296     CString input("caab");
1297     bool ret =
1298         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1299     ASSERT_FALSE(ret);
1300 }
1301 
HWTEST_F_L0(RegExpTest,ParseAndExec25)1302 HWTEST_F_L0(RegExpTest, ParseAndExec25)
1303 {
1304     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1305     RegExpParser parser = RegExpParser(thread, chunk_);
1306     CString source("(?<=(ab))");
1307     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1308     parser.Parse();
1309     bool parseResult = parser.IsError();
1310     ASSERT_FALSE(parseResult);
1311 
1312     RegExpExecutor executor(regExpCachedChunk_);
1313     CString input("cabab");
1314     bool ret =
1315         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1316     ASSERT_TRUE(ret);
1317 
1318     executor.GetResult(thread);
1319     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1320     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1321     JSHandle<EcmaString> inputStr = factory->NewFromASCII("cabab");
1322     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1323     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1324     JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1325     JSHandle<EcmaString> str1 = factory->NewFromASCII("ab");
1326     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1327     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1328 }
1329 
HWTEST_F_L0(RegExpTest,ParseAndExec26)1330 HWTEST_F_L0(RegExpTest, ParseAndExec26)
1331 {
1332     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1333     RegExpParser parser = RegExpParser(thread, chunk_);
1334     CString source("[a-z]");
1335     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1336     parser.Parse();
1337     bool parseResult = parser.IsError();
1338     ASSERT_FALSE(parseResult);
1339 
1340     RegExpExecutor executor(regExpCachedChunk_);
1341     CString input("A");
1342     bool ret =
1343         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1344     ASSERT_TRUE(ret);
1345 
1346     executor.GetResult(thread);
1347     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1348     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1349     JSHandle<EcmaString> inputStr = factory->NewFromASCII("A");
1350     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1351     JSHandle<EcmaString> str = factory->NewFromASCII("A");
1352     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1353 }
1354 
HWTEST_F_L0(RegExpTest,ParseAndExec27)1355 HWTEST_F_L0(RegExpTest, ParseAndExec27)
1356 {
1357     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1358     RegExpParser parser = RegExpParser(thread, chunk_);
1359     CString source("[^a-b]");
1360     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1361     parser.Parse();
1362     bool parseResult = parser.IsError();
1363     ASSERT_FALSE(parseResult);
1364 
1365     RegExpExecutor executor(regExpCachedChunk_);
1366     CString input("Z");
1367     bool ret =
1368         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1369     ASSERT_TRUE(ret);
1370 
1371     executor.GetResult(thread);
1372     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1373     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1374     JSHandle<EcmaString> inputStr = factory->NewFromASCII("Z");
1375     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1376     JSHandle<EcmaString> str = factory->NewFromASCII("Z");
1377     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1378 }
1379 
HWTEST_F_L0(RegExpTest,ParseAndExec28)1380 HWTEST_F_L0(RegExpTest, ParseAndExec28)
1381 {
1382     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1383     RegExpParser parser = RegExpParser(thread, chunk_);
1384     CString source("\\s");
1385     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1386     parser.Parse();
1387     bool parseResult = parser.IsError();
1388     ASSERT_FALSE(parseResult);
1389 
1390     RegExpExecutor executor(regExpCachedChunk_);
1391     CString input("\n");
1392     bool ret =
1393         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1394     ASSERT_TRUE(ret);
1395 
1396     executor.GetResult(thread);
1397     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1398     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1399     JSHandle<EcmaString> inputStr = factory->NewFromASCII("\n");
1400     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1401     JSHandle<EcmaString> str = factory->NewFromASCII("\n");
1402     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1403 }
1404 
HWTEST_F_L0(RegExpTest,ParseAndExec29)1405 HWTEST_F_L0(RegExpTest, ParseAndExec29)
1406 {
1407     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1408     RegExpParser parser = RegExpParser(thread, chunk_);
1409     CString source("()|");
1410     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1411     parser.Parse();
1412     bool parseResult = parser.IsError();
1413     ASSERT_FALSE(parseResult);
1414 
1415     RegExpExecutor executor(regExpCachedChunk_);
1416     CString input("");
1417     bool ret =
1418         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1419     ASSERT_TRUE(ret);
1420 
1421     executor.GetResult(thread);
1422     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1423     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1424     JSHandle<EcmaString> inputStr = factory->NewFromASCII("");
1425     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1426     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1427     JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1428     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1429     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str0) == 0);
1430 }
1431 
HWTEST_F_L0(RegExpTest,ParseAndExec30)1432 HWTEST_F_L0(RegExpTest, ParseAndExec30)
1433 {
1434     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1435     RegExpParser parser = RegExpParser(thread, chunk_);
1436     CString source("|()");
1437     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1438     parser.Parse();
1439     bool parseResult = parser.IsError();
1440     ASSERT_FALSE(parseResult);
1441 
1442     RegExpExecutor executor(regExpCachedChunk_);
1443     CString input("");
1444     bool ret =
1445         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1446     ASSERT_TRUE(ret);
1447 
1448     executor.GetResult(thread);
1449     JSHandle<builtins::RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1450     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1451     JSHandle<EcmaString> inputStr = factory->NewFromASCII("");
1452     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1453     JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1454     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1455     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(1).GetInt(), -1);
1456 }
1457 
HWTEST_F_L0(RegExpTest,ParseAndExec31)1458 HWTEST_F_L0(RegExpTest, ParseAndExec31)
1459 {
1460     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1461     RegExpParser parser = RegExpParser(thread, chunk_);
1462     CString source("a(a|b)\\1");
1463     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1464     parser.Parse();
1465     bool parseResult = parser.IsError();
1466     ASSERT_FALSE(parseResult);
1467     RegExpExecutor executor(regExpCachedChunk_);
1468     CString input("aabb");
1469     bool ret =
1470         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1471     ASSERT_TRUE(ret);
1472     executor.GetResult(thread);
1473     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1474     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1475     JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabb");
1476     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1477     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1478     JSHandle<EcmaString> str0 = factory->NewFromASCII("abb");
1479     JSHandle<EcmaString> str1 = factory->NewFromASCII("b");
1480     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1481     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1482 }
1483 
HWTEST_F_L0(RegExpTest,ParseAndExec32)1484 HWTEST_F_L0(RegExpTest, ParseAndExec32)
1485 {
1486     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1487     RegExpParser parser = RegExpParser(thread, chunk_);
1488     CString source("(a(a|b))\\2");
1489     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1490     parser.Parse();
1491     bool parseResult = parser.IsError();
1492     ASSERT_FALSE(parseResult);
1493     RegExpExecutor executor(regExpCachedChunk_);
1494     CString input("aabb");
1495     bool ret =
1496         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1497     ASSERT_TRUE(ret);
1498     executor.GetResult(thread);
1499     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1500     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1501     JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabb");
1502     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1503     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1504     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1505     JSHandle<EcmaString> str0 = factory->NewFromASCII("abb");
1506     JSHandle<EcmaString> str1 = factory->NewFromASCII("ab");
1507     JSHandle<EcmaString> str2 = factory->NewFromASCII("b");
1508     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1509     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1510     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1511 }
1512 
HWTEST_F_L0(RegExpTest,ParseAndExec33)1513 HWTEST_F_L0(RegExpTest, ParseAndExec33)
1514 {
1515     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1516     RegExpParser parser = RegExpParser(thread, chunk_);
1517     CString source("qya+");
1518     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1519     parser.Parse();
1520     bool parseResult = parser.IsError();
1521     ASSERT_FALSE(parseResult);
1522     RegExpExecutor executor(regExpCachedChunk_);
1523     CString input("qyqya");
1524     bool ret =
1525         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1526     ASSERT_TRUE(ret);
1527 
1528     executor.GetResult(thread);
1529     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1530     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1531     JSHandle<EcmaString> inputStr = factory->NewFromASCII("qyqya");
1532     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1533     JSHandle<EcmaString> str = factory->NewFromASCII("qya");
1534     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1535 }
1536 
HWTEST_F_L0(RegExpTest,ParseAndExec34)1537 HWTEST_F_L0(RegExpTest, ParseAndExec34)
1538 {
1539     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1540     RegExpParser parser = RegExpParser(thread, chunk_);
1541     CString source("qy(?=\\s+)");
1542     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1543     parser.Parse();
1544     bool parseResult = parser.IsError();
1545     ASSERT_FALSE(parseResult);
1546     RegExpExecutor executor(regExpCachedChunk_);
1547     CString input("qyqy ");
1548     bool ret =
1549         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1550     ASSERT_TRUE(ret);
1551     executor.GetResult(thread);
1552     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1553     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1554     JSHandle<EcmaString> inputStr = factory->NewFromASCII("qyqy ");
1555     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1556     JSHandle<EcmaString> str = factory->NewFromASCII("qy");
1557     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1558 }
1559 
HWTEST_F_L0(RegExpTest,ParseAndExec35)1560 HWTEST_F_L0(RegExpTest, ParseAndExec35)
1561 {
1562     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1563     RegExpParser parser = RegExpParser(thread, chunk_);
1564     CString source("(\\d{4})-(\\d{2})-(\\d{2})");
1565     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1566     parser.Parse();
1567     bool parseResult = parser.IsError();
1568     ASSERT_FALSE(parseResult);
1569     RegExpExecutor executor(regExpCachedChunk_);
1570     CString input("xx2021-01-09");
1571     bool ret =
1572         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1573     ASSERT_TRUE(ret);
1574     executor.GetResult(thread);
1575     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1576     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 4U);
1577     JSHandle<EcmaString> inputStr = factory->NewFromASCII("xx2021-01-09");
1578     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1579     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1580     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1581     JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1582     JSHandle<EcmaString> str0 = factory->NewFromASCII("2021-01-09");
1583     JSHandle<EcmaString> str1 = factory->NewFromASCII("2021");
1584     JSHandle<EcmaString> str2 = factory->NewFromASCII("01");
1585     JSHandle<EcmaString> str3 = factory->NewFromASCII("09");
1586     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1587     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1588     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1589     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1590 }
1591 
HWTEST_F_L0(RegExpTest,ParseAndExec36)1592 HWTEST_F_L0(RegExpTest, ParseAndExec36)
1593 {
1594     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1595     RegExpParser parser = RegExpParser(thread, chunk_);
1596     CString source("quick\\s(brown).+?(jumps)");
1597     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1598     parser.Parse();
1599     bool parseResult = parser.IsError();
1600     ASSERT_FALSE(parseResult);
1601     RegExpExecutor executor(regExpCachedChunk_);
1602     CString input("The Quick Brown Fox Jumps Over The Lazy Dog");
1603     bool ret =
1604         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1605     ASSERT_TRUE(ret);
1606     executor.GetResult(thread);
1607     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1608     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1609     JSHandle<EcmaString> inputStr = factory->NewFromASCII("The Quick Brown Fox Jumps Over The Lazy Dog");
1610     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1611     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1612     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1613     JSHandle<EcmaString> str0 = factory->NewFromASCII("Quick Brown Fox Jumps");
1614     JSHandle<EcmaString> str1 = factory->NewFromASCII("Brown");
1615     JSHandle<EcmaString> str2 = factory->NewFromASCII("Jumps");
1616     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1617     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1618     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1619 }
1620 
HWTEST_F_L0(RegExpTest,ParseAndExec37)1621 HWTEST_F_L0(RegExpTest, ParseAndExec37)
1622 {
1623     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1624     RegExpParser parser = RegExpParser(thread, chunk_);
1625     CString source("(ab){1,2}?c");
1626     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1627     parser.Parse();
1628     bool parseResult = parser.IsError();
1629     ASSERT_FALSE(parseResult);
1630     RegExpExecutor executor(regExpCachedChunk_);
1631     CString input("abABc");
1632     bool ret =
1633         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1634     ASSERT_TRUE(ret);
1635     executor.GetResult(thread);
1636     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1637     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1638     JSHandle<EcmaString> inputStr = factory->NewFromASCII("abABc");
1639     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1640     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1641     JSHandle<EcmaString> str0 = factory->NewFromASCII("abABc");
1642     JSHandle<EcmaString> str1 = factory->NewFromASCII("AB");
1643     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1644     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1645 }
1646 
HWTEST_F_L0(RegExpTest,ParseAndExec38)1647 HWTEST_F_L0(RegExpTest, ParseAndExec38)
1648 {
1649     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1650     RegExpParser parser = RegExpParser(thread, chunk_);
1651     CString source("^(([a-z]+)*[a-z]\\.)+[a-z]{2,}$");
1652     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1653     parser.Parse();
1654     bool parseResult = parser.IsError();
1655     ASSERT_FALSE(parseResult);
1656     RegExpExecutor executor(regExpCachedChunk_);
1657     CString input("www.netscape.com");
1658     bool ret =
1659         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1660     ASSERT_TRUE(ret);
1661     executor.GetResult(thread);
1662     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1663     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1664     JSHandle<EcmaString> inputStr = factory->NewFromASCII("www.netscape.com");
1665     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1666     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1667     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1668     JSHandle<EcmaString> str0 = factory->NewFromASCII("www.netscape.com");
1669     JSHandle<EcmaString> str1 = factory->NewFromASCII("netscape.");
1670     JSHandle<EcmaString> str2 = factory->NewFromASCII("netscap");
1671     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1672     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1673     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1674 }
1675 
HWTEST_F_L0(RegExpTest,ParseAndExec39)1676 HWTEST_F_L0(RegExpTest, ParseAndExec39)
1677 {
1678     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1679     RegExpParser parser = RegExpParser(thread, chunk_);
1680     CString source("(a*)b\\1+");
1681     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1682     parser.Parse();
1683     bool parseResult = parser.IsError();
1684     ASSERT_FALSE(parseResult);
1685     RegExpExecutor executor(regExpCachedChunk_);
1686     CString input("baaaac");
1687     bool ret =
1688         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1689     ASSERT_TRUE(ret);
1690     executor.GetResult(thread);
1691     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1692     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1693     JSHandle<EcmaString> inputStr = factory->NewFromASCII("baaaac");
1694     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1695     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1696     JSHandle<EcmaString> str0 = factory->NewFromASCII("b");
1697     JSHandle<EcmaString> str1 = factory->NewFromASCII("");
1698     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1699     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1700 }
1701 
HWTEST_F_L0(RegExpTest,ParseAndExec40)1702 HWTEST_F_L0(RegExpTest, ParseAndExec40)
1703 {
1704     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1705     RegExpParser parser = RegExpParser(thread, chunk_);
1706     CString source("a*?");
1707     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1708     parser.Parse();
1709     bool parseResult = parser.IsError();
1710     ASSERT_FALSE(parseResult);
1711     RegExpExecutor executor(regExpCachedChunk_);
1712     CString input("ab");
1713     bool ret =
1714         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1715     ASSERT_TRUE(ret);
1716     executor.GetResult(thread);
1717     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1718     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1719     JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab");
1720     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1721     JSHandle<EcmaString> str = factory->NewFromASCII("");
1722     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1723 }
1724 
HWTEST_F_L0(RegExpTest,ParseAndExec41)1725 HWTEST_F_L0(RegExpTest, ParseAndExec41)
1726 {
1727     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1728     RegExpParser parser = RegExpParser(thread, chunk_);
1729     CString source("(.*?)a(?!(a+)b\\2c)\\2(.*)");
1730     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1731     parser.Parse();
1732     bool parseResult = parser.IsError();
1733     ASSERT_FALSE(parseResult);
1734     RegExpExecutor executor(regExpCachedChunk_);
1735     CString input("baaabaac");
1736     bool ret =
1737         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1738     ASSERT_TRUE(ret);
1739     executor.GetResult(thread);
1740     JSHandle<builtins::RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1741     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 4U);
1742     JSHandle<EcmaString> inputStr = factory->NewFromASCII("baaabaac");
1743     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1744     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1745     JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1746     JSHandle<EcmaString> str0 = factory->NewFromASCII("baaabaac");
1747     JSHandle<EcmaString> str1 = factory->NewFromASCII("ba");
1748     JSHandle<EcmaString> str3 = factory->NewFromASCII("abaac");
1749     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1750     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1751     ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(2).GetInt(), -1);
1752     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1753 }
1754 
HWTEST_F_L0(RegExpTest,ParseAndExec42)1755 HWTEST_F_L0(RegExpTest, ParseAndExec42)
1756 {
1757     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1758     RegExpParser parser = RegExpParser(thread, chunk_);
1759     CString source("[a-c\\d]+");
1760     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1761     parser.Parse();
1762     bool parseResult = parser.IsError();
1763     ASSERT_FALSE(parseResult);
1764     RegExpExecutor executor(regExpCachedChunk_);
1765     CString input("\n\n\\abc324234");
1766     bool ret =
1767         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1768     ASSERT_TRUE(ret);
1769     executor.GetResult(thread);
1770     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1771     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1772     JSHandle<EcmaString> inputStr = factory->NewFromASCII("\n\n\\abc324234");
1773     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1774     JSHandle<EcmaString> str = factory->NewFromASCII("abc324234");
1775     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1776 }
1777 
HWTEST_F_L0(RegExpTest,ParseAndExec43)1778 HWTEST_F_L0(RegExpTest, ParseAndExec43)
1779 {
1780     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1781     RegExpParser parser = RegExpParser(thread, chunk_);
1782     CString source("[\\d][\n][^\\d]");
1783     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1784     parser.Parse();
1785     bool parseResult = parser.IsError();
1786     ASSERT_FALSE(parseResult);
1787     RegExpExecutor executor(regExpCachedChunk_);
1788     CString input("line1\nline2");
1789     bool ret =
1790         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1791     ASSERT_TRUE(ret);
1792     executor.GetResult(thread);
1793     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1794     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1795     JSHandle<EcmaString> inputStr = factory->NewFromASCII("line1\nline2");
1796     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1797     JSHandle<EcmaString> str = factory->NewFromASCII("1\nl");
1798     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1799 }
1800 
HWTEST_F_L0(RegExpTest,ParseAndExec44)1801 HWTEST_F_L0(RegExpTest, ParseAndExec44)
1802 {
1803     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1804     RegExpParser parser = RegExpParser(thread, chunk_);
1805     CString source(".[\b].");
1806     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1807     parser.Parse();
1808     bool parseResult = parser.IsError();
1809     ASSERT_FALSE(parseResult);
1810     RegExpExecutor executor(regExpCachedChunk_);
1811     CString input("abc\bdef");
1812     bool ret =
1813         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1814     ASSERT_TRUE(ret);
1815     executor.GetResult(thread);
1816     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1817     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1818     JSHandle<EcmaString> inputStr = factory->NewFromASCII("abc\bdef");
1819     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1820     JSHandle<EcmaString> str = factory->NewFromASCII("c\bd");
1821     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1822 }
1823 
HWTEST_F_L0(RegExpTest,ParseAndExec45)1824 HWTEST_F_L0(RegExpTest, ParseAndExec45)
1825 {
1826     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1827     RegExpParser parser = RegExpParser(thread, chunk_);
1828     CString source("[^\b]+");
1829     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1830     parser.Parse();
1831     bool parseResult = parser.IsError();
1832     ASSERT_FALSE(parseResult);
1833     RegExpExecutor executor(regExpCachedChunk_);
1834     CString input("easy\bto\u0008ride");
1835     bool ret =
1836         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1837     ASSERT_TRUE(ret);
1838     executor.GetResult(thread);
1839     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1840     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1841     JSHandle<EcmaString> inputStr = factory->NewFromASCII("easy\bto\u0008ride");
1842     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1843     JSHandle<EcmaString> str = factory->NewFromASCII("easy");
1844     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1845 }
1846 
HWTEST_F_L0(RegExpTest,ParseAndExec46)1847 HWTEST_F_L0(RegExpTest, ParseAndExec46)
1848 {
1849     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1850     RegExpParser parser = RegExpParser(thread, chunk_);
1851     CString source("([\\S]+([ \t]+[\\S]+)*)[ \t]*=[ \t]*[\\S]+");
1852     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1853     parser.Parse();
1854     bool parseResult = parser.IsError();
1855     ASSERT_FALSE(parseResult);
1856     RegExpExecutor executor(regExpCachedChunk_);
1857     CString input("Course_Creator = Test");
1858     bool ret =
1859         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1860     ASSERT_TRUE(ret);
1861     executor.GetResult(thread);
1862     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1863     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1864     JSHandle<EcmaString> inputStr = factory->NewFromASCII("Course_Creator = Test");
1865     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1866     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1867     JSHandle<EcmaString> str0 = factory->NewFromASCII("Course_Creator = Test");
1868     JSHandle<EcmaString> str1 = factory->NewFromASCII("Course_Creator");
1869     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1870     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1871 }
1872 
HWTEST_F_L0(RegExpTest,ParseAndExec47)1873 HWTEST_F_L0(RegExpTest, ParseAndExec47)
1874 {
1875     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1876     RegExpParser parser = RegExpParser(thread, chunk_);
1877     CString source("[^o]t\\b");
1878     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1879     parser.Parse();
1880     bool parseResult = parser.IsError();
1881     ASSERT_FALSE(parseResult);
1882     RegExpExecutor executor(regExpCachedChunk_);
1883     CString input("pilOt\nsoviet robot\topenoffice");
1884     bool ret =
1885         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1886     ASSERT_TRUE(ret);
1887     executor.GetResult(thread);
1888     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1889     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1890     JSHandle<EcmaString> inputStr = factory->NewFromASCII("pilOt\nsoviet robot\topenoffice");
1891     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1892     JSHandle<EcmaString> str = factory->NewFromASCII("et");
1893     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1894 }
1895 
HWTEST_F_L0(RegExpTest,ParseAndExec49)1896 HWTEST_F_L0(RegExpTest, ParseAndExec49)
1897 {
1898     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1899     RegExpParser parser = RegExpParser(thread, chunk_);
1900     CString source("(a(b)\\4(5)(5))");
1901     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1902     parser.Parse();
1903     bool parseResult = parser.IsError();
1904     ASSERT_FALSE(parseResult);
1905     RegExpExecutor executor(regExpCachedChunk_);
1906     CString input("ab55");
1907     bool ret =
1908         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1909     ASSERT_TRUE(ret);
1910     executor.GetResult(thread);
1911     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1912     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 5U);
1913     JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab55");
1914     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1915     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1916     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1917     JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1918     JSHandle<EcmaString> resultStr4 = GetSubString(regExpGlobalResult, inputStr, 4);
1919     JSHandle<EcmaString> str0 = factory->NewFromASCII("ab55");
1920     JSHandle<EcmaString> str1 = factory->NewFromASCII("ab55");
1921     JSHandle<EcmaString> str2 = factory->NewFromASCII("b");
1922     JSHandle<EcmaString> str3 = factory->NewFromASCII("5");
1923     JSHandle<EcmaString> str4 = factory->NewFromASCII("5");
1924     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1925     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1926     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1927     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1928     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr4, str4) == 0);
1929 }
1930 
HWTEST_F_L0(RegExpTest,ParseAndExec50)1931 HWTEST_F_L0(RegExpTest, ParseAndExec50)
1932 {
1933     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1934     RegExpParser parser = RegExpParser(thread, chunk_);
1935     CString source("(?<year>\\d{4})-(?<date>\\d{2}-(?<day>\\d\\d))");
1936     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1937     parser.Parse();
1938     bool parseResult = parser.IsError();
1939     ASSERT_FALSE(parseResult);
1940     RegExpExecutor executor(regExpCachedChunk_);
1941     CString input("2020-12-31");
1942     bool ret =
1943         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1944     ASSERT_TRUE(ret);
1945     executor.GetResult(thread);
1946     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1947     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 4U);
1948     JSHandle<EcmaString> inputStr = factory->NewFromASCII("2020-12-31");
1949     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1950     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1951     JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1952     JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1953     JSHandle<EcmaString> str0 = factory->NewFromASCII("2020-12-31");
1954     JSHandle<EcmaString> str1 = factory->NewFromASCII("2020");
1955     JSHandle<EcmaString> str2 = factory->NewFromASCII("12-31");
1956     JSHandle<EcmaString> str3 = factory->NewFromASCII("31");
1957     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1958     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1959     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1960     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1961 }
1962 
HWTEST_F_L0(RegExpTest,ParseAndExec51)1963 HWTEST_F_L0(RegExpTest, ParseAndExec51)
1964 {
1965     RegExpParser parser = RegExpParser(thread, chunk_);
1966     CString source("\\u0000");
1967     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1968     parser.Parse();
1969     bool parseResult = parser.IsError();
1970     ASSERT_FALSE(parseResult);
1971     RegExpExecutor executor(regExpCachedChunk_);
1972     std::u16string input(u"\u0000");
1973     bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length() + 1,
1974                                 parser.GetOriginBuffer(), true);
1975     ASSERT_TRUE(ret);
1976     executor.GetResult(thread);
1977     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1978     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1979 }
1980 
HWTEST_F_L0(RegExpTest,ParseAndExec52)1981 HWTEST_F_L0(RegExpTest, ParseAndExec52)
1982 {
1983     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1984     RegExpParser parser = RegExpParser(thread, chunk_);
1985     CString source("(aa).+\\1");
1986     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1987     parser.Parse();
1988     bool parseResult = parser.IsError();
1989     ASSERT_FALSE(parseResult);
1990     RegExpExecutor executor(regExpCachedChunk_);
1991     CString input("aabcdaabcd");
1992     bool ret =
1993         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1994     ASSERT_TRUE(ret);
1995     executor.GetResult(thread);
1996     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
1997     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1998     JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabcdaabcd");
1999     JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
2000     JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
2001     JSHandle<EcmaString> str0 = factory->NewFromASCII("aabcdaa");
2002     JSHandle<EcmaString> str1 = factory->NewFromASCII("aa");
2003     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
2004     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
2005 }
2006 
HWTEST_F_L0(RegExpTest,ParseAndExec53)2007 HWTEST_F_L0(RegExpTest, ParseAndExec53)
2008 {
2009     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2010     RegExpParser parser = RegExpParser(thread, chunk_);
2011     CString source("\\x01");
2012     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2013     parser.Parse();
2014     bool parseResult = parser.IsError();
2015     ASSERT_FALSE(parseResult);
2016     RegExpExecutor executor(regExpCachedChunk_);
2017     std::u16string input(u"\u0001");
2018     bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2019                                 parser.GetOriginBuffer(), true);
2020     ASSERT_TRUE(ret);
2021     executor.GetResult(thread);
2022     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
2023     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2024     JSHandle<EcmaString> inputStr = factory->NewFromUtf16(u"\u0001");
2025     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2026     JSHandle<EcmaString> str = factory->NewFromASCII("\u0001");
2027     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2028 }
2029 
HWTEST_F_L0(RegExpTest,ParseAndExec54)2030 HWTEST_F_L0(RegExpTest, ParseAndExec54)
2031 {
2032     RegExpParser parser = RegExpParser(thread, chunk_);
2033     CString source("\\bot");
2034     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2035     parser.Parse();
2036     bool parseResult = parser.IsError();
2037     ASSERT_FALSE(parseResult);
2038     RegExpExecutor executor(regExpCachedChunk_);
2039     CString input("pilot\nsoviet robot\topenoffice");
2040     bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2041                                 parser.GetOriginBuffer(), false);
2042     ASSERT_FALSE(ret);
2043 }
2044 
HWTEST_F_L0(RegExpTest,ParseAndExec55)2045 HWTEST_F_L0(RegExpTest, ParseAndExec55)
2046 {
2047     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2048     RegExpParser parser = RegExpParser(thread, chunk_);
2049     CString source("e\\b");
2050     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2051     parser.Parse();
2052     bool parseResult = parser.IsError();
2053     ASSERT_FALSE(parseResult);
2054     RegExpExecutor executor(regExpCachedChunk_);
2055     CString input("c\u0065");
2056     bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2057                                 parser.GetOriginBuffer(), false);
2058     ASSERT_TRUE(ret);
2059     executor.GetResult(thread);
2060     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
2061     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2062     JSHandle<EcmaString> inputStr = factory->NewFromASCII("c\u0065");
2063     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2064     JSHandle<EcmaString> str = factory->NewFromASCII("e");
2065     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2066 }
2067 
HWTEST_F_L0(RegExpTest,ParseAndExec56)2068 HWTEST_F_L0(RegExpTest, ParseAndExec56)
2069 {
2070     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2071     RegExpParser parser = RegExpParser(thread, chunk_);
2072     CString source("a啊");
2073     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2074     parser.Parse();
2075     bool parseResult = parser.IsError();
2076     ASSERT_FALSE(parseResult);
2077     RegExpExecutor executor(regExpCachedChunk_);
2078     std::u16string input(u"a啊");
2079     bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2080                                 parser.GetOriginBuffer(), true);
2081     ASSERT_TRUE(ret);
2082     executor.GetResult(thread);
2083     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
2084     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2085     JSHandle<EcmaString> inputStr = factory->NewFromUtf16(u"a啊");
2086     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2087     JSHandle<EcmaString> str = factory->NewFromUtf8("a啊");
2088     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2089 }
2090 
HWTEST_F_L0(RegExpTest,ParseAndExec57)2091 HWTEST_F_L0(RegExpTest, ParseAndExec57)
2092 {
2093     RegExpParser parser = RegExpParser(thread, chunk_);
2094     CString source("\\udf06");
2095     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
2096     parser.Parse();
2097     bool parseResult = parser.IsError();
2098     ASSERT_FALSE(parseResult);
2099     RegExpExecutor executor(regExpCachedChunk_);
2100     char16_t data[] = {0xd834, 0xdf06};
2101     bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(data), 0, 2, parser.GetOriginBuffer(), true);
2102     ASSERT_FALSE(ret);
2103 }
2104 
HWTEST_F_L0(RegExpTest,ParseAndExec58)2105 HWTEST_F_L0(RegExpTest, ParseAndExec58)
2106 {
2107     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2108     RegExpParser parser = RegExpParser(thread, chunk_);
2109     CString source("\\udf06");
2110     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2111     parser.Parse();
2112     bool parseResult = parser.IsError();
2113     ASSERT_FALSE(parseResult);
2114     RegExpExecutor executor(regExpCachedChunk_);
2115     char16_t data[] = {0xd834, 0xdf06};
2116     bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(data), 0, 2, parser.GetOriginBuffer(), true);
2117     ASSERT_TRUE(ret);
2118     executor.GetResult(thread);
2119     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
2120     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2121     JSHandle<EcmaString> inputStr = factory->NewFromUtf16(reinterpret_cast<const uint16_t *>(data), 2);
2122     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2123     char16_t data1[] = {0xdf06};
2124     JSHandle<EcmaString> str = factory->NewFromUtf16(reinterpret_cast<const uint16_t *>(data1), 1);
2125     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2126 }
2127 
HWTEST_F_L0(RegExpTest,ParseAndExec59)2128 HWTEST_F_L0(RegExpTest, ParseAndExec59)
2129 {
2130     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2131     RegExpParser parser = RegExpParser(thread, chunk_);
2132     CString source("\\v");
2133     parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2134     parser.Parse();
2135     bool parseResult = parser.IsError();
2136     ASSERT_FALSE(parseResult);
2137 
2138     RegExpExecutor executor(regExpCachedChunk_);
2139     CString input("\u000B");
2140     bool ret =
2141         executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
2142     ASSERT_TRUE(ret);
2143 
2144     executor.GetResult(thread);
2145     JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
2146     ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2147     JSHandle<EcmaString> inputStr = factory->NewFromASCII("\u000B");
2148     JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2149     JSHandle<EcmaString> str = factory->NewFromASCII("\u000B");
2150     ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2151 }
2152 
HWTEST_F_L0(RegExpTest,RangeSet1)2153 HWTEST_F_L0(RegExpTest, RangeSet1)
2154 {
2155     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2156         std::make_pair(1, 1),
2157         std::make_pair(2, 2),
2158         std::make_pair(3, 3),
2159     };
2160     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2161         std::make_pair(1, 5),
2162     };
2163     RangeSet rangeResult(listInput);
2164     RangeSet rangeExpected(listExpected);
2165     rangeResult.Insert(4, 5);
2166     rangeResult.Compress();
2167     EXPECT_EQ(rangeResult, rangeExpected);
2168 }
2169 
HWTEST_F_L0(RegExpTest,RangeSet2)2170 HWTEST_F_L0(RegExpTest, RangeSet2)
2171 {
2172     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2173         std::make_pair(4, 5),
2174     };
2175     RangeSet rangeResult;
2176     RangeSet rangeExpected(listExpected);
2177     rangeResult.Insert(4, 5);
2178     rangeResult.Compress();
2179     EXPECT_EQ(rangeResult, rangeExpected);
2180 }
2181 
HWTEST_F_L0(RegExpTest,RangeSet3)2182 HWTEST_F_L0(RegExpTest, RangeSet3)
2183 {
2184     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2185         std::make_pair(2, 2),
2186     };
2187     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2188         std::make_pair(1, 5),
2189     };
2190     RangeSet rangeResult(listInput);
2191     RangeSet rangeExpected(listExpected);
2192     rangeResult.Insert(1, 5);
2193     rangeResult.Compress();
2194     EXPECT_EQ(rangeResult, rangeExpected);
2195 }
2196 
HWTEST_F_L0(RegExpTest,RangeSet4)2197 HWTEST_F_L0(RegExpTest, RangeSet4)
2198 {
2199     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2200         std::make_pair(1, 5),
2201     };
2202     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2203         std::make_pair(1, 5),
2204     };
2205     RangeSet rangeResult(listInput);
2206     RangeSet rangeExpected(listExpected);
2207     rangeResult.Insert(2, 4);
2208     rangeResult.Compress();
2209     EXPECT_EQ(rangeResult, rangeExpected);
2210 }
2211 
HWTEST_F_L0(RegExpTest,RangeSet5)2212 HWTEST_F_L0(RegExpTest, RangeSet5)
2213 {
2214     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2215         std::make_pair(1, 2),
2216         std::make_pair(9, UINT16_MAX),
2217     };
2218     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2219         std::make_pair(1, 2),
2220         std::make_pair(4, 7),
2221         std::make_pair(9, UINT16_MAX),
2222     };
2223     RangeSet rangeResult(listInput);
2224     RangeSet rangeExpected(listExpected);
2225     rangeResult.Insert(4, 7);
2226     rangeResult.Compress();
2227     EXPECT_EQ(rangeResult, rangeExpected);
2228 }
2229 
HWTEST_F_L0(RegExpTest,RangeSet6)2230 HWTEST_F_L0(RegExpTest, RangeSet6)
2231 {
2232     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2233         std::make_pair(0, UINT16_MAX),
2234     };
2235     RangeSet rangeResult;
2236     RangeSet rangeExpected(listExpected);
2237     rangeResult.Invert(false);
2238     EXPECT_EQ(rangeResult, rangeExpected);
2239 }
2240 
HWTEST_F_L0(RegExpTest,RangeSet7)2241 HWTEST_F_L0(RegExpTest, RangeSet7)
2242 {
2243     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2244         std::make_pair(1, 5),
2245     };
2246     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2247         std::make_pair(0, 0),
2248         std::make_pair(6, UINT16_MAX),
2249     };
2250     RangeSet rangeResult(listInput);
2251     RangeSet rangeExpected(listExpected);
2252     rangeResult.Invert(false);
2253     EXPECT_EQ(rangeResult, rangeExpected);
2254 }
2255 
HWTEST_F_L0(RegExpTest,RangeSet8)2256 HWTEST_F_L0(RegExpTest, RangeSet8)
2257 {
2258     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2259         std::make_pair(1, 5),
2260         std::make_pair(0xfffe, UINT16_MAX),
2261     };
2262     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2263         std::make_pair(0, 0),
2264         std::make_pair(6, 0xfffd),
2265     };
2266     RangeSet rangeResult(listInput);
2267     RangeSet rangeExpected(listExpected);
2268     rangeResult.Invert(false);
2269     EXPECT_EQ(rangeResult, rangeExpected);
2270 }
2271 
HWTEST_F_L0(RegExpTest,RangeSet9)2272 HWTEST_F_L0(RegExpTest, RangeSet9)
2273 {
2274     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2275         std::make_pair(0, 5),
2276         std::make_pair(0xfffe, 0xfffe),
2277     };
2278     std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2279         std::make_pair(6, 0xfffd),
2280         std::make_pair(UINT16_MAX, UINT16_MAX),
2281     };
2282     RangeSet rangeResult(listInput);
2283     RangeSet rangeExpected(listExpected);
2284     rangeResult.Invert(false);
2285     EXPECT_EQ(rangeResult, rangeExpected);
2286 }
2287 
HWTEST_F_L0(RegExpTest,RangeSet10)2288 HWTEST_F_L0(RegExpTest, RangeSet10)
2289 {
2290     std::list<std::pair<uint32_t, uint32_t>> listInput = {
2291         std::make_pair(0, UINT16_MAX),
2292     };
2293     RangeSet rangeResult(listInput);
2294     RangeSet rangeExpected;
2295     rangeResult.Invert(false);
2296     EXPECT_EQ(rangeResult, rangeExpected);
2297 }
2298 }  // namespace panda::test
2299