TrigramIndexTest.cpp
3.93 KB
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
//===- TrigramIndexTest.cpp - Unit tests for TrigramIndex -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/TrigramIndex.h"
#include "llvm/ADT/STLExtras.h"
#include "gtest/gtest.h"
#include <string>
#include <vector>
using namespace llvm;
namespace {
class TrigramIndexTest : public ::testing::Test {
protected:
std::unique_ptr<TrigramIndex> makeTrigramIndex(
std::vector<std::string> Rules) {
std::unique_ptr<TrigramIndex> TI =
std::make_unique<TrigramIndex>();
for (auto &Rule : Rules)
TI->insert(Rule);
return TI;
}
};
TEST_F(TrigramIndexTest, Empty) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({});
EXPECT_FALSE(TI->isDefeated());
EXPECT_TRUE(TI->isDefinitelyOut("foo"));
}
TEST_F(TrigramIndexTest, Basic) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"*hello*", "*wor.d*"});
EXPECT_FALSE(TI->isDefeated());
EXPECT_TRUE(TI->isDefinitelyOut("foo"));
}
TEST_F(TrigramIndexTest, NoTrigramsInRules) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"b.r", "za*az"});
EXPECT_TRUE(TI->isDefeated());
EXPECT_FALSE(TI->isDefinitelyOut("foo"));
EXPECT_FALSE(TI->isDefinitelyOut("bar"));
EXPECT_FALSE(TI->isDefinitelyOut("zakaz"));
}
TEST_F(TrigramIndexTest, NoTrigramsInARule) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"*hello*", "*wo.ld*"});
EXPECT_TRUE(TI->isDefeated());
EXPECT_FALSE(TI->isDefinitelyOut("foo"));
}
TEST_F(TrigramIndexTest, RepetitiveRule) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"*bar*bar*bar*bar*bar", "bar*bar"});
EXPECT_FALSE(TI->isDefeated());
EXPECT_TRUE(TI->isDefinitelyOut("foo"));
EXPECT_TRUE(TI->isDefinitelyOut("bar"));
EXPECT_FALSE(TI->isDefinitelyOut("barbara"));
EXPECT_FALSE(TI->isDefinitelyOut("bar+bar"));
}
TEST_F(TrigramIndexTest, PopularTrigram) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"*aaa*", "*aaaa*", "*aaaaa*", "*aaaaa*", "*aaaaaa*"});
EXPECT_TRUE(TI->isDefeated());
}
TEST_F(TrigramIndexTest, PopularTrigram2) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"class1.h", "class2.h", "class3.h", "class4.h", "class.h"});
EXPECT_TRUE(TI->isDefeated());
}
TEST_F(TrigramIndexTest, TooComplicatedRegex) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"[0-9]+"});
EXPECT_TRUE(TI->isDefeated());
}
TEST_F(TrigramIndexTest, TooComplicatedRegex2) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"foo|bar"});
EXPECT_TRUE(TI->isDefeated());
}
TEST_F(TrigramIndexTest, EscapedSymbols) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"*c\\+\\+*", "*hello\\\\world*", "a\\tb", "a\\0b"});
EXPECT_FALSE(TI->isDefeated());
EXPECT_FALSE(TI->isDefinitelyOut("c++"));
EXPECT_TRUE(TI->isDefinitelyOut("c\\+\\+"));
EXPECT_FALSE(TI->isDefinitelyOut("hello\\world"));
EXPECT_TRUE(TI->isDefinitelyOut("hello\\\\world"));
EXPECT_FALSE(TI->isDefinitelyOut("atb"));
EXPECT_TRUE(TI->isDefinitelyOut("a\\tb"));
EXPECT_TRUE(TI->isDefinitelyOut("a\tb"));
EXPECT_FALSE(TI->isDefinitelyOut("a0b"));
}
TEST_F(TrigramIndexTest, Backreference1) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"*foo\\1*"});
EXPECT_TRUE(TI->isDefeated());
}
TEST_F(TrigramIndexTest, Backreference2) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"*foo\\2*"});
EXPECT_TRUE(TI->isDefeated());
}
TEST_F(TrigramIndexTest, Sequence) {
std::unique_ptr<TrigramIndex> TI =
makeTrigramIndex({"class1.h", "class2.h", "class3.h", "class4.h"});
EXPECT_FALSE(TI->isDefeated());
EXPECT_FALSE(TI->isDefinitelyOut("class1"));
EXPECT_TRUE(TI->isDefinitelyOut("class.h"));
EXPECT_TRUE(TI->isDefinitelyOut("class"));
}
} // namespace