exhaustive_tester.h revision 0d4c52358a1af421705c54bd8a9fdd8a30558a2e
1// Copyright 2009 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#ifndef RE2_TESTING_EXHAUSTIVE_TESTER_H__
6#define RE2_TESTING_EXHAUSTIVE_TESTER_H__
7
8#include <string>
9#include <vector>
10#include "util/util.h"
11#include "re2/testing/regexp_generator.h"
12#include "re2/testing/string_generator.h"
13
14namespace re2 {
15
16// Exhaustive regular expression test: generate all regexps within parameters,
17// then generate all strings of a given length over a given alphabet,
18// then check that NFA, DFA, and PCRE agree about whether each regexp matches
19// each possible string, and if so, where the match is.
20//
21// Can also be used in a "random" mode that generates a given number
22// of random regexp and strings, allowing testing of larger expressions
23// and inputs.
24class ExhaustiveTester : public RegexpGenerator {
25 public:
26  ExhaustiveTester(int maxatoms,
27                   int maxops,
28                   const vector<string>& alphabet,
29                   const vector<string>& ops,
30                   int maxstrlen,
31                   const vector<string>& stralphabet,
32                   const string& wrapper,
33                   const string& topwrapper)
34    : RegexpGenerator(maxatoms, maxops, alphabet, ops),
35      strgen_(maxstrlen, stralphabet),
36      wrapper_(wrapper),
37      topwrapper_(topwrapper),
38      regexps_(0), tests_(0), failures_(0),
39      randomstrings_(0), stringseed_(0), stringcount_(0)  { }
40
41  int regexps()  { return regexps_; }
42  int tests()    { return tests_; }
43  int failures() { return failures_; }
44
45  // Needed for RegexpGenerator interface.
46  void HandleRegexp(const string& regexp);
47
48  // Causes testing to generate random input strings.
49  void RandomStrings(int32 seed, int32 count) {
50    randomstrings_ = true;
51    stringseed_ = seed;
52    stringcount_ = count;
53  }
54
55 private:
56  StringGenerator strgen_;
57  string wrapper_;      // Regexp wrapper - either empty or has one %s.
58  string topwrapper_;   // Regexp top-level wrapper.
59  int regexps_;   // Number of HandleRegexp calls
60  int tests_;     // Number of regexp tests.
61  int failures_;  // Number of tests failed.
62
63  bool randomstrings_;  // Whether to use random strings
64  int32 stringseed_;    // If so, the seed.
65  int stringcount_;     // If so, how many to generate.
66  DISALLOW_EVIL_CONSTRUCTORS(ExhaustiveTester);
67};
68
69// Runs an exhaustive test on the given parameters.
70void ExhaustiveTest(int maxatoms, int maxops,
71                    const vector<string>& alphabet,
72                    const vector<string>& ops,
73                    int maxstrlen, const vector<string>& stralphabet,
74                    const string& wrapper,
75                    const string& topwrapper);
76
77// Runs an exhaustive test using the given parameters and
78// the basic egrep operators.
79void EgrepTest(int maxatoms, int maxops, const string& alphabet,
80               int maxstrlen, const string& stralphabet,
81               const string& wrapper);
82
83}  // namespace re2
84
85#endif  // RE2_TESTING_EXHAUSTIVE_TESTER_H__
86