| OLD | NEW |
| (Empty) |
| 1 // Copyright 2006 The RE2 Authors. All Rights Reserved. | |
| 2 // Use of this source code is governed by a BSD-style | |
| 3 // license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Test parse.cc, dump.cc, and tostring.cc. | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 #include "util/test.h" | |
| 10 #include "re2/regexp.h" | |
| 11 | |
| 12 namespace re2 { | |
| 13 | |
| 14 // Test that overflowed ref counts work. | |
| 15 TEST(Regexp, BigRef) { | |
| 16 Regexp* re; | |
| 17 re = Regexp::Parse("x", Regexp::NoParseFlags, NULL); | |
| 18 for (int i = 0; i < 100000; i++) | |
| 19 re->Incref(); | |
| 20 for (int i = 0; i < 100000; i++) | |
| 21 re->Decref(); | |
| 22 CHECK_EQ(re->Ref(), 1); | |
| 23 re->Decref(); | |
| 24 } | |
| 25 | |
| 26 // Test that very large Concats work. | |
| 27 // Depends on overflowed ref counts working. | |
| 28 TEST(Regexp, BigConcat) { | |
| 29 Regexp* x; | |
| 30 x = Regexp::Parse("x", Regexp::NoParseFlags, NULL); | |
| 31 vector<Regexp*> v(90000, x); // ToString bails out at 100000 | |
| 32 for (size_t i = 0; i < v.size(); i++) | |
| 33 x->Incref(); | |
| 34 CHECK_EQ(x->Ref(), 1 + static_cast<int>(v.size())) << x->Ref(); | |
| 35 Regexp* re = Regexp::Concat(v.data(), static_cast<int>(v.size()), | |
| 36 Regexp::NoParseFlags); | |
| 37 CHECK_EQ(re->ToString(), string(v.size(), 'x')); | |
| 38 re->Decref(); | |
| 39 CHECK_EQ(x->Ref(), 1) << x->Ref(); | |
| 40 x->Decref(); | |
| 41 } | |
| 42 | |
| 43 TEST(Regexp, NamedCaptures) { | |
| 44 Regexp* x; | |
| 45 RegexpStatus status; | |
| 46 x = Regexp::Parse( | |
| 47 "(?P<g1>a+)|(e)(?P<g2>w*)+(?P<g1>b+)", Regexp::PerlX, &status); | |
| 48 EXPECT_TRUE(status.ok()); | |
| 49 EXPECT_EQ(4, x->NumCaptures()); | |
| 50 const map<string, int>* have = x->NamedCaptures(); | |
| 51 EXPECT_TRUE(have != NULL); | |
| 52 EXPECT_EQ(2, have->size()); // there are only two named groups in | |
| 53 // the regexp: 'g1' and 'g2'. | |
| 54 map<string, int> want; | |
| 55 want["g1"] = 1; | |
| 56 want["g2"] = 3; | |
| 57 EXPECT_EQ(want, *have); | |
| 58 x->Decref(); | |
| 59 delete have; | |
| 60 } | |
| 61 | |
| 62 TEST(Regexp, CaptureNames) { | |
| 63 Regexp* x; | |
| 64 RegexpStatus status; | |
| 65 x = Regexp::Parse( | |
| 66 "(?P<g1>a+)|(e)(?P<g2>w*)+(?P<g1>b+)", Regexp::PerlX, &status); | |
| 67 EXPECT_TRUE(status.ok()); | |
| 68 EXPECT_EQ(4, x->NumCaptures()); | |
| 69 const map<int, string>* have = x->CaptureNames(); | |
| 70 EXPECT_TRUE(have != NULL); | |
| 71 EXPECT_EQ(3, have->size()); | |
| 72 map<int, string> want; | |
| 73 want[1] = "g1"; | |
| 74 want[3] = "g2"; | |
| 75 want[4] = "g1"; | |
| 76 | |
| 77 EXPECT_EQ(want, *have); | |
| 78 x->Decref(); | |
| 79 delete have; | |
| 80 } | |
| 81 | |
| 82 } // namespace re2 | |
| OLD | NEW |