-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdriver.cpp
More file actions
247 lines (239 loc) · 8.92 KB
/
driver.cpp
File metadata and controls
247 lines (239 loc) · 8.92 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <stack>
#include "PProcedure.h"
using namespace std;
void printResults(vector<vector<GPStore::Value>> &result) {
if (result.empty()) {
cout << "Empty result" << endl;
return;
}
for (const auto &row : result) {
size_t rowLen = row.size();
for (size_t i = 0; i < rowLen; i++) {
const auto &val = row[i];
cout << val.toString();
if (i != rowLen - 1)
cout << "|";
}
cout << endl;
}
}
std::string trim(const std::string& str, const std::string& whitespace = " \t\n\r") {
size_t start = str.find_first_not_of(whitespace);
if (start == std::string::npos) return ""; // No content
size_t end = str.find_last_not_of(whitespace);
return str.substr(start, end - start + 1);
}
std::vector<std::string> splitList(const std::string& str) {
std::vector<std::string> tokens;
std::string cur = "";
stack<char> st;
for (int i = 0; i < str.size(); ++i) {
if (str[i] == '[') {
st.push('[');
cur += str[i];
} else if (str[i] == ']') {
st.pop();
cur += str[i];
if (st.empty()) {
tokens.push_back(cur);
cur = "";
}
} else if (str[i] == ',' || str[i] == ' ') {
if (st.empty()) {
cur = "";
} else {
cur += str[i];
}
} else {
cur += str[i];
}
}
return tokens;
}
std::vector<std::string> split(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
tokens.push_back(trim(token));
}
return tokens;
}
bool compareResults(std::vector<std::vector<GPStore::Value>> &result, std::vector<std::vector<std::string>> &trueResults) {
if (result.size() != trueResults.size()) {
cout << "Mismatch in number of rows" << endl;
return false;
}
for (int i = 0; i < result.size(); ++i) {
if (result[i].size() != trueResults[i].size()) {
cout << "Mismatch in number of columns" << endl;
return false;
}
for (int j = 0; j < result[i].size(); ++j) {
if (result[i][j].getType() != GPStore::Value::LIST) {
if (result[i][j].toString() != trueResults[i][j]) {
cout << "Mismatch in value" << endl;
return false;
}
} else {
// Parse the trueResults into a vector<string>
string curTrue = trueResults[i][j].substr(1, trueResults[i][j].size() - 2); // get rid of outermost brackets
vector<string> trueList;
if (curTrue[0] == '[')
trueList = splitList(curTrue);
else
trueList = split(curTrue, ',');
size_t genListLen = 0;
std::vector<GPStore::Value *> *genListVal = result[i][j].data_.List;
for (int k = 0; k < genListVal->size(); ++k) {
string curVal = (*genListVal)[k]->toString();
if (find(trueList.begin(), trueList.end(), curVal) == trueList.end()) {
cout << "Mismatch in list value" << endl;
return false;
}
genListLen++;
}
if (genListLen != trueList.size()) {
cout << "Mismatch in list length" << endl;
return false;
}
}
}
}
return true;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
cout << "Usage: " << argv[0] << " <sf in {0.1, 3}>" << endl;
return 1;
}
string sf = argv[1];
if (sf != "0.1" && sf != "3") {
cout << "Usage: " << argv[0] << " <sf in {0.1, 3}>" << endl;
return 1;
}
// [FILL HERE] Load the dataset according to the scale factor
// Repeatedly read test cases from stdin
string line;
while (getline(cin, line)) {
if (line == "exit")
break;
if (line == "builtin_test") {
// Read test cases from file and compare the results with the ground truth
string groundTruthDir = "ground_truth/";
vector<string> procs = {"ic1", "ic2", "is1"};
for (const string &proc : procs) {
string groundTruthFile = groundTruthDir + proc + "-sf" + sf + ".txt";
ifstream fin(groundTruthFile);
if (!fin.is_open()) {
cout << "Failed to open " << groundTruthFile << endl;
return 1;
}
string line;
while (getline(fin, line)) {
cout << proc << " " << line << endl;
vector<GPStore::Value> args;
vector<vector<GPStore::Value>> result;
if (proc == "ic1") {
size_t pos = line.find(" ");
string personId_str = line.substr(0, pos);
string firstName = line.substr(pos + 1);
long long personId = stoll(personId_str);
args.emplace_back(personId);
args.emplace_back(firstName);
// ic1(args, result);
} else if (proc == "ic2") {
size_t pos = line.find(" ");
string personId_str = line.substr(0, pos);
string datetime_str = line.substr(pos + 1);
long long personId = stoll(personId_str);
long long datetime = stoll(datetime_str);
args.emplace_back(personId);
args.emplace_back(datetime);
ic2(args, result);
} else if (proc == "is1") {
long long personId = stoll(line);
args.emplace_back(personId);
is1(args, result);
}
printResults(result);
getline(fin, line);
int numRows = stoi(line);
vector<vector<string>> trueResults;
for (int i = 0; i < numRows; ++i) {
getline(fin, line);
trueResults.emplace_back(split(line, '|'));
}
// Compare results
if (compareResults(result, trueResults))
cout << "Test passed" << endl;
}
}
} else {
size_t pos = line.find(" ");
string proc = line.substr(0, pos);
vector<GPStore::Value> args;
vector<vector<GPStore::Value>> result;
if (proc == "ic1") {
size_t next_pos = line.find(" ", pos + 1);
if (next_pos == string::npos) {
cout << "Invalid input" << endl;
continue;
}
string personId_str = line.substr(pos + 1, next_pos - pos - 1);
string firstName = line.substr(next_pos + 1);
long long personId = -1;
try {
personId = stoll(personId_str);
} catch (invalid_argument &e) {
cout << "Invalid input" << endl;
continue;
}
args.emplace_back(personId);
args.emplace_back(firstName);
ic1(args, result);
} else if (proc == "ic2") {
size_t next_pos = line.find(" ", pos + 1);
if (next_pos == string::npos) {
cout << "Invalid input" << endl;
continue;
}
string personId_str = line.substr(pos + 1, next_pos - pos - 1);
string datetime_str = line.substr(next_pos + 1);
long long personId = -1, datetime = -1;
try {
personId = stoll(personId_str);
} catch (invalid_argument &e) {
cout << "Invalid input" << endl;
continue;
}
try {
datetime = stoll(datetime_str);
} catch (invalid_argument &e) {
cout << "Invalid input" << endl;
continue;
}
args.emplace_back(personId);
args.emplace_back(datetime);
ic2(args, result);
} else if (proc == "is1") {
string personId_str = line.substr(pos + 1);
long long personId = -1;
try {
personId = stoll(personId_str);
} catch (invalid_argument &e) {
cout << "Invalid input" << endl;
continue;
}
args.emplace_back(personId);
is1(args, result);
}
printResults(result);
}
}
return 0;
}