-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrojanmap_test_student.cc
316 lines (269 loc) · 14.4 KB
/
trojanmap_test_student.cc
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "gtest/gtest.h"
#include "src/lib/trojanmap.h"
TEST(TrojanMapStudentTest, P1_GetID) {
TrojanMap m;
EXPECT_EQ("", m.GetID("maidanglao"));
EXPECT_EQ("6813379473", m.GetID("Ramen KenJo"));
EXPECT_EQ("6808450114", m.GetID("Smoke Shop"));
}
TEST(TrojanMapStudentTest, P1_GetPosition) {
TrojanMap m;
auto position = m.GetPosition("Smoke Shop");
std::pair<double,double> gt1(34.0029211,-118.3091031);
EXPECT_EQ(gt1, position);
position = m.GetPosition("MASTER BURGER");
std::pair<double,double> gt2(34.0030909,-118.3091648);
EXPECT_EQ(gt2, position);
position = m.GetPosition("United Church of Christ Scientist");
std::pair<double,double> gt3(34.0380675,-118.3075755);
EXPECT_EQ(gt3, position);
position = m.GetPosition("Chipotle");
std::pair<double,double> gt4(34.0169985,-118.2822768);
EXPECT_EQ(gt4, position);
position = m.GetPosition("Jerry's home");
std::pair<double,double> gt5(-1,-1);
EXPECT_EQ(gt5, position);
}
TEST(TrojanMapStudentTest, P1_CalculateEditDistance) {
TrojanMap m;
EXPECT_EQ(m.CalculateEditDistance("love", "lovely"), 2);
EXPECT_EQ(m.CalculateEditDistance("love", "uvol"), 4);
EXPECT_EQ(m.CalculateEditDistance("sunday", "saturday"), 3);
EXPECT_EQ(m.CalculateEditDistance("SunDay", "saturday"), 3); //not case sensitive
}
TEST(TrojanMapStudentTest, P1_FindClosestName) {
TrojanMap m;
EXPECT_EQ(m.FindClosestName("Chiptle"), "Chipotle");
EXPECT_EQ(m.FindClosestName("Smoke Sh"), "Smoke Shop");
EXPECT_EQ(m.FindClosestName("Pic"), "Pico");
EXPECT_EQ(m.FindClosestName("c"), "KFC");
EXPECT_EQ(m.FindClosestName("ca"), "CAVA"); //avoid choosing the empty strings or substituted by names with only numbers
}
TEST(TrojanMapStudentTest, P1_Autocomplete) {
TrojanMap m;
// Test the simple case
auto names = m.Autocomplete("Expo/");
std::unordered_set<std::string> gt = {"Expo/Vermont 2", "Expo/Western 2", "Expo/Crenshaw", "Expo/Western 1", "Expo/Crenshaw 1", "Expo/Vermont", "Expo/Western", "Expo/Vermont 1"}; // groundtruth for "Expo/"
int success = 0;
for (auto& n: names) {
EXPECT_EQ(gt.count(n) > 0, true) << n + " is not in gt.";
if (gt.count(n) > 0){
success++;
}
}
EXPECT_EQ(success, gt.size());
// Test the lower case
names = m.Autocomplete("expo/");
success = 0;
for (auto& n: names) {
EXPECT_EQ(gt.count(n) > 0, true) << n + " is not in gt.";
if (gt.count(n) > 0){
success++;
}
}
EXPECT_EQ(success, gt.size());
// Test the lower and upper case
names = m.Autocomplete("eXPo/");
success = 0;
for (auto& n: names) {
EXPECT_EQ(gt.count(n) > 0, true) << n + " is not in gt.";
if (gt.count(n) > 0){
success++;
}
}
EXPECT_EQ(success, gt.size());
// Test the upper case
names = m.Autocomplete("EXPO/");
success = 0;
for (auto& n: names) {
EXPECT_EQ(gt.count(n) > 0, true) << n + " is not in gt.";
if (gt.count(n) > 0){
success++;
}
}
EXPECT_EQ(success, gt.size());
}
// To avoid unexpected error,
// When running this test, please modify the full diectory of input file to your current directory.
TEST(TrojanMapStudentTest, P2_ReadLocations) {
TrojanMap m;
std::vector<std::string> gt1 ={"Ralphs", "KFC","Chick-fil-A"};
// auto result1 = m.ReadLocationsFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/topologicalsort_locations.csv");
auto result1 = m.ReadLocationsFromCSVFile("/home/jerry/Desktop/final-project-sweetmaru/input/topologicalsort_locations.csv");
EXPECT_EQ(result1, gt1);
std::vector<std::string> gt2 ={"Target", "Chipotle","Ralphs","Leavey Library"};
// auto result2 = m.ReadLocationsFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/my_loc1.csv");
auto result2 = m.ReadLocationsFromCSVFile("/home/jerry/Desktop/final-project-sweetmaru/input/my_loc1.csv");
EXPECT_EQ(result2, gt2);
std::vector<std::string> gt3 ={"Target", "Chipotle","Ralphs","Leavey Library","Dulce"};
// auto result3 = m.ReadLocationsFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/my_loc2.csv");
auto result3 = m.ReadLocationsFromCSVFile("/home/jerry/Desktop/final-project-sweetmaru/input/my_loc2.csv");
EXPECT_EQ(result3, gt3);
}
// To avoid unexpected error,
// When running this test, please modify the full diectory of input file to your current directory.
TEST(TrojanMapStudentTest, P2_ReadDependencies) {
TrojanMap m;
std::vector<std::vector<std::string>> gt1 ={{"Ralphs","Chick-fil-A"}, {"Ralphs","KFC"}, {"Chick-fil-A","KFC"}};
// auto result1 = m.ReadDependenciesFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/topologicalsort_dependencies.csv");
auto result1 = m.ReadDependenciesFromCSVFile("/home/jerry/Desktop/final-project-sweetmaru/input/topologicalsort_dependencies.csv");
EXPECT_EQ(result1, gt1);
std::vector<std::vector<std::string>> gt2 ={{"Target","Chipotle"}, {"Target","Leavey Library"}, {"Chipotle","Ralphs"},{"Ralphs","Target"},{"Ralphs","Leavey Library"}};
// auto result2 = m.ReadDependenciesFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/my_dep1.csv");
auto result2 = m.ReadDependenciesFromCSVFile("/home/jerry/Desktop/final-project-sweetmaru/input/my_dep1.csv");
EXPECT_EQ(result2, gt2);
std::vector<std::vector<std::string>> gt3 ={{"Target","Chipotle"}, {"Target","Leavey Library"}, {"Chipotle","Ralphs"},{"Ralphs","Leavey Library"}};
// auto result3 = m.ReadDependenciesFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/my_dep2.csv");
auto result3 = m.ReadDependenciesFromCSVFile("/home/jerry/Desktop/final-project-sweetmaru/input/my_dep2.csv");
EXPECT_EQ(result3, gt3);
}
TEST(TrojanMapStudentTest, P2_DeliveringTrojan) {
TrojanMap m;
//impossible
std::vector<std::string> loc1 = {"Target","Chipotle","Ralphs","Leavey Library"};
std::vector<std::vector<std::string>> dep1 = {{"Target","Chipotle"}, {"Target","Leavey Library"}, {"Chipotle","Ralphs"},{"Ralphs","Target"},{"Ralphs","Leavey Library"}};
auto res1 = m.DeliveringTrojan(loc1, dep1);
EXPECT_EQ(res1.size(), 0);
//sort possible
std::vector<std::string> loc2 = {"Target","Chipotle","Ralphs","Leavey Library","Dulce"};
std::vector<std::vector<std::string>> dep2 = {{"Target","Chipotle"}, {"Target","Leavey Library"}, {"Chipotle","Ralphs"},{"Ralphs","Leavey Library"}};
auto res2 = m.DeliveringTrojan(loc2, dep2);
std::vector<std::string> gt2 ={"Dulce","Target","Chipotle","Ralphs","Leavey Library"};
EXPECT_EQ(res2, gt2);
//10 edges possioble case
//change to the local absolute path!
std::vector<std::string> loc3 = m.ReadLocationsFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/my_loc3.csv");
std::vector<std::vector<std::string>> dep3 = m.ReadDependenciesFromCSVFile("/Users/luoxiaofeng/Desktop/EE_538/2022sp_ee538_project/final-project-sweetmaru/input/my_dep3.csv");
auto res3 = m.DeliveringTrojan(loc3, dep3);
std::vector<std::string> gt3 ={"Lyons Center", "Starbucks", "Popeyes", "The Barber Shop", "CorePower Yoga", "Chase", "Ralphs", "Dulce", "CAVA", "Bank of America"};
EXPECT_EQ(res3, gt3);
}
TEST(TrojanMapStudentTest, P2_CalculateShortestPath_Dijkstra) {
TrojanMap m;
// Test from Ralphs to Target
auto path = m.CalculateShortestPath_Dijkstra("Ralphs", "Target");
std::vector<std::string> gt{
"2578244375","4380040154","4380040158","4380040167","6805802087",
"8410938469","6813416131","7645318201","6813416130","6813416129",
"123318563","452688940","6816193777","123408705","6816193774","452688933",
"452688931","123230412","6816193770","6787470576","4015442011","6816193692",
"6816193693","6816193694","4015377691","544693739","6816193696","6804883323",
"6807937309","6807937306","6816193698","4015377690","4015377689","122814447",
"6813416159","6813405266","4015372488","4015372487","6813405229","122719216",
"6813405232","4015372486","7071032399","4015372485","6813379479","6813379584","6814769289","5237417650"}; // Expected path
// Print the path lengths
std::cout << "My path length: " << m.CalculatePathLength(path) << "miles" << std::endl;
std::cout << "GT path length: " << m.CalculatePathLength(gt) << "miles" << std::endl;
EXPECT_EQ(path, gt);
// Reverse the input from Ralphs to Target
path = m.CalculateShortestPath_Dijkstra("Target", "Ralphs");
std::reverse(gt.begin(),gt.end()); // Reverse the path
// Print the path lengths
std::cout << "My path length: " << m.CalculatePathLength(path) << "miles" << std::endl;
std::cout << "GT path length: " << m.CalculatePathLength(gt) << "miles" << std::endl;
EXPECT_EQ(path, gt);
}
TEST(TrojanMapStudentTest, P2_CalculateShortestPath_Bellman_Ford) {
TrojanMap m;
// Test from Ralphs to Target
auto path = m.CalculateShortestPath_Bellman_Ford("Ralphs", "Target");
std::vector<std::string> gt{
"2578244375","4380040154","4380040158","4380040167","6805802087",
"8410938469","6813416131","7645318201","6813416130","6813416129",
"123318563","452688940","6816193777","123408705","6816193774","452688933",
"452688931","123230412","6816193770","6787470576","4015442011","6816193692",
"6816193693","6816193694","4015377691","544693739","6816193696","6804883323",
"6807937309","6807937306","6816193698","4015377690","4015377689","122814447",
"6813416159","6813405266","4015372488","4015372487","6813405229","122719216",
"6813405232","4015372486","7071032399","4015372485","6813379479","6813379584","6814769289","5237417650"}; // Expected path
// Print the path lengths
std::cout << "My path length: " << m.CalculatePathLength(path) << "miles" << std::endl;
std::cout << "GT path length: " << m.CalculatePathLength(gt) << "miles" << std::endl;
EXPECT_EQ(path, gt);
// Reverse the input from Ralphs to Target
path = m.CalculateShortestPath_Bellman_Ford("Target", "Ralphs");
std::reverse(gt.begin(),gt.end()); // Reverse the path
// Print the path lengths
std::cout << "My path length: " << m.CalculatePathLength(path) << "miles" << std::endl;
std::cout << "GT path length: " << m.CalculatePathLength(gt) << "miles" << std::endl;
EXPECT_EQ(path, gt);
}
TEST(TrojanMapStudentTest, P2_CycleDetection) {
TrojanMap m;
// Test case 1
std::vector<double> square1 = {-118.298, -118.270, 34.032, 34.011};
auto sub1 = m.GetSubgraph(square1);
bool result1 = m.CycleDetection(sub1, square1);
EXPECT_EQ(result1, true);
// Test case 2
std::vector<double> square2 = {-118.290, -118.289, 34.027, 34.020};
auto sub2 = m.GetSubgraph(square2);
bool result2 = m.CycleDetection(sub2, square2);
EXPECT_EQ(result2, false);
// Test case 3
std::vector<double> square3 = {-118.297, -118.272, 34.031, 34.015};
auto sub3 = m.GetSubgraph(square3);
bool result3 = m.CycleDetection(sub3, square3);
EXPECT_EQ(result3, true);
// Test case 4
std::vector<double> square4 = {-118.297, -118.275, 34.030, 34.018};
auto sub4 = m.GetSubgraph(square4);
bool result4 = m.CycleDetection(sub4, square4);
EXPECT_EQ(result4, true);
}
TEST(TrojanMapStudentTest, P3_TSP_Brute_Force) {
TrojanMap m;
std::vector<std::string> input{"8201681442","6197156485","7786565237","6820972477","6807600525","1832234142","6819144993","1873055949"}; // Input location ids
auto result = m.TravellingTrojan_Brute_force(input);
std::cout << "My path length: " << result.first << "miles" << std::endl; // Print the result path lengths
std::vector<std::string> gt{"8201681442","1873055949","6197156485","1832234142","6807600525","6819144993","7786565237","6820972477","8201681442"}; // Expected order
std::cout << "GT path length: " << m.CalculatePathLength(gt) << "miles" << std::endl; // Print the gt path lengths
bool flag = false;
if (gt == result.second[result.second.size()-1]) // clockwise
flag = true;
std::reverse(gt.begin(),gt.end()); // Reverse the expected order because the counterclockwise result is also correct
if (gt == result.second[result.second.size()-1])
flag = true;
EXPECT_EQ(flag, true);
}
TEST(TrojanMapStudentTest, P3_TSP_Backtracking) {
TrojanMap m;
std::vector<std::string> input{"8201681442","6197156485","7786565237","6820972477","6807600525","1832234142","6819144993","1873055949"}; // Input location ids
auto result = m.TravellingTrojan_Backtracking(input);
std::cout << "My path length: " << result.first << "miles" << std::endl; // Print the result path lengths
std::vector<std::string> gt{"8201681442","1873055949","6197156485","1832234142","6807600525","6819144993","7786565237","6820972477","8201681442"}; // Expected order
std::cout << "GT path length: " << m.CalculatePathLength(gt) << "miles" << std::endl; // Print the gt path lengths
bool flag = false;
if (gt == result.second[result.second.size()-1]) // clockwise
flag = true;
std::reverse(gt.begin(),gt.end()); // Reverse the expected order because the counterclockwise result is also correct
if (gt == result.second[result.second.size()-1])
flag = true;
EXPECT_EQ(flag, true);
}
TEST(TrojanMapStudentTest, P3_TSP_2opt) {
TrojanMap m;
std::vector<std::string> input{"8201681442","6197156485","7786565237","6820972477","6807600525","1832234142","6819144993","1873055949"}; // Input location ids
auto result = m.TravellingTrojan_2opt(input);
std::cout << "My path length: " << result.first << "miles" << std::endl; // Print the result path lengths
std::vector<std::string> gt{"8201681442","1873055949","6197156485","1832234142","6807600525","6819144993","7786565237","6820972477","8201681442"}; // Expected order
std::cout << "GT path length: " << m.CalculatePathLength(gt) << "miles" << std::endl; // Print the gt path lengths
bool flag = false;
if (gt == result.second[result.second.size()-1]) // clockwise
flag = true;
std::reverse(gt.begin(),gt.end()); // Reverse the expected order because the counterclockwise result is also correct
if (gt == result.second[result.second.size()-1])
flag = true;
EXPECT_EQ(flag, true);
}
TEST(TrojanMapTest, P3_FindNearby) {
TrojanMap m;
auto result = m.FindNearby("bank", "Chase", 10, 10);
std::vector<std::string> ans{"5237417651", "9591449465"};
EXPECT_EQ(result, ans);
//check the order
bool flag = false;
std::string name= m.GetID("Chase");
if(m.CalculateDistance(name, "5237417651")<m.CalculateDistance(name, "9591449465")) flag = true;
EXPECT_EQ(flag, true);
}