bloaty/third_party/re2/re2/testing/random_test.cc
Go to the documentation of this file.
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.
4 
5 // Random testing of regular expression matching.
6 
7 #include <stdio.h>
8 #include <string>
9 #include <vector>
10 
11 #include "util/test.h"
12 #include "re2/testing/exhaustive_tester.h"
13 
14 DEFINE_int32(regexpseed, 404, "Random regexp seed.");
15 DEFINE_int32(regexpcount, 100, "How many random regexps to generate.");
16 DEFINE_int32(stringseed, 200, "Random string seed.");
17 DEFINE_int32(stringcount, 100, "How many random strings to generate.");
18 
19 namespace re2 {
20 
21 // Runs a random test on the given parameters.
22 // (Always uses the same random seeds for reproducibility.
23 // Can give different seeds on command line.)
24 static void RandomTest(int maxatoms, int maxops,
25  const std::vector<std::string>& alphabet,
26  const std::vector<std::string>& ops,
27  int maxstrlen,
28  const std::vector<std::string>& stralphabet,
29  const std::string& wrapper) {
30  // Limit to smaller test cases in debug mode,
31  // because everything is so much slower.
32  if (RE2_DEBUG_MODE) {
33  maxatoms--;
34  maxops--;
35  maxstrlen /= 2;
36  }
37 
38  ExhaustiveTester t(maxatoms, maxops, alphabet, ops,
39  maxstrlen, stralphabet, wrapper, "");
40  t.RandomStrings(FLAGS_stringseed, FLAGS_stringcount);
41  t.GenerateRandom(FLAGS_regexpseed, FLAGS_regexpcount);
42  printf("%d regexps, %d tests, %d failures [%d/%d str]\n",
43  t.regexps(), t.tests(), t.failures(), maxstrlen, (int)stralphabet.size());
44  EXPECT_EQ(0, t.failures());
45 }
46 
47 // Tests random small regexps involving literals and egrep operators.
48 TEST(Random, SmallEgrepLiterals) {
50  15, Explode("abc"),
51  "");
52 }
53 
54 // Tests random bigger regexps involving literals and egrep operators.
55 TEST(Random, BigEgrepLiterals) {
56  RandomTest(10, 10, Explode("abc."), RegexpGenerator::EgrepOps(),
57  15, Explode("abc"),
58  "");
59 }
60 
61 // Tests random small regexps involving literals, capturing parens,
62 // and egrep operators.
63 TEST(Random, SmallEgrepCaptures) {
64  RandomTest(5, 5, Split(" ", "a (b) ."), RegexpGenerator::EgrepOps(),
65  15, Explode("abc"),
66  "");
67 }
68 
69 // Tests random bigger regexps involving literals, capturing parens,
70 // and egrep operators.
71 TEST(Random, BigEgrepCaptures) {
72  RandomTest(10, 10, Split(" ", "a (b) ."), RegexpGenerator::EgrepOps(),
73  15, Explode("abc"),
74  "");
75 }
76 
77 // Tests random large complicated expressions, using all the possible
78 // operators, some literals, some parenthesized literals, and predefined
79 // character classes like \d. (Adding larger character classes would
80 // make for too many possibilities.)
81 TEST(Random, Complicated) {
82  std::vector<std::string> ops = Split(" ",
83  "%s%s %s|%s %s* %s*? %s+ %s+? %s? %s?? "
84  "%s{0} %s{0,} %s{1} %s{1,} %s{0,1} %s{0,2} %s{1,2} "
85  "%s{2} %s{2,} %s{3,4} %s{4,5}");
86 
87  // Use (?:\b) and (?:\B) instead of \b and \B,
88  // because PCRE rejects \b* but accepts (?:\b)*.
89  // Ditto ^ and $.
90  std::vector<std::string> atoms = Split(" ",
91  ". (?:^) (?:$) \\a \\f \\n \\r \\t \\v "
92  "\\d \\D \\s \\S \\w \\W (?:\\b) (?:\\B) "
93  "a (a) b c - \\\\");
94  std::vector<std::string> alphabet = Explode("abc123\001\002\003\t\r\n\v\f\a");
95  RandomTest(10, 10, atoms, ops, 20, alphabet, "");
96 }
97 
98 } // namespace re2
99 
re2::Split
std::vector< std::string > Split(const StringPiece &sep, const StringPiece &s)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:256
DEFINE_int32
DEFINE_int32(regexpseed, 404, "Random regexp seed.")
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
re2::RandomTest
static void RandomTest(int maxatoms, int maxops, const std::vector< std::string > &alphabet, const std::vector< std::string > &ops, int maxstrlen, const std::vector< std::string > &stralphabet, const std::string &wrapper)
Definition: bloaty/third_party/re2/re2/testing/random_test.cc:24
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
alphabet
static const char alphabet[]
Definition: bin_encoder.cc:30
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
wrapper
grpc_channel_wrapper * wrapper
Definition: src/php/ext/grpc/channel.h:48
re2::RegexpGenerator::EgrepOps
static const std::vector< std::string > & EgrepOps()
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:41
re2::Explode
std::vector< std::string > Explode(const StringPiece &s)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:241
re2::RE2_DEBUG_MODE
const bool RE2_DEBUG_MODE
Definition: bloaty/third_party/re2/re2/testing/exhaustive_tester.h:25
re2::ExhaustiveTester
Definition: bloaty/third_party/re2/re2/testing/exhaustive_tester.h:41
absl::base_internal::Random
static int Random(uint32_t *state)
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:122
re2::TEST
TEST(TestCharClassBuilder, Adds)
Definition: bloaty/third_party/re2/re2/testing/charclass_test.cc:198
ops
static grpc_op ops[6]
Definition: test/core/fling/client.cc:39


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:50