aboutsummaryrefslogtreecommitdiff
path: root/re2/testing/possible_match_test.cc
blob: 7c2400eb5b4320670859db9a2624c761e36c357c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2006-2008 The RE2 Authors.  All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include <vector>
#include "util/test.h"
#include "re2/prog.h"
#include "re2/re2.h"
#include "re2/regexp.h"
#include "re2/testing/regexp_generator.h"
#include "re2/testing/string_generator.h"

namespace re2 {

// Test that C++ strings are compared as uint8s, not int8s.
// PossibleMatchRange doesn't depend on this, but callers probably will.
TEST(CplusplusStrings, EightBit) {
  string s = "\x70";
  string t = "\xA0";
  EXPECT_LT(s, t);
}

struct PrefixTest {
  const char* regexp;
  int maxlen;
  const char* min;
  const char* max;
};

static PrefixTest tests[] = {
  { "",                  10,  "",           "",        },
  { "Abcdef",            10,  "Abcdef",     "Abcdef"   },
  { "abc(def|ghi)",      10,  "abcdef",     "abcghi"   },
  { "a+hello",           10,  "aa",         "ahello"   },
  { "a*hello",           10,  "a",          "hello"    },
  { "def|abc",           10,  "abc",        "def"      },
  { "a(b)(c)[d]",        10,  "abcd",       "abcd"     },
  { "ab(cab|cat)",       10,  "abcab",      "abcat"    },
  { "ab(cab|ca)x",       10,  "abcabx",     "abcax"    },
  { "(ab|x)(c|de)",      10,  "abc",        "xde"      },
  { "(ab|x)?(c|z)?",     10,  "",           "z"        },
  { "[^\\s\\S]",         10,  "",           ""         },
  { "(abc)+",             5,  "abc",        "abcac"    },
  { "(abc)+",             2,  "ab",         "ac"       },
  { "(abc)+",             1,  "a",          "b"        },
  { "[a\xC3\xA1]",        4,  "a",          "\xC3\xA1" },
  { "a*",                10,  "",           "ab"       },

  { "(?i)Abcdef",        10,  "ABCDEF",     "abcdef"   },
  { "(?i)abc(def|ghi)",  10,  "ABCDEF",     "abcghi"   },
  { "(?i)a+hello",       10,  "AA",         "ahello"   },
  { "(?i)a*hello",       10,  "A",          "hello"    },
  { "(?i)def|abc",       10,  "ABC",        "def"      },
  { "(?i)a(b)(c)[d]",    10,  "ABCD",       "abcd"     },
  { "(?i)ab(cab|cat)",   10,  "ABCAB",      "abcat"    },
  { "(?i)ab(cab|ca)x",   10,  "ABCABX",     "abcax"    },
  { "(?i)(ab|x)(c|de)",  10,  "ABC",        "xde"      },
  { "(?i)(ab|x)?(c|z)?", 10,  "",           "z"        },
  { "(?i)[^\\s\\S]",     10,  "",           ""         },
  { "(?i)(abc)+",         5,  "ABC",        "abcac"    },
  { "(?i)(abc)+",         2,  "AB",         "ac"       },
  { "(?i)(abc)+",         1,  "A",          "b"        },
  { "(?i)[a\xC3\xA1]",    4,  "A",          "\xC3\xA1" },
  { "(?i)a*",            10,  "",           "ab"       },
  { "(?i)A*",            10,  "",           "ab"       },

  { "\\AAbcdef",         10,  "Abcdef",     "Abcdef"   },
  { "\\Aabc(def|ghi)",   10,  "abcdef",     "abcghi"   },
  { "\\Aa+hello",        10,  "aa",         "ahello"   },
  { "\\Aa*hello",        10,  "a",          "hello"    },
  { "\\Adef|abc",        10,  "abc",        "def"      },
  { "\\Aa(b)(c)[d]",     10,  "abcd",       "abcd"     },
  { "\\Aab(cab|cat)",    10,  "abcab",      "abcat"    },
  { "\\Aab(cab|ca)x",    10,  "abcabx",     "abcax"    },
  { "\\A(ab|x)(c|de)",   10,  "abc",        "xde"      },
  { "\\A(ab|x)?(c|z)?",  10,  "",           "z"        },
  { "\\A[^\\s\\S]",      10,  "",           ""         },
  { "\\A(abc)+",          5,  "abc",        "abcac"    },
  { "\\A(abc)+",          2,  "ab",         "ac"       },
  { "\\A(abc)+",          1,  "a",          "b"        },
  { "\\A[a\xC3\xA1]",     4,  "a",          "\xC3\xA1" },
  { "\\Aa*",             10,  "",           "ab"       },

  { "(?i)\\AAbcdef",         10,  "ABCDEF",     "abcdef"   },
  { "(?i)\\Aabc(def|ghi)",   10,  "ABCDEF",     "abcghi"   },
  { "(?i)\\Aa+hello",        10,  "AA",         "ahello"   },
  { "(?i)\\Aa*hello",        10,  "A",          "hello"    },
  { "(?i)\\Adef|abc",        10,  "ABC",        "def"      },
  { "(?i)\\Aa(b)(c)[d]",     10,  "ABCD",       "abcd"     },
  { "(?i)\\Aab(cab|cat)",    10,  "ABCAB",      "abcat"    },
  { "(?i)\\Aab(cab|ca)x",    10,  "ABCABX",     "abcax"    },
  { "(?i)\\A(ab|x)(c|de)",   10,  "ABC",        "xde"      },
  { "(?i)\\A(ab|x)?(c|z)?",  10,  "",           "z"        },
  { "(?i)\\A[^\\s\\S]",      10,  "",           ""         },
  { "(?i)\\A(abc)+",          5,  "ABC",        "abcac"    },
  { "(?i)\\A(abc)+",          2,  "AB",         "ac"       },
  { "(?i)\\A(abc)+",          1,  "A",          "b"        },
  { "(?i)\\A[a\xC3\xA1]",     4,  "A",          "\xC3\xA1" },
  { "(?i)\\Aa*",             10,  "",           "ab"       },
  { "(?i)\\AA*",             10,  "",           "ab"       },
};

TEST(PossibleMatchRange, HandWritten) {
  for (int i = 0; i < arraysize(tests); i++) {
    for (int j = 0; j < 2; j++) {
      const PrefixTest& t = tests[i];
      string min, max;
      if (j == 0) {
        LOG(INFO) << "Checking regexp=" << CEscape(t.regexp);
        Regexp* re = Regexp::Parse(t.regexp, Regexp::LikePerl, NULL);
        CHECK(re);
        Prog* prog = re->CompileToProg(0);
        CHECK(prog);
        CHECK(prog->PossibleMatchRange(&min, &max, t.maxlen))
          << " " << t.regexp;
        delete prog;
        re->Decref();
      } else {
        CHECK(RE2(t.regexp).PossibleMatchRange(&min, &max, t.maxlen));
      }
      EXPECT_EQ(t.min, min) << t.regexp;
      EXPECT_EQ(t.max, max) << t.regexp;
    }
  }
}

// Test cases where PossibleMatchRange should return false.
TEST(PossibleMatchRange, Failures) {
  string min, max;

  // Fails because no room to write max.
  EXPECT_FALSE(RE2("abc").PossibleMatchRange(&min, &max, 0));

  // Fails because there is no max -- any non-empty string matches
  // or begins a match.  Have to use Latin-1 input, because there
  // are no valid UTF-8 strings beginning with byte 0xFF.
  EXPECT_FALSE(RE2("[\\s\\S]+", RE2::Latin1).
               PossibleMatchRange(&min, &max, 10))
    << "min=" << CEscape(min) << ", max=" << CEscape(max);
  EXPECT_FALSE(RE2("[\\0-\xFF]+", RE2::Latin1).
               PossibleMatchRange(&min, &max, 10))
    << "min=" << CEscape(min) << ", max=" << CEscape(max);
  EXPECT_FALSE(RE2(".+hello", RE2::Latin1).
               PossibleMatchRange(&min, &max, 10))
    << "min=" << CEscape(min) << ", max=" << CEscape(max);
  EXPECT_FALSE(RE2(".*hello", RE2::Latin1).
               PossibleMatchRange(&min, &max, 10))
    << "min=" << CEscape(min) << ", max=" << CEscape(max);
  EXPECT_FALSE(RE2(".*", RE2::Latin1).
               PossibleMatchRange(&min, &max, 10))
    << "min=" << CEscape(min) << ", max=" << CEscape(max);
  EXPECT_FALSE(RE2("\\C*").
               PossibleMatchRange(&min, &max, 10))
    << "min=" << CEscape(min) << ", max=" << CEscape(max);

  // Fails because it's a malformed regexp.
  EXPECT_FALSE(RE2("*hello").PossibleMatchRange(&min, &max, 10))
    << "min=" << CEscape(min) << ", max=" << CEscape(max);
}

// Exhaustive test: generate all regexps within parameters,
// then generate all strings of a given length over a given alphabet,
// then check that the prefix information agrees with whether
// the regexp matches each of the strings.
class PossibleMatchTester : public RegexpGenerator {
 public:
  PossibleMatchTester(int maxatoms,
                      int maxops,
                      const vector<string>& alphabet,
                      const vector<string>& ops,
                      int maxstrlen,
                      const vector<string>& stralphabet)
    : RegexpGenerator(maxatoms, maxops, alphabet, ops),
      strgen_(maxstrlen, stralphabet),
      regexps_(0), tests_(0) { }

  int regexps()  { return regexps_; }
  int tests()    { return tests_; }

  // Needed for RegexpGenerator interface.
  void HandleRegexp(const string& regexp);

 private:
  StringGenerator strgen_;

  int regexps_;   // Number of HandleRegexp calls
  int tests_;     // Number of regexp tests.

  DISALLOW_EVIL_CONSTRUCTORS(PossibleMatchTester);
};

// Processes a single generated regexp.
// Checks that all accepted strings agree with the prefix range.
void PossibleMatchTester::HandleRegexp(const string& regexp) {
  regexps_++;

  VLOG(3) << CEscape(regexp);

  RE2 re(regexp, RE2::Latin1);
  CHECK_EQ(re.error(), "");

  string min, max;
  if(!re.PossibleMatchRange(&min, &max, 10)) {
    // There's no good max for "\\C*".  Can't use strcmp
    // because sometimes it gets embedded in more
    // complicated expressions.
    if(strstr(regexp.c_str(), "\\C*"))
      return;
    LOG(QFATAL) << "PossibleMatchRange failed on: " << CEscape(regexp);
  }

  strgen_.Reset();
  while (strgen_.HasNext()) {
    const StringPiece& s = strgen_.Next();
    tests_++;
    if (!RE2::FullMatch(s, re))
      continue;
    CHECK_GE(s, min) << " regexp: " << regexp << " max: " << max;
    CHECK_LE(s, max) << " regexp: " << regexp << " min: " << min;
  }
}

TEST(PossibleMatchRange, Exhaustive) {
  int natom = 3;
  int noperator = 3;
  int stringlen = 5;
  if (DEBUG_MODE) {
    natom = 2;
    noperator = 3;
    stringlen = 3;
  }
  PossibleMatchTester t(natom, noperator, Split(" ", "a b [0-9]"),
                 RegexpGenerator::EgrepOps(),
                 stringlen, Explode("ab4"));
  t.Generate();
  LOG(INFO) << t.regexps() << " regexps, "
            << t.tests() << " tests";
}

}  // namespace re2