File: substring_set_matcher_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (231 lines) | stat: -rw-r--r-- 6,844 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/substring_set_matcher/substring_set_matcher.h"

#include <stddef.h>

#include <set>
#include <string>
#include <vector>

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

namespace {

void TestOnePattern(const std::string& test_string,
                    const std::string& pattern,
                    bool is_match) {
  std::string test = "TestOnePattern(" + test_string + ", " + pattern + ", " +
                     (is_match ? "1" : "0") + ")";
  std::vector<MatcherStringPattern> patterns;
  patterns.emplace_back(pattern, 1);
  SubstringSetMatcher matcher;
  ASSERT_TRUE(matcher.Build(patterns));
  std::set<MatcherStringPattern::ID> matches;
  matcher.Match(test_string, &matches);

  size_t expected_matches = (is_match ? 1 : 0);
  EXPECT_EQ(expected_matches, matches.size()) << test;
  EXPECT_EQ(is_match, matches.find(1) != matches.end()) << test;
}

void TestTwoPatterns(const std::string& test_string,
                     const std::string& pattern_1,
                     const std::string& pattern_2,
                     bool is_match_1,
                     bool is_match_2) {
  std::string test = "TestTwoPatterns(" + test_string + ", " + pattern_1 +
                     ", " + pattern_2 + ", " + (is_match_1 ? "1" : "0") + ", " +
                     (is_match_2 ? "1" : "0") + ")";
  ASSERT_NE(pattern_1, pattern_2);
  MatcherStringPattern substring_pattern_1(pattern_1, 1);
  MatcherStringPattern substring_pattern_2(pattern_2, 2);
  // In order to make sure that the order in which patterns are registered
  // does not make any difference we try both permutations.
  for (int permutation = 0; permutation < 2; ++permutation) {
    std::vector<const MatcherStringPattern*> patterns;
    if (permutation == 0) {
      patterns.push_back(&substring_pattern_1);
      patterns.push_back(&substring_pattern_2);
    } else {
      patterns.push_back(&substring_pattern_2);
      patterns.push_back(&substring_pattern_1);
    }
    SubstringSetMatcher matcher;
    ASSERT_TRUE(matcher.Build(patterns));
    std::set<MatcherStringPattern::ID> matches;
    matcher.Match(test_string, &matches);

    size_t expected_matches = (is_match_1 ? 1 : 0) + (is_match_2 ? 1 : 0);
    EXPECT_EQ(expected_matches, matches.size()) << test;
    EXPECT_EQ(is_match_1, matches.find(1) != matches.end()) << test;
    EXPECT_EQ(is_match_2, matches.find(2) != matches.end()) << test;
  }
}

}  // namespace

TEST(SubstringSetMatcherTest, TestMatcher) {
  // Test overlapping patterns
  // String    abcde
  // Pattern 1  bc
  // Pattern 2   cd
  TestTwoPatterns("abcde", "bc", "cd", true, true);
  if (HasFatalFailure()) {
    return;
  }

  // Test subpatterns - part 1
  // String    abcde
  // Pattern 1  bc
  // Pattern 2  b
  TestTwoPatterns("abcde", "bc", "b", true, true);
  if (HasFatalFailure()) {
    return;
  }

  // Test subpatterns - part 2
  // String    abcde
  // Pattern 1  bc
  // Pattern 2   c
  TestTwoPatterns("abcde", "bc", "c", true, true);
  if (HasFatalFailure()) {
    return;
  }

  // Test identical matches
  // String    abcde
  // Pattern 1 abcde
  TestOnePattern("abcde", "abcde", true);
  if (HasFatalFailure()) {
    return;
  }

  // Test multiple matches
  // String    aaaaa
  // Pattern 1 a
  TestOnePattern("abcde", "a", true);
  if (HasFatalFailure()) {
    return;
  }

  // Test matches at beginning and end
  // String    abcde
  // Pattern 1 ab
  // Pattern 2    de
  TestTwoPatterns("abcde", "ab", "de", true, true);
  if (HasFatalFailure()) {
    return;
  }

  // Test non-match
  // String    abcde
  // Pattern 1        fg
  TestOnePattern("abcde", "fg", false);
  if (HasFatalFailure()) {
    return;
  }

  // Test empty pattern and too long pattern
  // String    abcde
  // Pattern 1
  // Pattern 2 abcdef
  TestTwoPatterns("abcde", std::string(), "abcdef", true, false);
  if (HasFatalFailure()) {
    return;
  }
}

TEST(SubstringSetMatcherTest, TestMatcher2) {
  MatcherStringPattern pattern_1("a", 1);
  MatcherStringPattern pattern_2("b", 2);
  MatcherStringPattern pattern_3("c", 3);

  std::vector<const MatcherStringPattern*> patterns = {&pattern_1, &pattern_2,
                                                       &pattern_3};
  auto matcher = std::make_unique<SubstringSetMatcher>();
  ASSERT_TRUE(matcher->Build(patterns));

  std::set<MatcherStringPattern::ID> matches;
  matcher->Match("abd", &matches);
  EXPECT_EQ(2u, matches.size());
  EXPECT_TRUE(matches.end() != matches.find(1));
  EXPECT_TRUE(matches.end() != matches.find(2));

  patterns = {&pattern_1, &pattern_3};
  matcher = std::make_unique<SubstringSetMatcher>();
  ASSERT_TRUE(matcher->Build(patterns));

  matches.clear();
  matcher->Match("abd", &matches);
  EXPECT_EQ(1u, matches.size());
  EXPECT_TRUE(matches.end() != matches.find(1));
  EXPECT_TRUE(matches.end() == matches.find(2));

  matcher = std::make_unique<SubstringSetMatcher>();
  ASSERT_TRUE(matcher->Build(std::vector<const MatcherStringPattern*>()));
  EXPECT_TRUE(matcher->IsEmpty());
}

TEST(SubstringSetMatcherTest, TestMatcher3) {
  std::string text = "abcde";

  std::vector<MatcherStringPattern> patterns;
  int id = 0;
  // Add all substrings of this string, including empty string.
  patterns.emplace_back("", id++);
  for (size_t i = 0; i < text.length(); i++) {
    for (size_t j = i; j < text.length(); j++) {
      patterns.emplace_back(text.substr(i, j - i + 1), id++);
    }
  }

  SubstringSetMatcher matcher;
  matcher.Build(patterns);
  std::set<MatcherStringPattern::ID> matches;
  matcher.Match(text, &matches);
  EXPECT_EQ(patterns.size(), matches.size());
  for (const MatcherStringPattern& pattern : patterns) {
    EXPECT_TRUE(matches.find(pattern.id()) != matches.end())
        << pattern.pattern();
  }
}

TEST(SubstringSetMatcherTest, TestEmptyMatcher) {
  std::vector<MatcherStringPattern> patterns;
  SubstringSetMatcher matcher;
  matcher.Build(patterns);
  std::set<MatcherStringPattern::ID> matches;
  matcher.Match("abd", &matches);
  EXPECT_TRUE(matches.empty());
  EXPECT_TRUE(matcher.IsEmpty());
}

// Test a case where we have more than 256 edges from one node
// (the “a” node gets one for each possible ASCII bytes, and then
// one for the output link).
TEST(SubstringSetMatcherTest, LotsOfEdges) {
  std::vector<MatcherStringPattern> patterns;
  for (int i = 0; i < 256; ++i) {
    std::string str;
    str.push_back('a');
    str.push_back(static_cast<char>(i));
    patterns.emplace_back(str, i);
  }

  {
    std::string str;
    str.push_back('a');
    patterns.emplace_back(str, 256);
  }

  SubstringSetMatcher matcher;
  matcher.Build(patterns);
}

}  // namespace base