forked from danielaparker/jsoncons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_decode_csv_tests.cpp
More file actions
325 lines (262 loc) · 8.84 KB
/
Copy pathencode_decode_csv_tests.cpp
File metadata and controls
325 lines (262 loc) · 8.84 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
// Copyright 2013-2026 Daniel Parker
// Distributed under Boost license
#if defined(_MSC_VER)
#include "windows.h" // test no inadvertant macro expansions
#endif
#include <jsoncons_ext/csv/csv.hpp>
#include <jsoncons/json.hpp>
#include <jsoncons/json_reader.hpp>
#include <map>
#include <vector>
#include <catch/catch.hpp>
using namespace jsoncons;
namespace
{
class MyIterator
{
const char* p_;
public:
using iterator_category = std::input_iterator_tag;
using value_type = char;
using difference_type = std::ptrdiff_t;
using pointer = const char*;
using reference = const char&;
MyIterator(const char* p)
: p_(p)
{
}
reference operator*() const
{
return *p_;
}
pointer operator->() const
{
return p_;
}
MyIterator& operator++()
{
++p_;
return *this;
}
MyIterator operator++(int)
{
MyIterator temp(*this);
++*this;
return temp;
}
bool operator!=(const MyIterator& rhs) const
{
return p_ != rhs.p_;
}
};
} // namespace
TEST_CASE("encode decode csv source")
{
using cpp_type = std::vector<std::tuple<std::string,int>>;
std::string input = "\"a\",1\n\"b\",2";
auto options = csv::csv_options{}
.mapping_kind(csv::csv_mapping_kind::n_rows)
.assume_header(false);
SECTION("from string")
{
cpp_type v = csv::decode_csv<cpp_type>(input, options);
REQUIRE(2 == v.size()); //-V521
CHECK(std::get<0>(v[0]) == "a"); //-V521
CHECK(1 == std::get<1>(v[0])); //-V521
CHECK(std::get<0>(v[1]) == "b"); //-V521
CHECK(2 == std::get<1>(v[1])); //-V521
std::string s2;
csv::encode_csv(v, s2, options);
json j1 = csv::decode_csv<json>(input);
json j2 = csv::decode_csv<json>(s2);
CHECK(j2 == j1); //-V521
json j3 = csv::decode_csv<json>(s2.begin(), s2.end());
CHECK(j3 == j1); //-V521
}
SECTION("from stream")
{
std::stringstream is(input);
cpp_type v = csv::decode_csv<cpp_type>(is, options);
REQUIRE(2 == v.size()); //-V521
CHECK(std::get<0>(v[0]) == "a"); //-V521
CHECK(1 == std::get<1>(v[0])); //-V521
CHECK(std::get<0>(v[1]) == "b"); //-V521
CHECK(2 == std::get<1>(v[1])); //-V521
std::stringstream ss2;
csv::encode_csv(v, ss2, options);
json j1 = csv::decode_csv<json>(input);
json j2 = csv::decode_csv<json>(ss2);
CHECK(j2 == j1); //-V521
}
SECTION("from iterator")
{
cpp_type v = csv::decode_csv<cpp_type>(input.begin(), input.end(), options);
REQUIRE(2 == v.size()); //-V521
CHECK(std::get<0>(v[0]) == "a"); //-V521
CHECK(1 == std::get<1>(v[0])); //-V521
CHECK(std::get<0>(v[1]) == "b"); //-V521
CHECK(2 == std::get<1>(v[1])); //-V521
std::stringstream ss2;
csv::encode_csv(v, ss2, options);
json j1 = csv::decode_csv<json>(input);
json j2 = csv::decode_csv<json>(ss2);
CHECK(j2 == j1); //-V521
}
SECTION("from custom iterator")
{
MyIterator it(input.data());
MyIterator end(input.data() + input.length());
cpp_type v = csv::decode_csv<cpp_type>(it, end, options);
REQUIRE(2 == v.size()); //-V521
CHECK(std::get<0>(v[0]) == "a"); //-V521
CHECK(1 == std::get<1>(v[0])); //-V521
CHECK(std::get<0>(v[1]) == "b"); //-V521
CHECK(2 == std::get<1>(v[1])); //-V521
std::stringstream ss2;
csv::encode_csv(v, ss2, options);
json j1 = csv::decode_csv<json>(input);
json j2 = csv::decode_csv<json>(ss2);
CHECK(j2 == j1); //-V521
}
}
TEST_CASE("decode_csv non-numeric string into double reports error")
{
// Regression test for staj_event::as_double silently returning 0.0
// when a string_value event is decoded into a floating-point type.
// Introduced in caacd258a ("Remove chars_to, replace with to_double"):
// the new to_double API's return code was not propagated to ec, so
// non-numeric and empty cells were coerced to 0.0 without error.
using cpp_type = std::vector<std::vector<double>>;
auto options = csv::csv_options{}
.mapping_kind(csv::csv_mapping_kind::n_rows)
.assume_header(false);
SECTION("non-numeric token")
{
std::string s = "hey\n";
auto result = csv::try_decode_csv<cpp_type>(s, options);
REQUIRE(!result); //-V521
REQUIRE_THROWS(csv::decode_csv<cpp_type>(s, options));
}
SECTION("empty trailing field")
{
std::string s = "0,\n";
auto result = csv::try_decode_csv<cpp_type>(s, options);
REQUIRE(!result); //-V521
REQUIRE_THROWS(csv::decode_csv<cpp_type>(s, options));
}
}
namespace
{
struct csv_string_encoder_reset_test_fixture
{
std::string output1;
std::string output2;
csv::csv_string_encoder encoder;
csv_string_encoder_reset_test_fixture()
: encoder(output1, csv::csv_options().assume_header(true))
{}
std::string string1() const {return output1;}
std::string string2() const {return output2;}
};
struct csv_stream_encoder_reset_test_fixture
{
std::ostringstream output1;
std::ostringstream output2;
csv::csv_stream_encoder encoder;
csv_stream_encoder_reset_test_fixture()
: encoder(output1, csv::csv_options().assume_header(true))
{}
std::string string1() const {return output1.str();}
std::string string2() const {return output2.str();}
};
} // namespace
TEMPLATE_TEST_CASE("test_csv_encoder_reset", "",
csv_string_encoder_reset_test_fixture,
csv_stream_encoder_reset_test_fixture)
{
using fixture_type = TestType;
fixture_type f;
// Parially encode, reset, then fully encode to same sink
f.encoder.begin_array();
f.encoder.begin_array();
f.encoder.string_value("h1");
f.encoder.string_value("h2");
f.encoder.end_array();
f.encoder.begin_array();
f.encoder.uint64_value(1);
// Missing column and array end
f.encoder.flush();
CHECK("h1,h2\n1" == f.string1()); // streaming case
f.encoder.reset();
f.encoder.begin_array();
f.encoder.begin_array();
f.encoder.string_value("h3");
f.encoder.string_value("h4");
f.encoder.end_array();
f.encoder.begin_array();
f.encoder.uint64_value(3);
f.encoder.uint64_value(4);
f.encoder.end_array();
f.encoder.end_array();
f.encoder.flush();
CHECK("h1,h2\n1h3,h4\n3,4\n" == f.string1()); // streaming case
// Reset and encode to different sink
f.encoder.reset(f.output2);
f.encoder.begin_array();
f.encoder.begin_array();
f.encoder.string_value("h5");
f.encoder.string_value("h6");
f.encoder.end_array();
f.encoder.begin_array();
f.encoder.uint64_value(5);
f.encoder.uint64_value(6);
f.encoder.end_array();
f.encoder.end_array();
f.encoder.flush();
CHECK("h5,h6\n5,6\n" == f.string2());
}
namespace { namespace ns {
struct Person
{
std::string name;
};
}}
JSONCONS_ALL_MEMBER_TRAITS(ns::Person, name)
#if defined(JSONCONS_HAS_STATEFUL_ALLOCATOR) && JSONCONS_HAS_STATEFUL_ALLOCATOR == 1
#include <scoped_allocator>
#include <common/mock_stateful_allocator.hpp>
template <typename T>
using MyScopedAllocator = std::scoped_allocator_adaptor<mock_stateful_allocator<T>>;
TEST_CASE("encode_csv allocator_set overloads")
{
MyScopedAllocator<char> temp_alloc(1);
//auto aset = make_alloc_set(temp_alloc_arg, temp_alloc);
json persons(json_array_arg);
json person(json_object_arg);
person.try_emplace("name", "John Smith");
persons.emplace_back(std::move(person));
SECTION("json, stream")
{
std::string s;
std::stringstream ss(s);
auto options = jsoncons::csv::csv_options{}
.assume_header(true);
options.mapping_kind(jsoncons::csv::csv_mapping_kind::n_objects);
csv::encode_csv(/*aset,*/ persons, ss, options);
json other = csv::decode_csv<json>(/*aset,*/ ss, options);
CHECK(other == persons);
}
SECTION("custom, stream")
{
std::string s;
std::stringstream ss(s);
auto options = jsoncons::csv::csv_options{}
.assume_header(true);
options.mapping_kind(jsoncons::csv::csv_mapping_kind::n_objects);
csv::encode_csv(/*aset,*/ persons, ss, options);
auto other = csv::decode_csv<std::vector<ns::Person>>(/*aset,*/ ss, options);
REQUIRE(1 == other.size());
CHECK(other[0].name == persons[0].at("name").as_string());
}
}
#endif