-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathjson_test.cpp
More file actions
514 lines (423 loc) · 14.7 KB
/
json_test.cpp
File metadata and controls
514 lines (423 loc) · 14.7 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright (c) 2013, 2014, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
//
// mFAST is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mFAST is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with mFast. If not, see <http://www.gnu.org/licenses/>.
//
#include <catch2/catch_test_macros.hpp>
#include "test3.h"
#include <mfast/json/json.h>
#include <sstream>
#include "debug_allocator.h"
namespace mfast {
namespace json {
bool get_quoted_string(std::istream& strm,
std::string* pstr,
const mfast::byte_vector_mref* pref,
bool first_quote_extracted=false);
bool skip_value (std::istream& strm);
namespace encode_detail {
struct quoted_string {
quoted_string(const char* str)
: str_(str)
{
}
const char* str_;
};
std::ostream& operator << (std::ostream& os, quoted_string str);
}
}
}
TEST_CASE("test json_encode with product type","[json_encode_product_test]")
{
using namespace test3;
Product product_holder;
Product_mref product_ref = product_holder.mref();
// product_ref.set_id().as(1);
product_ref.set_price().as(12356, -2);
product_ref.set_name().as("Foo");
Product_mref::tags_mref tags = product_ref.set_tags();
REQUIRE(tags.instruction()->field_type() == mfast::field_type_sequence);
REQUIRE(tags.instruction()->subinstructions().size() == 1U);
tags.resize(2);
REQUIRE(tags.size() == 2U);
mfast::ascii_string_mref tag0 = tags[0];
REQUIRE(tag0.instruction()->field_type() == mfast::field_type_ascii_string);
tags[0].as("Bar with \"quote\"");
REQUIRE(strcmp(tags[0].c_str(), "Bar with \"quote\"") == 0);
tags[1].as("Eek with \\");
REQUIRE(strcmp(tags[1].c_str(), "Eek with \\") == 0);
Product_mref::stock_mref stock = product_ref.set_stock();
stock.set_warehouse().as(300);
stock.set_retail().as(20);
const unsigned char ext_data[] = "{\"test1\":1}";
// product_ref.set_ext().assign(ext_data, ext_data+sizeof(ext_data)-1);
product_ref.set_ext().refers_to(ext_data, sizeof(ext_data)-1);
const char* result = "{\"name\":\"Foo\",\"price\":123.56,\"tags\":[\"Bar with \\\"quote\\\"\",\"Eek with \\\\\"],\"stock\":{\"warehouse\":300,\"retail\":20},\"ext\":{\"test1\":1}}";
{
std::ostringstream ostrm;
mfast::json::encode(ostrm,
product_ref,
mfast_tag::JSON_UNKNOWN);
// std::cout << strm.str() << "\n";
// std::cout << result << "\n";
REQUIRE(ostrm.str() == result);
}
{
std::ostringstream ostrm;
mfast::json::encode(ostrm,
product_ref,
mfast_tag::JSON_UNKNOWN,
mfast_tag::JSON_IGNORE);
const char* result = "{\"name\":\"Foo\",\"price\":123.56,\"stock\":{\"warehouse\":300,\"retail\":20},\"ext\":{\"test1\":1}}";
REQUIRE(ostrm.str() == result);
}
debug_allocator alloc;
Product product2_holder(product_ref, &alloc);
Product product3_holder;
std::istringstream istrm(result);
mfast::json::decode(istrm,
product3_holder.mref(),
mfast_tag::JSON_UNKNOWN);
//
REQUIRE(product3_holder.cref() == product_ref);
product_ref.omit_stock();
REQUIRE(product_ref.get_stock().absent());
REQUIRE_THROWS_AS(product_ref.try_get_stock(), mfast::bad_optional_access);
}
TEST_CASE("test json_encode with person type","[json_encode_person_test]")
{
using namespace test3;
Person person_holder;
Person_mref person_ref = person_holder.mref();
person_ref.set_firstName().as("John");
person_ref.set_lastName().as("Smith");
person_ref.set_age().as(25);
Person_mref::phoneNumbers_mref phones = person_ref.set_phoneNumbers();
phones.resize(2);
phones[0].set_type().as("home");
phones[0].set_number().as("212 555-1234");
phones[1].set_type().as("fax");
phones[1].set_number().as("646 555-4567");
LoginAccount_mref login = person_ref.set_login().as<LoginAccount>();
login.set_userName().as("John");
login.set_password().as("J0hnsm1th");
REQUIRE(person_ref.get_login().present());
person_ref.set_bankAccounts().grow_by(1);
REQUIRE(person_ref.get_bankAccounts().size() == 1U);
BankAccount_mref acct0 = person_ref.set_bankAccounts()[0].as<BankAccount>();
acct0.set_number().as(12345678);
acct0.set_routingNumber().as(87654321);
REQUIRE(person_ref.get_bankAccounts().size() == 1U);
mfast::nested_message_cref n0 = person_ref.get_bankAccounts()[0];
BankAccount_cref acct0_read = static_cast<BankAccount_cref>(n0.target());
REQUIRE(acct0_read.get_number().value() == 12345678U);
REQUIRE(acct0_read.get_routingNumber().value() == 87654321U);
std::stringstream strm;
mfast::json::encode(strm, person_ref);
auto result = R"({"firstName":"John","lastName":"Smith","age":25,)"
R"("phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"fax","number":"646 555-4567"}],)"
R"("emails":[],"login":{"userName":"John","password":"J0hnsm1th"},"bankAccounts":[{"number":12345678,"routingNumber":87654321}]})";
REQUIRE(strm.str() == result);
debug_allocator alloc;
Person person_holder2(person_ref, &alloc);
}
TEST_CASE("test json_decode with a value null","[json_decode_null_test]")
{
using namespace test3;
try {
LoginAccount account_holder;
auto& result = R"({"userName":"test","password": null})";
std::stringstream strm(result);
mfast::json::decode(strm, account_holder.mref());
REQUIRE(account_holder.cref().get_userName().value() == boost::string_ref("test"));
REQUIRE( account_holder.cref().get_password().absent() );
}
catch (boost::exception& ex)
{
std::cerr << diagnostic_information(ex);
}
}
TEST_CASE("test the json encoding with quoted string","[test_encode_quoted_string]")
{
{
std::stringstream stream;
mfast::json::encode_detail::quoted_string str("abcd\x01\b\t\f\n\r\\\"");
stream << str;
REQUIRE(stream.str() == std::string("\"abcd\\u0001\\b\\t\\f\\n\\r\\\\\\\"\"") );
}
{
std::stringstream stream;
mfast::json::encode_detail::quoted_string str("\xE2\x80\x93");
stream << str;
REQUIRE(stream.str() == std::string("\"\xE2\x80\x93\"") );
}
}
TEST_CASE("test the json decoding with quoted string","[test_get_quoted_string]")
{
using namespace mfast;
using namespace mfast::json;
debug_allocator alloc;
std::string str;
const byte_vector_field_instruction byte_vector_field_instruction_prototype(operator_none,presence_mandatory,0,nullptr,"",nullptr, string_value_storage(), 0, "", "");
value_storage storage;
byte_vector_field_instruction_prototype.construct_value(storage, &alloc);
byte_vector_mref bv_ref(&alloc,
&storage,
&byte_vector_field_instruction_prototype);
{
const char data[] = R"("abcd",)";
std::stringstream strm(data);
REQUIRE(get_quoted_string(strm, &str, &bv_ref, false));
REQUIRE(str == std::string("abcd"));
REQUIRE(bv_ref.size() == sizeof(data)-2 );
REQUIRE(memcmp(data, bv_ref.data(), sizeof(data)-2 ) ==0);
strm >> str;
REQUIRE(str == std::string(","));
}
{
bv_ref.clear();
const char data[] = R"("abc\"d",)";
std::stringstream strm(data);
REQUIRE(get_quoted_string(strm, &str, &bv_ref, false));
REQUIRE(str == std::string(R"(abc"d)") );
REQUIRE(bv_ref.size() == sizeof(data)-2 );
REQUIRE(memcmp(data, bv_ref.data(), sizeof(data)-2) == 0);
strm >> str;
REQUIRE(str == std::string(","));
}
{
bv_ref.clear();
const char data[] = R"("\u4e2d\u83EF\u6c11\u570B",)";
std::stringstream strm(data);
REQUIRE(get_quoted_string(strm, &str, &bv_ref, false));
REQUIRE(str == std::string("中華民國"));
REQUIRE(bv_ref.size() == sizeof(data)-2 );
REQUIRE(memcmp(data, bv_ref.data(), sizeof(data)-2 ) ==0);
strm >> str;
REQUIRE(str == std::string(","));
}
{
bv_ref.clear();
// U+10437 U+24B62
const char data[] = R"("\uD801\uDC37\uD852\uDF62",)";
std::stringstream strm(data);
REQUIRE(get_quoted_string(strm, &str, &bv_ref, false));
REQUIRE(str == std::string("\xF0\x90\x90\xB7\xF0\xA4\xAD\xA2"));
REQUIRE(bv_ref.size() == sizeof(data)-2 );
REQUIRE(memcmp(data, bv_ref.data(), sizeof(data)-2 ) ==0);
strm >> str;
REQUIRE(str == std::string(","));
}
// {
// bv_ref.clear();
// const char data[] = "\"Konu\\u0015fal\\u00131m bir gün\",";
// std::stringstream strm(data);
// REQUIRE(get_quoted_string(strm, &str, &bv_ref, false));
// }
{
bv_ref.clear();
const char data[] = "\"abc\\nd\\/\",";
std::stringstream strm(data);
REQUIRE(get_quoted_string(strm, &str, &bv_ref, false));
REQUIRE(str == std::string("abc\nd/"));
REQUIRE(bv_ref.size() == sizeof(data)-2);
REQUIRE(memcmp(data, bv_ref.data(), sizeof(data)-2) ==0);
strm >> str;
REQUIRE(str == std::string(","));
}
byte_vector_field_instruction_prototype.destruct_value(storage, &alloc);
}
TEST_CASE("test json skip value","[test_skip_value]")
{
using namespace mfast::json;
std::string str;
{
std::stringstream strm(" 123.45,");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" \"ab\\ncd\",");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" null,");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" true,");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" false,");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" [1, 2, 3],");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
REQUIRE(strm.eof());
}
{
std::stringstream strm(R"( [1, [ "abc" ], 3],)");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
REQUIRE(strm.eof());
}
{
std::stringstream strm(R"( [1, { "f1":"abc" }, 3],)");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
REQUIRE(strm.eof());
}
{
std::stringstream strm(R"( {"id":1,"name":"Foo"},)");
REQUIRE(skip_value(strm));
strm >> str;
REQUIRE(str == std::string(","));
REQUIRE(strm.eof());
}
}
TEST_CASE("test code generation for sequence","[test_seq_codegen]")
{
using namespace test3;
const UsingSeqTemplates::instruction_type* top_inst = UsingSeqTemplates::instruction();
REQUIRE(top_inst->subinstructions().size() == 3U);
const mfast::sequence_field_instruction* seq1_inst = dynamic_cast<const mfast::sequence_field_instruction*>(top_inst->subinstruction(1));
REQUIRE(seq1_inst);
REQUIRE(strcmp(seq1_inst->name(), "seq1")==0);
REQUIRE(seq1_inst->subinstructions().size() == 2U);
REQUIRE(seq1_inst->ref_instruction() == SeqTemplate1::instruction());
REQUIRE(seq1_inst->element_instruction() == (const mfast::group_field_instruction*) nullptr);
const mfast::sequence_field_instruction* seq2_inst = dynamic_cast<const mfast::sequence_field_instruction*>(top_inst->subinstruction(2));
REQUIRE(seq2_inst);
REQUIRE(strcmp(seq2_inst->name(), "seq2")==0);
REQUIRE(seq2_inst->subinstructions().size() == 4U);
REQUIRE(seq2_inst->ref_instruction() == SeqTemplate2::instruction());
REQUIRE(seq2_inst->element_instruction() == BankAccount::instruction());
}
TEST_CASE("test decimal output","[test_decimal_output]")
{
using mfast::decimal_value_storage;
{
std::stringstream strm;
strm << decimal_value_storage(12345, 0);
REQUIRE(strm.str() == std::string("12345"));
}
{
std::stringstream strm;
strm << decimal_value_storage(-12345, 0);
REQUIRE(strm.str() == std::string("-12345"));
}
{
std::stringstream strm;
strm << decimal_value_storage(12345, 2);
REQUIRE(strm.str() == std::string("1234500"));
}
{
std::stringstream strm;
strm << decimal_value_storage(-12345, 2);
REQUIRE(strm.str() == std::string("-1234500"));
}
{
std::stringstream strm;
strm << decimal_value_storage(12345, -2);
REQUIRE(strm.str() == std::string("123.45"));
}
{
std::stringstream strm;
strm << decimal_value_storage(-12345, -2);
REQUIRE(strm.str() == std::string("-123.45"));
}
{
std::stringstream strm;
strm << decimal_value_storage(12345, -5);
REQUIRE(strm.str() == std::string("0.12345"));
}
{
std::stringstream strm;
strm << decimal_value_storage(-12345, -5);
REQUIRE(strm.str() == std::string("-0.12345"));
}
{
std::stringstream strm;
strm << decimal_value_storage(12345, -7);
REQUIRE(strm.str() == std::string("0.0012345"));
}
{
std::stringstream strm;
strm << decimal_value_storage(-12345, -7);
REQUIRE(strm.str() == std::string("-0.0012345"));
}
}
TEST_CASE("test decimal input","[test_decimal_input]")
{
using namespace mfast::json;
std::string str;
mfast::decimal_value_storage storage;
{
std::stringstream strm(" 123.45,");
strm >> storage;
REQUIRE(storage.mantissa() == 12345LL);
REQUIRE(storage.exponent() == -2);
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" -123.45,");
strm >> storage;
REQUIRE(storage.mantissa() == -12345LL);
REQUIRE(storage.exponent() == -2);
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" 1.2345E+12,");
strm >> storage;
REQUIRE(storage.mantissa() == 12345LL);
REQUIRE(storage.exponent() == 8);
strm >> str;
REQUIRE(str == std::string(","));
}
{
std::stringstream strm(" 1.2345E-12,");
strm >> storage;
REQUIRE(storage.mantissa() == 12345LL);
REQUIRE(storage.exponent() == -16);
strm >> str;
REQUIRE(str == ",");
}
{
std::stringstream strm(" 0.123,");
strm >> storage;
REQUIRE(storage.mantissa() == 123LL);
REQUIRE(storage.exponent() == -3);
strm >> str;
REQUIRE(str == ",");
}
}