Repository navigation
Expand file tree
/
Copy pathtests.cpp
More file actions
254 lines (193 loc) · 8.01 KB
/
Copy pathtests.cpp
File metadata and controls
254 lines (193 loc) · 8.01 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
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
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include"graph.h"
#include"edge.h"
#include"vertex.h"
#include <vector>
TEST_CASE("BFS w/ small graph") {
// source, target and sentiment data
vector<string> source_data = {"C", "A", "D", "B", "C"};
vector<string> target_data = {"A", "D", "B", "C", "D"};
vector<string> sentiment_data = {"3", "1", "1", "3", "1"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i])); // pushes sentiments into the vector
}
Graph small_graph = Graph(target, source, sentiment); // create graph
vector<int> BFS = small_graph.BFS(0); // execute BFS on graph
// compare results
REQUIRE(BFS[0] == 0);
REQUIRE(BFS[1] == 1);
REQUIRE(BFS[2] == 2);
REQUIRE(BFS[3] == 3);
}
TEST_CASE("BFS w/ medium graph") {
//source, target and sentiment data
vector<string> source_data = {"A", "A", "B", "B", "C", "D", "E", "E", "E", "F"};
vector<string> target_data = {"B", "C", "C", "D", "D","E", "B", "A", "F", "D"};
vector<string> sentiment_data = {"3", "1", "1", "3", "1","3", "1", "1", "1", "3"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i])); // push sentiment
}
// create graph and execute BFS on it
Graph med_graph = Graph(target, source, sentiment);
vector<int> BFS = med_graph.BFS(4);
// compare results
REQUIRE(BFS[0] == 4);
REQUIRE(BFS[1] == 0);
REQUIRE(BFS[2] == 1);
REQUIRE(BFS[3] == 2);
REQUIRE(BFS[4] == 3);
REQUIRE(BFS[5] == 5);
}
TEST_CASE("BFS w/ big graph") {
// source, target, sentiment data
vector<string> source_data = {"A", "B", "C", "C", "D", "D", "E", "F", "F", "G", "G", "H", "I", "J", "J"};
vector<string> target_data = {"C", "F", "D", "I", "B", "E", "F", "C", "J", "E", "H", "D", "G", "G", "A"};
vector<string> sentiment_data = {"3", "1", "1", "3", "1","3", "1", "1", "1", "3", "3", "1", "1", "3", "1"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i]));
}
// create graph and execute BFS on it
Graph big_graph = Graph(target, source, sentiment);
vector<int> BFS = big_graph.BFS(9);
// compare results
REQUIRE(BFS[0] == 9);
REQUIRE(BFS[1] == 0);
REQUIRE(BFS[2] == 2);
REQUIRE(BFS[3] == 3);
REQUIRE(BFS[4] == 4);
REQUIRE(BFS[5] == 5);
REQUIRE(BFS[6] == 8);
REQUIRE(BFS[7] == 1);
REQUIRE(BFS[8] == 7);
REQUIRE(BFS[9] == 6);
}
TEST_CASE("Graph Constructor simple") {
// vector sets for data
vector<string> source_data = {"A", "B"};
vector<string> target_data = {"B", "A"};
vector<string> sentiment_data = {"-1", "-1"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i]));
}
// create graph to test constructor
Graph short_graph = Graph(target, source, sentiment);
// compare results
REQUIRE(short_graph.get_Subreddit_ID(0) == "B");
REQUIRE(short_graph.get_Subreddit_ID(1) == "A");
}
TEST_CASE("Graph Constructor Empty") {
// create vector data sets
vector<string> source_data = {};
vector<string> target_data = {};
vector<string> sentiment_data = {};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i]));
}
// create graph using constructor
Graph short_graph = Graph(target, source, sentiment);
// compare ressults
REQUIRE(short_graph.get_map()->size() == 0);
}
TEST_CASE("Page rank simple") {
// create data vectors
vector<string> source_data = {"A", "B"};
vector<string> target_data = {"B", "A"};
vector<string> sentiment_data = {"-1", "-1"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i]));
}
// create graph and execute page rank
Graph short_graph = Graph(target, source, sentiment);
vector<double> ranks = short_graph.pageRank(5);
// compare results
REQUIRE(ranks[0] == 0.5);
REQUIRE(ranks[1] == 0.5);
}
TEST_CASE("Page rank complex") {
// create data vectors
vector<string> source_data = {"A", "A", "C", "C", "C","B", "D"};
vector<string> target_data = {"C", "B", "A", "B", "D","D", "C"};
vector<string> sentiment_data = {"-1", "-1", "-1", "-1", "-1","-1", "1"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i]));
}
// create graph and execute page rank on it
Graph short_graph = Graph(target, source, sentiment);
vector<double> ranks = short_graph.pageRank(2);
// compare results
REQUIRE(ranks[0] == 0.375);
REQUIRE(ranks[1] == Approx(0.166667));
REQUIRE(ranks[2] == 0.125);
REQUIRE(ranks[3] == Approx(0.33333333));
}
TEST_CASE("Dijkstra simple") {
// create data vectors
vector<string> source_data = {"A", "A", "C"};
vector<string> target_data = {"B", "C", "B"};
vector<string> sentiment_data = {"3", "1", "1"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i]));
}
// create graph and execute djikstra on it
Graph short_graph = Graph(target, source, sentiment);
vector<int> ranks = short_graph.dijkstra(2);
// compare results
REQUIRE(ranks[0] == 2);
REQUIRE(ranks[1] == 1);
REQUIRE(ranks[2] == 0);
}
TEST_CASE("Dijkstra complex") {
// create vector data sets
vector<string> source_data = {"A", "A", "D", "D", "B","B", "E"};
vector<string> target_data = {"B", "D", "B", "E", "E","C", "C"};
vector<string> sentiment_data = {"3", "1", "1", "3", "1","3", "1"};
vector<Vertex> source, target;
vector<int> sentiment;
for (unsigned i = 0; i < source_data.size(); i++) {
source.push_back(Vertex(source_data[i])); // convert source and target strings to Vertices
target.push_back(Vertex(target_data[i]));
sentiment.push_back(stoi(sentiment_data[i]));
}
// create graph and execute djikstra on it
Graph short_graph = Graph(target, source, sentiment);
vector<int> ranks = short_graph.dijkstra(4);
// compare results
REQUIRE(ranks[0] == 2);
REQUIRE(ranks[1] == 1);
REQUIRE(ranks[2] == 3);
REQUIRE(ranks[3] == 4);
REQUIRE(ranks[4] == 0);
}