blob: a253fc80f6906ed8551651e97b8d7c677a841d8a [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]fb5bcc02012-02-17 14:05:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Steinar H. Gunderson5570febc2022-05-12 10:39:485#include "base/substring_set_matcher/substring_set_matcher.h"
[email protected]fb5bcc02012-02-17 14:05:426
avi5dd91f82015-12-25 22:30:467#include <stddef.h>
8
[email protected]fb5bcc02012-02-17 14:05:429#include <set>
10#include <string>
11#include <vector>
12
Karan Bhatia60409e892020-02-11 04:14:3613#include "testing/gmock/include/gmock/gmock.h"
[email protected]fb5bcc02012-02-17 14:05:4214#include "testing/gtest/include/gtest/gtest.h"
15
Steinar H. Gunderson5570febc2022-05-12 10:39:4816namespace base {
[email protected]fb5bcc02012-02-17 14:05:4217
[email protected]fb5bcc02012-02-17 14:05:4218namespace {
[email protected]716c0162013-12-13 20:36:5319
[email protected]fb5bcc02012-02-17 14:05:4220void TestOnePattern(const std::string& test_string,
21 const std::string& pattern,
22 bool is_match) {
Steinar H. Gunderson45e5abb2022-05-10 09:42:5423 std::string test = "TestOnePattern(" + test_string + ", " + pattern + ", " +
24 (is_match ? "1" : "0") + ")";
Steinar H. Gundersonf8174932022-05-21 00:25:1725 std::vector<MatcherStringPattern> patterns;
Karan Bhatiae39bc57a2020-02-06 20:04:1726 patterns.emplace_back(pattern, 1);
Steinar H. Gunderson45e5abb2022-05-10 09:42:5427 SubstringSetMatcher matcher;
28 ASSERT_TRUE(matcher.Build(patterns));
Peter Kasting78549f32022-05-31 18:20:2029 std::set<MatcherStringPattern::ID> matches;
[email protected]fb5bcc02012-02-17 14:05:4230 matcher.Match(test_string, &matches);
31
32 size_t expected_matches = (is_match ? 1 : 0);
[email protected]78033cd2012-02-29 03:56:1533 EXPECT_EQ(expected_matches, matches.size()) << test;
34 EXPECT_EQ(is_match, matches.find(1) != matches.end()) << test;
[email protected]fb5bcc02012-02-17 14:05:4235}
36
37void TestTwoPatterns(const std::string& test_string,
38 const std::string& pattern_1,
39 const std::string& pattern_2,
40 bool is_match_1,
41 bool is_match_2) {
Steinar H. Gunderson45e5abb2022-05-10 09:42:5442 std::string test = "TestTwoPatterns(" + test_string + ", " + pattern_1 +
43 ", " + pattern_2 + ", " + (is_match_1 ? "1" : "0") + ", " +
44 (is_match_2 ? "1" : "0") + ")";
Karan Bhatia60409e892020-02-11 04:14:3645 ASSERT_NE(pattern_1, pattern_2);
Steinar H. Gundersonf8174932022-05-21 00:25:1746 MatcherStringPattern substring_pattern_1(pattern_1, 1);
47 MatcherStringPattern substring_pattern_2(pattern_2, 2);
[email protected]fb5bcc02012-02-17 14:05:4248 // In order to make sure that the order in which patterns are registered
49 // does not make any difference we try both permutations.
50 for (int permutation = 0; permutation < 2; ++permutation) {
Steinar H. Gundersonf8174932022-05-21 00:25:1751 std::vector<const MatcherStringPattern*> patterns;
[email protected]fb5bcc02012-02-17 14:05:4252 if (permutation == 0) {
53 patterns.push_back(&substring_pattern_1);
54 patterns.push_back(&substring_pattern_2);
55 } else {