Electroneum
readertest.cpp
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #include "unittest.h"
16 
17 #include "rapidjson/reader.h"
20 #include "rapidjson/memorystream.h"
21 
22 #include <limits>
23 
24 using namespace rapidjson;
25 
26 RAPIDJSON_DIAG_PUSH
27 #ifdef __GNUC__
28 RAPIDJSON_DIAG_OFF(effc++)
29 RAPIDJSON_DIAG_OFF(float-equal)
30 RAPIDJSON_DIAG_OFF(missing-noreturn)
31 #if __GNUC__ >= 7
32 RAPIDJSON_DIAG_OFF(dangling-else)
33 #endif
34 #endif // __GNUC__
35 
36 #ifdef __clang__
37 RAPIDJSON_DIAG_OFF(variadic-macros)
38 RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
39 #endif
40 
41 template<bool expect>
42 struct ParseBoolHandler : BaseReaderHandler<UTF8<>, ParseBoolHandler<expect> > {
43  ParseBoolHandler() : step_(0) {}
44  bool Default() { ADD_FAILURE(); return false; }
45  // gcc 4.8.x generates warning in EXPECT_EQ(bool, bool) on this gtest version.
46  // Workaround with EXPECT_TRUE().
47  bool Bool(bool b) { /*EXPECT_EQ(expect, b); */EXPECT_TRUE(expect == b); ++step_; return true; }
48 
49  unsigned step_;
50 };
51 
52 TEST(Reader, ParseTrue) {
53  StringStream s("true");
55  Reader reader;
56  reader.Parse(s, h);
57  EXPECT_EQ(1u, h.step_);
58 }
59 
60 TEST(Reader, ParseFalse) {
61  StringStream s("false");
63  Reader reader;
64  reader.Parse(s, h);
65  EXPECT_EQ(1u, h.step_);
66 }
67 
68 struct ParseIntHandler : BaseReaderHandler<UTF8<>, ParseIntHandler> {
69  ParseIntHandler() : step_(0), actual_() {}
70  bool Default() { ADD_FAILURE(); return false; }
71  bool Int(int i) { actual_ = i; step_++; return true; }
72 
73  unsigned step_;
74  int actual_;
75 };
76 
77 struct ParseUintHandler : BaseReaderHandler<UTF8<>, ParseUintHandler> {
78  ParseUintHandler() : step_(0), actual_() {}
79  bool Default() { ADD_FAILURE(); return false; }
80  bool Uint(unsigned i) { actual_ = i; step_++; return true; }
81 
82  unsigned step_;
83  unsigned actual_;
84 };
85 
86 struct ParseInt64Handler : BaseReaderHandler<UTF8<>, ParseInt64Handler> {
87  ParseInt64Handler() : step_(0), actual_() {}
88  bool Default() { ADD_FAILURE(); return false; }
89  bool Int64(int64_t i) { actual_ = i; step_++; return true; }
90 
91  unsigned step_;
93 };
94 
95 struct ParseUint64Handler : BaseReaderHandler<UTF8<>, ParseUint64Handler> {
96  ParseUint64Handler() : step_(0), actual_() {}
97  bool Default() { ADD_FAILURE(); return false; }
98  bool Uint64(uint64_t i) { actual_ = i; step_++; return true; }
99 
100  unsigned step_;
102 };
103 
104 struct ParseDoubleHandler : BaseReaderHandler<UTF8<>, ParseDoubleHandler> {
105  ParseDoubleHandler() : step_(0), actual_() {}
106  bool Default() { ADD_FAILURE(); return false; }
107  bool Double(double d) { actual_ = d; step_++; return true; }
108 
109  unsigned step_;
110  double actual_;
111 };
112 
113 TEST(Reader, ParseNumber_Integer) {
114 #define TEST_INTEGER(Handler, str, x) \
115  { \
116  StringStream s(str); \
117  Handler h; \
118  Reader reader; \
119  reader.Parse(s, h); \
120  EXPECT_EQ(1u, h.step_); \
121  EXPECT_EQ(x, h.actual_); \
122  }
123 
124  TEST_INTEGER(ParseUintHandler, "0", 0u);
125  TEST_INTEGER(ParseUintHandler, "123", 123u);
126  TEST_INTEGER(ParseUintHandler, "2147483648", 2147483648u); // 2^31 - 1 (cannot be stored in int)
127  TEST_INTEGER(ParseUintHandler, "4294967295", 4294967295u);
128 
129  TEST_INTEGER(ParseIntHandler, "-123", -123);
130  TEST_INTEGER(ParseIntHandler, "-2147483648", static_cast<int32_t>(0x80000000)); // -2^31 (min of int)
131 
132  TEST_INTEGER(ParseUint64Handler, "4294967296", RAPIDJSON_UINT64_C2(1, 0)); // 2^32 (max of unsigned + 1, force to use uint64_t)
133  TEST_INTEGER(ParseUint64Handler, "18446744073709551615", RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF)); // 2^64 - 1 (max of uint64_t)
134 
135  TEST_INTEGER(ParseInt64Handler, "-2147483649", static_cast<int64_t>(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x7FFFFFFF))); // -2^31 -1 (min of int - 1, force to use int64_t)
136  TEST_INTEGER(ParseInt64Handler, "-9223372036854775808", static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0x00000000))); // -2^63 (min of int64_t)
137 
138  // Random test for uint32_t/int32_t
139  {
140  union {
141  uint32_t u;
142  int32_t i;
143  }u;
144  Random r;
145 
146  for (unsigned i = 0; i < 100000; i++) {
147  u.u = r();
148 
149  char buffer[32];
150  *internal::u32toa(u.u, buffer) = '\0';
151  TEST_INTEGER(ParseUintHandler, buffer, u.u);
152 
153  if (u.i < 0) {
154  *internal::i32toa(u.i, buffer) = '\0';
155  TEST_INTEGER(ParseIntHandler, buffer, u.i);
156  }
157  }
158  }
159 
160  // Random test for uint64_t/int64_t
161  {
162  union {
163  uint64_t u;
164  int64_t i;
165  }u;
166  Random r;
167 
168  for (unsigned i = 0; i < 100000; i++) {
169  u.u = uint64_t(r()) << 32;
170  u.u |= r();
171 
172  char buffer[32];
173  if (u.u > uint64_t(4294967295u)) {
174  *internal::u64toa(u.u, buffer) = '\0';
175  TEST_INTEGER(ParseUint64Handler, buffer, u.u);
176  }
177 
178  if (u.i < -int64_t(2147483648u)) {
179  *internal::i64toa(u.i, buffer) = '\0';
180  TEST_INTEGER(ParseInt64Handler, buffer, u.i);
181  }
182  }
183  }
184 #undef TEST_INTEGER
185 }
186 
187 template<bool fullPrecision>
188 static void TestParseDouble() {
189 #define TEST_DOUBLE(fullPrecision, str, x) \
190  { \
191  StringStream s(str); \
192  ParseDoubleHandler h; \
193  Reader reader; \
194  ASSERT_EQ(kParseErrorNone, reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h).Code()); \
195  EXPECT_EQ(1u, h.step_); \
196  internal::Double e(x), a(h.actual_); \
197  if (fullPrecision) { \
198  EXPECT_EQ(e.Uint64Value(), a.Uint64Value()); \
199  if (e.Uint64Value() != a.Uint64Value()) \
200  printf(" String: %s\n Actual: %.17g\nExpected: %.17g\n", str, h.actual_, x); \
201  } \
202  else { \
203  EXPECT_EQ(e.Sign(), a.Sign()); /* for 0.0 != -0.0 */ \
204  EXPECT_DOUBLE_EQ(x, h.actual_); \
205  } \
206  }
207 
208  TEST_DOUBLE(fullPrecision, "0.0", 0.0);
209  TEST_DOUBLE(fullPrecision, "-0.0", -0.0); // For checking issue #289
210  TEST_DOUBLE(fullPrecision, "0e100", 0.0); // For checking issue #1249
211  TEST_DOUBLE(fullPrecision, "1.0", 1.0);
212  TEST_DOUBLE(fullPrecision, "-1.0", -1.0);
213  TEST_DOUBLE(fullPrecision, "1.5", 1.5);
214  TEST_DOUBLE(fullPrecision, "-1.5", -1.5);
215  TEST_DOUBLE(fullPrecision, "3.1416", 3.1416);
216  TEST_DOUBLE(fullPrecision, "1E10", 1E10);
217  TEST_DOUBLE(fullPrecision, "1e10", 1e10);
218  TEST_DOUBLE(fullPrecision, "1E+10", 1E+10);
219  TEST_DOUBLE(fullPrecision, "1E-10", 1E-10);
220  TEST_DOUBLE(fullPrecision, "-1E10", -1E10);
221  TEST_DOUBLE(fullPrecision, "-1e10", -1e10);
222  TEST_DOUBLE(fullPrecision, "-1E+10", -1E+10);
223  TEST_DOUBLE(fullPrecision, "-1E-10", -1E-10);
224  TEST_DOUBLE(fullPrecision, "1.234E+10", 1.234E+10);
225  TEST_DOUBLE(fullPrecision, "1.234E-10", 1.234E-10);
226  TEST_DOUBLE(fullPrecision, "1.79769e+308", 1.79769e+308);
227  TEST_DOUBLE(fullPrecision, "2.22507e-308", 2.22507e-308);
228  TEST_DOUBLE(fullPrecision, "-1.79769e+308", -1.79769e+308);
229  TEST_DOUBLE(fullPrecision, "-2.22507e-308", -2.22507e-308);
230  TEST_DOUBLE(fullPrecision, "4.9406564584124654e-324", 4.9406564584124654e-324); // minimum denormal
231  TEST_DOUBLE(fullPrecision, "2.2250738585072009e-308", 2.2250738585072009e-308); // Max subnormal double
232  TEST_DOUBLE(fullPrecision, "2.2250738585072014e-308", 2.2250738585072014e-308); // Min normal positive double
233  TEST_DOUBLE(fullPrecision, "1.7976931348623157e+308", 1.7976931348623157e+308); // Max double
234  TEST_DOUBLE(fullPrecision, "1e-10000", 0.0); // must underflow
235  TEST_DOUBLE(fullPrecision, "18446744073709551616", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double)
236  TEST_DOUBLE(fullPrecision, "-9223372036854775809", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double)
237  TEST_DOUBLE(fullPrecision, "0.9868011474609375", 0.9868011474609375); // https://github.com/Tencent/rapidjson/issues/120
238  TEST_DOUBLE(fullPrecision, "123e34", 123e34); // Fast Path Cases In Disguise
239  TEST_DOUBLE(fullPrecision, "45913141877270640000.0", 45913141877270640000.0);
240  TEST_DOUBLE(fullPrecision, "2.2250738585072011e-308", 2.2250738585072011e-308); // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
241  TEST_DOUBLE(fullPrecision, "1e-00011111111111", 0.0); // Issue #313
242  TEST_DOUBLE(fullPrecision, "-1e-00011111111111", -0.0);
243  TEST_DOUBLE(fullPrecision, "1e-214748363", 0.0); // Maximum supported negative exponent
244  TEST_DOUBLE(fullPrecision, "1e-214748364", 0.0);
245  TEST_DOUBLE(fullPrecision, "1e-21474836311", 0.0);
246  TEST_DOUBLE(fullPrecision, "1.00000000001e-2147483638", 0.0);
247  TEST_DOUBLE(fullPrecision, "0.017976931348623157e+310", 1.7976931348623157e+308); // Max double in another form
248  TEST_DOUBLE(fullPrecision, "128.74836467836484838364836483643636483648e-336", 0.0); // Issue #1251
249 
250  // Since
251  // abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... x 10^-324
252  // abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... x 10 ^ -324
253  // So 2.2250738585072012e-308 should round to 2^-1022 = 2.2250738585072014e-308
254  TEST_DOUBLE(fullPrecision, "2.2250738585072012e-308", 2.2250738585072014e-308); // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
255 
256  // More closer to normal/subnormal boundary
257  // boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... x 10^-308
258  TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164564e-308", 2.2250738585072009e-308);
259  TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164565e-308", 2.2250738585072014e-308);
260 
261  // 1.0 is in (1.0 - 2^-54, 1.0 + 2^-53)
262  // 1.0 - 2^-54 = 0.999999999999999944488848768742172978818416595458984375
263  TEST_DOUBLE(fullPrecision, "0.999999999999999944488848768742172978818416595458984375", 1.0); // round to even
264  TEST_DOUBLE(fullPrecision, "0.999999999999999944488848768742172978818416595458984374", 0.99999999999999989); // previous double
265  TEST_DOUBLE(fullPrecision, "0.999999999999999944488848768742172978818416595458984376", 1.0); // next double
266  // 1.0 + 2^-53 = 1.00000000000000011102230246251565404236316680908203125
267  TEST_DOUBLE(fullPrecision, "1.00000000000000011102230246251565404236316680908203125", 1.0); // round to even
268  TEST_DOUBLE(fullPrecision, "1.00000000000000011102230246251565404236316680908203124", 1.0); // previous double
269  TEST_DOUBLE(fullPrecision, "1.00000000000000011102230246251565404236316680908203126", 1.00000000000000022); // next double
270 
271  // Numbers from https://github.com/floitsch/double-conversion/blob/master/test/cctest/test-strtod.cc
272 
273  TEST_DOUBLE(fullPrecision, "72057594037927928.0", 72057594037927928.0);
274  TEST_DOUBLE(fullPrecision, "72057594037927936.0", 72057594037927936.0);
275  TEST_DOUBLE(fullPrecision, "72057594037927932.0", 72057594037927936.0);
276  TEST_DOUBLE(fullPrecision, "7205759403792793199999e-5", 72057594037927928.0);
277  TEST_DOUBLE(fullPrecision, "7205759403792793200001e-5", 72057594037927936.0);
278 
279  TEST_DOUBLE(fullPrecision, "9223372036854774784.0", 9223372036854774784.0);
280  TEST_DOUBLE(fullPrecision, "9223372036854775808.0", 9223372036854775808.0);
281  TEST_DOUBLE(fullPrecision, "9223372036854775296.0", 9223372036854775808.0);
282  TEST_DOUBLE(fullPrecision, "922337203685477529599999e-5", 9223372036854774784.0);
283  TEST_DOUBLE(fullPrecision, "922337203685477529600001e-5", 9223372036854775808.0);
284 
285  TEST_DOUBLE(fullPrecision, "10141204801825834086073718800384", 10141204801825834086073718800384.0);
286  TEST_DOUBLE(fullPrecision, "10141204801825835211973625643008", 10141204801825835211973625643008.0);
287  TEST_DOUBLE(fullPrecision, "10141204801825834649023672221696", 10141204801825835211973625643008.0);
288  TEST_DOUBLE(fullPrecision, "1014120480182583464902367222169599999e-5", 10141204801825834086073718800384.0);
289  TEST_DOUBLE(fullPrecision, "1014120480182583464902367222169600001e-5", 10141204801825835211973625643008.0);
290 
291  TEST_DOUBLE(fullPrecision, "5708990770823838890407843763683279797179383808", 5708990770823838890407843763683279797179383808.0);
292  TEST_DOUBLE(fullPrecision, "5708990770823839524233143877797980545530986496", 5708990770823839524233143877797980545530986496.0);
293  TEST_DOUBLE(fullPrecision, "5708990770823839207320493820740630171355185152", 5708990770823839524233143877797980545530986496.0);
294  TEST_DOUBLE(fullPrecision, "5708990770823839207320493820740630171355185151999e-3", 5708990770823838890407843763683279797179383808.0);
295  TEST_DOUBLE(fullPrecision, "5708990770823839207320493820740630171355185152001e-3", 5708990770823839524233143877797980545530986496.0);
296 
297  {
298  char n1e308[310]; // '1' followed by 308 '0'
299  n1e308[0] = '1';
300  for (int i = 1; i < 309; i++)
301  n1e308[i] = '0';
302  n1e308[309] = '\0';
303  TEST_DOUBLE(fullPrecision, n1e308, 1E308);
304  }
305 
306  // Cover trimming
307  TEST_DOUBLE(fullPrecision,
308 "2.22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508"
309 "7914149158913039621106870086438694594645527657207407820621743379988141063267329253552286881372149012"
310 "9811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306"
311 "6665599493827577257201576306269066333264756530000924588831643303777979186961204949739037782970490505"
312 "1080609940730262937128958950003583799967207254304360284078895771796150945516748243471030702609144621"
313 "5722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844"
314 "2390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042"
315 "7567186443383770486037861622771738545623065874679014086723327636718751234567890123456789012345678901"
316 "e-308",
317  2.2250738585072014e-308);
318 
319  {
320  static const unsigned count = 100; // Tested with 1000000 locally
321  Random r;
322  Reader reader; // Reusing reader to prevent heap allocation
323 
324  // Exhaustively test different exponents with random significant
325  for (uint64_t exp = 0; exp < 2047; exp++) {
326  ;
327  for (unsigned i = 0; i < count; i++) {
328  // Need to call r() in two statements for cross-platform coherent sequence.
329  uint64_t u = (exp << 52) | uint64_t(r() & 0x000FFFFF) << 32;
330  u |= uint64_t(r());
332 
333  char buffer[32];
334  *internal::dtoa(d.Value(), buffer) = '\0';
335 
336  StringStream s(buffer);
338  ASSERT_EQ(kParseErrorNone, reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h).Code());
339  EXPECT_EQ(1u, h.step_);
341  if (fullPrecision) {
342  EXPECT_EQ(d.Uint64Value(), a.Uint64Value());
343  if (d.Uint64Value() != a.Uint64Value())
344  printf(" String: %s\n Actual: %.17g\nExpected: %.17g\n", buffer, h.actual_, d.Value());
345  }
346  else {
347  EXPECT_EQ(d.Sign(), a.Sign()); // for 0.0 != -0.0
349  }
350  }
351  }
352  }
353 
354  // Issue #340
355  TEST_DOUBLE(fullPrecision, "7.450580596923828e-9", 7.450580596923828e-9);
356  {
357  internal::Double d(1.0);
358  for (int i = 0; i < 324; i++) {
359  char buffer[32];
360  *internal::dtoa(d.Value(), buffer) = '\0';
361 
362  StringStream s(buffer);
364  Reader reader;
365  ASSERT_EQ(kParseErrorNone, reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h).Code());
366  EXPECT_EQ(1u, h.step_);
368  if (fullPrecision) {
369  EXPECT_EQ(d.Uint64Value(), a.Uint64Value());
370  if (d.Uint64Value() != a.Uint64Value())
371  printf(" String: %s\n Actual: %.17g\nExpected: %.17g\n", buffer, h.actual_, d.Value());
372  }
373  else {
374  EXPECT_EQ(d.Sign(), a.Sign()); // for 0.0 != -0.0
376  }
377 
378 
379  d = d.Value() * 0.5;
380  }
381  }
382 
383  // Issue 1249
384  TEST_DOUBLE(fullPrecision, "0e100", 0.0);
385 
386  // Issue 1251
387  TEST_DOUBLE(fullPrecision, "128.74836467836484838364836483643636483648e-336", 0.0);
388 
389  // Issue 1256
390  TEST_DOUBLE(fullPrecision,
391  "6223372036854775296.1701512723685473547372536854755293372036854685477"
392  "529752233737201701512337200972013723685473123372036872036854236854737"
393  "247372368372367752975258547752975254729752547372368737201701512354737"
394  "83723677529752585477247372368372368547354737253685475529752",
395  6223372036854775808.0);
396 
397 #if 0
398  // Test (length + exponent) overflow
399  TEST_DOUBLE(fullPrecision, "0e+2147483647", 0.0);
400  TEST_DOUBLE(fullPrecision, "0e-2147483648", 0.0);
401  TEST_DOUBLE(fullPrecision, "1e-2147483648", 0.0);
402  TEST_DOUBLE(fullPrecision, "0e+9223372036854775807", 0.0);
403  TEST_DOUBLE(fullPrecision, "0e-9223372036854775808", 0.0);
404 #endif
405 
406  if (fullPrecision)
407  {
408  TEST_DOUBLE(fullPrecision, "1e-325", 0.0);
409  TEST_DOUBLE(fullPrecision, "1e-324", 0.0);
410  TEST_DOUBLE(fullPrecision, "2e-324", 0.0);
411  TEST_DOUBLE(fullPrecision, "2.4703282292062327e-324", 0.0);
412  TEST_DOUBLE(fullPrecision, "2.4703282292062328e-324", 5e-324);
413  TEST_DOUBLE(fullPrecision, "2.48e-324",5e-324);
414  TEST_DOUBLE(fullPrecision, "2.5e-324", 5e-324);
415 
416  // Slightly above max-normal
417  TEST_DOUBLE(fullPrecision, "1.7976931348623158e+308", 1.7976931348623158e+308);
418 
419  TEST_DOUBLE(fullPrecision,
420  "17976931348623157081452742373170435679807056752584499659891747680315726"
421  "07800285387605895586327668781715404589535143824642343213268894641827684"
422  "67546703537516986049910576551282076245490090389328944075868508455133942"
423  "30458323690322294816580855933212334827479782620414472316873817718091929"
424  "9881250404026184124858368",
425  std::numeric_limits<double>::max());
426 
427  TEST_DOUBLE(fullPrecision,
428  "243546080556034731077856379609316893158278902575447060151047"
429  "212703405344938119816206067372775299130836050315842578309818"
430  "316450894337978612745889730079163798234256495613858256849283"
431  "467066859489192118352020514036083287319232435355752493038825"
432  "828481044358810649108367633313557305310641892225870327827273"
433  "41408256.000000",
434  2.4354608055603473e+307);
435  // 9007199254740991 * 2^971 (max normal)
436  TEST_DOUBLE(fullPrecision,
437  "1.797693134862315708145274237317043567980705675258449965989174768031572607800285"
438  "38760589558632766878171540458953514382464234321326889464182768467546703537516986"
439  "04991057655128207624549009038932894407586850845513394230458323690322294816580855"
440  "9332123348274797826204144723168738177180919299881250404026184124858368e+308",
441  1.797693134862315708e+308 // 0x1.fffffffffffffp1023
442  );
443 #if 0
444  // TODO:
445  // Should work at least in full-precision mode...
446  TEST_DOUBLE(fullPrecision,
447  "0.00000000000000000000000000000000000000000000000000000000000"
448  "0000000000000000000000000000000000000000000000000000000000000"
449  "0000000000000000000000000000000000000000000000000000000000000"
450  "0000000000000000000000000000000000000000000000000000000000000"
451  "0000000000000000000000000000000000000000000000000000000000000"
452  "0000000000000000000024703282292062327208828439643411068618252"
453  "9901307162382212792841250337753635104375932649918180817996189"
454  "8982823477228588654633283551779698981993873980053909390631503"
455  "5659515570226392290858392449105184435931802849936536152500319"
456  "3704576782492193656236698636584807570015857692699037063119282"
457  "7955855133292783433840935197801553124659726357957462276646527"
458  "2827220056374006485499977096599470454020828166226237857393450"
459  "7363390079677619305775067401763246736009689513405355374585166"
460  "6113422376667860416215968046191446729184030053005753084904876"
461  "5391711386591646239524912623653881879636239373280423891018672"
462  "3484976682350898633885879256283027559956575244555072551893136"
463  "9083625477918694866799496832404970582102851318545139621383772"
464  "2826145437693412532098591327667236328125",
465  0.0);
466 #endif
467  // 9007199254740991 * 2^-1074 = (2^53 - 1) * 2^-1074
468  TEST_DOUBLE(fullPrecision,
469  "4.450147717014402272114819593418263951869639092703291296046852219449644444042153"
470  "89103305904781627017582829831782607924221374017287738918929105531441481564124348"
471  "67599762821265346585071045737627442980259622449029037796981144446145705102663115"
472  "10031828794952795966823603998647925096578034214163701381261333311989876551545144"
473  "03152612538132666529513060001849177663286607555958373922409899478075565940981010"
474  "21612198814605258742579179000071675999344145086087205681577915435923018910334964"
475  "86942061405218289243144579760516365090360651414037721744226256159024466852576737"
476  "24464300755133324500796506867194913776884780053099639677097589658441378944337966"
477  "21993967316936280457084866613206797017728916080020698679408551343728867675409720"
478  "757232455434770912461317493580281734466552734375e-308",
479  4.450147717014402272e-308 // 0x1.fffffffffffffp-1022
480  );
481  // 9007199254740990 * 2^-1074
482  TEST_DOUBLE(fullPrecision,
483  "4.450147717014401778049173752171719775300846224481918930987049605124880018456471"
484  "39035755177760751831052846195619008686241717547743167145836439860405887584484471"
485  "19639655002484083577939142623582164522087943959208000909794783876158397872163051"
486  "22622675229968408654350206725478309956546318828765627255022767720818849892988457"
487  "26333908582101604036318532842699932130356061901518261174396928478121372742040102"
488  "17446565569357687263889031732270082446958029584739170416643195242132750803227473"
489  "16608838720742955671061336566907126801014814608027120593609275183716632624844904"
490  "31985250929886016737037234388448352929102742708402644340627409931664203093081360"
491  "70794835812045179006047003875039546061891526346421705014598610179523165038319441"
492  "51446491086954182492263498716056346893310546875e-308",
493  4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
494  );
495  // half way between the two numbers above.
496  // round to nearest even.
497  TEST_DOUBLE(fullPrecision,
498  "4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
499  "64069530541271189424317838013700808305231545782515453032382772695923684574304409"
500  "93619708911874715081505094180604803751173783204118519353387964161152051487413083"
501  "16327252012460602310586905362063117526562176521464664318142050516404363222266800"
502  "64743260560117135282915796422274554896821334728738317548403413978098469341510556"
503  "19529382191981473003234105366170879223151087335413188049110555339027884856781219"
504  "01775450062980622457102958163711745945687733011032421168917765671370549738710820"
505  "78224775842509670618916870627821633352993761380751142008862499795052791018709663"
506  "46394401564490729731565935244123171539810221213221201847003580761626016356864581"
507  "1358486831521563686919762403704226016998291015625e-308",
508  4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
509  );
510  TEST_DOUBLE(fullPrecision,
511  "4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
512  "64069530541271189424317838013700808305231545782515453032382772695923684574304409"
513  "93619708911874715081505094180604803751173783204118519353387964161152051487413083"
514  "16327252012460602310586905362063117526562176521464664318142050516404363222266800"
515  "64743260560117135282915796422274554896821334728738317548403413978098469341510556"
516  "19529382191981473003234105366170879223151087335413188049110555339027884856781219"
517  "01775450062980622457102958163711745945687733011032421168917765671370549738710820"
518  "78224775842509670618916870627821633352993761380751142008862499795052791018709663"
519  "46394401564490729731565935244123171539810221213221201847003580761626016356864581"
520  "13584868315215636869197624037042260169982910156250000000000000000000000000000000"
521  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
522  "00000000000000000000000000000000000000000000000000000000000000000000000000000000e-308",
523  4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
524  );
525 #if 0
526  // ... round up
527  // TODO:
528  // Should work at least in full-precision mode...
529  TEST_DOUBLE(fullPrecision,
530  "4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
531  "64069530541271189424317838013700808305231545782515453032382772695923684574304409"
532  "93619708911874715081505094180604803751173783204118519353387964161152051487413083"
533  "16327252012460602310586905362063117526562176521464664318142050516404363222266800"
534  "64743260560117135282915796422274554896821334728738317548403413978098469341510556"
535  "19529382191981473003234105366170879223151087335413188049110555339027884856781219"
536  "01775450062980622457102958163711745945687733011032421168917765671370549738710820"
537  "78224775842509670618916870627821633352993761380751142008862499795052791018709663"
538  "46394401564490729731565935244123171539810221213221201847003580761626016356864581"
539  "13584868315215636869197624037042260169982910156250000000000000000000000000000000"
540  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
541  "00000000000000000000000000000000000000000000000000000000000000000000000000000001e-308",
542  4.450147717014402272e-308 // 0x1.fffffffffffffp-1022
543  );
544 #endif
545  // ... round down
546  TEST_DOUBLE(fullPrecision,
547  "4.450147717014402025081996672794991863585242658592605113516950912287262231249312"
548  "64069530541271189424317838013700808305231545782515453032382772695923684574304409"
549  "93619708911874715081505094180604803751173783204118519353387964161152051487413083"
550  "16327252012460602310586905362063117526562176521464664318142050516404363222266800"
551  "64743260560117135282915796422274554896821334728738317548403413978098469341510556"
552  "19529382191981473003234105366170879223151087335413188049110555339027884856781219"
553  "01775450062980622457102958163711745945687733011032421168917765671370549738710820"
554  "78224775842509670618916870627821633352993761380751142008862499795052791018709663"
555  "46394401564490729731565935244123171539810221213221201847003580761626016356864581"
556  "13584868315215636869197624037042260169982910156249999999999999999999999999999999"
557  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
558  "99999999999999999999999999999999999999999999999999999999999999999999999999999999e-308",
559  4.450147717014401778e-308 // 0x1.ffffffffffffep-1022
560  );
561  // Slightly below half way between max-normal and infinity.
562  // Should round down.
563  TEST_DOUBLE(fullPrecision,
564  "1.797693134862315807937289714053034150799341327100378269361737789804449682927647"
565  "50946649017977587207096330286416692887910946555547851940402630657488671505820681"
566  "90890200070838367627385484581771153176447573027006985557136695962284291481986083"
567  "49364752927190741684443655107043427115596995080930428801779041744977919999999999"
568  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
569  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
570  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
571  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
572  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
573  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
574  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
575  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
576  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
577  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
578  "99999999999999999999999999999999999999999999999999999999999999999999999999999999"
579  "99999999999999999999999999999999999999999999999999999999999999999999999999999999e+308",
580  1.797693134862315708e+308 // 0x1.fffffffffffffp1023
581  );
582  }
583 
584 #undef TEST_DOUBLE
585 }
586 
587 TEST(Reader, ParseNumber_NormalPrecisionDouble) {
588  TestParseDouble<false>();
589 }
590 
591 TEST(Reader, ParseNumber_FullPrecisionDouble) {
592  TestParseDouble<true>();
593 }
594 
595 TEST(Reader, ParseNumber_NormalPrecisionError) {
596  static unsigned count = 1000000;
597  Random r;
598 
599  double ulpSum = 0.0;
600  double ulpMax = 0.0;
601  for (unsigned i = 0; i < count; i++) {
602  internal::Double e, a;
603  do {
604  // Need to call r() in two statements for cross-platform coherent sequence.
605  uint64_t u = uint64_t(r()) << 32;
606  u |= uint64_t(r());
607  e = u;
608  } while (e.IsNan() || e.IsInf() || !e.IsNormal());
609 
610  char buffer[32];
611  *internal::dtoa(e.Value(), buffer) = '\0';
612 
613  StringStream s(buffer);
615  Reader reader;
616  ASSERT_EQ(kParseErrorNone, reader.Parse(s, h).Code());
617  EXPECT_EQ(1u, h.step_);
618 
619  a = h.actual_;
620  uint64_t bias1 = e.ToBias();
621  uint64_t bias2 = a.ToBias();
622  double ulp = static_cast<double>(bias1 >= bias2 ? bias1 - bias2 : bias2 - bias1);
623  ulpMax = (std::max)(ulpMax, ulp);
624  ulpSum += ulp;
625  }
626  printf("ULP Average = %g, Max = %g \n", ulpSum / count, ulpMax);
627 }
628 
629 template<bool fullPrecision>
630 static void TestParseNumberError() {
631 #define TEST_NUMBER_ERROR(errorCode, str, errorOffset, streamPos) \
632  { \
633  char buffer[2048]; \
634  ASSERT_LT(std::strlen(str), 2048u); \
635  sprintf(buffer, "%s", str); \
636  InsituStringStream s(buffer); \
637  BaseReaderHandler<> h; \
638  Reader reader; \
639  EXPECT_FALSE(reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h)); \
640  EXPECT_EQ(errorCode, reader.GetParseErrorCode());\
641  EXPECT_EQ(errorOffset, reader.GetErrorOffset());\
642  EXPECT_EQ(streamPos, s.Tell());\
643  }
644 
645  // Number too big to be stored in double.
646  {
647  char n1e309[311]; // '1' followed by 309 '0'
648  n1e309[0] = '1';
649  for (int i = 1; i < 310; i++)
650  n1e309[i] = '0';
651  n1e309[310] = '\0';
653  }
655 
656  // Miss fraction part in number.
659 
660  // Miss exponent in number.
663 
664  // Issue 849
665  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.8e308", 0, 7);
668  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.0e310", 0, 7);
669  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.00e310", 0, 8);
670  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "-1.8e308", 0, 8);
672 
673  // Issue 1253
675 
676  // Issue 1259
678  "88474320368547737236837236775298547354737253685475547552933720368546854775297525"
679  "29337203685468547770151233720097201372368547312337203687203685423685123372036872"
680  "03685473724737236837236775297525854775297525472975254737236873720170151235473783"
681  "7236737247372368772473723683723456789012E66", 0, 283);
682 
683 #if 0
684  // Test (length + exponent) overflow
685  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+2147483647", 0, 13);
686  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+9223372036854775807", 0, 22);
687  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+10000", 0, 8);
688  TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+50000", 0, 8);
689 #endif
690 
691  // 9007199254740992 * 2^971 ("infinity")
693  "1.797693134862315907729305190789024733617976978942306572734300811577326758055009"
694  "63132708477322407536021120113879871393357658789768814416622492847430639474124377"
695  "76789342486548527630221960124609411945308295208500576883815068234246288147391311"
696  "0540827237163350510684586298239947245938479716304835356329624224137216e+308", 0, 315);
697 
698  // TODO:
699  // These tests (currently) fail in normal-precision mode
700  if (fullPrecision)
701  {
702  // Half way between max-normal and infinity
703  // Should round to infinity in nearest-even mode.
705  "1.797693134862315807937289714053034150799341327100378269361737789804449682927647"
706  "50946649017977587207096330286416692887910946555547851940402630657488671505820681"
707  "90890200070838367627385484581771153176447573027006985557136695962284291481986083"
708  "49364752927190741684443655107043427115596995080930428801779041744977920000000000"
709  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
710  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
711  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
712  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
713  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
714  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
715  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
716  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
717  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
718  "00000000000000000000000000000000000000000000000000000000000000000000000000000000e+308", 0, 1125);
719  // ...round up
721  "1.797693134862315807937289714053034150799341327100378269361737789804449682927647"
722  "50946649017977587207096330286416692887910946555547851940402630657488671505820681"
723  "90890200070838367627385484581771153176447573027006985557136695962284291481986083"
724  "49364752927190741684443655107043427115596995080930428801779041744977920000000000"
725  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
726  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
727  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
728  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
729  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
730  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
731  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
732  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
733  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
734  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
735  "00000000000000000000000000000000000000000000000000000000000000000000000000000001e+308", 0, 1205);
736  }
737 
739  "10000000000000000000000000000000000000000000000000000000000000000000000000000000"
740  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
741  "00000000000000000000000000000000000000000000000000000000000000000000000000000000"
742  "0000000000000000000000000000000000000000000000000000000000000000000001", 0, 310);
743 
744 #undef TEST_NUMBER_ERROR
745 }
746 
747 TEST(Reader, ParseNumberError_NormalPrecisionDouble) {
748  TestParseNumberError<false>();
749 }
750 
751 TEST(Reader, ParseNumberError_FullPrecisionDouble) {
752  TestParseNumberError<true>();
753 }
754 
755 template <typename Encoding>
756 struct ParseStringHandler : BaseReaderHandler<Encoding, ParseStringHandler<Encoding> > {
757  ParseStringHandler() : str_(0), length_(0), copy_() {}
758  ~ParseStringHandler() { EXPECT_TRUE(str_ != 0); if (copy_) free(const_cast<typename Encoding::Ch*>(str_)); }
759 
762 
763  bool Default() { ADD_FAILURE(); return false; }
764  bool String(const typename Encoding::Ch* str, size_t length, bool copy) {
765  EXPECT_EQ(0, str_);
766  if (copy) {
767  str_ = static_cast<typename Encoding::Ch*>(malloc((length + 1) * sizeof(typename Encoding::Ch)));
768  memcpy(const_cast<typename Encoding::Ch*>(str_), str, (length + 1) * sizeof(typename Encoding::Ch));
769  }
770  else
771  str_ = str;
772  length_ = length;
773  copy_ = copy;
774  return true;
775  }
776 
777  const typename Encoding::Ch* str_;
778  size_t length_;
779  bool copy_;
780 };
781 
782 TEST(Reader, ParseString) {
783 #define TEST_STRING(Encoding, e, x) \
784  { \
785  Encoding::Ch* buffer = StrDup(x); \
786  GenericInsituStringStream<Encoding> is(buffer); \
787  ParseStringHandler<Encoding> h; \
788  GenericReader<Encoding, Encoding> reader; \
789  reader.Parse<kParseInsituFlag | kParseValidateEncodingFlag>(is, h); \
790  EXPECT_EQ(0, StrCmp<Encoding::Ch>(e, h.str_)); \
791  EXPECT_EQ(StrLen(e), h.length_); \
792  free(buffer); \
793  GenericStringStream<Encoding> s(x); \
794  ParseStringHandler<Encoding> h2; \
795  GenericReader<Encoding, Encoding> reader2; \
796  reader2.Parse(s, h2); \
797  EXPECT_EQ(0, StrCmp<Encoding::Ch>(e, h2.str_)); \
798  EXPECT_EQ(StrLen(e), h2.length_); \
799  }
800 
801  // String constant L"\xXX" can only specify character code in bytes, which is not endianness-neutral.
802  // And old compiler does not support u"" and U"" string literal. So here specify string literal by array of Ch.
803  // In addition, GCC 4.8 generates -Wnarrowing warnings when character code >= 128 are assigned to signed integer types.
804  // Therefore, utype is added for declaring unsigned array, and then cast it to Encoding::Ch.
805 #define ARRAY(...) { __VA_ARGS__ }
806 #define TEST_STRINGARRAY(Encoding, utype, array, x) \
807  { \
808  static const utype ue[] = array; \
809  static const Encoding::Ch* e = reinterpret_cast<const Encoding::Ch *>(&ue[0]); \
810  TEST_STRING(Encoding, e, x); \
811  }
812 
813 #define TEST_STRINGARRAY2(Encoding, utype, earray, xarray) \
814  { \
815  static const utype ue[] = earray; \
816  static const utype xe[] = xarray; \
817  static const Encoding::Ch* e = reinterpret_cast<const Encoding::Ch *>(&ue[0]); \
818  static const Encoding::Ch* x = reinterpret_cast<const Encoding::Ch *>(&xe[0]); \
819  TEST_STRING(Encoding, e, x); \
820  }
821 
822  TEST_STRING(UTF8<>, "", "\"\"");
823  TEST_STRING(UTF8<>, "Hello", "\"Hello\"");
824  TEST_STRING(UTF8<>, "Hello\nWorld", "\"Hello\\nWorld\"");
825  TEST_STRING(UTF8<>, "\"\\/\b\f\n\r\t", "\"\\\"\\\\/\\b\\f\\n\\r\\t\"");
826  TEST_STRING(UTF8<>, "\x24", "\"\\u0024\""); // Dollar sign U+0024
827  TEST_STRING(UTF8<>, "\xC2\xA2", "\"\\u00A2\""); // Cents sign U+00A2
828  TEST_STRING(UTF8<>, "\xE2\x82\xAC", "\"\\u20AC\""); // Euro sign U+20AC
829  TEST_STRING(UTF8<>, "\xF0\x9D\x84\x9E", "\"\\uD834\\uDD1E\""); // G clef sign U+1D11E
830 
831  // UTF16
832  TEST_STRING(UTF16<>, L"", L"\"\"");
833  TEST_STRING(UTF16<>, L"Hello", L"\"Hello\"");
834  TEST_STRING(UTF16<>, L"Hello\nWorld", L"\"Hello\\nWorld\"");
835  TEST_STRING(UTF16<>, L"\"\\/\b\f\n\r\t", L"\"\\\"\\\\/\\b\\f\\n\\r\\t\"");
836  TEST_STRINGARRAY(UTF16<>, wchar_t, ARRAY(0x0024, 0x0000), L"\"\\u0024\"");
837  TEST_STRINGARRAY(UTF16<>, wchar_t, ARRAY(0x00A2, 0x0000), L"\"\\u00A2\""); // Cents sign U+00A2
838  TEST_STRINGARRAY(UTF16<>, wchar_t, ARRAY(0x20AC, 0x0000), L"\"\\u20AC\""); // Euro sign U+20AC
839  TEST_STRINGARRAY(UTF16<>, wchar_t, ARRAY(0xD834, 0xDD1E, 0x0000), L"\"\\uD834\\uDD1E\""); // G clef sign U+1D11E
840 
841  // UTF32
842  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY('\0'), ARRAY('\"', '\"', '\0'));
843  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY('H', 'e', 'l', 'l', 'o', '\0'), ARRAY('\"', 'H', 'e', 'l', 'l', 'o', '\"', '\0'));
844  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY('H', 'e', 'l', 'l', 'o', '\n', 'W', 'o', 'r', 'l', 'd', '\0'), ARRAY('\"', 'H', 'e', 'l', 'l', 'o', '\\', 'n', 'W', 'o', 'r', 'l', 'd', '\"', '\0'));
845  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY('\"', '\\', '/', '\b', '\f', '\n', '\r', '\t', '\0'), ARRAY('\"', '\\', '\"', '\\', '\\', '/', '\\', 'b', '\\', 'f', '\\', 'n', '\\', 'r', '\\', 't', '\"', '\0'));
846  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY(0x00024, 0x0000), ARRAY('\"', '\\', 'u', '0', '0', '2', '4', '\"', '\0'));
847  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY(0x000A2, 0x0000), ARRAY('\"', '\\', 'u', '0', '0', 'A', '2', '\"', '\0')); // Cents sign U+00A2
848  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY(0x020AC, 0x0000), ARRAY('\"', '\\', 'u', '2', '0', 'A', 'C', '\"', '\0')); // Euro sign U+20AC
849  TEST_STRINGARRAY2(UTF32<>, unsigned, ARRAY(0x1D11E, 0x0000), ARRAY('\"', '\\', 'u', 'D', '8', '3', '4', '\\', 'u', 'D', 'D', '1', 'E', '\"', '\0')); // G clef sign U+1D11E
850 
851 #undef TEST_STRINGARRAY
852 #undef ARRAY
853 #undef TEST_STRING
854 
855  // Support of null character in string
856  {
857  StringStream s("\"Hello\\u0000World\"");
858  const char e[] = "Hello\0World";
860  Reader reader;
861  reader.Parse(s, h);
862  EXPECT_EQ(0, memcmp(e, h.str_, h.length_ + 1));
863  EXPECT_EQ(11u, h.length_);
864  }
865 }
866 
867 TEST(Reader, ParseString_Transcoding) {
868  const char* x = "\"Hello\"";
869  const wchar_t* e = L"Hello";
871  GenericReader<UTF8<>, UTF16<> > reader;
873  reader.Parse(is, h);
874  EXPECT_EQ(0, StrCmp<UTF16<>::Ch>(e, h.str_));
875  EXPECT_EQ(StrLen(e), h.length_);
876 }
877 
878 TEST(Reader, ParseString_TranscodingWithValidation) {
879  const char* x = "\"Hello\"";
880  const wchar_t* e = L"Hello";
882  GenericReader<UTF8<>, UTF16<> > reader;
884  reader.Parse<kParseValidateEncodingFlag>(is, h);
885  EXPECT_EQ(0, StrCmp<UTF16<>::Ch>(e, h.str_));
886  EXPECT_EQ(StrLen(e), h.length_);
887 }
888 
889 TEST(Reader, ParseString_NonDestructive) {
890  StringStream s("\"Hello\\nWorld\"");
892  Reader reader;
893  reader.Parse(s, h);
894  EXPECT_EQ(0, StrCmp("Hello\nWorld", h.str_));
895  EXPECT_EQ(11u, h.length_);
896 }
897 
898 template <typename Encoding>
899 ParseErrorCode TestString(const typename Encoding::Ch* str) {
903  reader.template Parse<kParseValidateEncodingFlag>(s, h);
904  return reader.GetParseErrorCode();
905 }
906 
907 TEST(Reader, ParseString_Error) {
908 #define TEST_STRING_ERROR(errorCode, str, errorOffset, streamPos)\
909 {\
910  GenericStringStream<UTF8<> > s(str);\
911  BaseReaderHandler<UTF8<> > h;\
912  GenericReader<UTF8<> , UTF8<> > reader;\
913  reader.Parse<kParseValidateEncodingFlag>(s, h);\
914  EXPECT_EQ(errorCode, reader.GetParseErrorCode());\
915  EXPECT_EQ(errorOffset, reader.GetErrorOffset());\
916  EXPECT_EQ(streamPos, s.Tell());\
917 }
918 
919 #define ARRAY(...) { __VA_ARGS__ }
920 #define TEST_STRINGENCODING_ERROR(Encoding, TargetEncoding, utype, array) \
921  { \
922  static const utype ue[] = array; \
923  static const Encoding::Ch* e = reinterpret_cast<const Encoding::Ch *>(&ue[0]); \
924  EXPECT_EQ(kParseErrorStringInvalidEncoding, TestString<Encoding>(e));\
925  /* decode error */\
926  GenericStringStream<Encoding> s(e);\
927  BaseReaderHandler<TargetEncoding> h;\
928  GenericReader<Encoding, TargetEncoding> reader;\
929  reader.Parse(s, h);\
930  EXPECT_EQ(kParseErrorStringInvalidEncoding, reader.GetParseErrorCode());\
931  }
932 
933  // Invalid escape character in string.
935 
936  // Incorrect hex digit after \\u escape in string.
938 
939  // Quotation in \\u escape in string (Issue #288)
941  TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uD800\\uFFF\"]", 2, 13);
942 
943  // The surrogate pair in string is invalid.
945  TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800\\uFFFF\"]", 2, 14);
946 
947  // Missing a closing quotation mark in string.
949 
950  // http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
951 
952  // 3 Malformed sequences
953 
954  // 3.1 Unexpected continuation bytes
955  {
956  char e[] = { '[', '\"', 0, '\"', ']', '\0' };
957  for (unsigned char c = 0x80u; c <= 0xBFu; c++) {
958  e[2] = static_cast<char>(c);
959  ParseErrorCode error = TestString<UTF8<> >(e);
962  std::cout << static_cast<unsigned>(c) << std::endl;
963  }
964  }
965 
966  // 3.2 Lonely start characters, 3.5 Impossible bytes
967  {
968  char e[] = { '[', '\"', 0, ' ', '\"', ']', '\0' };
969  for (unsigned c = 0xC0u; c <= 0xFFu; c++) {
970  e[2] = static_cast<char>(c);
971  int streamPos;
972  if (c <= 0xC1u)
973  streamPos = 3; // 0xC0 - 0xC1
974  else if (c <= 0xDFu)
975  streamPos = 4; // 0xC2 - 0xDF
976  else if (c <= 0xEFu)
977  streamPos = 5; // 0xE0 - 0xEF
978  else if (c <= 0xF4u)
979  streamPos = 6; // 0xF0 - 0xF4
980  else
981  streamPos = 3; // 0xF5 - 0xFF
983  }
984  }
985 
986  // 4 Overlong sequences
987 
988  // 4.1 Examples of an overlong ASCII character
989  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0xAFu, '\"', ']', '\0'));
990  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0xAFu, '\"', ']', '\0'));
991  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0xAFu, '\"', ']', '\0'));
992 
993  // 4.2 Maximum overlong sequences
994  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC1u, 0xBFu, '\"', ']', '\0'));
995  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x9Fu, 0xBFu, '\"', ']', '\0'));
996  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x8Fu, 0xBFu, 0xBFu, '\"', ']', '\0'));
997 
998  // 4.3 Overlong representation of the NUL character
999  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0x80u, '\"', ']', '\0'));
1000  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0x80u, '\"', ']', '\0'));
1001  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0x80u, '\"', ']', '\0'));
1002 
1003  // 5 Illegal code positions
1004 
1005  // 5.1 Single UTF-16 surrogates
1006  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xA0u, 0x80u, '\"', ']', '\0'));
1007  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xADu, 0xBFu, '\"', ']', '\0'));
1008  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xAEu, 0x80u, '\"', ']', '\0'));
1009  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xAFu, 0xBFu, '\"', ']', '\0'));
1010  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xB0u, 0x80u, '\"', ']', '\0'));
1011  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xBEu, 0x80u, '\"', ']', '\0'));
1012  TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xBFu, 0xBFu, '\"', ']', '\0'));
1013 
1014  // Malform UTF-16 sequences
1015  TEST_STRINGENCODING_ERROR(UTF16<>, UTF8<>, wchar_t, ARRAY('[', '\"', 0xDC00, 0xDC00, '\"', ']', '\0'));
1016  TEST_STRINGENCODING_ERROR(UTF16<>, UTF8<>, wchar_t, ARRAY('[', '\"', 0xD800, 0xD800, '\"', ']', '\0'));
1017 
1018  // Malform UTF-32 sequence
1019  TEST_STRINGENCODING_ERROR(UTF32<>, UTF8<>, unsigned, ARRAY('[', '\"', 0x110000, '\"', ']', '\0'));
1020 
1021  // Malform ASCII sequence
1022  TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80u), '\"', ']', '\0'));
1023  TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x01u), '\"', ']', '\0'));
1024  TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x1Cu), '\"', ']', '\0'));
1025 
1026 #undef ARRAY
1027 #undef TEST_STRINGARRAY_ERROR
1028 }
1029 
1030 template <unsigned count>
1031 struct ParseArrayHandler : BaseReaderHandler<UTF8<>, ParseArrayHandler<count> > {
1032  ParseArrayHandler() : step_(0) {}
1033 
1034  bool Default() { ADD_FAILURE(); return false; }
1035  bool Uint(unsigned i) { EXPECT_EQ(step_, i); step_++; return true; }
1036  bool StartArray() { EXPECT_EQ(0u, step_); step_++; return true; }
1037  bool EndArray(SizeType) { step_++; return true; }
1038 
1039  unsigned step_;
1040 };
1041 
1042 TEST(Reader, ParseEmptyArray) {
1043  char *json = StrDup("[ ] ");
1046  Reader reader;
1047  reader.Parse(s, h);
1048  EXPECT_EQ(2u, h.step_);
1049  free(json);
1050 }
1051 
1052 TEST(Reader, ParseArray) {
1053  char *json = StrDup("[1, 2, 3, 4]");
1056  Reader reader;
1057  reader.Parse(s, h);
1058  EXPECT_EQ(6u, h.step_);
1059  free(json);
1060 }
1061 
1062 TEST(Reader, ParseArray_Error) {
1063 #define TEST_ARRAY_ERROR(errorCode, str, errorOffset) \
1064  { \
1065  int streamPos = errorOffset; \
1066  char buffer[1001]; \
1067  strncpy(buffer, str, 1000); \
1068  InsituStringStream s(buffer); \
1069  BaseReaderHandler<> h; \
1070  GenericReader<UTF8<>, UTF8<>, CrtAllocator> reader; \
1071  EXPECT_FALSE(reader.Parse(s, h)); \
1072  EXPECT_EQ(errorCode, reader.GetParseErrorCode());\
1073  EXPECT_EQ(errorOffset, reader.GetErrorOffset());\
1074  EXPECT_EQ(streamPos, s.Tell());\
1075  }
1076 
1077  // Missing a comma or ']' after an array element.
1081 
1082  // Array cannot have a trailing comma (without kParseTrailingCommasFlag);
1083  // a value must follow a comma
1085 
1086 #undef TEST_ARRAY_ERROR
1087 }
1088 
1089 struct ParseObjectHandler : BaseReaderHandler<UTF8<>, ParseObjectHandler> {
1090  ParseObjectHandler() : step_(0) {}
1091 
1092  bool Default() { ADD_FAILURE(); return false; }
1093  bool Null() { EXPECT_EQ(8u, step_); step_++; return true; }
1094  bool Bool(bool b) {
1095  switch(step_) {
1096  case 4: EXPECT_TRUE(b); step_++; return true;
1097  case 6: EXPECT_FALSE(b); step_++; return true;
1098  default: ADD_FAILURE(); return false;
1099  }
1100  }
1101  bool Int(int i) {
1102  switch(step_) {
1103  case 10: EXPECT_EQ(123, i); step_++; return true;
1104  case 15: EXPECT_EQ(1, i); step_++; return true;
1105  case 16: EXPECT_EQ(2, i); step_++; return true;
1106  case 17: EXPECT_EQ(3, i); step_++; return true;
1107  default: ADD_FAILURE(); return false;
1108  }
1109  }
1110  bool Uint(unsigned i) { return Int(static_cast<int>(i)); }
1111  bool Double(double d) { EXPECT_EQ(12u, step_); EXPECT_DOUBLE_EQ(3.1416, d); step_++; return true; }
1112  bool String(const char* str, size_t, bool) {
1113  switch(step_) {
1114  case 1: EXPECT_STREQ("hello", str); step_++; return true;
1115  case 2: EXPECT_STREQ("world", str); step_++; return true;
1116  case 3: EXPECT_STREQ("t", str); step_++; return true;
1117  case 5: EXPECT_STREQ("f", str); step_++; return true;
1118  case 7: EXPECT_STREQ("n", str); step_++; return true;
1119  case 9: EXPECT_STREQ("i", str); step_++; return true;
1120  case 11: EXPECT_STREQ("pi", str); step_++; return true;
1121  case 13: EXPECT_STREQ("a", str); step_++; return true;
1122  default: ADD_FAILURE(); return false;
1123  }
1124  }
1125  bool StartObject() { EXPECT_EQ(0u, step_); step_++; return true; }
1126  bool EndObject(SizeType memberCount) { EXPECT_EQ(19u, step_); EXPECT_EQ(7u, memberCount); step_++; return true; }
1127  bool StartArray() { EXPECT_EQ(14u, step_); step_++; return true; }
1128  bool EndArray(SizeType elementCount) { EXPECT_EQ(18u, step_); EXPECT_EQ(3u, elementCount); step_++; return true; }
1129 
1130  unsigned step_;
1131 };
1132 
1133 TEST(Reader, ParseObject) {
1134  const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ";
1135 
1136  // Insitu
1137  {
1138  char* json2 = StrDup(json);
1139  InsituStringStream s(json2);
1141  Reader reader;
1142  reader.Parse<kParseInsituFlag>(s, h);
1143  EXPECT_EQ(20u, h.step_);
1144  free(json2);
1145  }
1146 
1147  // Normal
1148  {
1149  StringStream s(json);
1151  Reader reader;
1152  reader.Parse(s, h);
1153  EXPECT_EQ(20u, h.step_);
1154  }
1155 }
1156 
1157 struct ParseEmptyObjectHandler : BaseReaderHandler<UTF8<>, ParseEmptyObjectHandler> {
1158  ParseEmptyObjectHandler() : step_(0) {}
1159 
1160  bool Default() { ADD_FAILURE(); return false; }
1161  bool StartObject() { EXPECT_EQ(0u, step_); step_++; return true; }
1162  bool EndObject(SizeType) { EXPECT_EQ(1u, step_); step_++; return true; }
1163 
1164  unsigned step_;
1165 };
1166 
1167 TEST(Reader, Parse_EmptyObject) {
1168  StringStream s("{ } ");
1170  Reader reader;
1171  reader.Parse(s, h);
1172  EXPECT_EQ(2u, h.step_);
1173 }
1174 
1175 struct ParseMultipleRootHandler : BaseReaderHandler<UTF8<>, ParseMultipleRootHandler> {
1177 
1178  bool Default() { ADD_FAILURE(); return false; }
1179  bool StartObject() { EXPECT_EQ(0u, step_); step_++; return true; }
1180  bool EndObject(SizeType) { EXPECT_EQ(1u, step_); step_++; return true; }
1181  bool StartArray() { EXPECT_EQ(2u, step_); step_++; return true; }
1182  bool EndArray(SizeType) { EXPECT_EQ(3u, step_); step_++; return true; }
1183 
1184  unsigned step_;
1185 };
1186 
1187 template <unsigned parseFlags>
1189  StringStream s("{}[] a");
1191  Reader reader;
1192  EXPECT_TRUE(reader.Parse<parseFlags>(s, h));
1193  EXPECT_EQ(2u, h.step_);
1194  EXPECT_TRUE(reader.Parse<parseFlags>(s, h));
1195  EXPECT_EQ(4u, h.step_);
1196  EXPECT_EQ(' ', s.Take());
1197  EXPECT_EQ('a', s.Take());
1198 }
1199 
1200 TEST(Reader, Parse_MultipleRoot) {
1201  TestMultipleRoot<kParseStopWhenDoneFlag>();
1202 }
1203 
1204 TEST(Reader, ParseIterative_MultipleRoot) {
1205  TestMultipleRoot<kParseIterativeFlag | kParseStopWhenDoneFlag>();
1206 }
1207 
1208 template <unsigned parseFlags>
1210  char* buffer = strdup("{}[] a");
1211  InsituStringStream s(buffer);
1213  Reader reader;
1214  EXPECT_TRUE(reader.Parse<kParseInsituFlag | parseFlags>(s, h));
1215  EXPECT_EQ(2u, h.step_);
1216  EXPECT_TRUE(reader.Parse<kParseInsituFlag | parseFlags>(s, h));
1217  EXPECT_EQ(4u, h.step_);
1218  EXPECT_EQ(' ', s.Take());
1219  EXPECT_EQ('a', s.Take());
1220  free(buffer);
1221 }
1222 
1223 TEST(Reader, ParseInsitu_MultipleRoot) {
1224  TestInsituMultipleRoot<kParseStopWhenDoneFlag>();
1225 }
1226 
1227 TEST(Reader, ParseInsituIterative_MultipleRoot) {
1228  TestInsituMultipleRoot<kParseIterativeFlag | kParseStopWhenDoneFlag>();
1229 }
1230 
1231 #define TEST_ERROR(errorCode, str, errorOffset) \
1232  { \
1233  int streamPos = errorOffset; \
1234  char buffer[1001]; \
1235  strncpy(buffer, str, 1000); \
1236  InsituStringStream s(buffer); \
1237  BaseReaderHandler<> h; \
1238  Reader reader; \
1239  EXPECT_FALSE(reader.Parse(s, h)); \
1240  EXPECT_EQ(errorCode, reader.GetParseErrorCode());\
1241  EXPECT_EQ(errorOffset, reader.GetErrorOffset());\
1242  EXPECT_EQ(streamPos, s.Tell());\
1243  }
1244 
1245 TEST(Reader, ParseDocument_Error) {
1246  // The document is empty.
1250 
1251  // The document root must not follow by other values.
1256 }
1257 
1258 TEST(Reader, ParseValue_Error) {
1259  // Invalid value.
1260  TEST_ERROR(kParseErrorValueInvalid, "nulL", 3);
1261  TEST_ERROR(kParseErrorValueInvalid, "truE", 3);
1262  TEST_ERROR(kParseErrorValueInvalid, "falsE", 4);
1265 }
1266 
1267 TEST(Reader, ParseObject_Error) {
1268  // Missing a name for object member.
1271  TEST_ERROR(kParseErrorObjectMissName, "{null:1}", 1);
1272  TEST_ERROR(kParseErrorObjectMissName, "{true:1}", 1);
1273  TEST_ERROR(kParseErrorObjectMissName, "{false:1}", 1);
1275  TEST_ERROR(kParseErrorObjectMissName, "{[]:1}", 1);
1276  TEST_ERROR(kParseErrorObjectMissName, "{{}:1}", 1);
1277  TEST_ERROR(kParseErrorObjectMissName, "{xyz:1}", 1);
1278 
1279  // Missing a colon after a name of object member.
1280  TEST_ERROR(kParseErrorObjectMissColon, "{\"a\" 1}", 5);
1281  TEST_ERROR(kParseErrorObjectMissColon, "{\"a\",1}", 4);
1282 
1283  // Must be a comma or '}' after an object member
1285 
1286  // Object cannot have a trailing comma (without kParseTrailingCommasFlag);
1287  // an object member name must follow a comma
1288  TEST_ERROR(kParseErrorObjectMissName, "{\"a\":1,}", 7);
1289 
1290  // This tests that MemoryStream is checking the length in Peek().
1291  {
1292  MemoryStream ms("{\"a\"", 1);
1294  Reader reader;
1295  EXPECT_FALSE(reader.Parse<kParseStopWhenDoneFlag>(ms, h));
1297  }
1298 }
1299 
1300 #undef TEST_ERROR
1301 
1303  StringStream ss(" A \t\tB\n \n\nC\r\r \rD \t\n\r E");
1304  const char* expected = "ABCDE";
1305  for (size_t i = 0; i < 5; i++) {
1306  SkipWhitespace(ss);
1307  EXPECT_EQ(expected[i], ss.Take());
1308  }
1309 }
1310 
1311 // Test implementing a stream without copy stream optimization.
1312 // Clone from GenericStringStream except that copy constructor is disabled.
1313 template <typename Encoding>
1315 public:
1316  typedef typename Encoding::Ch Ch;
1317 
1318  CustomStringStream(const Ch *src) : src_(src), head_(src) {}
1319 
1320  Ch Peek() const { return *src_; }
1321  Ch Take() { return *src_++; }
1322  size_t Tell() const { return static_cast<size_t>(src_ - head_); }
1323 
1324  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
1325  void Put(Ch) { RAPIDJSON_ASSERT(false); }
1326  void Flush() { RAPIDJSON_ASSERT(false); }
1327  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
1328 
1329 private:
1330  // Prohibit copy constructor & assignment operator.
1333 
1334  const Ch* src_;
1335  const Ch* head_;
1336 };
1337 
1338 // If the following code is compiled, it should generate compilation error as predicted.
1339 // Because CustomStringStream<> is not copyable via making copy constructor private.
1340 #if 0
1341 namespace rapidjson {
1342 
1343 template <typename Encoding>
1345  enum { copyOptimization = 1 };
1346 };
1347 
1348 } // namespace rapidjson
1349 #endif
1350 
1352  const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ";
1355  Reader reader;
1356  reader.Parse(s, h);
1357  EXPECT_EQ(20u, h.step_);
1358 }
1359 
1360 #include <sstream>
1361 
1363 public:
1364  typedef char Ch;
1365 
1366  IStreamWrapper(std::istream& is) : is_(is) {}
1367 
1368  Ch Peek() const {
1369  int c = is_.peek();
1370  return c == std::char_traits<char>::eof() ? '\0' : static_cast<Ch>(c);
1371  }
1372 
1373  Ch Take() {
1374  int c = is_.get();
1375  return c == std::char_traits<char>::eof() ? '\0' : static_cast<Ch>(c);
1376  }
1377 
1378  size_t Tell() const { return static_cast<size_t>(is_.tellg()); }
1379 
1380  Ch* PutBegin() { assert(false); return 0; }
1381  void Put(Ch) { assert(false); }
1382  void Flush() { assert(false); }
1383  size_t PutEnd(Ch*) { assert(false); return 0; }
1384 
1385 private:
1388 
1389  std::istream& is_;
1390 };
1391 
1392 TEST(Reader, Parse_IStreamWrapper_StringStream) {
1393  const char* json = "[1,2,3,4]";
1394 
1395  std::stringstream ss(json);
1396  IStreamWrapper is(ss);
1397 
1398  Reader reader;
1400  reader.Parse(is, h);
1401  EXPECT_FALSE(reader.HasParseError());
1402 }
1403 
1404 // Test iterative parsing.
1405 
1406 #define TESTERRORHANDLING(text, errorCode, offset)\
1407 {\
1408  int streamPos = offset; \
1409  StringStream json(text); \
1410  BaseReaderHandler<> handler; \
1411  Reader reader; \
1412  reader.Parse<kParseIterativeFlag>(json, handler); \
1413  EXPECT_TRUE(reader.HasParseError()); \
1414  EXPECT_EQ(errorCode, reader.GetParseErrorCode()); \
1415  EXPECT_EQ(offset, reader.GetErrorOffset()); \
1416  EXPECT_EQ(streamPos, json.Tell()); \
1417 }
1418 
1419 TEST(Reader, IterativeParsing_ErrorHandling) {
1420  TESTERRORHANDLING("{\"a\": a}", kParseErrorValueInvalid, 6u);
1421 
1424 
1426  TESTERRORHANDLING("{\"a\", 1}", kParseErrorObjectMissColon, 4u);
1431  TESTERRORHANDLING("{\"a\":}", kParseErrorValueInvalid, 5u);
1432  TESTERRORHANDLING("{\"a\":]", kParseErrorValueInvalid, 5u);
1437 
1438  // Trailing commas are not allowed without kParseTrailingCommasFlag
1439  TESTERRORHANDLING("{\"a\": 1,}", kParseErrorObjectMissName, 8u);
1440  TESTERRORHANDLING("[1,2,3,]", kParseErrorValueInvalid, 7u);
1441 
1442  // Any JSON value can be a valid root element in RFC7159.
1451 }
1452 
1453 template<typename Encoding = UTF8<> >
1455  typedef typename Encoding::Ch Ch;
1456 
1457  const static uint32_t LOG_NULL = 0x10000000;
1458  const static uint32_t LOG_BOOL = 0x20000000;
1459  const static uint32_t LOG_INT = 0x30000000;
1460  const static uint32_t LOG_UINT = 0x40000000;
1461  const static uint32_t LOG_INT64 = 0x50000000;
1462  const static uint32_t LOG_UINT64 = 0x60000000;
1463  const static uint32_t LOG_DOUBLE = 0x70000000;
1464  const static uint32_t LOG_STRING = 0x80000000;
1465  const static uint32_t LOG_STARTOBJECT = 0x90000000;
1466  const static uint32_t LOG_KEY = 0xA0000000;
1467  const static uint32_t LOG_ENDOBJECT = 0xB0000000;
1468  const static uint32_t LOG_STARTARRAY = 0xC0000000;
1469  const static uint32_t LOG_ENDARRAY = 0xD0000000;
1470 
1471  const static size_t LogCapacity = 256;
1472  uint32_t Logs[LogCapacity];
1473  size_t LogCount;
1474 
1476  }
1477 
1478  bool Null() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_NULL; return true; }
1479 
1480  bool Bool(bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_BOOL; return true; }
1481 
1482  bool Int(int) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_INT; return true; }
1483 
1484  bool Uint(unsigned) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_INT; return true; }
1485 
1486  bool Int64(int64_t) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_INT64; return true; }
1487 
1488  bool Uint64(uint64_t) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_UINT64; return true; }
1489 
1490  bool Double(double) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_DOUBLE; return true; }
1491 
1492  bool RawNumber(const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STRING; return true; }
1493 
1494  bool String(const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STRING; return true; }
1495 
1496  bool StartObject() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STARTOBJECT; return true; }
1497 
1498  bool Key (const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_KEY; return true; }
1499 
1501  RAPIDJSON_ASSERT(LogCount < LogCapacity);
1502  RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0);
1503  Logs[LogCount++] = LOG_ENDOBJECT | static_cast<uint32_t>(c);
1504  return true;
1505  }
1506 
1507  bool StartArray() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STARTARRAY; return true; }
1508 
1509  bool EndArray(SizeType c) {
1510  RAPIDJSON_ASSERT(LogCount < LogCapacity);
1511  RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0);
1512  Logs[LogCount++] = LOG_ENDARRAY | static_cast<uint32_t>(c);
1513  return true;
1514  }
1515 };
1516 
1517 TEST(Reader, IterativeParsing_General) {
1518  {
1519  StringStream is("[1, {\"k\": [1, 2]}, null, false, true, \"string\", 1.2]");
1520  Reader reader;
1522 
1523  ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler);
1524 
1525  EXPECT_FALSE(r.IsError());
1526  EXPECT_FALSE(reader.HasParseError());
1527 
1528  uint32_t e[] = {
1529  handler.LOG_STARTARRAY,
1530  handler.LOG_INT,
1531  handler.LOG_STARTOBJECT,
1532  handler.LOG_KEY,
1533  handler.LOG_STARTARRAY,
1534  handler.LOG_INT,
1535  handler.LOG_INT,
1536  handler.LOG_ENDARRAY | 2,
1537  handler.LOG_ENDOBJECT | 1,
1538  handler.LOG_NULL,
1539  handler.LOG_BOOL,
1540  handler.LOG_BOOL,
1541  handler.LOG_STRING,
1542  handler.LOG_DOUBLE,
1543  handler.LOG_ENDARRAY | 7
1544  };
1545 
1546  EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount);
1547 
1548  for (size_t i = 0; i < handler.LogCount; ++i) {
1549  EXPECT_EQ(e[i], handler.Logs[i]) << "i = " << i;
1550  }
1551  }
1552 }
1553 
1554 TEST(Reader, IterativeParsing_Count) {
1555  {
1556  StringStream is("[{}, {\"k\": 1}, [1], []]");
1557  Reader reader;
1559 
1560  ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler);
1561 
1562  EXPECT_FALSE(r.IsError());
1563  EXPECT_FALSE(reader.HasParseError());
1564 
1565  uint32_t e[] = {
1566  handler.LOG_STARTARRAY,
1567  handler.LOG_STARTOBJECT,
1568  handler.LOG_ENDOBJECT | 0,
1569  handler.LOG_STARTOBJECT,
1570  handler.LOG_KEY,
1571  handler.LOG_INT,
1572  handler.LOG_ENDOBJECT | 1,
1573  handler.LOG_STARTARRAY,
1574  handler.LOG_INT,
1575  handler.LOG_ENDARRAY | 1,
1576  handler.LOG_STARTARRAY,
1577  handler.LOG_ENDARRAY | 0,
1578  handler.LOG_ENDARRAY | 4
1579  };
1580 
1581  EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount);
1582 
1583  for (size_t i = 0; i < handler.LogCount; ++i) {
1584  EXPECT_EQ(e[i], handler.Logs[i]) << "i = " << i;
1585  }
1586  }
1587 }
1588 
1589 TEST(Reader, IterativePullParsing_General) {
1590  {
1592  uint32_t e[] = {
1593  handler.LOG_STARTARRAY,
1594  handler.LOG_INT,
1595  handler.LOG_STARTOBJECT,
1596  handler.LOG_KEY,
1597  handler.LOG_STARTARRAY,
1598  handler.LOG_INT,
1599  handler.LOG_INT,
1600  handler.LOG_ENDARRAY | 2,
1601  handler.LOG_ENDOBJECT | 1,
1602  handler.LOG_NULL,
1603  handler.LOG_BOOL,
1604  handler.LOG_BOOL,
1605  handler.LOG_STRING,
1606  handler.LOG_DOUBLE,
1607  handler.LOG_ENDARRAY | 7
1608  };
1609 
1610  StringStream is("[1, {\"k\": [1, 2]}, null, false, true, \"string\", 1.2]");
1611  Reader reader;
1612 
1613  reader.IterativeParseInit();
1614  while (!reader.IterativeParseComplete()) {
1615  size_t oldLogCount = handler.LogCount;
1616  EXPECT_TRUE(oldLogCount < sizeof(e) / sizeof(int)) << "overrun";
1617 
1618  EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse fail";
1619  EXPECT_EQ(handler.LogCount, oldLogCount + 1) << "handler should be invoked exactly once each time";
1620  EXPECT_EQ(e[oldLogCount], handler.Logs[oldLogCount]) << "wrong event returned";
1621  }
1622 
1623  EXPECT_FALSE(reader.HasParseError());
1624  EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount) << "handler invoked wrong number of times";
1625 
1626  // The handler should not be invoked when the JSON has been fully read, but it should not fail
1627  size_t oldLogCount = handler.LogCount;
1628  EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse-next past complete is allowed";
1629  EXPECT_EQ(handler.LogCount, oldLogCount) << "parse-next past complete should not invoke handler";
1630  EXPECT_FALSE(reader.HasParseError()) << "parse-next past complete should not generate parse error";
1631  }
1632 }
1633 
1634 // Test iterative parsing on kParseErrorTermination.
1636  bool StartObject() { return false; }
1637 };
1638 
1640  bool StartArray() { return false; }
1641 };
1642 
1644  bool EndObject(SizeType) { return false; }
1645 };
1646 
1648  bool EndArray(SizeType) { return false; }
1649 };
1650 
1651 TEST(Reader, IterativeParsing_ShortCircuit) {
1652  {
1654  Reader reader;
1655  StringStream is("[1, {}]");
1656 
1657  ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler);
1658 
1659  EXPECT_TRUE(reader.HasParseError());
1661  EXPECT_EQ(4u, r.Offset());
1662  }
1663 
1664  {
1666  Reader reader;
1667  StringStream is("{\"a\": []}");
1668 
1669  ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler);
1670 
1671  EXPECT_TRUE(reader.HasParseError());
1673  EXPECT_EQ(6u, r.Offset());
1674  }
1675 
1676  {
1678  Reader reader;
1679  StringStream is("[1, {}]");
1680 
1681  ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler);
1682 
1683  EXPECT_TRUE(reader.HasParseError());
1685  EXPECT_EQ(5u, r.Offset());
1686  }
1687 
1688  {
1690  Reader reader;
1691  StringStream is("{\"a\": []}");
1692 
1693  ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler);
1694 
1695  EXPECT_TRUE(reader.HasParseError());
1697  EXPECT_EQ(7u, r.Offset());
1698  }
1699 }
1700 
1701 // For covering BaseReaderHandler default functions
1702 TEST(Reader, BaseReaderHandler_Default) {
1704  Reader reader;
1705  StringStream is("[null, true, -1, 1, -1234567890123456789, 1234567890123456789, 3.14, \"s\", { \"a\" : 1 }]");
1706  EXPECT_TRUE(reader.Parse(is, h));
1707 }
1708 
1709 template <int e>
1711  bool Null() { return e != 0; }
1712  bool Bool(bool) { return e != 1; }
1713  bool Int(int) { return e != 2; }
1714  bool Uint(unsigned) { return e != 3; }
1715  bool Int64(int64_t) { return e != 4; }
1716  bool Uint64(uint64_t) { return e != 5; }
1717  bool Double(double) { return e != 6; }
1718  bool RawNumber(const char*, SizeType, bool) { return e != 7; }
1719  bool String(const char*, SizeType, bool) { return e != 8; }
1720  bool StartObject() { return e != 9; }
1721  bool Key(const char*, SizeType, bool) { return e != 10; }
1722  bool EndObject(SizeType) { return e != 11; }
1723  bool StartArray() { return e != 12; }
1724  bool EndArray(SizeType) { return e != 13; }
1725 };
1726 
1727 #define TEST_TERMINATION(e, json)\
1728 {\
1729  Reader reader;\
1730  TerminateHandler<e> h;\
1731  StringStream is(json);\
1732  EXPECT_FALSE(reader.Parse(is, h));\
1733  EXPECT_EQ(kParseErrorTermination, reader.GetParseErrorCode());\
1734 }
1735 
1736 TEST(Reader, ParseTerminationByHandler) {
1737  TEST_TERMINATION(0, "[null");
1738  TEST_TERMINATION(1, "[true");
1739  TEST_TERMINATION(1, "[false");
1740  TEST_TERMINATION(2, "[-1");
1741  TEST_TERMINATION(3, "[1");
1742  TEST_TERMINATION(4, "[-1234567890123456789");
1743  TEST_TERMINATION(5, "[1234567890123456789");
1744  TEST_TERMINATION(6, "[0.5]");
1745  // RawNumber() is never called
1746  TEST_TERMINATION(8, "[\"a\"");
1747  TEST_TERMINATION(9, "[{");
1748  TEST_TERMINATION(10, "[{\"a\"");
1749  TEST_TERMINATION(11, "[{}");
1750  TEST_TERMINATION(11, "[{\"a\":1}"); // non-empty object
1751  TEST_TERMINATION(12, "{\"a\":[");
1752  TEST_TERMINATION(13, "{\"a\":[]");
1753  TEST_TERMINATION(13, "{\"a\":[1]"); // non-empty array
1754 }
1755 
1756 TEST(Reader, ParseComments) {
1757  const char* json =
1758  "// Here is a one-line comment.\n"
1759  "{// And here's another one\n"
1760  " /*And here's an in-line one.*/\"hello\" : \"world\","
1761  " \"t\" :/* And one with '*' symbol*/true ,"
1762  "/* A multiline comment\n"
1763  " goes here*/"
1764  " \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3]"
1765  "}/*And the last one to be sure */";
1766 
1767  StringStream s(json);
1769  Reader reader;
1770  EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h));
1771  EXPECT_EQ(20u, h.step_);
1772 }
1773 
1774 TEST(Reader, ParseEmptyInlineComment) {
1775  const char* json = "{/**/\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }";
1776 
1777  StringStream s(json);
1779  Reader reader;
1780  EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h));
1781  EXPECT_EQ(20u, h.step_);
1782 }
1783 
1784 TEST(Reader, ParseEmptyOnelineComment) {
1785  const char* json = "{//\n\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }";
1786 
1787  StringStream s(json);
1789  Reader reader;
1790  EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h));
1791  EXPECT_EQ(20u, h.step_);
1792 }
1793 
1794 TEST(Reader, ParseMultipleCommentsInARow) {
1795  const char* json =
1796  "{/* first comment *//* second */\n"
1797  "/* third */ /*fourth*/// last one\n"
1798  "\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }";
1799 
1800  StringStream s(json);
1802  Reader reader;
1803  EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h));
1804  EXPECT_EQ(20u, h.step_);
1805 }
1806 
1807 TEST(Reader, InlineCommentsAreDisabledByDefault) {
1808  {
1809  const char* json = "{/* Inline comment. */\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }";
1810 
1811  StringStream s(json);
1813  Reader reader;
1814  EXPECT_FALSE(reader.Parse<kParseDefaultFlags>(s, h));
1815  }
1816 
1817  {
1818  const char* json =
1819  "{\"hello\" : /* Multiline comment starts here\n"
1820  " continues here\n"
1821  " and ends here */\"world\", \"t\" :true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }";
1822 
1823  StringStream s(json);
1825  Reader reader;
1826  EXPECT_FALSE(reader.Parse<kParseDefaultFlags>(s, h));
1827  }
1828 }
1829 
1830 TEST(Reader, OnelineCommentsAreDisabledByDefault) {
1831  const char* json = "{// One-line comment\n\"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }";
1832 
1833  StringStream s(json);
1835  Reader reader;
1836  EXPECT_FALSE(reader.Parse<kParseDefaultFlags>(s, h));
1837 }
1838 
1839 TEST(Reader, EofAfterOneLineComment) {
1840  const char* json = "{\"hello\" : \"world\" // EOF is here -->\0 \n}";
1841 
1842  StringStream s(json);
1844  Reader reader;
1845  EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h));
1847 }
1848 
1849 TEST(Reader, IncompleteMultilineComment) {
1850  const char* json = "{\"hello\" : \"world\" /* EOF is here -->\0 */}";
1851 
1852  StringStream s(json);
1854  Reader reader;
1855  EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h));
1857 }
1858 
1859 TEST(Reader, IncompleteMultilineComment2) {
1860  const char* json = "{\"hello\" : \"world\" /* *\0 */}";
1861 
1862  StringStream s(json);
1864  Reader reader;
1865  EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h));
1867 }
1868 
1869 TEST(Reader, UnrecognizedComment) {
1870  const char* json = "{\"hello\" : \"world\" /! }";
1871 
1872  StringStream s(json);
1874  Reader reader;
1875  EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h));
1877 }
1878 
1880  bool Null() { return true; }
1881  bool Bool(bool) { return true; }
1882  bool Int(int) { return true; }
1883  bool Uint(unsigned) { return true; }
1884  bool Int64(int64_t) { return true; }
1885  bool Uint64(uint64_t) { return true; }
1886  bool Double(double) { return true; }
1887  // 'str' is not null-terminated
1888  bool RawNumber(const char* str, SizeType length, bool) {
1889  EXPECT_TRUE(str != 0);
1890  EXPECT_TRUE(expected_len_ == length);
1891  EXPECT_TRUE(strncmp(str, expected_, length) == 0);
1892  return true;
1893  }
1894  bool String(const char*, SizeType, bool) { return true; }
1895  bool StartObject() { return true; }
1896  bool Key(const char*, SizeType, bool) { return true; }
1897  bool EndObject(SizeType) { return true; }
1898  bool StartArray() { return true; }
1899  bool EndArray(SizeType) { return true; }
1900 
1901  NumbersAsStringsHandler(const char* expected)
1902  : expected_(expected)
1903  , expected_len_(strlen(expected)) {}
1904 
1905  const char* expected_;
1907 };
1908 
1909 TEST(Reader, NumbersAsStrings) {
1910  {
1911  const char* json = "{ \"pi\": 3.1416 } ";
1912  StringStream s(json);
1913  NumbersAsStringsHandler h("3.1416");
1914  Reader reader;
1916  }
1917  {
1918  char* json = StrDup("{ \"pi\": 3.1416 } ");
1920  NumbersAsStringsHandler h("3.1416");
1921  Reader reader;
1923  free(json);
1924  }
1925  {
1926  const char* json = "{ \"gigabyte\": 1.0e9 } ";
1927  StringStream s(json);
1928  NumbersAsStringsHandler h("1.0e9");
1929  Reader reader;
1931  }
1932  {
1933  char* json = StrDup("{ \"gigabyte\": 1.0e9 } ");
1935  NumbersAsStringsHandler h("1.0e9");
1936  Reader reader;
1938  free(json);
1939  }
1940  {
1941  const char* json = "{ \"pi\": 314.159e-2 } ";
1942  StringStream s(json);
1943  NumbersAsStringsHandler h("314.159e-2");
1944  Reader reader;
1946  }
1947  {
1948  char* json = StrDup("{ \"gigabyte\": 314.159e-2 } ");
1950  NumbersAsStringsHandler h("314.159e-2");
1951  Reader reader;
1953  free(json);
1954  }
1955  {
1956  const char* json = "{ \"negative\": -1.54321 } ";
1957  StringStream s(json);
1958  NumbersAsStringsHandler h("-1.54321");
1959  Reader reader;
1961  }
1962  {
1963  char* json = StrDup("{ \"negative\": -1.54321 } ");
1965  NumbersAsStringsHandler h("-1.54321");
1966  Reader reader;
1968  free(json);
1969  }
1970  {
1971  const char* json = "{ \"pi\": 314.159e-2 } ";
1972  std::stringstream ss(json);
1973  IStreamWrapper s(ss);
1974  NumbersAsStringsHandler h("314.159e-2");
1975  Reader reader;
1977  }
1978 }
1979 
1980 template <unsigned extraFlags>
1982  {
1983  StringStream s("[1,2,3,]");
1985  Reader reader;
1986  EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h));
1987  EXPECT_EQ(5u, h.step_);
1988  }
1989  {
1990  const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false,"
1991  "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3],}";
1992  StringStream s(json);
1994  Reader reader;
1995  EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h));
1996  EXPECT_EQ(20u, h.step_);
1997  }
1998  {
1999  // whitespace around trailing commas
2000  const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false,"
2001  "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3\n,\n]\n,\n} ";
2002  StringStream s(json);
2004  Reader reader;
2005  EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h));
2006  EXPECT_EQ(20u, h.step_);
2007  }
2008  {
2009  // comments around trailing commas
2010  const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null,"
2011  "\"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3/*test*/,/*test*/]/*test*/,/*test*/}";
2012  StringStream s(json);
2014  Reader reader;
2016  EXPECT_EQ(20u, h.step_);
2017  }
2018 }
2019 
2020 TEST(Reader, TrailingCommas) {
2021  TestTrailingCommas<kParseNoFlags>();
2022 }
2023 
2024 TEST(Reader, TrailingCommasIterative) {
2025  TestTrailingCommas<kParseIterativeFlag>();
2026 }
2027 
2028 template <unsigned extraFlags>
2030  // only a single trailing comma is allowed.
2031  {
2032  StringStream s("[1,2,3,,]");
2034  Reader reader;
2035  ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
2036  EXPECT_TRUE(reader.HasParseError());
2038  EXPECT_EQ(7u, r.Offset());
2039  }
2040  {
2041  const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false,"
2042  "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3,],,}";
2043  StringStream s(json);
2045  Reader reader;
2046  ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
2047  EXPECT_TRUE(reader.HasParseError());
2049  EXPECT_EQ(95u, r.Offset());
2050  }
2051 }
2052 
2053 TEST(Reader, MultipleTrailingCommaErrors) {
2054  TestMultipleTrailingCommaErrors<kParseNoFlags>();
2055 }
2056 
2057 TEST(Reader, MultipleTrailingCommaErrorsIterative) {
2058  TestMultipleTrailingCommaErrors<kParseIterativeFlag>();
2059 }
2060 
2061 template <unsigned extraFlags>
2063  // not allowed even with trailing commas enabled; the
2064  // trailing comma must follow a value.
2065  {
2066  StringStream s("[,]");
2068  Reader reader;
2069  ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
2070  EXPECT_TRUE(reader.HasParseError());
2072  EXPECT_EQ(1u, r.Offset());
2073  }
2074  {
2075  StringStream s("{,}");
2077  Reader reader;
2078  ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
2079  EXPECT_TRUE(reader.HasParseError());
2081  EXPECT_EQ(1u, r.Offset());
2082  }
2083 }
2084 
2085 TEST(Reader, EmptyExceptForCommaErrors) {
2086  TestEmptyExceptForCommaErrors<kParseNoFlags>();
2087 }
2088 
2089 TEST(Reader, EmptyExceptForCommaErrorsIterative) {
2090  TestEmptyExceptForCommaErrors<kParseIterativeFlag>();
2091 }
2092 
2093 template <unsigned extraFlags>
2095  {
2097  Reader reader;
2098  StringStream s("[1,2,3,]");
2099  ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
2100  EXPECT_TRUE(reader.HasParseError());
2102  EXPECT_EQ(7u, r.Offset());
2103  }
2104  {
2106  Reader reader;
2107  StringStream s("{\"t\": true, \"f\": false,}");
2108  ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h);
2109  EXPECT_TRUE(reader.HasParseError());
2111  EXPECT_EQ(23u, r.Offset());
2112  }
2113 }
2114 
2115 TEST(Reader, TrailingCommaHandlerTermination) {
2116  TestTrailingCommaHandlerTermination<kParseNoFlags>();
2117 }
2118 
2119 TEST(Reader, TrailingCommaHandlerTerminationIterative) {
2120  TestTrailingCommaHandlerTermination<kParseIterativeFlag>();
2121 }
2122 
2123 TEST(Reader, ParseNanAndInfinity) {
2124 #define TEST_NAN_INF(str, x) \
2125  { \
2126  { \
2127  StringStream s(str); \
2128  ParseDoubleHandler h; \
2129  Reader reader; \
2130  ASSERT_EQ(kParseErrorNone, reader.Parse<kParseNanAndInfFlag>(s, h).Code()); \
2131  EXPECT_EQ(1u, h.step_); \
2132  internal::Double e(x), a(h.actual_); \
2133  EXPECT_EQ(e.IsNan(), a.IsNan()); \
2134  EXPECT_EQ(e.IsInf(), a.IsInf()); \
2135  if (!e.IsNan()) \
2136  EXPECT_EQ(e.Sign(), a.Sign()); \
2137  } \
2138  { \
2139  const char* json = "{ \"naninfdouble\": " str " } "; \
2140  StringStream s(json); \
2141  NumbersAsStringsHandler h(str); \
2142  Reader reader; \
2143  EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag|kParseNanAndInfFlag>(s, h)); \
2144  } \
2145  { \
2146  char* json = StrDup("{ \"naninfdouble\": " str " } "); \
2147  InsituStringStream s(json); \
2148  NumbersAsStringsHandler h(str); \
2149  Reader reader; \
2150  EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag|kParseNanAndInfFlag>(s, h)); \
2151  free(json); \
2152  } \
2153  }
2154 #define TEST_NAN_INF_ERROR(errorCode, str, errorOffset) \
2155  { \
2156  int streamPos = errorOffset; \
2157  char buffer[1001]; \
2158  strncpy(buffer, str, 1000); \
2159  InsituStringStream s(buffer); \
2160  BaseReaderHandler<> h; \
2161  Reader reader; \
2162  EXPECT_FALSE(reader.Parse<kParseNanAndInfFlag>(s, h)); \
2163  EXPECT_EQ(errorCode, reader.GetParseErrorCode());\
2164  EXPECT_EQ(errorOffset, reader.GetErrorOffset());\
2165  EXPECT_EQ(streamPos, s.Tell());\
2166  }
2167 
2168  double nan = std::numeric_limits<double>::quiet_NaN();
2169  double inf = std::numeric_limits<double>::infinity();
2170 
2171  TEST_NAN_INF("NaN", nan);
2172  TEST_NAN_INF("-NaN", nan);
2173  TEST_NAN_INF("Inf", inf);
2174  TEST_NAN_INF("Infinity", inf);
2175  TEST_NAN_INF("-Inf", -inf);
2176  TEST_NAN_INF("-Infinity", -inf);
2185 
2186 #undef TEST_NAN_INF_ERROR
2187 #undef TEST_NAN_INF
2188 }
2189 
2190 RAPIDJSON_DIAG_POP
size_t Tell() const
#define TEST_INTEGER(Handler, str, x)
BasicIStreamWrapper< std::istream > IStreamWrapper
bool Uint(unsigned i)
const Encoding::Ch * str_
Definition: readertest.cpp:777
char * u64toa(uint64_t value, char *buffer)
Definition: itoa.h:126
bool EndArray(SizeType)
double Value() const
Definition: ieee754.h:29
uint64_t ToBias() const
Definition: ieee754.h:49
#define TEST_NAN_INF_ERROR(errorCode, str, errorOffset)
#define TEST_ERROR(errorCode, str, errorOffset)
void TestEmptyExceptForCommaErrors()
bool EndObject(SizeType memberCount)
#define ADD_FAILURE()
Definition: gtest.h:1808
uint64_t Uint64Value() const
Definition: ieee754.h:30
Miss fraction part in number.
Definition: error.h:85
The document is empty.
Definition: error.h:67
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
Iterative(constant complexity in terms of function call stack size) parsing.
Definition: reader.h:149
bool String(const char *, SizeType, bool)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static const uint32_t LOG_BOOL
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:1995
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:294
static const uint32_t LOG_NULL
bool IsNormal() const
Definition: ieee754.h:44
No error.
Definition: error.h:65
#define TEST_NUMBER_ERROR(errorCode, str, errorOffset, streamPos)
#define TEST_TERMINATION(e, json)
static const uint32_t LOG_STARTOBJECT
ParseErrorCode Code() const
Get the error code.
Definition: error.h:116
Missing a colon after a name of object member.
Definition: error.h:73
CustomStringStream(const Ch *src)
Missing a comma or &#39;]&#39; after an array element.
Definition: error.h:76
size_t Tell() const
Miss exponent in number.
Definition: error.h:86
bool RawNumber(const char *str, SizeType length, bool)
bool Double(double)
Missing a closing quotation mark in string.
Definition: error.h:81
void TestInsituMultipleRoot()
size_t PutEnd(Ch *)
bool String(const char *, SizeType, bool)
size_t PutEnd(Ch *)
bool IsNan() const
Definition: ieee754.h:41
static const uint32_t LOG_DOUBLE
Allow trailing commas at the end of objects and arrays.
Definition: reader.h:154
ParseErrorCode
Error code of parsing.
Definition: error.h:64
void copy(key &AA, const key &A)
Definition: rctOps.h:79
static const uint32_t LOG_KEY
Provides additional information for stream.
Definition: stream.h:73
static const uint32_t LOG_ENDARRAY
bool EndObject(SizeType)
bool EndObject(SizeType)
Incorrect hex digit after \u escape in string.
Definition: error.h:78
static const uint32_t LOG_STARTARRAY
bool Key(const char *, SizeType, bool)
Parse all numbers (ints/doubles) as strings.
Definition: reader.h:153
After parsing a complete JSON root from stream, stop further processing the rest of stream...
Definition: reader.h:150
static const uint32_t LOG_INT
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition: reader.h:557
bool String(const typename Encoding::Ch *str, size_t length, bool copy)
Definition: readertest.cpp:764
Missing a name for object member.
Definition: error.h:72
unsigned actual_
Definition: readertest.cpp:83
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS. ...
Definition: reader.h:156
#define TEST_STRINGARRAY(Encoding, utype, array, x)
Default implementation of Handler.
Definition: fwd.h:85
Parse number in full precision (but slower).
Definition: reader.h:151
bool RawNumber(const char *, SizeType, bool)
bool IsInf() const
Definition: ieee754.h:42
mdb_size_t count(MDB_cursor *cur)
Number too big to be stored in double.
Definition: error.h:84
bool EndArray(SizeType)
Represents an in-memory input byte stream.
Definition: memorystream.h:40
bool Double(double d)
Definition: readertest.cpp:107
#define TEST_ARRAY_ERROR(errorCode, str, errorOffset)
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition: reader.h:680
bool Key(const Ch *, SizeType, bool)
Concept for encoding of Unicode characters.
bool String(const char *str, size_t, bool)
unsigned int uint32_t
Definition: stdint.h:126
bool Bool(bool b)
bool Sign() const
Definition: ieee754.h:37
static const uint32_t LOG_ENDOBJECT
ASCII encoding.
Definition: encodings.h:542
Definition: expect.h:70
void TestTrailingCommas()
Read-only string stream.
Definition: fwd.h:47
Ch Peek() const
#define TEST_DOUBLE(fullPrecision, str, x)
void TestTrailingCommaHandlerTermination()
Ch * StrDup(const Ch *str)
Definition: unittest.h:73
unsigned __int64 uint64_t
Definition: stdint.h:136
TEST(Reader, ParseTrue)
Definition: readertest.cpp:52
#define TESTERRORHANDLING(text, errorCode, offset)
bool Uint64(uint64_t i)
Definition: readertest.cpp:98
Invalid encoding in string.
Definition: error.h:82
Invalid value.
Definition: error.h:70
bool IterativeParseNext(InputStream &is, Handler &handler)
Parse one token from JSON text.
Definition: reader.h:618
bool Uint(unsigned i)
Missing a comma or &#39;}&#39; after an object member.
Definition: error.h:74
bool Int(int i)
Definition: readertest.cpp:71
#define TEST_STRINGENCODING_ERROR(Encoding, TargetEncoding, utype, array)
bool RawNumber(const Ch *, SizeType, bool)
main RapidJSON namespace
char * u32toa(uint32_t value, char *buffer)
Definition: itoa.h:39
bool Uint(unsigned i)
Definition: readertest.cpp:80
#define TEST_STRING_ERROR(errorCode, str, errorOffset, streamPos)
uint32_t Logs[LogCapacity]
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
bool Uint(unsigned)
bool IsError() const
Whether the result is an error.
Definition: error.h:123
int StrCmp(const Ch *s1, const Ch *s2)
Definition: unittest.h:67
IStreamWrapper(std::istream &is)
bool Bool(bool b)
Definition: readertest.cpp:47
#define TEST_STRINGARRAY2(Encoding, utype, earray, xarray)
Unspecific syntax error.
Definition: error.h:89
void SkipWhitespace(InputStream &is)
Skip the JSON white spaces in a stream.
Definition: reader.h:264
Invalid escape character in string.
Definition: error.h:80
void IterativeParseInit()
Initialize JSON text token-by-token parsing.
Definition: reader.h:605
void TestMultipleTrailingCommaErrors()
FloatingPoint< double > Double
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition: reader.h:683
void * memcpy(void *a, const void *b, size_t c)
#define Ch(x, y, z)
Definition: hash_impl.h:17
unsigned StrLen(const Ch *s)
Definition: unittest.h:60
#define TEST_NAN_INF(str, x)
UTF-32 encoding.
Definition: encodings.h:418
Validate encoding of JSON strings.
Definition: reader.h:148
CharType Ch
Definition: encodings.h:270
bool EndArray(SizeType)
signed __int64 int64_t
Definition: stdint.h:135
The document root must not follow by other values.
Definition: error.h:68
bool Int64(int64_t i)
Definition: readertest.cpp:89
Result of parsing (wraps ParseErrorCode)
Definition: error.h:106
UTF-16 encoding.
Definition: encodings.h:269
static const uint32_t LOG_STRING
bool Uint64(uint64_t)
Parsing was terminated.
Definition: error.h:88
connection< TProtocol > & operator=(const connection< TProtocol > &obj)
#define EXPECT_DOUBLE_EQ(val1, val2)
Definition: gtest.h:2031
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
bool EndArray(SizeType elementCount)
void TestMultipleRoot()
bool EndObject(SizeType)
NumbersAsStringsHandler(const char *expected)
bool Double(double d)
bool Key(const char *, SizeType, bool)
signed int int32_t
Definition: stdint.h:123
bool String(const Ch *, SizeType, bool)
#define ARRAY(...)
#define TEST_STRING(Encoding, e, x)
bool Int64(int64_t)
char * i64toa(int64_t value, char *buffer)
Definition: itoa.h:295
error
Tracks LMDB error codes.
Definition: error.h:44
unsigned step_
Definition: readertest.cpp:73
char * i32toa(int32_t value, char *buffer)
Definition: itoa.h:115
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
RAPIDJSON_FORCEINLINE bool IterativeParseComplete() const
Check if token-by-token parsing JSON text is complete.
Definition: reader.h:675
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:411
char * dtoa(double value, char *buffer, int maxDecimalPlaces=324)
Definition: dtoa.h:216
In-situ(destructive) parsing.
Definition: reader.h:147
rapidjson::Document json
Definition: transport.cpp:49
ParseErrorCode TestString(const typename Encoding::Ch *str)
Definition: readertest.cpp:899
The surrogate pair in string is invalid.
Definition: error.h:79
UTF-8 encoding.
Definition: encodings.h:96
Allow one-line (//) and multi-line (/**/) comments.
Definition: reader.h:152
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
Definition: error.h:118
bool EndObject(SizeType)