re2/re2/testing/regexp_test.cc
Go to the documentation of this file.
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 <stddef.h>
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "util/test.h"
13 #include "util/logging.h"
14 #include "re2/regexp.h"
15 
16 namespace re2 {
17 
18 // Test that overflowed ref counts work.
19 TEST(Regexp, BigRef) {
20  Regexp* re;
21  re = Regexp::Parse("x", Regexp::NoParseFlags, NULL);
22  for (int i = 0; i < 100000; i++)
23  re->Incref();
24  for (int i = 0; i < 100000; i++)
25  re->Decref();
26  ASSERT_EQ(re->Ref(), 1);
27  re->Decref();
28 }
29 
30 // Test that very large Concats work.
31 // Depends on overflowed ref counts working.
32 TEST(Regexp, BigConcat) {
33  Regexp* x;
34  x = Regexp::Parse("x", Regexp::NoParseFlags, NULL);
35  std::vector<Regexp*> v(90000, x); // ToString bails out at 100000
36  for (size_t i = 0; i < v.size(); i++)
37  x->Incref();
38  ASSERT_EQ(x->Ref(), 1 + static_cast<int>(v.size())) << x->Ref();
39  Regexp* re = Regexp::Concat(v.data(), static_cast<int>(v.size()),
41  ASSERT_EQ(re->ToString(), std::string(v.size(), 'x'));
42  re->Decref();
43  ASSERT_EQ(x->Ref(), 1) << x->Ref();
44  x->Decref();
45 }
46 
47 TEST(Regexp, NamedCaptures) {
48  Regexp* x;
49  RegexpStatus status;
50  x = Regexp::Parse(
51  "(?P<g1>a+)|(e)(?P<g2>w*)+(?P<g1>b+)", Regexp::PerlX, &status);
53  EXPECT_EQ(4, x->NumCaptures());
54  const std::map<std::string, int>* have = x->NamedCaptures();
55  EXPECT_TRUE(have != NULL);
56  EXPECT_EQ(2, have->size()); // there are only two named groups in
57  // the regexp: 'g1' and 'g2'.
58  std::map<std::string, int> want;
59  want["g1"] = 1;
60  want["g2"] = 3;
61  EXPECT_EQ(want, *have);
62  x->Decref();
63  delete have;
64 }
65 
66 TEST(Regexp, CaptureNames) {
67  Regexp* x;
68  RegexpStatus status;
69  x = Regexp::Parse(
70  "(?P<g1>a+)|(e)(?P<g2>w*)+(?P<g1>b+)", Regexp::PerlX, &status);
72  EXPECT_EQ(4, x->NumCaptures());
73  const std::map<int, std::string>* have = x->CaptureNames();
74  EXPECT_TRUE(have != NULL);
75  EXPECT_EQ(3, have->size());
76  std::map<int, std::string> want;
77  want[1] = "g1";
78  want[3] = "g2";
79  want[4] = "g1";
80 
81  EXPECT_EQ(want, *have);
82  x->Decref();
83  delete have;
84 }
85 
86 } // namespace re2
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
status
absl::Status status
Definition: rls.cc:251
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
re2::Regexp::Concat
static Regexp * Concat(Regexp **subs, int nsubs, ParseFlags flags)
Definition: bloaty/third_party/re2/re2/regexp.cc:286
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
re2::Regexp::NoParseFlags
@ NoParseFlags
Definition: bloaty/third_party/re2/re2/regexp.h:279
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
re2::Regexp::Parse
static Regexp * Parse(const StringPiece &s, ParseFlags flags, RegexpStatus *status)
Definition: bloaty/third_party/re2/re2/parse.cc:2200
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
re2::TEST
TEST(TestCharClassBuilder, Adds)
Definition: bloaty/third_party/re2/re2/testing/charclass_test.cc:198
re2::Regexp::PerlX
@ PerlX
Definition: bloaty/third_party/re2/re2/regexp.h:293
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:02