10d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Copyright 2009 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#ifndef RE2_TESTING_EXHAUSTIVE_TESTER_H__
60d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#define RE2_TESTING_EXHAUSTIVE_TESTER_H__
70d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
80d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include <string>
90d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include <vector>
100d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "util/util.h"
110d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "re2/testing/regexp_generator.h"
120d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "re2/testing/string_generator.h"
130d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
140d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinnamespace re2 {
150d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
160d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Exhaustive regular expression test: generate all regexps within parameters,
170d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// then generate all strings of a given length over a given alphabet,
180d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// then check that NFA, DFA, and PCRE agree about whether each regexp matches
190d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// each possible string, and if so, where the match is.
200d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin//
210d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Can also be used in a "random" mode that generates a given number
220d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// of random regexp and strings, allowing testing of larger expressions
230d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// and inputs.
240d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinclass ExhaustiveTester : public RegexpGenerator {
250d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin public:
260d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  ExhaustiveTester(int maxatoms,
270d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   int maxops,
280d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   const vector<string>& alphabet,
290d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   const vector<string>& ops,
300d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   int maxstrlen,
310d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   const vector<string>& stralphabet,
320d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   const string& wrapper,
330d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                   const string& topwrapper)
340d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    : RegexpGenerator(maxatoms, maxops, alphabet, ops),
350d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin      strgen_(maxstrlen, stralphabet),
360d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin      wrapper_(wrapper),
370d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin      topwrapper_(topwrapper),
380d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin      regexps_(0), tests_(0), failures_(0),
390d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin      randomstrings_(0), stringseed_(0), stringcount_(0)  { }
400d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
410d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int regexps()  { return regexps_; }
420d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int tests()    { return tests_; }
430d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int failures() { return failures_; }
440d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
450d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  // Needed for RegexpGenerator interface.
460d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  void HandleRegexp(const string& regexp);
470d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
480d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  // Causes testing to generate random input strings.
490d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  void RandomStrings(int32 seed, int32 count) {
500d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    randomstrings_ = true;
510d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    stringseed_ = seed;
520d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin    stringcount_ = count;
530d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  }
540d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
550d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin private:
560d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  StringGenerator strgen_;
570d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  string wrapper_;      // Regexp wrapper - either empty or has one %s.
580d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  string topwrapper_;   // Regexp top-level wrapper.
590d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int regexps_;   // Number of HandleRegexp calls
600d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int tests_;     // Number of regexp tests.
610d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int failures_;  // Number of tests failed.
620d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
630d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  bool randomstrings_;  // Whether to use random strings
640d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int32 stringseed_;    // If so, the seed.
650d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  int stringcount_;     // If so, how many to generate.
660d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin  DISALLOW_EVIL_CONSTRUCTORS(ExhaustiveTester);
670d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin};
680d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
690d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Runs an exhaustive test on the given parameters.
700d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinvoid ExhaustiveTest(int maxatoms, int maxops,
710d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                    const vector<string>& alphabet,
720d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                    const vector<string>& ops,
730d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                    int maxstrlen, const vector<string>& stralphabet,
740d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                    const string& wrapper,
750d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin                    const string& topwrapper);
760d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
770d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Runs an exhaustive test using the given parameters and
780d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// the basic egrep operators.
790d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinvoid EgrepTest(int maxatoms, int maxops, const string& alphabet,
800d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin               int maxstrlen, const string& stralphabet,
810d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin               const string& wrapper);
820d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
830d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin}  // namespace re2
840d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin
850d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#endif  // RE2_TESTING_EXHAUSTIVE_TESTER_H__
86