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