10d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Copyright 2008 The RE2 Authors.  All Rights Reserved.
20d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Use of this source code is governed by a BSD-style
30d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// license that can be found in the LICENSE file.
40d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
50d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Exhaustive testing of regular expression matching.
60d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
70d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "util/test.h"
80d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "re2/testing/exhaustive_tester.h"
90d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
100d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinDECLARE_string(regexp_engines);
110d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
120d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinnamespace re2 {
130d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
140d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Test simple repetition operators
150d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(Repetition, Simple) {
160d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  vector<string> ops = Split(" ",
170d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    "%s{0} %s{0,} %s{1} %s{1,} %s{0,1} %s{0,2} "
180d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    "%s{1,2} %s{2} %s{2,} %s{3,4} %s{4,5} "
190d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    "%s* %s+ %s? %s*? %s+? %s??");
200d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  ExhaustiveTest(3, 2, Explode("abc."), ops,
210d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                 6, Explode("ab"), "(?:%s)", "");
220d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  ExhaustiveTest(3, 2, Explode("abc."), ops,
230d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                 40, Explode("a"), "(?:%s)", "");
240d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin}
250d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
260d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Test capturing parens -- (a) -- inside repetition operators
270d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(Repetition, Capturing) {
280d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  vector<string> ops = Split(" ",
290d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    "%s{0} %s{0,} %s{1} %s{1,} %s{0,1} %s{0,2} "
300d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    "%s{1,2} %s{2} %s{2,} %s{3,4} %s{4,5} "
310d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    "%s* %s+ %s? %s*? %s+? %s??");
320d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  ExhaustiveTest(3, 2, Split(" ", "a (a) b"), ops,
330d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                 7, Explode("ab"), "(?:%s)", "");
340d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
350d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  // This would be a great test, but it runs forever when PCRE is enabled.
360d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  if (strstr("PCRE", FLAGS_regexp_engines.c_str()) == NULL)
370d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    ExhaustiveTest(4, 3, Split(" ", "a (a)"), ops,
380d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   100, Explode("a"), "(?:%s)", "");
390d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin}
400d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
410d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin}  // namespace re2
420d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
43