1 // Copyright 2008 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.
5 // Comparative tester for regular expression matching.
6 // Checks all implementations against each other.
8 #ifndef RE2_TESTING_TESTER_H__
9 #define RE2_TESTING_TESTER_H__
11 #include "re2/stringpiece.h"
13 #include "re2/regexp.h"
15 #include "util/pcre.h"
21 // All the supported regexp engines.
23 kEngineBacktrack = 0, // Prog::BadSearchBacktrack
24 kEngineNFA, // Prog::SearchNFA
25 kEngineDFA, // Prog::SearchDFA, only ask whether it matched
26 kEngineDFA1, // Prog::SearchDFA, ask for match[0]
27 kEngineOnePass, // Prog::SearchOnePass, if applicable
28 kEngineBitState, // Prog::SearchBitState
29 kEngineRE2, // RE2, all submatches
30 kEngineRE2a, // RE2, only ask for match[0]
31 kEngineRE2b, // RE2, only ask whether it matched
32 kEnginePCRE, // PCRE (util/pcre.h)
37 // Make normal math on the enum preserve the type.
38 // By default, C++ doesn't define ++ on enum, and e+1 has type int.
39 static inline void operator++(Engine& e, int unused) {
40 e = static_cast<Engine>(e+1);
43 static inline Engine operator+(Engine e, int i) {
44 return static_cast<Engine>(static_cast<int>(e)+i);
47 // A TestInstance caches per-regexp state for a given
48 // regular expression in a given configuration
49 // (UTF-8 vs Latin1, longest vs first match, etc.).
54 TestInstance(const StringPiece& regexp, Prog::MatchKind kind,
55 Regexp::ParseFlags flags);
57 Regexp::ParseFlags flags() { return flags_; }
58 bool error() { return error_; }
60 // Runs a single test case: search in text, which is in context,
61 // using the given anchoring.
62 bool RunCase(const StringPiece& text, const StringPiece& context,
66 // Runs a single search using the named engine type.
67 void RunSearch(Engine type,
68 const StringPiece& text, const StringPiece& context,
72 void LogMatch(const char* prefix, Engine e, const StringPiece& text,
73 const StringPiece& context, Prog::Anchor anchor);
75 const StringPiece& regexp_str_; // regexp being tested
76 Prog::MatchKind kind_; // kind of match
77 Regexp::ParseFlags flags_; // flags for parsing regexp_str_
78 bool error_; // error during constructor?
80 Regexp* regexp_; // parsed regexp
81 int num_captures_; // regexp_->NumCaptures() cached
82 Prog* prog_; // compiled program
83 Prog* rprog_; // compiled reverse program
84 PCRE* re_; // PCRE implementation
85 RE2* re2_; // RE2 implementation
87 DISALLOW_EVIL_CONSTRUCTORS(TestInstance);
90 // A group of TestInstances for all possible configurations.
93 explicit Tester(const StringPiece& regexp);
96 bool error() { return error_; }
98 // Runs a single test case: search in text, which is in context,
99 // using the given anchoring.
100 bool TestCase(const StringPiece& text, const StringPiece& context,
101 Prog::Anchor anchor);
103 // Run TestCase(text, text, anchor) for all anchoring modes.
104 bool TestInput(const StringPiece& text);
106 // Run TestCase(text, context, anchor) for all anchoring modes.
107 bool TestInputInContext(const StringPiece& text, const StringPiece& context);
111 vector<TestInstance*> v_;
113 DISALLOW_EVIL_CONSTRUCTORS(Tester);
116 // Run all possible tests using regexp and text.
117 bool TestRegexpOnText(const StringPiece& regexp, const StringPiece& text);
121 #endif // RE2_TESTING_TESTER_H__