-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
229 lines (213 loc) · 5.98 KB
/
main.cpp
File metadata and controls
229 lines (213 loc) · 5.98 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
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
#include "search_server.h"
#include "parse.h"
#include "test_runner.h"
#include "profile.h"
#include <algorithm>
#include <iterator>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <random>
#include <thread>
using namespace std;
void TestFunctionality(
const vector<string>& docs,
const vector<string>& queries,
const vector<string>& expected
) {
istringstream docs_input(Join('\n', docs));
istringstream queries_input(Join('\n', queries));
// SearchServer srv;
SearchServer srv(docs_input);
// srv.UpdateDocumentBase(docs_input);
ostringstream queries_output;
srv.AddQueriesStream(queries_input, queries_output);
const string result = queries_output.str();
const auto lines = SplitBy(Strip(result), '\n');
ASSERT_EQUAL(lines.size(), expected.size());
for (size_t i = 0; i < lines.size(); ++i) {
ASSERT_EQUAL(lines[i], expected[i]);
}
}
void TestSerpFormat() {
const vector<string> docs = {
"london is the capital of great britain",
"i am travelling down the river"
};
const vector<string> queries = {"london", "the"};
const vector<string> expected = {
"london: {docid: 0, hitcount: 1}",
Join(' ', vector<string>{
"the:",
"{docid: 0, hitcount: 1}",
"{docid: 1, hitcount: 1}"
})
};
TestFunctionality(docs, queries, expected);
}
void TestTop5() {
const vector<string> docs = {
"milk a",
"milk b",
"milk c",
"milk d",
"milk e",
"milk f",
"milk g",
"water a",
"water b",
"fire and earth"
};
const vector<string> queries = {"milk", "water", "rock"};
const vector<string> expected = {
Join(' ', vector<string>{
"milk:",
"{docid: 0, hitcount: 1}",
"{docid: 1, hitcount: 1}",
"{docid: 2, hitcount: 1}",
"{docid: 3, hitcount: 1}",
"{docid: 4, hitcount: 1}"
}),
Join(' ', vector<string>{
"water:",
"{docid: 7, hitcount: 1}",
"{docid: 8, hitcount: 1}",
}),
"rock:",
};
TestFunctionality(docs, queries, expected);
}
void TestHitcount() {
const vector<string> docs = {
"the river goes through the entire city there is a house near it",
"the wall",
"walle",
"is is is is",
};
const vector<string> queries = {"the", "wall", "all", "is", "the is"};
const vector<string> expected = {
Join(' ', vector<string>{
"the:",
"{docid: 0, hitcount: 2}",
"{docid: 1, hitcount: 1}",
}),
"wall: {docid: 1, hitcount: 1}",
"all:",
Join(' ', vector<string>{
"is:",
"{docid: 3, hitcount: 4}",
"{docid: 0, hitcount: 1}",
}),
Join(' ', vector<string>{
"the is:",
"{docid: 3, hitcount: 4}",
"{docid: 0, hitcount: 3}",
"{docid: 1, hitcount: 1}",
}),
};
TestFunctionality(docs, queries, expected);
}
void TestRanking() {
const vector<string> docs = {
"london is the capital of great britain",
"paris is the capital of france",
"berlin is the capital of germany",
"rome is the capital of italy",
"madrid is the capital of spain",
"lisboa is the capital of portugal",
"bern is the capital of switzerland",
"moscow is the capital of russia",
"kiev is the capital of ukraine",
"minsk is the capital of belarus",
"astana is the capital of kazakhstan",
"beijing is the capital of china",
"tokyo is the capital of japan",
"bangkok is the capital of thailand",
"welcome to moscow the capital of russia the third rome",
"amsterdam is the capital of netherlands",
"helsinki is the capital of finland",
"oslo is the capital of norway",
"stockgolm is the capital of sweden",
"riga is the capital of latvia",
"tallin is the capital of estonia",
"warsaw is the capital of poland",
};
const vector<string> queries = {"moscow is the capital of russia"};
const vector<string> expected = {
Join(' ', vector<string>{
"moscow is the capital of russia:",
"{docid: 7, hitcount: 6}",
"{docid: 14, hitcount: 6}",
"{docid: 0, hitcount: 4}",
"{docid: 1, hitcount: 4}",
"{docid: 2, hitcount: 4}",
})
};
TestFunctionality(docs, queries, expected);
}
void TestBasicSearch() {
const vector<string> docs = {
"we are ready to go",
"come on everybody shake you hands",
"i love this game",
"just like exception safety is not about writing try catch everywhere in your code move semantics are not about typing double ampersand everywhere in your code",
"daddy daddy daddy dad dad dad",
"tell me the meaning of being lonely",
"just keep track of it",
"how hard could it be",
"it is going to be legen wait for it dary legendary",
"we dont need no education"
};
const vector<string> queries = {
"we need some help",
"it",
"i love this game",
"tell me why",
"dislike",
"about"
};
const vector<string> expected = {
Join(' ', vector<string>{
"we need some help:",
"{docid: 9, hitcount: 2}",
"{docid: 0, hitcount: 1}"
}),
Join(' ', vector<string>{
"it:",
"{docid: 8, hitcount: 2}",
"{docid: 6, hitcount: 1}",
"{docid: 7, hitcount: 1}",
}),
"i love this game: {docid: 2, hitcount: 4}",
"tell me why: {docid: 5, hitcount: 2}",
"dislike:",
"about: {docid: 3, hitcount: 2}",
};
TestFunctionality(docs, queries, expected);
}
void TestTime() {
map<int, int> m;
vector<pair<int, int>> v(1000000);
for (int i = 0; i < 1000000; ++i) m[i];
{
LOG_DURATION("map itaration");
for (auto& i : m)
i.second++;
}
{
LOG_DURATION("vector iteration");
for (auto& i : v)
i.second++;
}
}
int main() {
TestRunner tr;
RUN_TEST(tr, TestSerpFormat);
RUN_TEST(tr, TestTop5);
RUN_TEST(tr, TestHitcount);
RUN_TEST(tr, TestRanking);
RUN_TEST(tr, TestBasicSearch);
return 0;
}