re2/re2/testing/string_generator_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 // Test StringGenerator.
6 
7 #include <stdint.h>
8 #include <string>
9 
10 #include "util/test.h"
11 #include "util/utf.h"
12 #include "re2/testing/string_generator.h"
13 #include "re2/testing/regexp_generator.h"
14 
15 namespace re2 {
16 
17 // Returns i to the e.
18 static int64_t IntegerPower(int i, int e) {
19  int64_t p = 1;
20  while (e-- > 0)
21  p *= i;
22  return p;
23 }
24 
25 // Checks that for given settings of the string generator:
26 // * it generates strings that are non-decreasing in length.
27 // * strings of the same length are sorted in alphabet order.
28 // * it doesn't generate the same string twice.
29 // * it generates the right number of strings.
30 //
31 // If all of these hold, the StringGenerator is behaving.
32 // Assumes that the alphabet is sorted, so that the generated
33 // strings can just be compared lexicographically.
34 static void RunTest(int len, const std::string& alphabet, bool donull) {
36 
37  int n = 0;
38  int last_l = -1;
39  std::string last_s;
40 
41  if (donull) {
42  g.GenerateNULL();
43  EXPECT_TRUE(g.HasNext());
44  StringPiece sp = g.Next();
45  EXPECT_EQ(sp.data(), static_cast<const char*>(NULL));
46  EXPECT_EQ(sp.size(), 0);
47  }
48 
49  while (g.HasNext()) {
50  std::string s = std::string(g.Next());
51  n++;
52 
53  // Check that all characters in s appear in alphabet.
54  for (const char *p = s.c_str(); *p != '\0'; ) {
55  Rune r;
56  p += chartorune(&r, p);
57  EXPECT_TRUE(utfrune(alphabet.c_str(), r) != NULL);
58  }
59 
60  // Check that string is properly ordered w.r.t. previous string.
61  int l = utflen(s.c_str());
62  EXPECT_LE(l, len);
63  if (last_l < l) {
64  last_l = l;
65  } else {
66  EXPECT_EQ(last_l, l);
67  EXPECT_LT(last_s, s);
68  }
69  last_s = s;
70  }
71 
72  // Check total string count.
73  int64_t m = 0;
74  int alpha = utflen(alphabet.c_str());
75  if (alpha == 0) // Degenerate case.
76  len = 0;
77  for (int i = 0; i <= len; i++)
78  m += IntegerPower(alpha, i);
79  EXPECT_EQ(n, m);
80 }
81 
82 TEST(StringGenerator, NoLength) {
83  RunTest(0, "abc", false);
84 }
85 
86 TEST(StringGenerator, NoLengthNoAlphabet) {
87  RunTest(0, "", false);
88 }
89 
90 TEST(StringGenerator, NoAlphabet) {
91  RunTest(5, "", false);
92 }
93 
94 TEST(StringGenerator, Simple) {
95  RunTest(3, "abc", false);
96 }
97 
98 TEST(StringGenerator, UTF8) {
99  RunTest(4, "abc\xE2\x98\xBA", false);
100 }
101 
102 TEST(StringGenerator, GenNULL) {
103  RunTest(0, "abc", true);
104  RunTest(0, "", true);
105  RunTest(5, "", true);
106  RunTest(3, "abc", true);
107  RunTest(4, "abc\xE2\x98\xBA", true);
108 }
109 
110 } // namespace re2
re2::utflen
int utflen(const char *s)
Definition: bloaty/third_party/re2/util/rune.cc:212
re2::RunTest
static void RunTest(int len, const std::string &alphabet, bool donull)
Definition: bloaty/third_party/re2/re2/testing/string_generator_test.cc:34
re2::utfrune
char * utfrune(const char *s, Rune c)
Definition: bloaty/third_party/re2/util/rune.cc:233
re2::StringPiece::size
size_type size() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:80
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
re2
Definition: bloaty/third_party/re2/re2/bitmap256.h:17
alphabet
static const char alphabet[]
Definition: bin_encoder.cc:30
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
re2::UTF8
static std::string UTF8(Rune r)
Definition: bloaty/third_party/re2/re2/testing/exhaustive3_test.cc:35
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
re2::IntegerPower
static int64_t IntegerPower(int i, int e)
Definition: bloaty/third_party/re2/re2/testing/string_generator_test.cc:18
g
struct @717 g
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
stdint.h
re2::StringPiece::data
const_pointer data() const
Definition: bloaty/third_party/re2/re2/stringpiece.h:85
re2::Explode
std::vector< std::string > Explode(const StringPiece &s)
Definition: bloaty/third_party/re2/re2/testing/regexp_generator.cc:241
re2::Rune
signed int Rune
Definition: bloaty/third_party/re2/util/utf.h:25
fix_build_deps.r
r
Definition: fix_build_deps.py:491
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
re2::chartorune
int chartorune(Rune *rune, const char *str)
Definition: bloaty/third_party/re2/util/rune.cc:51
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
re2::TEST
TEST(TestCharClassBuilder, Adds)
Definition: bloaty/third_party/re2/re2/testing/charclass_test.cc:198
regress.m
m
Definition: regress/regress.py:25
re2::StringPiece
Definition: bloaty/third_party/re2/re2/stringpiece.h:39
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
re2::StringGenerator
Definition: bloaty/third_party/re2/re2/testing/string_generator.h:22


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:21