1 //===- TrigramIndexTest.cpp - Unit tests for TrigramIndex -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/TrigramIndex.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "gtest/gtest.h"
13
14 #include <string>
15 #include <vector>
16
17 using namespace llvm;
18
19 namespace {
20
21 class TrigramIndexTest : public ::testing::Test {
22 protected:
makeTrigramIndex(std::vector<std::string> Rules)23 std::unique_ptr<TrigramIndex> makeTrigramIndex(
24 std::vector<std::string> Rules) {
25 std::unique_ptr<TrigramIndex> TI =
26 make_unique<TrigramIndex>();
27 for (auto &Rule : Rules)
28 TI->insert(Rule);
29 return TI;
30 }
31 };
32
TEST_F(TrigramIndexTest,Empty)33 TEST_F(TrigramIndexTest, Empty) {
34 std::unique_ptr<TrigramIndex> TI =
35 makeTrigramIndex({});
36 EXPECT_FALSE(TI->isDefeated());
37 EXPECT_TRUE(TI->isDefinitelyOut("foo"));
38 }
39
TEST_F(TrigramIndexTest,Basic)40 TEST_F(TrigramIndexTest, Basic) {
41 std::unique_ptr<TrigramIndex> TI =
42 makeTrigramIndex({"*hello*", "*wor.d*"});
43 EXPECT_FALSE(TI->isDefeated());
44 EXPECT_TRUE(TI->isDefinitelyOut("foo"));
45 }
46
TEST_F(TrigramIndexTest,NoTrigramsInRules)47 TEST_F(TrigramIndexTest, NoTrigramsInRules) {
48 std::unique_ptr<TrigramIndex> TI =
49 makeTrigramIndex({"b.r", "za*az"});
50 EXPECT_TRUE(TI->isDefeated());
51 EXPECT_FALSE(TI->isDefinitelyOut("foo"));
52 EXPECT_FALSE(TI->isDefinitelyOut("bar"));
53 EXPECT_FALSE(TI->isDefinitelyOut("zakaz"));
54 }
55
TEST_F(TrigramIndexTest,NoTrigramsInARule)56 TEST_F(TrigramIndexTest, NoTrigramsInARule) {
57 std::unique_ptr<TrigramIndex> TI =
58 makeTrigramIndex({"*hello*", "*wo.ld*"});
59 EXPECT_TRUE(TI->isDefeated());
60 EXPECT_FALSE(TI->isDefinitelyOut("foo"));
61 }
62
TEST_F(TrigramIndexTest,RepetitiveRule)63 TEST_F(TrigramIndexTest, RepetitiveRule) {
64 std::unique_ptr<TrigramIndex> TI =
65 makeTrigramIndex({"*bar*bar*bar*bar*bar", "bar*bar"});
66 EXPECT_FALSE(TI->isDefeated());
67 EXPECT_TRUE(TI->isDefinitelyOut("foo"));
68 EXPECT_TRUE(TI->isDefinitelyOut("bar"));
69 EXPECT_FALSE(TI->isDefinitelyOut("barbara"));
70 EXPECT_FALSE(TI->isDefinitelyOut("bar+bar"));
71 }
72
TEST_F(TrigramIndexTest,PopularTrigram)73 TEST_F(TrigramIndexTest, PopularTrigram) {
74 std::unique_ptr<TrigramIndex> TI =
75 makeTrigramIndex({"*aaa*", "*aaaa*", "*aaaaa*", "*aaaaa*", "*aaaaaa*"});
76 EXPECT_TRUE(TI->isDefeated());
77 }
78
TEST_F(TrigramIndexTest,PopularTrigram2)79 TEST_F(TrigramIndexTest, PopularTrigram2) {
80 std::unique_ptr<TrigramIndex> TI =
81 makeTrigramIndex({"class1.h", "class2.h", "class3.h", "class4.h", "class.h"});
82 EXPECT_TRUE(TI->isDefeated());
83 }
84
TEST_F(TrigramIndexTest,TooComplicatedRegex)85 TEST_F(TrigramIndexTest, TooComplicatedRegex) {
86 std::unique_ptr<TrigramIndex> TI =
87 makeTrigramIndex({"[0-9]+"});
88 EXPECT_TRUE(TI->isDefeated());
89 }
90
TEST_F(TrigramIndexTest,TooComplicatedRegex2)91 TEST_F(TrigramIndexTest, TooComplicatedRegex2) {
92 std::unique_ptr<TrigramIndex> TI =
93 makeTrigramIndex({"foo|bar"});
94 EXPECT_TRUE(TI->isDefeated());
95 }
96
TEST_F(TrigramIndexTest,EscapedSymbols)97 TEST_F(TrigramIndexTest, EscapedSymbols) {
98 std::unique_ptr<TrigramIndex> TI =
99 makeTrigramIndex({"*c\\+\\+*", "*hello\\\\world*", "a\\tb", "a\\0b"});
100 EXPECT_FALSE(TI->isDefeated());
101 EXPECT_FALSE(TI->isDefinitelyOut("c++"));
102 EXPECT_TRUE(TI->isDefinitelyOut("c\\+\\+"));
103 EXPECT_FALSE(TI->isDefinitelyOut("hello\\world"));
104 EXPECT_TRUE(TI->isDefinitelyOut("hello\\\\world"));
105 EXPECT_FALSE(TI->isDefinitelyOut("atb"));
106 EXPECT_TRUE(TI->isDefinitelyOut("a\\tb"));
107 EXPECT_TRUE(TI->isDefinitelyOut("a\tb"));
108 EXPECT_FALSE(TI->isDefinitelyOut("a0b"));
109 }
110
TEST_F(TrigramIndexTest,Backreference1)111 TEST_F(TrigramIndexTest, Backreference1) {
112 std::unique_ptr<TrigramIndex> TI =
113 makeTrigramIndex({"*foo\\1*"});
114 EXPECT_TRUE(TI->isDefeated());
115 }
116
TEST_F(TrigramIndexTest,Backreference2)117 TEST_F(TrigramIndexTest, Backreference2) {
118 std::unique_ptr<TrigramIndex> TI =
119 makeTrigramIndex({"*foo\\2*"});
120 EXPECT_TRUE(TI->isDefeated());
121 }
122
TEST_F(TrigramIndexTest,Sequence)123 TEST_F(TrigramIndexTest, Sequence) {
124 std::unique_ptr<TrigramIndex> TI =
125 makeTrigramIndex({"class1.h", "class2.h", "class3.h", "class4.h"});
126 EXPECT_FALSE(TI->isDefeated());
127 EXPECT_FALSE(TI->isDefinitelyOut("class1"));
128 EXPECT_TRUE(TI->isDefinitelyOut("class.h"));
129 EXPECT_TRUE(TI->isDefinitelyOut("class"));
130 }
131
132 } // namespace
133