-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cc
More file actions
345 lines (312 loc) · 10 KB
/
tests.cc
File metadata and controls
345 lines (312 loc) · 10 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "csv.hpp"
#include <map>
#include <iostream>
#include <boost/noncopyable.hpp>
int errors = 0;
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
os << "[";
for (typename std::vector<T>::const_iterator it = vec.begin();
it != vec.end();) {
os << *it;
for (++it; it != vec.end(); ++it) {
os << "," << *it;
}
}
os << "]";
return os;
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::map<T1, T2>& map) {
os << "[";
for (typename std::map<T1, T2>::const_iterator it = map.begin();
it != map.end();) {
os << it->first << ":" << it->second;
for (++it; it != map.end(); ++it) {
os << "," << it->first << ":" << it->second;
}
}
os << "]";
return os;
}
template <typename S>
static void EXPECT_TRUE(bool val, S& message) {
if (val)
return;
++errors;
std::cout << "[ERROR] " << message << "\n";
}
template <typename T>
static void EXPECT_EQ(T expected, T actual) {
if (expected == actual)
return;
++errors;
std::cout << "[ERROR] expected <" << expected << "> got <" << actual << ">\n";
}
static void test_spaces() {
std::string csv_data = "hi there\nhow are\nyou doing\n";
std::vector<std::string> words;
auto f = [&words](const std::string& a, const std::string& b) {
words.push_back(a);
words.push_back(b);
};
auto parser = csv::make_parser(f);
parser.set_delim_char(' ');
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::vector<std::string> expected = {"hi", "there", "how",
"are", "you", "doing"};
EXPECT_EQ(expected, words);
}
static void test_ignore_field() {
std::string csv_data = "hi there\nhow are\nyou doing\n";
std::vector<std::string> words;
auto f = [&words](const csv::ignore&,
const std::string& b) { words.push_back(b); };
auto parser = csv::make_parser(f);
parser.set_delim_char(' ');
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::vector<std::string> expected = {"there", "are", "doing"};
EXPECT_EQ(expected, words);
}
static void test_header() {
std::string csv_data = "name num\nlarry 1\nmary 3\n";
std::map<std::string, uint32_t> values;
auto f = [&values](const std::string& a, const uint32_t b) { values[a] = b; };
auto parser = csv::make_parser(f);
parser.set_skip_header();
parser.set_delim_char(' ');
parser.set_comment_mark("#");
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::map<std::string, uint32_t> expected = {{"larry", 1}, {"mary", 3}};
EXPECT_EQ(expected, values);
}
static void test_comments() {
std::string csv_data = "hi there\n#how are\nyou doing\n";
std::vector<std::string> words;
auto f = [&words](const std::string& a, const std::string& b) {
words.push_back(a);
words.push_back(b);
};
auto parser = csv::make_parser(f);
parser.set_delim_char(' ');
parser.set_comment_mark("#");
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::vector<std::string> expected = {"hi", "there", "you", "doing"};
EXPECT_EQ(expected, words);
}
static void test_filter() {
std::string csv_data = "hi there\nhow are\nyou doing\n";
std::vector<std::string> words;
auto f = [&words](const std::string& a, const std::string& b) {
words.push_back(a);
words.push_back(b);
};
auto parser = csv::make_parser(f);
parser.set_delim_char(' ');
parser.add_row_filter([](size_t, const char* buf, size_t len) {
return len == 3 && std::equal(buf, buf + len, "how");
});
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::vector<std::string> expected = {"hi", "there", "you", "doing"};
EXPECT_EQ(expected, words);
}
static void test_accept_filter() {
std::string filter_term = "how";
std::string csv_data = "hi there\nhow are\nyou doing\n";
std::vector<std::string> words;
auto f = [&words](const std::string& a, const std::string& b) {
words.push_back(a);
words.push_back(b);
};
auto parser = csv::make_parser(f);
parser.set_delim_char(' ');
parser.add_row_filter([filter_term](size_t field_num, const char* buf,
size_t len) {
if (field_num > 0) {
return csv::ROW_OK;
} else if (len == filter_term.size() &&
std::equal(buf, buf + len, filter_term.data())) {
return csv::ROW_OK;
} else {
return csv::ROW_DROP;
}
});
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::vector<std::string> expected = {"how", "are"};
EXPECT_EQ(expected, words);
}
static void test_quote_escaping() {
std::string csv_data = "13,'Tiki,'\n14,'Let''s get busy'\n";
std::map<int, std::string> words;
auto f = [&words](const int& a, const std::string& b) { words[a] = b; };
auto parser = csv::make_parser(f);
parser.set_quote_char('\'');
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::map<int, std::string> expected = {{13, "Tiki,"}, {14, "Let's get busy"}};
EXPECT_EQ(expected, words);
}
static void test_grouping() {
std::string csv_data = //
"6,joe\n" //
"3,louise\n" //
"2,mary\n" //
"1,louise\n";
std::map<std::string, int> groups;
auto parser = csv::make_parser( //
[&groups](const int count, const std::string name) { //
groups[name] += count; //
});
auto r = parser.Parse(csv_data) && parser.Finish();
EXPECT_TRUE(r, parser.ErrorString());
std::map<std::string, int> expected{ //
{"joe", 6}, //
{"mary", 2}, //
{"louise", 4}, //
};
EXPECT_EQ(expected, groups);
}
static void test_number_file() {
int tot_a = 0;
int tot_b = 0;
auto parser = csv::make_parser([&tot_a, &tot_b](int a, int b) {
tot_a += a;
tot_b += b;
});
auto r = parser.ParseFile("test_numbers.csv");
EXPECT_TRUE(r, parser.ErrorString());
EXPECT_EQ(46, tot_a);
EXPECT_EQ(512, tot_b);
}
static void test_functor() {
struct Adder : boost::noncopyable {
int tot_a = 0;
int tot_b = 0;
void operator()(int a, int b) {
tot_a += a;
tot_b += b;
}
};
Adder adder;
auto parser = csv::make_parser(adder);
auto r = parser.ParseFile("test_numbers.csv");
EXPECT_TRUE(r, parser.ErrorString());
EXPECT_EQ(46, adder.tot_a);
EXPECT_EQ(512, adder.tot_b);
}
template <typename T>
void template_func(const std::string& path, T& command) {
auto parser = csv::make_parser(command);
auto r = parser.ParseFile(path);
EXPECT_TRUE(r, parser.ErrorString());
}
static void test_template_func() {
struct Adder : boost::noncopyable {
int tot_a = 0;
int tot_b = 0;
void operator()(int a, int b) {
tot_a += a;
tot_b += b;
}
};
Adder adder;
template_func("test_numbers.csv", adder);
EXPECT_EQ(46, adder.tot_a);
EXPECT_EQ(512, adder.tot_b);
}
static int free_func_total_a = 0;
static int free_func_total_b = 0;
void free_func(int a, int b) {
free_func_total_a += a;
free_func_total_b += b;
}
static void test_free_func() {
free_func_total_a = 0;
free_func_total_b = 0;
auto parser = csv::make_parser(free_func);
auto r = parser.ParseFile("test_numbers.csv");
EXPECT_TRUE(r, parser.ErrorString());
EXPECT_EQ(46, free_func_total_a);
EXPECT_EQ(512, free_func_total_b);
}
static void test_parse_stream() {
free_func_total_a = 0;
free_func_total_b = 0;
std::istringstream input("1,2\n3,4\n");
auto parser = csv::make_parser(free_func);
auto r = parser.ParseStream(input);
EXPECT_TRUE(r, parser.ErrorString());
EXPECT_EQ(4, free_func_total_a);
EXPECT_EQ(6, free_func_total_b);
}
static void test_bad_cast() {
free_func_total_a = 0;
free_func_total_b = 0;
std::istringstream input("1,hi\n3,4\n");
auto parser = csv::make_parser(free_func);
parser.set_error_func([](size_t, size_t, const std::string&,
const std::exception_ptr) {
return csv::ROW_DROP; // squash stderr
});
auto r = parser.ParseStream(input);
EXPECT_TRUE(r, parser.ErrorString());
EXPECT_EQ(3, free_func_total_a);
EXPECT_EQ(4, free_func_total_b);
}
static void test_bad_cast_callback() {
free_func_total_a = 0;
free_func_total_b = 0;
std::istringstream input("1,hi\n3,4\n");
auto parser = csv::make_parser(free_func);
bool handler_called = false;
parser.set_error_func([&handler_called](size_t row, size_t column,
const std::string& message,
const std::exception_ptr ex) {
handler_called = true;
EXPECT_EQ(size_t(1), row);
EXPECT_EQ(size_t(2), column);
EXPECT_TRUE(!message.empty(), "empty message");
EXPECT_TRUE(bool(ex), "no exception");
return csv::ROW_DROP;
});
auto r = parser.ParseStream(input);
EXPECT_TRUE(r, parser.ErrorString());
EXPECT_EQ(3, free_func_total_a);
EXPECT_EQ(4, free_func_total_b);
EXPECT_TRUE(handler_called, "handler not called");
}
#define run(fp) \
std::cout << "[START] " << #fp << "\n"; \
try { \
fp(); \
} \
catch (const std::exception& e) { \
std::cerr << e.what() << "\n"; \
throw; \
} \
std::cout << "[END ] " << #fp << "\n";
int main(int, char**) {
run(test_number_file);
run(test_grouping);
run(test_spaces);
run(test_ignore_field);
run(test_header);
run(test_comments);
run(test_filter);
run(test_accept_filter);
run(test_quote_escaping);
run(test_functor);
run(test_template_func);
run(test_free_func);
run(test_parse_stream);
run(test_bad_cast);
run(test_bad_cast_callback);
std::cout << (errors ? "ERRORS!\n" : "Ok\n");
return errors;
}