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->GetGlobalEnv()->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
714
HWTEST_F_L0(RegExpTest,ParseAndExec2)715 HWTEST_F_L0(RegExpTest, ParseAndExec2)
716 {
717 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
718 RegExpParser parser = RegExpParser(thread, chunk_);
719 CString source("(((ab)|(cd)|(de))|((ef)|(gh)|(jk)))");
720 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
721 parser.Parse();
722 bool parseResult = parser.IsError();
723 ASSERT_FALSE(parseResult);
724
725 RegExpExecutor executor(regExpCachedChunk_);
726 CString input("cabd");
727 bool ret =
728 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
729 ASSERT_TRUE(ret);
730
731 executor.GetResult(thread);
732 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
733 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 10U);
734 JSHandle<EcmaString> inputStr = factory->NewFromASCII("cabd");
735 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
736 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
737 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
738 JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
739 JSHandle<EcmaString> str = factory->NewFromASCII("ab");
740 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str) == 0);
741 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str) == 0);
742 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str) == 0);
743 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str) == 0);
744 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(4).GetInt(), -1);
745 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(5).GetInt(), -1);
746 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(6).GetInt(), -1);
747 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(7).GetInt(), -1);
748 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(8).GetInt(), -1);
749 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(9).GetInt(), -1);
750 }
751
HWTEST_F_L0(RegExpTest,ParseAndExec3)752 HWTEST_F_L0(RegExpTest, ParseAndExec3)
753 {
754 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
755 RegExpParser parser = RegExpParser(thread, chunk_);
756 CString source("(aa|aabaac|ba|b|c)*");
757 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
758 parser.Parse();
759 bool parseResult = parser.IsError();
760 ASSERT_FALSE(parseResult);
761
762 RegExpExecutor executor(regExpCachedChunk_);
763 CString input("aabaac");
764 bool ret =
765 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
766 ASSERT_TRUE(ret);
767
768 executor.GetResult(thread);
769 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
770 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
771 JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabaac");
772 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
773 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
774 JSHandle<EcmaString> str1 = factory->NewFromASCII("aaba");
775 JSHandle<EcmaString> str2 = factory->NewFromASCII("ba");
776 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str1) == 0);
777 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str2) == 0);
778 }
779
HWTEST_F_L0(RegExpTest,ParseAndExec4)780 HWTEST_F_L0(RegExpTest, ParseAndExec4)
781 {
782 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
783 RegExpParser parser = RegExpParser(thread, chunk_);
784 CString source("a*");
785 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
786 parser.Parse();
787 bool parseResult = parser.IsError();
788 ASSERT_FALSE(parseResult);
789
790 RegExpExecutor executor(regExpCachedChunk_);
791 CString input("aabaac");
792 bool ret =
793 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
794 ASSERT_TRUE(ret);
795
796 executor.GetResult(thread);
797 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
798 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
799 JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabaac");
800 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
801 JSHandle<EcmaString> str = factory->NewFromASCII("aa");
802 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
803 }
804
HWTEST_F_L0(RegExpTest,ParseAndExec5)805 HWTEST_F_L0(RegExpTest, ParseAndExec5)
806 {
807 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
808 RegExpParser parser = RegExpParser(thread, chunk_);
809 CString source("a?");
810 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
811 parser.Parse();
812 bool parseResult = parser.IsError();
813 ASSERT_FALSE(parseResult);
814
815 RegExpExecutor executor(regExpCachedChunk_);
816 CString input("b");
817 bool ret =
818 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
819 ASSERT_TRUE(ret);
820
821 executor.GetResult(thread);
822 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
823 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
824 JSHandle<EcmaString> inputStr = factory->NewFromASCII("b");
825 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
826 JSHandle<EcmaString> str = factory->NewFromASCII("");
827 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
828 }
829
HWTEST_F_L0(RegExpTest,ParseAndExec6)830 HWTEST_F_L0(RegExpTest, ParseAndExec6)
831 {
832 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
833 RegExpParser parser = RegExpParser(thread, chunk_);
834 CString source("(z)((a+)?(b+)?(c))*");
835 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
836 parser.Parse();
837 bool parseResult = parser.IsError();
838 ASSERT_FALSE(parseResult);
839
840 RegExpExecutor executor(regExpCachedChunk_);
841 CString input("zaacbbbcac");
842 bool ret =
843 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
844 ASSERT_TRUE(ret);
845
846 executor.GetResult(thread);
847 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
848 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 6U);
849 JSHandle<EcmaString> inputStr = factory->NewFromASCII("zaacbbbcac");
850 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
851 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
852 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
853 JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
854 JSHandle<EcmaString> resultStr5 = GetSubString(regExpGlobalResult, inputStr, 5);
855 JSHandle<EcmaString> str0 = factory->NewFromASCII("zaacbbbcac");
856 JSHandle<EcmaString> str1 = factory->NewFromASCII("z");
857 JSHandle<EcmaString> str2 = factory->NewFromASCII("ac");
858 JSHandle<EcmaString> str3 = factory->NewFromASCII("a");
859 JSHandle<EcmaString> str5 = factory->NewFromASCII("c");
860 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
861 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
862 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
863 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
864 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(4).GetInt(), -1);
865 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr5, str5) == 0);
866 }
867
HWTEST_F_L0(RegExpTest,ParseAndExec7)868 HWTEST_F_L0(RegExpTest, ParseAndExec7)
869 {
870 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
871 RegExpParser parser = RegExpParser(thread, chunk_);
872 CString source("^abc");
873 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
874 parser.Parse();
875 bool parseResult = parser.IsError();
876 ASSERT_FALSE(parseResult);
877
878 RegExpExecutor executor(regExpCachedChunk_);
879 CString input("ab\nabc");
880 bool ret =
881 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
882 ASSERT_TRUE(ret);
883
884 executor.GetResult(thread);
885 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
886 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
887 JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab\nabc");
888 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
889 JSHandle<EcmaString> str = factory->NewFromASCII("abc");
890 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
891 }
892
HWTEST_F_L0(RegExpTest,ParseAndExec8)893 HWTEST_F_L0(RegExpTest, ParseAndExec8)
894 {
895 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
896 RegExpParser parser = RegExpParser(thread, chunk_);
897 CString source("abc$");
898 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
899 parser.Parse();
900 bool parseResult = parser.IsError();
901 ASSERT_FALSE(parseResult);
902
903 RegExpExecutor executor(regExpCachedChunk_);
904 CString input("ab\nabc");
905 bool ret =
906 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
907 ASSERT_TRUE(ret);
908
909 executor.GetResult(thread);
910 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
911 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
912 JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab\nabc");
913 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
914 JSHandle<EcmaString> str = factory->NewFromASCII("abc");
915 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
916 }
917
HWTEST_F_L0(RegExpTest,ParseAndExec9)918 HWTEST_F_L0(RegExpTest, ParseAndExec9)
919 {
920 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
921 RegExpParser parser = RegExpParser(thread, chunk_);
922 CString source("er\\B");
923 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
924 parser.Parse();
925 bool parseResult = parser.IsError();
926 ASSERT_FALSE(parseResult);
927
928 RegExpExecutor executor(regExpCachedChunk_);
929 CString input("erv");
930 bool ret =
931 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
932 ASSERT_TRUE(ret);
933
934 executor.GetResult(thread);
935 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
936 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
937 JSHandle<EcmaString> inputStr = factory->NewFromASCII("erv");
938 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
939 JSHandle<EcmaString> str = factory->NewFromASCII("er");
940 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
941 }
942
HWTEST_F_L0(RegExpTest,ParseAndExec10)943 HWTEST_F_L0(RegExpTest, ParseAndExec10)
944 {
945 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
946 RegExpParser parser = RegExpParser(thread, chunk_);
947 CString source("d\\b");
948 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
949 parser.Parse();
950 bool parseResult = parser.IsError();
951 ASSERT_FALSE(parseResult);
952
953 RegExpExecutor executor(regExpCachedChunk_);
954 CString input("bad good");
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 executor.GetResult(thread);
960 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
961 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
962 JSHandle<EcmaString> inputStr = factory->NewFromASCII("bad good");
963 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
964 JSHandle<EcmaString> str = factory->NewFromASCII("d");
965 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
966 }
967
HWTEST_F_L0(RegExpTest,ParseAndExec11)968 HWTEST_F_L0(RegExpTest, ParseAndExec11)
969 {
970 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
971 RegExpParser parser = RegExpParser(thread, chunk_);
972 CString source(".");
973 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
974 parser.Parse();
975 bool parseResult = parser.IsError();
976 ASSERT_FALSE(parseResult);
977
978 RegExpExecutor executor(regExpCachedChunk_);
979 CString input("\na");
980 bool ret =
981 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
982 ASSERT_TRUE(ret);
983
984 executor.GetResult(thread);
985 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
986 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
987 JSHandle<EcmaString> inputStr = factory->NewFromASCII("\na");
988 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
989 JSHandle<EcmaString> str = factory->NewFromASCII("a");
990 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
991 }
992
HWTEST_F_L0(RegExpTest,ParseAndExec12)993 HWTEST_F_L0(RegExpTest, ParseAndExec12)
994 {
995 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
996 RegExpParser parser = RegExpParser(thread, chunk_);
997 CString source(".");
998 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 8);
999 parser.Parse();
1000 bool parseResult = parser.IsError();
1001 ASSERT_FALSE(parseResult);
1002
1003 RegExpExecutor executor(regExpCachedChunk_);
1004 CString input("\n");
1005 bool ret =
1006 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1007 ASSERT_TRUE(ret);
1008
1009 executor.GetResult(thread);
1010 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1011 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1012 JSHandle<EcmaString> inputStr = factory->NewFromASCII("\n");
1013 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1014 JSHandle<EcmaString> str = factory->NewFromASCII("\n");
1015 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1016 }
1017
HWTEST_F_L0(RegExpTest,ParseAndExec13)1018 HWTEST_F_L0(RegExpTest, ParseAndExec13)
1019 {
1020 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1021 RegExpParser parser = RegExpParser(thread, chunk_);
1022 CString source("abc");
1023 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
1024 parser.Parse();
1025 bool parseResult = parser.IsError();
1026 ASSERT_FALSE(parseResult);
1027
1028 RegExpExecutor executor(regExpCachedChunk_);
1029 CString input("\naabc");
1030 bool ret =
1031 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1032 ASSERT_TRUE(ret);
1033
1034 executor.GetResult(thread);
1035 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1036 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1037 JSHandle<EcmaString> inputStr = factory->NewFromASCII("\naabc");
1038 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1039 JSHandle<EcmaString> str = factory->NewFromASCII("abc");
1040 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1041 }
1042
HWTEST_F_L0(RegExpTest,ParseAndExec14)1043 HWTEST_F_L0(RegExpTest, ParseAndExec14)
1044 {
1045 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1046 RegExpParser parser = RegExpParser(thread, chunk_);
1047 CString source("abc");
1048 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 4);
1049 parser.Parse();
1050 bool parseResult = parser.IsError();
1051 ASSERT_FALSE(parseResult);
1052
1053 RegExpExecutor executor(regExpCachedChunk_);
1054 CString input("\nbbabc");
1055 bool ret =
1056 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1057 ASSERT_TRUE(ret);
1058
1059 executor.GetResult(thread);
1060 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1061 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1062 JSHandle<EcmaString> inputStr = factory->NewFromASCII("\nbbabc");
1063 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1064 JSHandle<EcmaString> str = factory->NewFromASCII("abc");
1065 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1066 }
1067
HWTEST_F_L0(RegExpTest,ParseAndExec15)1068 HWTEST_F_L0(RegExpTest, ParseAndExec15)
1069 {
1070 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1071 RegExpParser parser = RegExpParser(thread, chunk_);
1072 CString source("a(?=a)");
1073 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1074 parser.Parse();
1075 bool parseResult = parser.IsError();
1076 ASSERT_FALSE(parseResult);
1077
1078 RegExpExecutor executor(regExpCachedChunk_);
1079 CString input("aabc");
1080 bool ret =
1081 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1082 ASSERT_TRUE(ret);
1083
1084 executor.GetResult(thread);
1085 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1086 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1087 JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabc");
1088 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1089 JSHandle<EcmaString> str = factory->NewFromASCII("a");
1090 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1091 }
1092
HWTEST_F_L0(RegExpTest,ParseAndExec16)1093 HWTEST_F_L0(RegExpTest, ParseAndExec16)
1094 {
1095 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1096 RegExpParser parser = RegExpParser(thread, chunk_);
1097 CString source("abc");
1098 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1099 parser.Parse();
1100 bool parseResult = parser.IsError();
1101 ASSERT_FALSE(parseResult);
1102
1103 RegExpExecutor executor(regExpCachedChunk_);
1104 CString input("ABC");
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 executor.GetResult(thread);
1110 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1111 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1112 JSHandle<EcmaString> inputStr = factory->NewFromASCII("ABC");
1113 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1114 JSHandle<EcmaString> str = factory->NewFromASCII("ABC");
1115 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1116 }
1117
HWTEST_F_L0(RegExpTest,ParseAndExec17)1118 HWTEST_F_L0(RegExpTest, ParseAndExec17)
1119 {
1120 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1121 RegExpParser parser = RegExpParser(thread, chunk_);
1122 CString source("a\\n");
1123 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1124 parser.Parse();
1125 bool parseResult = parser.IsError();
1126 ASSERT_FALSE(parseResult);
1127
1128 RegExpExecutor executor(regExpCachedChunk_);
1129 CString input("a\n");
1130 bool ret =
1131 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1132 ASSERT_TRUE(ret);
1133
1134 executor.GetResult(thread);
1135 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1136 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1137 JSHandle<EcmaString> inputStr = factory->NewFromASCII("a\n");
1138 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1139 JSHandle<EcmaString> str = factory->NewFromASCII("a\n");
1140 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1141 }
1142
HWTEST_F_L0(RegExpTest,ParseAndExec18)1143 HWTEST_F_L0(RegExpTest, ParseAndExec18)
1144 {
1145 RegExpParser parser = RegExpParser(thread, chunk_);
1146 CString source("a(?=a)");
1147 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1148 parser.Parse();
1149 bool parseResult = parser.IsError();
1150 ASSERT_FALSE(parseResult);
1151
1152 RegExpExecutor executor(regExpCachedChunk_);
1153 CString input("ababc");
1154 bool ret =
1155 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1156 ASSERT_FALSE(ret);
1157 }
1158
HWTEST_F_L0(RegExpTest,ParseAndExec19)1159 HWTEST_F_L0(RegExpTest, ParseAndExec19)
1160 {
1161 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1162 RegExpParser parser = RegExpParser(thread, chunk_);
1163 CString source("a(?!a)");
1164 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1165 parser.Parse();
1166 bool parseResult = parser.IsError();
1167 ASSERT_FALSE(parseResult);
1168
1169 RegExpExecutor executor(regExpCachedChunk_);
1170 CString input("ababc");
1171 bool ret =
1172 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1173 ASSERT_TRUE(ret);
1174
1175 executor.GetResult(thread);
1176 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1177 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1178 JSHandle<EcmaString> inputStr = factory->NewFromASCII("ababc");
1179 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1180 JSHandle<EcmaString> str = factory->NewFromASCII("a");
1181 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1182 }
1183
HWTEST_F_L0(RegExpTest,ParseAndExec20)1184 HWTEST_F_L0(RegExpTest, ParseAndExec20)
1185 {
1186 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1187 RegExpParser parser = RegExpParser(thread, chunk_);
1188 CString source("(?=(a+))");
1189 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1190 parser.Parse();
1191 bool parseResult = parser.IsError();
1192 ASSERT_FALSE(parseResult);
1193
1194 RegExpExecutor executor(regExpCachedChunk_);
1195 CString input("baaabac");
1196 bool ret =
1197 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1198 ASSERT_TRUE(ret);
1199
1200 executor.GetResult(thread);
1201 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1202 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1203 JSHandle<EcmaString> inputStr = factory->NewFromASCII("baaabac");
1204 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1205 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1206 JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1207 JSHandle<EcmaString> str1 = factory->NewFromASCII("aaa");
1208 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1209 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1210 }
1211
HWTEST_F_L0(RegExpTest,ParseAndExec21)1212 HWTEST_F_L0(RegExpTest, ParseAndExec21)
1213 {
1214 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1215 RegExpParser parser = RegExpParser(thread, chunk_);
1216 CString source("a(?=a(?=b))");
1217 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1218 parser.Parse();
1219 bool parseResult = parser.IsError();
1220 ASSERT_FALSE(parseResult);
1221
1222 RegExpExecutor executor(regExpCachedChunk_);
1223 CString input("caab");
1224 bool ret =
1225 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1226 ASSERT_TRUE(ret);
1227
1228 executor.GetResult(thread);
1229 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1230 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1231 JSHandle<EcmaString> inputStr = factory->NewFromASCII("caab");
1232 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1233 JSHandle<EcmaString> str = factory->NewFromASCII("a");
1234 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1235 }
1236
HWTEST_F_L0(RegExpTest,ParseAndExec22)1237 HWTEST_F_L0(RegExpTest, ParseAndExec22)
1238 {
1239 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1240 RegExpParser parser = RegExpParser(thread, chunk_);
1241 CString source(".+:");
1242 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1243 parser.Parse();
1244 bool parseResult = parser.IsError();
1245 ASSERT_FALSE(parseResult);
1246
1247 RegExpExecutor executor(regExpCachedChunk_);
1248 CString input("aaaa:aa");
1249 bool ret =
1250 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1251 ASSERT_TRUE(ret);
1252
1253 executor.GetResult(thread);
1254 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1255 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1256 JSHandle<EcmaString> inputStr = factory->NewFromASCII("aaaa:aa");
1257 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1258 JSHandle<EcmaString> str = factory->NewFromASCII("aaaa:");
1259 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1260 }
1261
HWTEST_F_L0(RegExpTest,ParseAndExec23)1262 HWTEST_F_L0(RegExpTest, ParseAndExec23)
1263 {
1264 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1265 RegExpParser parser = RegExpParser(thread, chunk_);
1266 CString source("a(?<=a(?<!b))");
1267 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1268 parser.Parse();
1269 bool parseResult = parser.IsError();
1270 ASSERT_FALSE(parseResult);
1271
1272 RegExpExecutor executor(regExpCachedChunk_);
1273 CString input("caab");
1274 bool ret =
1275 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1276 ASSERT_TRUE(ret);
1277
1278 executor.GetResult(thread);
1279 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1280 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1281 JSHandle<EcmaString> inputStr = factory->NewFromASCII("caab");
1282 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1283 JSHandle<EcmaString> str = factory->NewFromASCII("a");
1284 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1285 }
1286
HWTEST_F_L0(RegExpTest,ParseAndExec24)1287 HWTEST_F_L0(RegExpTest, ParseAndExec24)
1288 {
1289 RegExpParser parser = RegExpParser(thread, chunk_);
1290 CString source("a(?<=ab(?<!bc))");
1291 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1292 parser.Parse();
1293 bool parseResult = parser.IsError();
1294 ASSERT_FALSE(parseResult);
1295
1296 RegExpExecutor executor(regExpCachedChunk_);
1297 CString input("caab");
1298 bool ret =
1299 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1300 ASSERT_FALSE(ret);
1301 }
1302
HWTEST_F_L0(RegExpTest,ParseAndExec25)1303 HWTEST_F_L0(RegExpTest, ParseAndExec25)
1304 {
1305 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1306 RegExpParser parser = RegExpParser(thread, chunk_);
1307 CString source("(?<=(ab))");
1308 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1309 parser.Parse();
1310 bool parseResult = parser.IsError();
1311 ASSERT_FALSE(parseResult);
1312
1313 RegExpExecutor executor(regExpCachedChunk_);
1314 CString input("cabab");
1315 bool ret =
1316 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1317 ASSERT_TRUE(ret);
1318
1319 executor.GetResult(thread);
1320 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1321 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1322 JSHandle<EcmaString> inputStr = factory->NewFromASCII("cabab");
1323 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1324 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1325 JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1326 JSHandle<EcmaString> str1 = factory->NewFromASCII("ab");
1327 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1328 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1329 }
1330
HWTEST_F_L0(RegExpTest,ParseAndExec26)1331 HWTEST_F_L0(RegExpTest, ParseAndExec26)
1332 {
1333 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1334 RegExpParser parser = RegExpParser(thread, chunk_);
1335 CString source("[a-z]");
1336 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1337 parser.Parse();
1338 bool parseResult = parser.IsError();
1339 ASSERT_FALSE(parseResult);
1340
1341 RegExpExecutor executor(regExpCachedChunk_);
1342 CString input("A");
1343 bool ret =
1344 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1345 ASSERT_TRUE(ret);
1346
1347 executor.GetResult(thread);
1348 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1349 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1350 JSHandle<EcmaString> inputStr = factory->NewFromASCII("A");
1351 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1352 JSHandle<EcmaString> str = factory->NewFromASCII("A");
1353 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1354 }
1355
HWTEST_F_L0(RegExpTest,ParseAndExec27)1356 HWTEST_F_L0(RegExpTest, ParseAndExec27)
1357 {
1358 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1359 RegExpParser parser = RegExpParser(thread, chunk_);
1360 CString source("[^a-b]");
1361 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1362 parser.Parse();
1363 bool parseResult = parser.IsError();
1364 ASSERT_FALSE(parseResult);
1365
1366 RegExpExecutor executor(regExpCachedChunk_);
1367 CString input("Z");
1368 bool ret =
1369 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1370 ASSERT_TRUE(ret);
1371
1372 executor.GetResult(thread);
1373 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1374 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1375 JSHandle<EcmaString> inputStr = factory->NewFromASCII("Z");
1376 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1377 JSHandle<EcmaString> str = factory->NewFromASCII("Z");
1378 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1379 }
1380
HWTEST_F_L0(RegExpTest,ParseAndExec28)1381 HWTEST_F_L0(RegExpTest, ParseAndExec28)
1382 {
1383 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1384 RegExpParser parser = RegExpParser(thread, chunk_);
1385 CString source("\\s");
1386 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1387 parser.Parse();
1388 bool parseResult = parser.IsError();
1389 ASSERT_FALSE(parseResult);
1390
1391 RegExpExecutor executor(regExpCachedChunk_);
1392 CString input("\n");
1393 bool ret =
1394 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1395 ASSERT_TRUE(ret);
1396
1397 executor.GetResult(thread);
1398 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1399 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1400 JSHandle<EcmaString> inputStr = factory->NewFromASCII("\n");
1401 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1402 JSHandle<EcmaString> str = factory->NewFromASCII("\n");
1403 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1404 }
1405
HWTEST_F_L0(RegExpTest,ParseAndExec29)1406 HWTEST_F_L0(RegExpTest, ParseAndExec29)
1407 {
1408 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1409 RegExpParser parser = RegExpParser(thread, chunk_);
1410 CString source("()|");
1411 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1412 parser.Parse();
1413 bool parseResult = parser.IsError();
1414 ASSERT_FALSE(parseResult);
1415
1416 RegExpExecutor executor(regExpCachedChunk_);
1417 CString input("");
1418 bool ret =
1419 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1420 ASSERT_TRUE(ret);
1421
1422 executor.GetResult(thread);
1423 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1424 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1425 JSHandle<EcmaString> inputStr = factory->NewFromASCII("");
1426 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1427 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1428 JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1429 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1430 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str0) == 0);
1431 }
1432
HWTEST_F_L0(RegExpTest,ParseAndExec30)1433 HWTEST_F_L0(RegExpTest, ParseAndExec30)
1434 {
1435 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1436 RegExpParser parser = RegExpParser(thread, chunk_);
1437 CString source("|()");
1438 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1439 parser.Parse();
1440 bool parseResult = parser.IsError();
1441 ASSERT_FALSE(parseResult);
1442
1443 RegExpExecutor executor(regExpCachedChunk_);
1444 CString input("");
1445 bool ret =
1446 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
1447 ASSERT_TRUE(ret);
1448
1449 executor.GetResult(thread);
1450 JSHandle<builtins::RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1451 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1452 JSHandle<EcmaString> inputStr = factory->NewFromASCII("");
1453 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1454 JSHandle<EcmaString> str0 = factory->NewFromASCII("");
1455 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1456 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(1).GetInt(), -1);
1457 }
1458
HWTEST_F_L0(RegExpTest,ParseAndExec31)1459 HWTEST_F_L0(RegExpTest, ParseAndExec31)
1460 {
1461 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1462 RegExpParser parser = RegExpParser(thread, chunk_);
1463 CString source("a(a|b)\\1");
1464 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1465 parser.Parse();
1466 bool parseResult = parser.IsError();
1467 ASSERT_FALSE(parseResult);
1468 RegExpExecutor executor(regExpCachedChunk_);
1469 CString input("aabb");
1470 bool ret =
1471 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1472 ASSERT_TRUE(ret);
1473 executor.GetResult(thread);
1474 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1475 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1476 JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabb");
1477 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1478 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1479 JSHandle<EcmaString> str0 = factory->NewFromASCII("abb");
1480 JSHandle<EcmaString> str1 = factory->NewFromASCII("b");
1481 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1482 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1483 }
1484
HWTEST_F_L0(RegExpTest,ParseAndExec32)1485 HWTEST_F_L0(RegExpTest, ParseAndExec32)
1486 {
1487 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1488 RegExpParser parser = RegExpParser(thread, chunk_);
1489 CString source("(a(a|b))\\2");
1490 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1491 parser.Parse();
1492 bool parseResult = parser.IsError();
1493 ASSERT_FALSE(parseResult);
1494 RegExpExecutor executor(regExpCachedChunk_);
1495 CString input("aabb");
1496 bool ret =
1497 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1498 ASSERT_TRUE(ret);
1499 executor.GetResult(thread);
1500 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1501 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1502 JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabb");
1503 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1504 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1505 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1506 JSHandle<EcmaString> str0 = factory->NewFromASCII("abb");
1507 JSHandle<EcmaString> str1 = factory->NewFromASCII("ab");
1508 JSHandle<EcmaString> str2 = factory->NewFromASCII("b");
1509 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1510 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1511 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1512 }
1513
HWTEST_F_L0(RegExpTest,ParseAndExec33)1514 HWTEST_F_L0(RegExpTest, ParseAndExec33)
1515 {
1516 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1517 RegExpParser parser = RegExpParser(thread, chunk_);
1518 CString source("qya+");
1519 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1520 parser.Parse();
1521 bool parseResult = parser.IsError();
1522 ASSERT_FALSE(parseResult);
1523 RegExpExecutor executor(regExpCachedChunk_);
1524 CString input("qyqya");
1525 bool ret =
1526 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1527 ASSERT_TRUE(ret);
1528
1529 executor.GetResult(thread);
1530 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1531 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1532 JSHandle<EcmaString> inputStr = factory->NewFromASCII("qyqya");
1533 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1534 JSHandle<EcmaString> str = factory->NewFromASCII("qya");
1535 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1536 }
1537
HWTEST_F_L0(RegExpTest,ParseAndExec34)1538 HWTEST_F_L0(RegExpTest, ParseAndExec34)
1539 {
1540 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1541 RegExpParser parser = RegExpParser(thread, chunk_);
1542 CString source("qy(?=\\s+)");
1543 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1544 parser.Parse();
1545 bool parseResult = parser.IsError();
1546 ASSERT_FALSE(parseResult);
1547 RegExpExecutor executor(regExpCachedChunk_);
1548 CString input("qyqy ");
1549 bool ret =
1550 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1551 ASSERT_TRUE(ret);
1552 executor.GetResult(thread);
1553 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1554 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1555 JSHandle<EcmaString> inputStr = factory->NewFromASCII("qyqy ");
1556 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1557 JSHandle<EcmaString> str = factory->NewFromASCII("qy");
1558 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1559 }
1560
HWTEST_F_L0(RegExpTest,ParseAndExec35)1561 HWTEST_F_L0(RegExpTest, ParseAndExec35)
1562 {
1563 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1564 RegExpParser parser = RegExpParser(thread, chunk_);
1565 CString source("(\\d{4})-(\\d{2})-(\\d{2})");
1566 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1567 parser.Parse();
1568 bool parseResult = parser.IsError();
1569 ASSERT_FALSE(parseResult);
1570 RegExpExecutor executor(regExpCachedChunk_);
1571 CString input("xx2021-01-09");
1572 bool ret =
1573 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1574 ASSERT_TRUE(ret);
1575 executor.GetResult(thread);
1576 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1577 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 4U);
1578 JSHandle<EcmaString> inputStr = factory->NewFromASCII("xx2021-01-09");
1579 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1580 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1581 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1582 JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1583 JSHandle<EcmaString> str0 = factory->NewFromASCII("2021-01-09");
1584 JSHandle<EcmaString> str1 = factory->NewFromASCII("2021");
1585 JSHandle<EcmaString> str2 = factory->NewFromASCII("01");
1586 JSHandle<EcmaString> str3 = factory->NewFromASCII("09");
1587 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1588 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1589 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1590 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1591 }
1592
HWTEST_F_L0(RegExpTest,ParseAndExec36)1593 HWTEST_F_L0(RegExpTest, ParseAndExec36)
1594 {
1595 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1596 RegExpParser parser = RegExpParser(thread, chunk_);
1597 CString source("quick\\s(brown).+?(jumps)");
1598 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1599 parser.Parse();
1600 bool parseResult = parser.IsError();
1601 ASSERT_FALSE(parseResult);
1602 RegExpExecutor executor(regExpCachedChunk_);
1603 CString input("The Quick Brown Fox Jumps Over The Lazy Dog");
1604 bool ret =
1605 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1606 ASSERT_TRUE(ret);
1607 executor.GetResult(thread);
1608 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1609 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1610 JSHandle<EcmaString> inputStr = factory->NewFromASCII("The Quick Brown Fox Jumps Over The Lazy Dog");
1611 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1612 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1613 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1614 JSHandle<EcmaString> str0 = factory->NewFromASCII("Quick Brown Fox Jumps");
1615 JSHandle<EcmaString> str1 = factory->NewFromASCII("Brown");
1616 JSHandle<EcmaString> str2 = factory->NewFromASCII("Jumps");
1617 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1618 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1619 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1620 }
1621
HWTEST_F_L0(RegExpTest,ParseAndExec37)1622 HWTEST_F_L0(RegExpTest, ParseAndExec37)
1623 {
1624 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1625 RegExpParser parser = RegExpParser(thread, chunk_);
1626 CString source("(ab){1,2}?c");
1627 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1628 parser.Parse();
1629 bool parseResult = parser.IsError();
1630 ASSERT_FALSE(parseResult);
1631 RegExpExecutor executor(regExpCachedChunk_);
1632 CString input("abABc");
1633 bool ret =
1634 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1635 ASSERT_TRUE(ret);
1636 executor.GetResult(thread);
1637 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1638 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1639 JSHandle<EcmaString> inputStr = factory->NewFromASCII("abABc");
1640 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1641 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1642 JSHandle<EcmaString> str0 = factory->NewFromASCII("abABc");
1643 JSHandle<EcmaString> str1 = factory->NewFromASCII("AB");
1644 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1645 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1646 }
1647
HWTEST_F_L0(RegExpTest,ParseAndExec38)1648 HWTEST_F_L0(RegExpTest, ParseAndExec38)
1649 {
1650 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1651 RegExpParser parser = RegExpParser(thread, chunk_);
1652 CString source("^(([a-z]+)*[a-z]\\.)+[a-z]{2,}$");
1653 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1654 parser.Parse();
1655 bool parseResult = parser.IsError();
1656 ASSERT_FALSE(parseResult);
1657 RegExpExecutor executor(regExpCachedChunk_);
1658 CString input("www.netscape.com");
1659 bool ret =
1660 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1661 ASSERT_TRUE(ret);
1662 executor.GetResult(thread);
1663 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1664 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1665 JSHandle<EcmaString> inputStr = factory->NewFromASCII("www.netscape.com");
1666 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1667 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1668 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1669 JSHandle<EcmaString> str0 = factory->NewFromASCII("www.netscape.com");
1670 JSHandle<EcmaString> str1 = factory->NewFromASCII("netscape.");
1671 JSHandle<EcmaString> str2 = factory->NewFromASCII("netscap");
1672 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1673 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1674 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1675 }
1676
HWTEST_F_L0(RegExpTest,ParseAndExec39)1677 HWTEST_F_L0(RegExpTest, ParseAndExec39)
1678 {
1679 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1680 RegExpParser parser = RegExpParser(thread, chunk_);
1681 CString source("(a*)b\\1+");
1682 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1683 parser.Parse();
1684 bool parseResult = parser.IsError();
1685 ASSERT_FALSE(parseResult);
1686 RegExpExecutor executor(regExpCachedChunk_);
1687 CString input("baaaac");
1688 bool ret =
1689 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1690 ASSERT_TRUE(ret);
1691 executor.GetResult(thread);
1692 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1693 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1694 JSHandle<EcmaString> inputStr = factory->NewFromASCII("baaaac");
1695 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1696 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1697 JSHandle<EcmaString> str0 = factory->NewFromASCII("b");
1698 JSHandle<EcmaString> str1 = factory->NewFromASCII("");
1699 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1700 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1701 }
1702
HWTEST_F_L0(RegExpTest,ParseAndExec40)1703 HWTEST_F_L0(RegExpTest, ParseAndExec40)
1704 {
1705 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1706 RegExpParser parser = RegExpParser(thread, chunk_);
1707 CString source("a*?");
1708 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1709 parser.Parse();
1710 bool parseResult = parser.IsError();
1711 ASSERT_FALSE(parseResult);
1712 RegExpExecutor executor(regExpCachedChunk_);
1713 CString input("ab");
1714 bool ret =
1715 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1716 ASSERT_TRUE(ret);
1717 executor.GetResult(thread);
1718 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1719 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1720 JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab");
1721 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1722 JSHandle<EcmaString> str = factory->NewFromASCII("");
1723 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1724 }
1725
HWTEST_F_L0(RegExpTest,ParseAndExec41)1726 HWTEST_F_L0(RegExpTest, ParseAndExec41)
1727 {
1728 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1729 RegExpParser parser = RegExpParser(thread, chunk_);
1730 CString source("(.*?)a(?!(a+)b\\2c)\\2(.*)");
1731 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1732 parser.Parse();
1733 bool parseResult = parser.IsError();
1734 ASSERT_FALSE(parseResult);
1735 RegExpExecutor executor(regExpCachedChunk_);
1736 CString input("baaabaac");
1737 bool ret =
1738 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1739 ASSERT_TRUE(ret);
1740 executor.GetResult(thread);
1741 JSHandle<builtins::RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1742 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 4U);
1743 JSHandle<EcmaString> inputStr = factory->NewFromASCII("baaabaac");
1744 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1745 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1746 JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1747 JSHandle<EcmaString> str0 = factory->NewFromASCII("baaabaac");
1748 JSHandle<EcmaString> str1 = factory->NewFromASCII("ba");
1749 JSHandle<EcmaString> str3 = factory->NewFromASCII("abaac");
1750 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1751 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1752 ASSERT_EQ(regExpGlobalResult->GetEndOfCaptureIndex(2).GetInt(), -1);
1753 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1754 }
1755
HWTEST_F_L0(RegExpTest,ParseAndExec42)1756 HWTEST_F_L0(RegExpTest, ParseAndExec42)
1757 {
1758 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1759 RegExpParser parser = RegExpParser(thread, chunk_);
1760 CString source("[a-c\\d]+");
1761 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1762 parser.Parse();
1763 bool parseResult = parser.IsError();
1764 ASSERT_FALSE(parseResult);
1765 RegExpExecutor executor(regExpCachedChunk_);
1766 CString input("\n\n\\abc324234");
1767 bool ret =
1768 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1769 ASSERT_TRUE(ret);
1770 executor.GetResult(thread);
1771 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1772 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1773 JSHandle<EcmaString> inputStr = factory->NewFromASCII("\n\n\\abc324234");
1774 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1775 JSHandle<EcmaString> str = factory->NewFromASCII("abc324234");
1776 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1777 }
1778
HWTEST_F_L0(RegExpTest,ParseAndExec43)1779 HWTEST_F_L0(RegExpTest, ParseAndExec43)
1780 {
1781 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1782 RegExpParser parser = RegExpParser(thread, chunk_);
1783 CString source("[\\d][\n][^\\d]");
1784 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1785 parser.Parse();
1786 bool parseResult = parser.IsError();
1787 ASSERT_FALSE(parseResult);
1788 RegExpExecutor executor(regExpCachedChunk_);
1789 CString input("line1\nline2");
1790 bool ret =
1791 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1792 ASSERT_TRUE(ret);
1793 executor.GetResult(thread);
1794 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1795 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1796 JSHandle<EcmaString> inputStr = factory->NewFromASCII("line1\nline2");
1797 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1798 JSHandle<EcmaString> str = factory->NewFromASCII("1\nl");
1799 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1800 }
1801
HWTEST_F_L0(RegExpTest,ParseAndExec44)1802 HWTEST_F_L0(RegExpTest, ParseAndExec44)
1803 {
1804 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1805 RegExpParser parser = RegExpParser(thread, chunk_);
1806 CString source(".[\b].");
1807 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1808 parser.Parse();
1809 bool parseResult = parser.IsError();
1810 ASSERT_FALSE(parseResult);
1811 RegExpExecutor executor(regExpCachedChunk_);
1812 CString input("abc\bdef");
1813 bool ret =
1814 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1815 ASSERT_TRUE(ret);
1816 executor.GetResult(thread);
1817 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1818 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1819 JSHandle<EcmaString> inputStr = factory->NewFromASCII("abc\bdef");
1820 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1821 JSHandle<EcmaString> str = factory->NewFromASCII("c\bd");
1822 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1823 }
1824
HWTEST_F_L0(RegExpTest,ParseAndExec45)1825 HWTEST_F_L0(RegExpTest, ParseAndExec45)
1826 {
1827 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1828 RegExpParser parser = RegExpParser(thread, chunk_);
1829 CString source("[^\b]+");
1830 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1831 parser.Parse();
1832 bool parseResult = parser.IsError();
1833 ASSERT_FALSE(parseResult);
1834 RegExpExecutor executor(regExpCachedChunk_);
1835 CString input("easy\bto\u0008ride");
1836 bool ret =
1837 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1838 ASSERT_TRUE(ret);
1839 executor.GetResult(thread);
1840 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1841 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1842 JSHandle<EcmaString> inputStr = factory->NewFromASCII("easy\bto\u0008ride");
1843 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1844 JSHandle<EcmaString> str = factory->NewFromASCII("easy");
1845 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1846 }
1847
HWTEST_F_L0(RegExpTest,ParseAndExec46)1848 HWTEST_F_L0(RegExpTest, ParseAndExec46)
1849 {
1850 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1851 RegExpParser parser = RegExpParser(thread, chunk_);
1852 CString source("([\\S]+([ \t]+[\\S]+)*)[ \t]*=[ \t]*[\\S]+");
1853 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1854 parser.Parse();
1855 bool parseResult = parser.IsError();
1856 ASSERT_FALSE(parseResult);
1857 RegExpExecutor executor(regExpCachedChunk_);
1858 CString input("Course_Creator = Test");
1859 bool ret =
1860 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1861 ASSERT_TRUE(ret);
1862 executor.GetResult(thread);
1863 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1864 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 3U);
1865 JSHandle<EcmaString> inputStr = factory->NewFromASCII("Course_Creator = Test");
1866 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1867 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1868 JSHandle<EcmaString> str0 = factory->NewFromASCII("Course_Creator = Test");
1869 JSHandle<EcmaString> str1 = factory->NewFromASCII("Course_Creator");
1870 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1871 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1872 }
1873
HWTEST_F_L0(RegExpTest,ParseAndExec47)1874 HWTEST_F_L0(RegExpTest, ParseAndExec47)
1875 {
1876 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1877 RegExpParser parser = RegExpParser(thread, chunk_);
1878 CString source("[^o]t\\b");
1879 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1880 parser.Parse();
1881 bool parseResult = parser.IsError();
1882 ASSERT_FALSE(parseResult);
1883 RegExpExecutor executor(regExpCachedChunk_);
1884 CString input("pilOt\nsoviet robot\topenoffice");
1885 bool ret =
1886 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1887 ASSERT_TRUE(ret);
1888 executor.GetResult(thread);
1889 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1890 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1891 JSHandle<EcmaString> inputStr = factory->NewFromASCII("pilOt\nsoviet robot\topenoffice");
1892 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
1893 JSHandle<EcmaString> str = factory->NewFromASCII("et");
1894 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
1895 }
1896
HWTEST_F_L0(RegExpTest,ParseAndExec49)1897 HWTEST_F_L0(RegExpTest, ParseAndExec49)
1898 {
1899 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1900 RegExpParser parser = RegExpParser(thread, chunk_);
1901 CString source("(a(b)\\4(5)(5))");
1902 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 2);
1903 parser.Parse();
1904 bool parseResult = parser.IsError();
1905 ASSERT_FALSE(parseResult);
1906 RegExpExecutor executor(regExpCachedChunk_);
1907 CString input("ab55");
1908 bool ret =
1909 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1910 ASSERT_TRUE(ret);
1911 executor.GetResult(thread);
1912 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1913 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 5U);
1914 JSHandle<EcmaString> inputStr = factory->NewFromASCII("ab55");
1915 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1916 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1917 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1918 JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1919 JSHandle<EcmaString> resultStr4 = GetSubString(regExpGlobalResult, inputStr, 4);
1920 JSHandle<EcmaString> str0 = factory->NewFromASCII("ab55");
1921 JSHandle<EcmaString> str1 = factory->NewFromASCII("ab55");
1922 JSHandle<EcmaString> str2 = factory->NewFromASCII("b");
1923 JSHandle<EcmaString> str3 = factory->NewFromASCII("5");
1924 JSHandle<EcmaString> str4 = factory->NewFromASCII("5");
1925 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1926 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1927 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1928 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1929 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr4, str4) == 0);
1930 }
1931
HWTEST_F_L0(RegExpTest,ParseAndExec50)1932 HWTEST_F_L0(RegExpTest, ParseAndExec50)
1933 {
1934 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1935 RegExpParser parser = RegExpParser(thread, chunk_);
1936 CString source("(?<year>\\d{4})-(?<date>\\d{2}-(?<day>\\d\\d))");
1937 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1938 parser.Parse();
1939 bool parseResult = parser.IsError();
1940 ASSERT_FALSE(parseResult);
1941 RegExpExecutor executor(regExpCachedChunk_);
1942 CString input("2020-12-31");
1943 bool ret =
1944 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1945 ASSERT_TRUE(ret);
1946 executor.GetResult(thread);
1947 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1948 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 4U);
1949 JSHandle<EcmaString> inputStr = factory->NewFromASCII("2020-12-31");
1950 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
1951 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
1952 JSHandle<EcmaString> resultStr2 = GetSubString(regExpGlobalResult, inputStr, 2);
1953 JSHandle<EcmaString> resultStr3 = GetSubString(regExpGlobalResult, inputStr, 3);
1954 JSHandle<EcmaString> str0 = factory->NewFromASCII("2020-12-31");
1955 JSHandle<EcmaString> str1 = factory->NewFromASCII("2020");
1956 JSHandle<EcmaString> str2 = factory->NewFromASCII("12-31");
1957 JSHandle<EcmaString> str3 = factory->NewFromASCII("31");
1958 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
1959 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
1960 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr2, str2) == 0);
1961 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr3, str3) == 0);
1962 }
1963
HWTEST_F_L0(RegExpTest,ParseAndExec51)1964 HWTEST_F_L0(RegExpTest, ParseAndExec51)
1965 {
1966 RegExpParser parser = RegExpParser(thread, chunk_);
1967 CString source("\\u0000");
1968 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1969 parser.Parse();
1970 bool parseResult = parser.IsError();
1971 ASSERT_FALSE(parseResult);
1972 RegExpExecutor executor(regExpCachedChunk_);
1973 std::u16string input(u"\u0000");
1974 bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length() + 1,
1975 parser.GetOriginBuffer(), true);
1976 ASSERT_TRUE(ret);
1977 executor.GetResult(thread);
1978 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1979 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
1980 }
1981
HWTEST_F_L0(RegExpTest,ParseAndExec52)1982 HWTEST_F_L0(RegExpTest, ParseAndExec52)
1983 {
1984 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1985 RegExpParser parser = RegExpParser(thread, chunk_);
1986 CString source("(aa).+\\1");
1987 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
1988 parser.Parse();
1989 bool parseResult = parser.IsError();
1990 ASSERT_FALSE(parseResult);
1991 RegExpExecutor executor(regExpCachedChunk_);
1992 CString input("aabcdaabcd");
1993 bool ret =
1994 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(), parser.GetOriginBuffer());
1995 ASSERT_TRUE(ret);
1996 executor.GetResult(thread);
1997 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
1998 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 2U);
1999 JSHandle<EcmaString> inputStr = factory->NewFromASCII("aabcdaabcd");
2000 JSHandle<EcmaString> resultStr0 = GetSubString(regExpGlobalResult, inputStr, 0);
2001 JSHandle<EcmaString> resultStr1 = GetSubString(regExpGlobalResult, inputStr, 1);
2002 JSHandle<EcmaString> str0 = factory->NewFromASCII("aabcdaa");
2003 JSHandle<EcmaString> str1 = factory->NewFromASCII("aa");
2004 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr0, str0) == 0);
2005 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr1, str1) == 0);
2006 }
2007
HWTEST_F_L0(RegExpTest,ParseAndExec53)2008 HWTEST_F_L0(RegExpTest, ParseAndExec53)
2009 {
2010 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2011 RegExpParser parser = RegExpParser(thread, chunk_);
2012 CString source("\\x01");
2013 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2014 parser.Parse();
2015 bool parseResult = parser.IsError();
2016 ASSERT_FALSE(parseResult);
2017 RegExpExecutor executor(regExpCachedChunk_);
2018 std::u16string input(u"\u0001");
2019 bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2020 parser.GetOriginBuffer(), true);
2021 ASSERT_TRUE(ret);
2022 executor.GetResult(thread);
2023 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
2024 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2025 JSHandle<EcmaString> inputStr = factory->NewFromUtf16(u"\u0001");
2026 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2027 JSHandle<EcmaString> str = factory->NewFromASCII("\u0001");
2028 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2029 }
2030
HWTEST_F_L0(RegExpTest,ParseAndExec54)2031 HWTEST_F_L0(RegExpTest, ParseAndExec54)
2032 {
2033 RegExpParser parser = RegExpParser(thread, chunk_);
2034 CString source("\\bot");
2035 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2036 parser.Parse();
2037 bool parseResult = parser.IsError();
2038 ASSERT_FALSE(parseResult);
2039 RegExpExecutor executor(regExpCachedChunk_);
2040 CString input("pilot\nsoviet robot\topenoffice");
2041 bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2042 parser.GetOriginBuffer(), false);
2043 ASSERT_FALSE(ret);
2044 }
2045
HWTEST_F_L0(RegExpTest,ParseAndExec55)2046 HWTEST_F_L0(RegExpTest, ParseAndExec55)
2047 {
2048 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2049 RegExpParser parser = RegExpParser(thread, chunk_);
2050 CString source("e\\b");
2051 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2052 parser.Parse();
2053 bool parseResult = parser.IsError();
2054 ASSERT_FALSE(parseResult);
2055 RegExpExecutor executor(regExpCachedChunk_);
2056 CString input("c\u0065");
2057 bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2058 parser.GetOriginBuffer(), false);
2059 ASSERT_TRUE(ret);
2060 executor.GetResult(thread);
2061 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
2062 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2063 JSHandle<EcmaString> inputStr = factory->NewFromASCII("c\u0065");
2064 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2065 JSHandle<EcmaString> str = factory->NewFromASCII("e");
2066 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2067 }
2068
HWTEST_F_L0(RegExpTest,ParseAndExec56)2069 HWTEST_F_L0(RegExpTest, ParseAndExec56)
2070 {
2071 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2072 RegExpParser parser = RegExpParser(thread, chunk_);
2073 CString source("a啊");
2074 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2075 parser.Parse();
2076 bool parseResult = parser.IsError();
2077 ASSERT_FALSE(parseResult);
2078 RegExpExecutor executor(regExpCachedChunk_);
2079 std::u16string input(u"a啊");
2080 bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.length(),
2081 parser.GetOriginBuffer(), true);
2082 ASSERT_TRUE(ret);
2083 executor.GetResult(thread);
2084 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
2085 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2086 JSHandle<EcmaString> inputStr = factory->NewFromUtf16(u"a啊");
2087 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2088 JSHandle<EcmaString> str = factory->NewFromUtf8("a啊");
2089 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2090 }
2091
HWTEST_F_L0(RegExpTest,ParseAndExec57)2092 HWTEST_F_L0(RegExpTest, ParseAndExec57)
2093 {
2094 RegExpParser parser = RegExpParser(thread, chunk_);
2095 CString source("\\udf06");
2096 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 16);
2097 parser.Parse();
2098 bool parseResult = parser.IsError();
2099 ASSERT_FALSE(parseResult);
2100 RegExpExecutor executor(regExpCachedChunk_);
2101 char16_t data[] = {0xd834, 0xdf06};
2102 bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(data), 0, 2, parser.GetOriginBuffer(), true);
2103 ASSERT_FALSE(ret);
2104 }
2105
HWTEST_F_L0(RegExpTest,ParseAndExec58)2106 HWTEST_F_L0(RegExpTest, ParseAndExec58)
2107 {
2108 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2109 RegExpParser parser = RegExpParser(thread, chunk_);
2110 CString source("\\udf06");
2111 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2112 parser.Parse();
2113 bool parseResult = parser.IsError();
2114 ASSERT_FALSE(parseResult);
2115 RegExpExecutor executor(regExpCachedChunk_);
2116 char16_t data[] = {0xd834, 0xdf06};
2117 bool ret = executor.Execute(reinterpret_cast<const uint8_t *>(data), 0, 2, parser.GetOriginBuffer(), true);
2118 ASSERT_TRUE(ret);
2119 executor.GetResult(thread);
2120 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
2121 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2122 JSHandle<EcmaString> inputStr = factory->NewFromUtf16(reinterpret_cast<const uint16_t *>(data), 2);
2123 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2124 char16_t data1[] = {0xdf06};
2125 JSHandle<EcmaString> str = factory->NewFromUtf16(reinterpret_cast<const uint16_t *>(data1), 1);
2126 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2127 }
2128
HWTEST_F_L0(RegExpTest,ParseAndExec59)2129 HWTEST_F_L0(RegExpTest, ParseAndExec59)
2130 {
2131 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2132 RegExpParser parser = RegExpParser(thread, chunk_);
2133 CString source("\\v");
2134 parser.Init(const_cast<char *>(reinterpret_cast<const char *>(source.c_str())), source.size(), 0);
2135 parser.Parse();
2136 bool parseResult = parser.IsError();
2137 ASSERT_FALSE(parseResult);
2138
2139 RegExpExecutor executor(regExpCachedChunk_);
2140 CString input("\u000B");
2141 bool ret =
2142 executor.Execute(reinterpret_cast<const uint8_t *>(input.c_str()), 0, input.size(), parser.GetOriginBuffer());
2143 ASSERT_TRUE(ret);
2144
2145 executor.GetResult(thread);
2146 JSHandle<RegExpGlobalResult> regExpGlobalResult(thread->GetGlobalEnv()->GetRegExpGlobalResult());
2147 ASSERT_EQ(regExpGlobalResult->GetTotalCaptureCounts().GetInt(), 1U);
2148 JSHandle<EcmaString> inputStr = factory->NewFromASCII("\u000B");
2149 JSHandle<EcmaString> resultStr = GetSubString(regExpGlobalResult, inputStr, 0);
2150 JSHandle<EcmaString> str = factory->NewFromASCII("\u000B");
2151 ASSERT_TRUE(EcmaStringAccessor::Compare(instance, resultStr, str) == 0);
2152 }
2153
HWTEST_F_L0(RegExpTest,RangeSet1)2154 HWTEST_F_L0(RegExpTest, RangeSet1)
2155 {
2156 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2157 std::make_pair(1, 1),
2158 std::make_pair(2, 2),
2159 std::make_pair(3, 3),
2160 };
2161 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2162 std::make_pair(1, 5),
2163 };
2164 RangeSet rangeResult(listInput);
2165 RangeSet rangeExpected(listExpected);
2166 rangeResult.Insert(4, 5);
2167 rangeResult.Compress();
2168 EXPECT_EQ(rangeResult, rangeExpected);
2169 }
2170
HWTEST_F_L0(RegExpTest,RangeSet2)2171 HWTEST_F_L0(RegExpTest, RangeSet2)
2172 {
2173 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2174 std::make_pair(4, 5),
2175 };
2176 RangeSet rangeResult;
2177 RangeSet rangeExpected(listExpected);
2178 rangeResult.Insert(4, 5);
2179 rangeResult.Compress();
2180 EXPECT_EQ(rangeResult, rangeExpected);
2181 }
2182
HWTEST_F_L0(RegExpTest,RangeSet3)2183 HWTEST_F_L0(RegExpTest, RangeSet3)
2184 {
2185 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2186 std::make_pair(2, 2),
2187 };
2188 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2189 std::make_pair(1, 5),
2190 };
2191 RangeSet rangeResult(listInput);
2192 RangeSet rangeExpected(listExpected);
2193 rangeResult.Insert(1, 5);
2194 rangeResult.Compress();
2195 EXPECT_EQ(rangeResult, rangeExpected);
2196 }
2197
HWTEST_F_L0(RegExpTest,RangeSet4)2198 HWTEST_F_L0(RegExpTest, RangeSet4)
2199 {
2200 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2201 std::make_pair(1, 5),
2202 };
2203 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2204 std::make_pair(1, 5),
2205 };
2206 RangeSet rangeResult(listInput);
2207 RangeSet rangeExpected(listExpected);
2208 rangeResult.Insert(2, 4);
2209 rangeResult.Compress();
2210 EXPECT_EQ(rangeResult, rangeExpected);
2211 }
2212
HWTEST_F_L0(RegExpTest,RangeSet5)2213 HWTEST_F_L0(RegExpTest, RangeSet5)
2214 {
2215 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2216 std::make_pair(1, 2),
2217 std::make_pair(9, UINT16_MAX),
2218 };
2219 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2220 std::make_pair(1, 2),
2221 std::make_pair(4, 7),
2222 std::make_pair(9, UINT16_MAX),
2223 };
2224 RangeSet rangeResult(listInput);
2225 RangeSet rangeExpected(listExpected);
2226 rangeResult.Insert(4, 7);
2227 rangeResult.Compress();
2228 EXPECT_EQ(rangeResult, rangeExpected);
2229 }
2230
HWTEST_F_L0(RegExpTest,RangeSet6)2231 HWTEST_F_L0(RegExpTest, RangeSet6)
2232 {
2233 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2234 std::make_pair(0, UINT16_MAX),
2235 };
2236 RangeSet rangeResult;
2237 RangeSet rangeExpected(listExpected);
2238 rangeResult.Invert(false);
2239 EXPECT_EQ(rangeResult, rangeExpected);
2240 }
2241
HWTEST_F_L0(RegExpTest,RangeSet7)2242 HWTEST_F_L0(RegExpTest, RangeSet7)
2243 {
2244 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2245 std::make_pair(1, 5),
2246 };
2247 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2248 std::make_pair(0, 0),
2249 std::make_pair(6, UINT16_MAX),
2250 };
2251 RangeSet rangeResult(listInput);
2252 RangeSet rangeExpected(listExpected);
2253 rangeResult.Invert(false);
2254 EXPECT_EQ(rangeResult, rangeExpected);
2255 }
2256
HWTEST_F_L0(RegExpTest,RangeSet8)2257 HWTEST_F_L0(RegExpTest, RangeSet8)
2258 {
2259 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2260 std::make_pair(1, 5),
2261 std::make_pair(0xfffe, UINT16_MAX),
2262 };
2263 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2264 std::make_pair(0, 0),
2265 std::make_pair(6, 0xfffd),
2266 };
2267 RangeSet rangeResult(listInput);
2268 RangeSet rangeExpected(listExpected);
2269 rangeResult.Invert(false);
2270 EXPECT_EQ(rangeResult, rangeExpected);
2271 }
2272
HWTEST_F_L0(RegExpTest,RangeSet9)2273 HWTEST_F_L0(RegExpTest, RangeSet9)
2274 {
2275 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2276 std::make_pair(0, 5),
2277 std::make_pair(0xfffe, 0xfffe),
2278 };
2279 std::list<std::pair<uint32_t, uint32_t>> listExpected = {
2280 std::make_pair(6, 0xfffd),
2281 std::make_pair(UINT16_MAX, UINT16_MAX),
2282 };
2283 RangeSet rangeResult(listInput);
2284 RangeSet rangeExpected(listExpected);
2285 rangeResult.Invert(false);
2286 EXPECT_EQ(rangeResult, rangeExpected);
2287 }
2288
HWTEST_F_L0(RegExpTest,RangeSet10)2289 HWTEST_F_L0(RegExpTest, RangeSet10)
2290 {
2291 std::list<std::pair<uint32_t, uint32_t>> listInput = {
2292 std::make_pair(0, UINT16_MAX),
2293 };
2294 RangeSet rangeResult(listInput);
2295 RangeSet rangeExpected;
2296 rangeResult.Invert(false);
2297 EXPECT_EQ(rangeResult, rangeExpected);
2298 }
2299 } // namespace panda::test
2300