-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_tests.cpp
293 lines (227 loc) · 9.06 KB
/
search_tests.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <gtest/gtest.h>
#include "search.h"
#include "tests/build_index.h"
#include "tests/clean_token.h"
#include "tests/find_query_matches.h"
#include "tests/gather_tokens.h"
/*
**TESTS FOR CleanToken**
*/
//tests empty/space tokens
TEST(CleanToken, ExampleEmptyTest) {
map<string, set<string>> tests;
tests[""].insert("");
testCleanToken(tests);
}
//tests tokens punctuations in front
TEST(CleanToken, PunctuationInFront) {
map<string, set<string>> tests;
tests["hello"].insert("!hello");
tests["hello"].insert("??hello");
tests["hello"].insert(".....hello");
testCleanToken(tests);
}
//tests tokens punctuations in back
TEST(CleanToken, PunctuationInBack) {
map<string, set<string>> tests;
tests["hello"].insert("hello!");
tests["hello"].insert("hello??");
tests["hello"].insert("hello......");
testCleanToken(tests);
}
//tests tokens punctuations in front and back
TEST(CleanToken, PunctuationFrontAndBack) {
map<string, set<string>> tests;
tests["hello"].insert("!hello!");
tests["hello"].insert("??hello??");
tests["hello"].insert("......hello.......");
testCleanToken(tests);
}
//tests when token has no alphabet letters
TEST(CleanToken, NoAlphabetLetters) {
map<string, set<string>> tests;
tests[""].insert("123456");
tests[""].insert("!!!???");
tests[""].insert("1234!@#$");
testCleanToken(tests);
}
//tests when token has mixed alphabet letters
TEST(CleanToken, MixedAlphabetLetters) {
map<string, set<string>> tests;
tests["hello"].insert("HeLlO");
tests["hello"].insert("hElLo");
tests["hello"].insert("HELLo");
testCleanToken(tests);
}
//tokens punctuation in random places
TEST(CleanToken, PunctuationRandomPlaces) {
map<string, set<string>> tests;
tests["he.l!lo"].insert("he.l!lo");
tests["he,l?lo"].insert("he,l?lo");
tests["h.e.l.l.o"].insert(".h.e.l.l.o.");
tests["hel...lo"].insert("...hel...lo");
tests["hel...lo"].insert("hel...lo...");
testCleanToken(tests);
}
//tests token punctuations in middle
TEST(CleanToken, PunctuationInMiddle) {
map<string, set<string>> tests;
tests["he,llo"].insert("he,llo");
tests["he...llo"].insert("he...llo");
tests["hel-----lo"].insert("hel-----lo");
testCleanToken(tests);
}
//tests token numbers
TEST(CleanToken, NumericalCharacters) {
map<string, set<string>> tests;
tests["hello2"].insert("hello2");
tests["123world"].insert("123world");
tests["example123"].insert("example123");
testCleanToken(tests);
}
//tests whitespace tokens
TEST(CleanToken, WhitespaceHandling) {
map<string, set<string>> tests;
tests[""].insert(" ");
tests[""].insert("\t");
tests["hello world"].insert("hello world");
tests[" hello world "].insert(" hello world ");
testCleanToken(tests);
}
/*
**TESTS FOR GatherTokens**
*/
//tests for leading space in text
TEST(GatherTokens, LeadingSpace) {
string text = " leading space in text";
set<string> expected = {"leading", "space", "in", "text"};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
//tests for trailing space in text
TEST(GatherTokens, TrailingSpace) {
string text = "trailing space in text ";
set<string> expected = {"trailing", "space", "in", "text"};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
//tests for multiple spaces between words in text
TEST(GatherTokens, MultipleSpaces) {
string text = "multiple spaces between words";
set<string> expected = {"multiple", "spaces", "between", "words"};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
//tests for multiple of same words in text
TEST(GatherTokens, RepeatedWords) {
string text = "repeat repeat repeat the word repeat";
set<string> expected = {"repeat", "the", "word"};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
//tests for uppercase and lowercase of the same word in text
TEST(GatherTokens, CaseSensitivity) {
string text = "Case and case and CASE";
set<string> expected = {"case", "and"};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
//tests for empty text
TEST(GatherTokens, EmptyText) {
string text = "";
set<string> expected = {};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
//tests for punctuation within and outside the word
TEST(GatherTokens, PunctuationHandling) {
string text = "punctuation, inside and outside: word; another-word!";
set<string> expected = {"punctuation", "inside", "and", "outside", "word", "another-word"};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
//tests for unique case where cleaned token is the same as another word
TEST(GatherTokens, CleanedToken) {
string text = "This is a cleaned token ...TOKEN...";
set<string> expected = { "a", "cleaned", "is", "this", "token"};
set<string> studentResult = gatherTokens(text);
string testFeedback = "gatherTokens(\"" + text + "\") incorrect\n";
EXPECT_EQ(expected, studentResult) << testFeedback;
}
/*
**TESTS FOR BuildIndex**
*/
//tests when file doesnt open
TEST(BuildIndex, FileDoesNotOpen) {
string filename = "nonexistent_file.txt";
map<string, set<string>> studentIndex;
int studentNumProcessed = buildIndex(filename, studentIndex);
EXPECT_EQ(true, studentIndex.empty()) << "Index should be empty if the file cannot be opened";
EXPECT_EQ(0, studentNumProcessed) << "Number of processed URLs should be 0 if the file cannot be opened";
}
//tests when a cleaned word is the same as another word in a different website
TEST(BuildIndex, HandleWordsInMultipleSites) {
string filename = "tiny.txt";
map<string, set<string>> expected = {
{"fish", {"www.shoppinglist.com", "www.dr.seuss.net"}},
{"eggs", {"www.shoppinglist.com"}},
{"milk", {"www.shoppinglist.com"}},
{"bread", {"www.shoppinglist.com"}},
{"cheese", {"www.shoppinglist.com"}},
{"red", {"www.rainbow.org", "www.dr.seuss.net"}},
{"green", {"www.rainbow.org"}},
{"orange", {"www.rainbow.org"}},
{"yellow", {"www.rainbow.org"}},
{"blue", {"www.rainbow.org", "www.dr.seuss.net"}},
{"indigo", {"www.rainbow.org"}},
{"violet", {"www.rainbow.org"}},
{"one", {"www.dr.seuss.net"}},
{"two", {"www.dr.seuss.net"}},
{"i'm", {"www.bigbadwolf.com"}},
{"not", {"www.bigbadwolf.com"}},
{"trying", {"www.bigbadwolf.com"}},
{"to", {"www.bigbadwolf.com"}},
{"eat", {"www.bigbadwolf.com"}},
{"you", {"www.bigbadwolf.com"}}
};
map<string, set<string>> studentIndex;
int studentNumProcessed = buildIndex(filename, studentIndex);
EXPECT_EQ(expected, studentIndex) << "buildIndex did not correctly handle words appearing in multiple sites";
string retTestFeedback = "buildIndex(\"" + filename + "\", ...) return value incorrect\n";
EXPECT_EQ(4, studentNumProcessed) << retTestFeedback;
}
/*
**TESTS FOR FindQueryMatches**
*/
//tests for empty sentence as a parameter
TEST(FindQueryMatches, EmptyQuery) {
set<string> expected = {};
EXPECT_EQ(expected, findQueryMatches(INDEX, "")) << "Empty query should return no matches";
}
//tests for first word not in map
TEST(FindQueryMatches, FirstWordNotInMap) {
set<string> expected = {"example.com", "uic.edu"};
EXPECT_EQ(expected, findQueryMatches(INDEX, "test hello")) << "Query with first word not in index should return no matches";
}
//tests for later word with + modifier not in map
TEST(FindQueryMatches, PlusModifierWordNotInMap) {
set<string> expected = {};
EXPECT_EQ(expected, findQueryMatches(INDEX, "hello +test")) << "Query with a required word not in index should return no matches";
}
//tests for later word with - modifier not in map
TEST(FindQueryMatches, MinusModifierWordNotInMap) {
set<string> expected = {"example.com", "uic.edu"};
EXPECT_EQ(expected, findQueryMatches(INDEX, "hello -test")) << "Query with a non existent exclusion word should not affect results";
}
//tests for later word with no modifier not in map
TEST(FindQueryMatches, OptionalWordNotInMap) {
set<string> expected = {"example.com", "uic.edu"};
EXPECT_EQ(expected, findQueryMatches(INDEX, "hello test")) << "Query with an optional word not in index should return matches for other words";
}