Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
regextest.cpp
Go to the documentation of this file.
1// Tencent is pleased to support the open source community by making RapidJSON available.
2//
3// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4//
5// Licensed under the MIT License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// http://opensource.org/licenses/MIT
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "unittest.h"
17
18using namespace rapidjson::internal;
19
20TEST(Regex, Single) {
21 Regex re("a");
22 ASSERT_TRUE(re.IsValid());
23 RegexSearch rs(re);
24 EXPECT_TRUE(rs.Match("a"));
25 EXPECT_FALSE(rs.Match(""));
26 EXPECT_FALSE(rs.Match("b"));
27}
28
29TEST(Regex, Concatenation) {
30 Regex re("abc");
31 ASSERT_TRUE(re.IsValid());
32 RegexSearch rs(re);
33 EXPECT_TRUE(rs.Match("abc"));
34 EXPECT_FALSE(rs.Match(""));
35 EXPECT_FALSE(rs.Match("a"));
36 EXPECT_FALSE(rs.Match("b"));
37 EXPECT_FALSE(rs.Match("ab"));
38 EXPECT_FALSE(rs.Match("abcd"));
39}
40
41TEST(Regex, Alternation1) {
42 Regex re("abab|abbb");
43 ASSERT_TRUE(re.IsValid());
44 RegexSearch rs(re);
45 EXPECT_TRUE(rs.Match("abab"));
46 EXPECT_TRUE(rs.Match("abbb"));
47 EXPECT_FALSE(rs.Match(""));
48 EXPECT_FALSE(rs.Match("ab"));
49 EXPECT_FALSE(rs.Match("ababa"));
50 EXPECT_FALSE(rs.Match("abb"));
51 EXPECT_FALSE(rs.Match("abbbb"));
52}
53
54TEST(Regex, Alternation2) {
55 Regex re("a|b|c");
56 ASSERT_TRUE(re.IsValid());
57 RegexSearch rs(re);
58 EXPECT_TRUE(rs.Match("a"));
59 EXPECT_TRUE(rs.Match("b"));
60 EXPECT_TRUE(rs.Match("c"));
61 EXPECT_FALSE(rs.Match(""));
62 EXPECT_FALSE(rs.Match("aa"));
63 EXPECT_FALSE(rs.Match("ab"));
64}
65
66TEST(Regex, Parenthesis1) {
67 Regex re("(ab)c");
68 ASSERT_TRUE(re.IsValid());
69 RegexSearch rs(re);
70 EXPECT_TRUE(rs.Match("abc"));
71 EXPECT_FALSE(rs.Match(""));
72 EXPECT_FALSE(rs.Match("a"));
73 EXPECT_FALSE(rs.Match("b"));
74 EXPECT_FALSE(rs.Match("ab"));
75 EXPECT_FALSE(rs.Match("abcd"));
76}
77
78TEST(Regex, Parenthesis2) {
79 Regex re("a(bc)");
80 ASSERT_TRUE(re.IsValid());
81 RegexSearch rs(re);
82 EXPECT_TRUE(rs.Match("abc"));
83 EXPECT_FALSE(rs.Match(""));
84 EXPECT_FALSE(rs.Match("a"));
85 EXPECT_FALSE(rs.Match("b"));
86 EXPECT_FALSE(rs.Match("ab"));
87 EXPECT_FALSE(rs.Match("abcd"));
88}
89
90TEST(Regex, Parenthesis3) {
91 Regex re("(a|b)(c|d)");
92 ASSERT_TRUE(re.IsValid());
93 RegexSearch rs(re);
94 EXPECT_TRUE(rs.Match("ac"));
95 EXPECT_TRUE(rs.Match("ad"));
96 EXPECT_TRUE(rs.Match("bc"));
97 EXPECT_TRUE(rs.Match("bd"));
98 EXPECT_FALSE(rs.Match(""));
99 EXPECT_FALSE(rs.Match("ab"));
100 EXPECT_FALSE(rs.Match("cd"));
101}
102
103TEST(Regex, ZeroOrOne1) {
104 Regex re("a?");
105 ASSERT_TRUE(re.IsValid());
106 RegexSearch rs(re);
107 EXPECT_TRUE(rs.Match(""));
108 EXPECT_TRUE(rs.Match("a"));
109 EXPECT_FALSE(rs.Match("aa"));
110}
111
112TEST(Regex, ZeroOrOne2) {
113 Regex re("a?b");
114 ASSERT_TRUE(re.IsValid());
115 RegexSearch rs(re);
116 EXPECT_TRUE(rs.Match("b"));
117 EXPECT_TRUE(rs.Match("ab"));
118 EXPECT_FALSE(rs.Match("a"));
119 EXPECT_FALSE(rs.Match("aa"));
120 EXPECT_FALSE(rs.Match("bb"));
121 EXPECT_FALSE(rs.Match("ba"));
122}
123
124TEST(Regex, ZeroOrOne3) {
125 Regex re("ab?");
126 ASSERT_TRUE(re.IsValid());
127 RegexSearch rs(re);
128 EXPECT_TRUE(rs.Match("a"));
129 EXPECT_TRUE(rs.Match("ab"));
130 EXPECT_FALSE(rs.Match("b"));
131 EXPECT_FALSE(rs.Match("aa"));
132 EXPECT_FALSE(rs.Match("bb"));
133 EXPECT_FALSE(rs.Match("ba"));
134}
135
136TEST(Regex, ZeroOrOne4) {
137 Regex re("a?b?");
138 ASSERT_TRUE(re.IsValid());
139 RegexSearch rs(re);
140 EXPECT_TRUE(rs.Match(""));
141 EXPECT_TRUE(rs.Match("a"));
142 EXPECT_TRUE(rs.Match("b"));
143 EXPECT_TRUE(rs.Match("ab"));
144 EXPECT_FALSE(rs.Match("aa"));
145 EXPECT_FALSE(rs.Match("bb"));
146 EXPECT_FALSE(rs.Match("ba"));
147 EXPECT_FALSE(rs.Match("abc"));
148}
149
150TEST(Regex, ZeroOrOne5) {
151 Regex re("a(ab)?b");
152 ASSERT_TRUE(re.IsValid());
153 RegexSearch rs(re);
154 EXPECT_TRUE(rs.Match("ab"));
155 EXPECT_TRUE(rs.Match("aabb"));
156 EXPECT_FALSE(rs.Match("aab"));
157 EXPECT_FALSE(rs.Match("abb"));
158}
159
160TEST(Regex, ZeroOrMore1) {
161 Regex re("a*");
162 ASSERT_TRUE(re.IsValid());
163 RegexSearch rs(re);
164 EXPECT_TRUE(rs.Match(""));
165 EXPECT_TRUE(rs.Match("a"));
166 EXPECT_TRUE(rs.Match("aa"));
167 EXPECT_FALSE(rs.Match("b"));
168 EXPECT_FALSE(rs.Match("ab"));
169}
170
171TEST(Regex, ZeroOrMore2) {
172 Regex re("a*b");
173 ASSERT_TRUE(re.IsValid());
174 RegexSearch rs(re);
175 EXPECT_TRUE(rs.Match("b"));
176 EXPECT_TRUE(rs.Match("ab"));
177 EXPECT_TRUE(rs.Match("aab"));
178 EXPECT_FALSE(rs.Match(""));
179 EXPECT_FALSE(rs.Match("bb"));
180}
181
182TEST(Regex, ZeroOrMore3) {
183 Regex re("a*b*");
184 ASSERT_TRUE(re.IsValid());
185 RegexSearch rs(re);
186 EXPECT_TRUE(rs.Match(""));
187 EXPECT_TRUE(rs.Match("a"));
188 EXPECT_TRUE(rs.Match("aa"));
189 EXPECT_TRUE(rs.Match("b"));
190 EXPECT_TRUE(rs.Match("bb"));
191 EXPECT_TRUE(rs.Match("ab"));
192 EXPECT_TRUE(rs.Match("aabb"));
193 EXPECT_FALSE(rs.Match("ba"));
194}
195
196TEST(Regex, ZeroOrMore4) {
197 Regex re("a(ab)*b");
198 ASSERT_TRUE(re.IsValid());
199 RegexSearch rs(re);
200 EXPECT_TRUE(rs.Match("ab"));
201 EXPECT_TRUE(rs.Match("aabb"));
202 EXPECT_TRUE(rs.Match("aababb"));
203 EXPECT_FALSE(rs.Match(""));
204 EXPECT_FALSE(rs.Match("aa"));
205}
206
207TEST(Regex, OneOrMore1) {
208 Regex re("a+");
209 ASSERT_TRUE(re.IsValid());
210 RegexSearch rs(re);
211 EXPECT_TRUE(rs.Match("a"));
212 EXPECT_TRUE(rs.Match("aa"));
213 EXPECT_FALSE(rs.Match(""));
214 EXPECT_FALSE(rs.Match("b"));
215 EXPECT_FALSE(rs.Match("ab"));
216}
217
218TEST(Regex, OneOrMore2) {
219 Regex re("a+b");
220 ASSERT_TRUE(re.IsValid());
221 RegexSearch rs(re);
222 EXPECT_TRUE(rs.Match("ab"));
223 EXPECT_TRUE(rs.Match("aab"));
224 EXPECT_FALSE(rs.Match(""));
225 EXPECT_FALSE(rs.Match("b"));
226}
227
228TEST(Regex, OneOrMore3) {
229 Regex re("a+b+");
230 ASSERT_TRUE(re.IsValid());
231 RegexSearch rs(re);
232 EXPECT_TRUE(rs.Match("ab"));
233 EXPECT_TRUE(rs.Match("aab"));
234 EXPECT_TRUE(rs.Match("abb"));
235 EXPECT_TRUE(rs.Match("aabb"));
236 EXPECT_FALSE(rs.Match(""));
237 EXPECT_FALSE(rs.Match("b"));
238 EXPECT_FALSE(rs.Match("ba"));
239}
240
241TEST(Regex, OneOrMore4) {
242 Regex re("a(ab)+b");
243 ASSERT_TRUE(re.IsValid());
244 RegexSearch rs(re);
245 EXPECT_TRUE(rs.Match("aabb"));
246 EXPECT_TRUE(rs.Match("aababb"));
247 EXPECT_FALSE(rs.Match(""));
248 EXPECT_FALSE(rs.Match("ab"));
249}
250
251TEST(Regex, QuantifierExact1) {
252 Regex re("ab{3}c");
253 ASSERT_TRUE(re.IsValid());
254 RegexSearch rs(re);
255 EXPECT_TRUE(rs.Match("abbbc"));
256 EXPECT_FALSE(rs.Match("ac"));
257 EXPECT_FALSE(rs.Match("abc"));
258 EXPECT_FALSE(rs.Match("abbc"));
259 EXPECT_FALSE(rs.Match("abbbbc"));
260}
261
262TEST(Regex, QuantifierExact2) {
263 Regex re("a(bc){3}d");
264 ASSERT_TRUE(re.IsValid());
265 RegexSearch rs(re);
266 EXPECT_TRUE(rs.Match("abcbcbcd"));
267 EXPECT_FALSE(rs.Match("ad"));
268 EXPECT_FALSE(rs.Match("abcd"));
269 EXPECT_FALSE(rs.Match("abcbcd"));
270 EXPECT_FALSE(rs.Match("abcbcbcbcd"));
271}
272
273TEST(Regex, QuantifierExact3) {
274 Regex re("a(b|c){3}d");
275 ASSERT_TRUE(re.IsValid());
276 RegexSearch rs(re);
277 EXPECT_TRUE(rs.Match("abbbd"));
278 EXPECT_TRUE(rs.Match("acccd"));
279 EXPECT_TRUE(rs.Match("abcbd"));
280 EXPECT_FALSE(rs.Match("ad"));
281 EXPECT_FALSE(rs.Match("abbd"));
282 EXPECT_FALSE(rs.Match("accccd"));
283 EXPECT_FALSE(rs.Match("abbbbd"));
284}
285
286TEST(Regex, QuantifierMin1) {
287 Regex re("ab{3,}c");
288 ASSERT_TRUE(re.IsValid());
289 RegexSearch rs(re);
290 EXPECT_TRUE(rs.Match("abbbc"));
291 EXPECT_TRUE(rs.Match("abbbbc"));
292 EXPECT_TRUE(rs.Match("abbbbbc"));
293 EXPECT_FALSE(rs.Match("ac"));
294 EXPECT_FALSE(rs.Match("abc"));
295 EXPECT_FALSE(rs.Match("abbc"));
296}
297
298TEST(Regex, QuantifierMin2) {
299 Regex re("a(bc){3,}d");
300 ASSERT_TRUE(re.IsValid());
301 RegexSearch rs(re);
302 EXPECT_TRUE(rs.Match("abcbcbcd"));
303 EXPECT_TRUE(rs.Match("abcbcbcbcd"));
304 EXPECT_FALSE(rs.Match("ad"));
305 EXPECT_FALSE(rs.Match("abcd"));
306 EXPECT_FALSE(rs.Match("abcbcd"));
307}
308
309TEST(Regex, QuantifierMin3) {
310 Regex re("a(b|c){3,}d");
311 ASSERT_TRUE(re.IsValid());
312 RegexSearch rs(re);
313 EXPECT_TRUE(rs.Match("abbbd"));
314 EXPECT_TRUE(rs.Match("acccd"));
315 EXPECT_TRUE(rs.Match("abcbd"));
316 EXPECT_TRUE(rs.Match("accccd"));
317 EXPECT_TRUE(rs.Match("abbbbd"));
318 EXPECT_FALSE(rs.Match("ad"));
319 EXPECT_FALSE(rs.Match("abbd"));
320}
321
322TEST(Regex, QuantifierMinMax1) {
323 Regex re("ab{3,5}c");
324 ASSERT_TRUE(re.IsValid());
325 RegexSearch rs(re);
326 EXPECT_TRUE(rs.Match("abbbc"));
327 EXPECT_TRUE(rs.Match("abbbbc"));
328 EXPECT_TRUE(rs.Match("abbbbbc"));
329 EXPECT_FALSE(rs.Match("ac"));
330 EXPECT_FALSE(rs.Match("abc"));
331 EXPECT_FALSE(rs.Match("abbc"));
332 EXPECT_FALSE(rs.Match("abbbbbbc"));
333}
334
335TEST(Regex, QuantifierMinMax2) {
336 Regex re("a(bc){3,5}d");
337 ASSERT_TRUE(re.IsValid());
338 RegexSearch rs(re);
339 EXPECT_TRUE(rs.Match("abcbcbcd"));
340 EXPECT_TRUE(rs.Match("abcbcbcbcd"));
341 EXPECT_TRUE(rs.Match("abcbcbcbcbcd"));
342 EXPECT_FALSE(rs.Match("ad"));
343 EXPECT_FALSE(rs.Match("abcd"));
344 EXPECT_FALSE(rs.Match("abcbcd"));
345 EXPECT_FALSE(rs.Match("abcbcbcbcbcbcd"));
346}
347
348TEST(Regex, QuantifierMinMax3) {
349 Regex re("a(b|c){3,5}d");
350 ASSERT_TRUE(re.IsValid());
351 RegexSearch rs(re);
352 EXPECT_TRUE(rs.Match("abbbd"));
353 EXPECT_TRUE(rs.Match("acccd"));
354 EXPECT_TRUE(rs.Match("abcbd"));
355 EXPECT_TRUE(rs.Match("accccd"));
356 EXPECT_TRUE(rs.Match("abbbbd"));
357 EXPECT_TRUE(rs.Match("acccccd"));
358 EXPECT_TRUE(rs.Match("abbbbbd"));
359 EXPECT_FALSE(rs.Match("ad"));
360 EXPECT_FALSE(rs.Match("abbd"));
361 EXPECT_FALSE(rs.Match("accccccd"));
362 EXPECT_FALSE(rs.Match("abbbbbbd"));
363}
364
365// Issue538
366TEST(Regex, QuantifierMinMax4) {
367 Regex re("a(b|c){0,3}d");
368 ASSERT_TRUE(re.IsValid());
369 RegexSearch rs(re);
370 EXPECT_TRUE(rs.Match("ad"));
371 EXPECT_TRUE(rs.Match("abd"));
372 EXPECT_TRUE(rs.Match("acd"));
373 EXPECT_TRUE(rs.Match("abbd"));
374 EXPECT_TRUE(rs.Match("accd"));
375 EXPECT_TRUE(rs.Match("abcd"));
376 EXPECT_TRUE(rs.Match("abbbd"));
377 EXPECT_TRUE(rs.Match("acccd"));
378 EXPECT_FALSE(rs.Match("abbbbd"));
379 EXPECT_FALSE(rs.Match("add"));
380 EXPECT_FALSE(rs.Match("accccd"));
381 EXPECT_FALSE(rs.Match("abcbcd"));
382}
383
384// Issue538
385TEST(Regex, QuantifierMinMax5) {
386 Regex re("a(b|c){0,}d");
387 ASSERT_TRUE(re.IsValid());
388 RegexSearch rs(re);
389 EXPECT_TRUE(rs.Match("ad"));
390 EXPECT_TRUE(rs.Match("abd"));
391 EXPECT_TRUE(rs.Match("acd"));
392 EXPECT_TRUE(rs.Match("abbd"));
393 EXPECT_TRUE(rs.Match("accd"));
394 EXPECT_TRUE(rs.Match("abcd"));
395 EXPECT_TRUE(rs.Match("abbbd"));
396 EXPECT_TRUE(rs.Match("acccd"));
397 EXPECT_TRUE(rs.Match("abbbbd"));
398 EXPECT_TRUE(rs.Match("accccd"));
399 EXPECT_TRUE(rs.Match("abcbcd"));
400 EXPECT_FALSE(rs.Match("add"));
401 EXPECT_FALSE(rs.Match("aad"));
402}
403
404#define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 rsquence of Euro sign U+20AC
405
406TEST(Regex, Unicode) {
407 Regex re("a" EURO "+b");
408 ASSERT_TRUE(re.IsValid());
409 RegexSearch rs(re);
410 EXPECT_TRUE(rs.Match("a" EURO "b"));
411 EXPECT_TRUE(rs.Match("a" EURO EURO "b"));
412 EXPECT_FALSE(rs.Match("a?b"));
413 EXPECT_FALSE(rs.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match
414}
415
416TEST(Regex, AnyCharacter) {
417 Regex re(".");
418 ASSERT_TRUE(re.IsValid());
419 RegexSearch rs(re);
420 EXPECT_TRUE(rs.Match("a"));
421 EXPECT_TRUE(rs.Match("b"));
422 EXPECT_TRUE(rs.Match(EURO));
423 EXPECT_FALSE(rs.Match(""));
424 EXPECT_FALSE(rs.Match("aa"));
425}
426
427TEST(Regex, CharacterRange1) {
428 Regex re("[abc]");
429 ASSERT_TRUE(re.IsValid());
430 RegexSearch rs(re);
431 EXPECT_TRUE(rs.Match("a"));
432 EXPECT_TRUE(rs.Match("b"));
433 EXPECT_TRUE(rs.Match("c"));
434 EXPECT_FALSE(rs.Match(""));
435 EXPECT_FALSE(rs.Match("`"));
436 EXPECT_FALSE(rs.Match("d"));
437 EXPECT_FALSE(rs.Match("aa"));
438}
439
440TEST(Regex, CharacterRange2) {
441 Regex re("[^abc]");
442 ASSERT_TRUE(re.IsValid());
443 RegexSearch rs(re);
444 EXPECT_TRUE(rs.Match("`"));
445 EXPECT_TRUE(rs.Match("d"));
446 EXPECT_FALSE(rs.Match("a"));
447 EXPECT_FALSE(rs.Match("b"));
448 EXPECT_FALSE(rs.Match("c"));
449 EXPECT_FALSE(rs.Match(""));
450 EXPECT_FALSE(rs.Match("aa"));
451}
452
453TEST(Regex, CharacterRange3) {
454 Regex re("[a-c]");
455 ASSERT_TRUE(re.IsValid());
456 RegexSearch rs(re);
457 EXPECT_TRUE(rs.Match("a"));
458 EXPECT_TRUE(rs.Match("b"));
459 EXPECT_TRUE(rs.Match("c"));
460 EXPECT_FALSE(rs.Match(""));
461 EXPECT_FALSE(rs.Match("`"));
462 EXPECT_FALSE(rs.Match("d"));
463 EXPECT_FALSE(rs.Match("aa"));
464}
465
466TEST(Regex, CharacterRange4) {
467 Regex re("[^a-c]");
468 ASSERT_TRUE(re.IsValid());
469 RegexSearch rs(re);
470 EXPECT_TRUE(rs.Match("`"));
471 EXPECT_TRUE(rs.Match("d"));
472 EXPECT_FALSE(rs.Match("a"));
473 EXPECT_FALSE(rs.Match("b"));
474 EXPECT_FALSE(rs.Match("c"));
475 EXPECT_FALSE(rs.Match(""));
476 EXPECT_FALSE(rs.Match("aa"));
477}
478
479TEST(Regex, CharacterRange5) {
480 Regex re("[-]");
481 ASSERT_TRUE(re.IsValid());
482 RegexSearch rs(re);
483 EXPECT_TRUE(rs.Match("-"));
484 EXPECT_FALSE(rs.Match(""));
485 EXPECT_FALSE(rs.Match("a"));
486}
487
488TEST(Regex, CharacterRange6) {
489 Regex re("[a-]");
490 ASSERT_TRUE(re.IsValid());
491 RegexSearch rs(re);
492 EXPECT_TRUE(rs.Match("a"));
493 EXPECT_TRUE(rs.Match("-"));
494 EXPECT_FALSE(rs.Match(""));
495 EXPECT_FALSE(rs.Match("`"));
496 EXPECT_FALSE(rs.Match("b"));
497}
498
499TEST(Regex, CharacterRange7) {
500 Regex re("[-a]");
501 ASSERT_TRUE(re.IsValid());
502 RegexSearch rs(re);
503 EXPECT_TRUE(rs.Match("a"));
504 EXPECT_TRUE(rs.Match("-"));
505 EXPECT_FALSE(rs.Match(""));
506 EXPECT_FALSE(rs.Match("`"));
507 EXPECT_FALSE(rs.Match("b"));
508}
509
510TEST(Regex, CharacterRange8) {
511 Regex re("[a-zA-Z0-9]*");
512 ASSERT_TRUE(re.IsValid());
513 RegexSearch rs(re);
514 EXPECT_TRUE(rs.Match("Milo"));
515 EXPECT_TRUE(rs.Match("MT19937"));
516 EXPECT_TRUE(rs.Match("43"));
517 EXPECT_FALSE(rs.Match("a_b"));
518 EXPECT_FALSE(rs.Match("!"));
519}
520
521TEST(Regex, Search) {
522 Regex re("abc");
523 ASSERT_TRUE(re.IsValid());
524 RegexSearch rs(re);
525 EXPECT_TRUE(rs.Search("abc"));
526 EXPECT_TRUE(rs.Search("_abc"));
527 EXPECT_TRUE(rs.Search("abc_"));
528 EXPECT_TRUE(rs.Search("_abc_"));
529 EXPECT_TRUE(rs.Search("__abc__"));
530 EXPECT_TRUE(rs.Search("abcabc"));
531 EXPECT_FALSE(rs.Search("a"));
532 EXPECT_FALSE(rs.Search("ab"));
533 EXPECT_FALSE(rs.Search("bc"));
534 EXPECT_FALSE(rs.Search("cba"));
535}
536
537TEST(Regex, Search_BeginAnchor) {
538 Regex re("^abc");
539 ASSERT_TRUE(re.IsValid());
540 RegexSearch rs(re);
541 EXPECT_TRUE(rs.Search("abc"));
542 EXPECT_TRUE(rs.Search("abc_"));
543 EXPECT_TRUE(rs.Search("abcabc"));
544 EXPECT_FALSE(rs.Search("_abc"));
545 EXPECT_FALSE(rs.Search("_abc_"));
546 EXPECT_FALSE(rs.Search("a"));
547 EXPECT_FALSE(rs.Search("ab"));
548 EXPECT_FALSE(rs.Search("bc"));
549 EXPECT_FALSE(rs.Search("cba"));
550}
551
552TEST(Regex, Search_EndAnchor) {
553 Regex re("abc$");
554 ASSERT_TRUE(re.IsValid());
555 RegexSearch rs(re);
556 EXPECT_TRUE(rs.Search("abc"));
557 EXPECT_TRUE(rs.Search("_abc"));
558 EXPECT_TRUE(rs.Search("abcabc"));
559 EXPECT_FALSE(rs.Search("abc_"));
560 EXPECT_FALSE(rs.Search("_abc_"));
561 EXPECT_FALSE(rs.Search("a"));
562 EXPECT_FALSE(rs.Search("ab"));
563 EXPECT_FALSE(rs.Search("bc"));
564 EXPECT_FALSE(rs.Search("cba"));
565}
566
567TEST(Regex, Search_BothAnchor) {
568 Regex re("^abc$");
569 ASSERT_TRUE(re.IsValid());
570 RegexSearch rs(re);
571 EXPECT_TRUE(rs.Search("abc"));
572 EXPECT_FALSE(rs.Search(""));
573 EXPECT_FALSE(rs.Search("a"));
574 EXPECT_FALSE(rs.Search("b"));
575 EXPECT_FALSE(rs.Search("ab"));
576 EXPECT_FALSE(rs.Search("abcd"));
577}
578
579TEST(Regex, Escape) {
580 const char* s = "\\^\\$\\|\\(\\)\\?\\*\\+\\.\\[\\]\\{\\}\\\\\\f\\n\\r\\t\\v[\\b][\\[][\\]]";
581 Regex re(s);
582 ASSERT_TRUE(re.IsValid());
583 RegexSearch rs(re);
584 EXPECT_TRUE(rs.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]"));
585 EXPECT_FALSE(rs.Match(s)); // Not escaping
586}
587
588TEST(Regex, Invalid) {
589#define TEST_INVALID(s) \
590 {\
591 Regex re(s);\
592 EXPECT_FALSE(re.IsValid());\
593 }
594
595 TEST_INVALID("");
596 TEST_INVALID("a|");
597 TEST_INVALID("()");
598 TEST_INVALID("(");
599 TEST_INVALID(")");
600 TEST_INVALID("(a))");
601 TEST_INVALID("(a|)");
602 TEST_INVALID("(a||b)");
603 TEST_INVALID("(|b)");
604 TEST_INVALID("?");
605 TEST_INVALID("*");
606 TEST_INVALID("+");
607 TEST_INVALID("{");
608 TEST_INVALID("{}");
609 TEST_INVALID("a{a}");
610 TEST_INVALID("a{0}");
611 TEST_INVALID("a{-1}");
612 TEST_INVALID("a{}");
613 // TEST_INVALID("a{0,}"); // Support now
614 TEST_INVALID("a{,0}");
615 TEST_INVALID("a{1,0}");
616 TEST_INVALID("a{-1,0}");
617 TEST_INVALID("a{-1,1}");
618 TEST_INVALID("a{4294967296}"); // overflow of unsigned
619 TEST_INVALID("a{1a}");
620 TEST_INVALID("[");
621 TEST_INVALID("[]");
622 TEST_INVALID("[^]");
623 TEST_INVALID("[\\a]");
624 TEST_INVALID("\\a");
625
626#undef TEST_INVALID
627}
628
629TEST(Regex, Issue538) {
630 Regex re("^[0-9]+(\\\\.[0-9]+){0,2}");
631 EXPECT_TRUE(re.IsValid());
632}
633
634TEST(Regex, Issue583) {
635 Regex re("[0-9]{99999}");
636 ASSERT_TRUE(re.IsValid());
637}
638
639#undef EURO
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define ASSERT_TRUE(condition)
Definition gtest.h:1901
#define EXPECT_FALSE(condition)
Definition gtest.h:1898
#define EURO
#define TEST_INVALID(s)
char * s