| 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. |
| 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" |
| 18 |
#include "rapidjson/internal/dtoa.h" |
| 19 |
#include "rapidjson/internal/itoa.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"); |
| 54 |
ParseBoolHandler<true> h; |
| 55 |
Reader reader; |
| 56 |
reader.Parse(s, h); |
| 57 |
EXPECT_EQ(1u, h.step_); |
| 58 |
} |
| 59 |
|
| 60 |
TEST(Reader, ParseFalse) { |
| 61 |
StringStream s("false"); |
| 62 |
ParseBoolHandler<false> h; |
| 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_; |
| 92 |
int64_t actual_; |
| 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_; |
| 101 |
uint64_t actual_; |
| 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()); |
| 331 |
internal::Double d = internal::Double(u); |
| 332 |
|
| 333 |
char buffer[32]; |
| 334 |
*internal::dtoa(d.Value(), buffer) = '\0'; |
| 335 |
|
| 336 |
StringStream s(buffer); |
| 337 |
ParseDoubleHandler h; |
| 338 |
ASSERT_EQ(kParseErrorNone, reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h).Code()); |
| 339 |
EXPECT_EQ(1u, h.step_); |
| 340 |
internal::Double a(h.actual_); |
| 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 |
| 348 |
EXPECT_DOUBLE_EQ(d.Value(), h.actual_); |
| 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); |
| 363 |
ParseDoubleHandler h; |
| 364 |
Reader reader; |
| 365 |
ASSERT_EQ(kParseErrorNone, reader.Parse<fullPrecision ? kParseFullPrecisionFlag : 0>(s, h).Code()); |
| 366 |
EXPECT_EQ(1u, h.step_); |
| 367 |
internal::Double a(h.actual_); |
| 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 |
| 375 |
EXPECT_DOUBLE_EQ(d.Value(), h.actual_); |
| 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); |
| 614 |
ParseDoubleHandler h; |
| 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'; |
| 652 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, n1e309, 0u, 310u); |
| 653 |
} |
| 654 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e309", 0u, 5u); |
| 655 |
|
| 656 |
// Miss fraction part in number. |
| 657 |
TEST_NUMBER_ERROR(kParseErrorNumberMissFraction, "1.", 2u, 2u); |
| 658 |
TEST_NUMBER_ERROR(kParseErrorNumberMissFraction, "1.a", 2u, 2u); |
| 659 |
|
| 660 |
// Miss exponent in number. |
| 661 |
TEST_NUMBER_ERROR(kParseErrorNumberMissExponent, "1e", 2u, 2u); |
| 662 |
TEST_NUMBER_ERROR(kParseErrorNumberMissExponent, "1e_", 2u, 2u); |
| 663 |
|
| 664 |
// Issue 849 |
| 665 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.8e308", 0u, 7u); |
| 666 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "5e308", 0u, 5u); |
| 667 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e309", 0u, 5u); |
| 668 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.0e310", 0u, 7u); |
| 669 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1.00e310", 0u, 8u); |
| 670 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "-1.8e308", 0u, 8u); |
| 671 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "-1e309", 0u, 6u); |
| 672 |
|
| 673 |
// Issue 1253 |
| 674 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "2e308", 0u, 5u); |
| 675 |
|
| 676 |
// Issue 1259 |
| 677 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, |
| 678 |
"88474320368547737236837236775298547354737253685475547552933720368546854775297525" |
| 679 |
"29337203685468547770151233720097201372368547312337203687203685423685123372036872" |
| 680 |
"03685473724737236837236775297525854775297525472975254737236873720170151235473783" |
| 681 |
"7236737247372368772473723683723456789012E66", 0u, 283u); |
| 682 |
|
| 683 |
#if 0 |
| 684 |
// Test (length + exponent) overflow |
| 685 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+2147483647", 0u, 13u); |
| 686 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+9223372036854775807", 0u, 22u); |
| 687 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+10000", 0u, 8u); |
| 688 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, "1e+50000", 0u, 8u); |
| 689 |
#endif |
| 690 |
|
| 691 |
// 9007199254740992 * 2^971 ("infinity") |
| 692 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, |
| 693 |
"1.797693134862315907729305190789024733617976978942306572734300811577326758055009" |
| 694 |
"63132708477322407536021120113879871393357658789768814416622492847430639474124377" |
| 695 |
"76789342486548527630221960124609411945308295208500576883815068234246288147391311" |
| 696 |
"0540827237163350510684586298239947245938479716304835356329624224137216e+308", 0u, 315u); |
| 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. |
| 704 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, |
| 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", 0u, 1125u); |
| 719 |
// ...round up |
| 720 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, |
| 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", 0u, 1205u); |
| 736 |
} |
| 737 |
|
| 738 |
TEST_NUMBER_ERROR(kParseErrorNumberTooBig, |
| 739 |
"10000000000000000000000000000000000000000000000000000000000000000000000000000000" |
| 740 |
"00000000000000000000000000000000000000000000000000000000000000000000000000000000" |
| 741 |
"00000000000000000000000000000000000000000000000000000000000000000000000000000000" |
| 742 |
"0000000000000000000000000000000000000000000000000000000000000000000001", 0u, 310u); |
| 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 |
|
| 760 |
ParseStringHandler(const ParseStringHandler&); |
| 761 |
ParseStringHandler& operator=(const ParseStringHandler&); |
| 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"; |
| 859 |
ParseStringHandler<UTF8<> > h; |
| 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"; |
| 870 |
GenericStringStream<UTF8<> > is(x); |
| 871 |
GenericReader<UTF8<>, UTF16<> > reader; |
| 872 |
ParseStringHandler<UTF16<> > h; |
| 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"; |
| 881 |
GenericStringStream<UTF8<> > is(x); |
| 882 |
GenericReader<UTF8<>, UTF16<> > reader; |
| 883 |
ParseStringHandler<UTF16<> > h; |
| 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\""); |
| 891 |
ParseStringHandler<UTF8<> > h; |
| 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) { |
| 900 |
GenericStringStream<Encoding> s(str); |
| 901 |
BaseReaderHandler<Encoding> h; |
| 902 |
GenericReader<Encoding, Encoding> reader; |
| 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. |
| 934 |
TEST_STRING_ERROR(kParseErrorStringEscapeInvalid, "[\"\\a\"]", 2u, 3u); |
| 935 |
|
| 936 |
// Incorrect hex digit after \\u escape in string. |
| 937 |
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uABCG\"]", 2u, 7u); |
| 938 |
|
| 939 |
// Quotation in \\u escape in string (Issue #288) |
| 940 |
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uaaa\"]", 2u, 7u); |
| 941 |
TEST_STRING_ERROR(kParseErrorStringUnicodeEscapeInvalidHex, "[\"\\uD800\\uFFF\"]", 2u, 13u); |
| 942 |
|
| 943 |
// The surrogate pair in string is invalid. |
| 944 |
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800X\"]", 2u, 8u); |
| 945 |
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\uD800\\uFFFF\"]", 2u, 14u); |
| 946 |
|
| 947 |
// Single low surrogate pair in string is invalid. |
| 948 |
TEST_STRING_ERROR(kParseErrorStringUnicodeSurrogateInvalid, "[\"\\udc4d\"]", 2u, 8u); |
| 949 |
|
| 950 |
// Missing a closing quotation mark in string. |
| 951 |
TEST_STRING_ERROR(kParseErrorStringMissQuotationMark, "[\"Test]", 7u, 7u); |
| 952 |
|
| 953 |
// http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt |
| 954 |
|
| 955 |
// 3 Malformed sequences |
| 956 |
|
| 957 |
// 3.1 Unexpected continuation bytes |
| 958 |
{ |
| 959 |
char e[] = { '[', '\"', 0, '\"', ']', '\0' }; |
| 960 |
for (unsigned char c = 0x80u; c <= 0xBFu; c++) { |
| 961 |
e[2] = static_cast<char>(c); |
| 962 |
ParseErrorCode error = TestString<UTF8<> >(e); |
| 963 |
EXPECT_EQ(kParseErrorStringInvalidEncoding, error); |
| 964 |
if (error != kParseErrorStringInvalidEncoding) |
| 965 |
std::cout << static_cast<unsigned>(c) << std::endl; |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
// 3.2 Lonely start characters, 3.5 Impossible bytes |
| 970 |
{ |
| 971 |
char e[] = { '[', '\"', 0, ' ', '\"', ']', '\0' }; |
| 972 |
for (unsigned c = 0xC0u; c <= 0xFFu; c++) { |
| 973 |
e[2] = static_cast<char>(c); |
| 974 |
unsigned streamPos; |
| 975 |
if (c <= 0xC1u) |
| 976 |
streamPos = 3; // 0xC0 - 0xC1 |
| 977 |
else if (c <= 0xDFu) |
| 978 |
streamPos = 4; // 0xC2 - 0xDF |
| 979 |
else if (c <= 0xEFu) |
| 980 |
streamPos = 5; // 0xE0 - 0xEF |
| 981 |
else if (c <= 0xF4u) |
| 982 |
streamPos = 6; // 0xF0 - 0xF4 |
| 983 |
else |
| 984 |
streamPos = 3; // 0xF5 - 0xFF |
| 985 |
TEST_STRING_ERROR(kParseErrorStringInvalidEncoding, e, 2u, streamPos); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
// 4 Overlong sequences |
| 990 |
|
| 991 |
// 4.1 Examples of an overlong ASCII character |
| 992 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0xAFu, '\"', ']', '\0')); |
| 993 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0xAFu, '\"', ']', '\0')); |
| 994 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0xAFu, '\"', ']', '\0')); |
| 995 |
|
| 996 |
// 4.2 Maximum overlong sequences |
| 997 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC1u, 0xBFu, '\"', ']', '\0')); |
| 998 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x9Fu, 0xBFu, '\"', ']', '\0')); |
| 999 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x8Fu, 0xBFu, 0xBFu, '\"', ']', '\0')); |
| 1000 |
|
| 1001 |
// 4.3 Overlong representation of the NUL character |
| 1002 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0x80u, '\"', ']', '\0')); |
| 1003 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0x80u, '\"', ']', '\0')); |
| 1004 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0x80u, '\"', ']', '\0')); |
| 1005 |
|
| 1006 |
// 5 Illegal code positions |
| 1007 |
|
| 1008 |
// 5.1 Single UTF-16 surrogates |
| 1009 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xA0u, 0x80u, '\"', ']', '\0')); |
| 1010 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xADu, 0xBFu, '\"', ']', '\0')); |
| 1011 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xAEu, 0x80u, '\"', ']', '\0')); |
| 1012 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xAFu, 0xBFu, '\"', ']', '\0')); |
| 1013 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xB0u, 0x80u, '\"', ']', '\0')); |
| 1014 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xBEu, 0x80u, '\"', ']', '\0')); |
| 1015 |
TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xEDu, 0xBFu, 0xBFu, '\"', ']', '\0')); |
| 1016 |
|
| 1017 |
// Malform UTF-16 sequences |
| 1018 |
TEST_STRINGENCODING_ERROR(UTF16<>, UTF8<>, wchar_t, ARRAY('[', '\"', 0xDC00, 0xDC00, '\"', ']', '\0')); |
| 1019 |
TEST_STRINGENCODING_ERROR(UTF16<>, UTF8<>, wchar_t, ARRAY('[', '\"', 0xD800, 0xD800, '\"', ']', '\0')); |
| 1020 |
|
| 1021 |
// Malform UTF-32 sequence |
| 1022 |
TEST_STRINGENCODING_ERROR(UTF32<>, UTF8<>, unsigned, ARRAY('[', '\"', 0x110000, '\"', ']', '\0')); |
| 1023 |
|
| 1024 |
// Malform ASCII sequence |
| 1025 |
TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80u), '\"', ']', '\0')); |
| 1026 |
TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x01u), '\"', ']', '\0')); |
| 1027 |
TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x1Cu), '\"', ']', '\0')); |
| 1028 |
|
| 1029 |
#undef ARRAY |
| 1030 |
#undef TEST_STRINGARRAY_ERROR |
| 1031 |
} |
| 1032 |
|
| 1033 |
template <unsigned count> |
| 1034 |
struct ParseArrayHandler : BaseReaderHandler<UTF8<>, ParseArrayHandler<count> > { |
| 1035 |
ParseArrayHandler() : step_(0) {} |
| 1036 |
|
| 1037 |
bool Default() { ADD_FAILURE(); return false; } |
| 1038 |
bool Uint(unsigned i) { EXPECT_EQ(step_, i); step_++; return true; } |
| 1039 |
bool StartArray() { EXPECT_EQ(0u, step_); step_++; return true; } |
| 1040 |
bool EndArray(SizeType) { step_++; return true; } |
| 1041 |
|
| 1042 |
unsigned step_; |
| 1043 |
}; |
| 1044 |
|
| 1045 |
TEST(Reader, ParseEmptyArray) { |
| 1046 |
char *json = StrDup("[ ] "); |
| 1047 |
InsituStringStream s(json); |
| 1048 |
ParseArrayHandler<0> h; |
| 1049 |
Reader reader; |
| 1050 |
reader.Parse(s, h); |
| 1051 |
EXPECT_EQ(2u, h.step_); |
| 1052 |
free(json); |
| 1053 |
} |
| 1054 |
|
| 1055 |
TEST(Reader, ParseArray) { |
| 1056 |
char *json = StrDup("[1, 2, 3, 4]"); |
| 1057 |
InsituStringStream s(json); |
| 1058 |
ParseArrayHandler<4> h; |
| 1059 |
Reader reader; |
| 1060 |
reader.Parse(s, h); |
| 1061 |
EXPECT_EQ(6u, h.step_); |
| 1062 |
free(json); |
| 1063 |
} |
| 1064 |
|
| 1065 |
TEST(Reader, ParseArray_Error) { |
| 1066 |
#define TEST_ARRAY_ERROR(errorCode, str, errorOffset) \ |
| 1067 |
{ \ |
| 1068 |
unsigned streamPos = errorOffset; \ |
| 1069 |
char buffer[1001]; \ |
| 1070 |
strncpy(buffer, str, 1000); \ |
| 1071 |
InsituStringStream s(buffer); \ |
| 1072 |
BaseReaderHandler<> h; \ |
| 1073 |
GenericReader<UTF8<>, UTF8<>, CrtAllocator> reader; \ |
| 1074 |
EXPECT_FALSE(reader.Parse(s, h)); \ |
| 1075 |
EXPECT_EQ(errorCode, reader.GetParseErrorCode());\ |
| 1076 |
EXPECT_EQ(errorOffset, reader.GetErrorOffset());\ |
| 1077 |
EXPECT_EQ(streamPos, s.Tell());\ |
| 1078 |
} |
| 1079 |
|
| 1080 |
// Missing a comma or ']' after an array element. |
| 1081 |
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1", 2u); |
| 1082 |
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1}", 2u); |
| 1083 |
TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1 2]", 3u); |
| 1084 |
|
| 1085 |
// Array cannot have a trailing comma (without kParseTrailingCommasFlag); |
| 1086 |
// a value must follow a comma |
| 1087 |
TEST_ARRAY_ERROR(kParseErrorValueInvalid, "[1,]", 3u); |
| 1088 |
|
| 1089 |
#undef TEST_ARRAY_ERROR |
| 1090 |
} |
| 1091 |
|
| 1092 |
struct ParseObjectHandler : BaseReaderHandler<UTF8<>, ParseObjectHandler> { |
| 1093 |
ParseObjectHandler() : step_(0) {} |
| 1094 |
|
| 1095 |
bool Default() { ADD_FAILURE(); return false; } |
| 1096 |
bool Null() { EXPECT_EQ(8u, step_); step_++; return true; } |
| 1097 |
bool Bool(bool b) { |
| 1098 |
switch(step_) { |
| 1099 |
case 4: EXPECT_TRUE(b); step_++; return true; |
| 1100 |
case 6: EXPECT_FALSE(b); step_++; return true; |
| 1101 |
default: ADD_FAILURE(); return false; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
bool Int(int i) { |
| 1105 |
switch(step_) { |
| 1106 |
case 10: EXPECT_EQ(123, i); step_++; return true; |
| 1107 |
case 15: EXPECT_EQ(1, i); step_++; return true; |
| 1108 |
case 16: EXPECT_EQ(2, i); step_++; return true; |
| 1109 |
case 17: EXPECT_EQ(3, i); step_++; return true; |
| 1110 |
default: ADD_FAILURE(); return false; |
| 1111 |
} |
| 1112 |
} |
| 1113 |
bool Uint(unsigned i) { return Int(static_cast<int>(i)); } |
| 1114 |
bool Double(double d) { EXPECT_EQ(12u, step_); EXPECT_DOUBLE_EQ(3.1416, d); step_++; return true; } |
| 1115 |
bool String(const char* str, size_t, bool) { |
| 1116 |
switch(step_) { |
| 1117 |
case 1: EXPECT_STREQ("hello", str); step_++; return true; |
| 1118 |
case 2: EXPECT_STREQ("world", str); step_++; return true; |
| 1119 |
case 3: EXPECT_STREQ("t", str); step_++; return true; |
| 1120 |
case 5: EXPECT_STREQ("f", str); step_++; return true; |
| 1121 |
case 7: EXPECT_STREQ("n", str); step_++; return true; |
| 1122 |
case 9: EXPECT_STREQ("i", str); step_++; return true; |
| 1123 |
case 11: EXPECT_STREQ("pi", str); step_++; return true; |
| 1124 |
case 13: EXPECT_STREQ("a", str); step_++; return true; |
| 1125 |
default: ADD_FAILURE(); return false; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
bool StartObject() { EXPECT_EQ(0u, step_); step_++; return true; } |
| 1129 |
bool EndObject(SizeType memberCount) { EXPECT_EQ(19u, step_); EXPECT_EQ(7u, memberCount); step_++; return true; } |
| 1130 |
bool StartArray() { EXPECT_EQ(14u, step_); step_++; return true; } |
| 1131 |
bool EndArray(SizeType elementCount) { EXPECT_EQ(18u, step_); EXPECT_EQ(3u, elementCount); step_++; return true; } |
| 1132 |
|
| 1133 |
unsigned step_; |
| 1134 |
}; |
| 1135 |
|
| 1136 |
TEST(Reader, ParseObject) { |
| 1137 |
const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } "; |
| 1138 |
|
| 1139 |
// Insitu |
| 1140 |
{ |
| 1141 |
char* json2 = StrDup(json); |
| 1142 |
InsituStringStream s(json2); |
| 1143 |
ParseObjectHandler h; |
| 1144 |
Reader reader; |
| 1145 |
reader.Parse<kParseInsituFlag>(s, h); |
| 1146 |
EXPECT_EQ(20u, h.step_); |
| 1147 |
free(json2); |
| 1148 |
} |
| 1149 |
|
| 1150 |
// Normal |
| 1151 |
{ |
| 1152 |
StringStream s(json); |
| 1153 |
ParseObjectHandler h; |
| 1154 |
Reader reader; |
| 1155 |
reader.Parse(s, h); |
| 1156 |
EXPECT_EQ(20u, h.step_); |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
struct ParseEmptyObjectHandler : BaseReaderHandler<UTF8<>, ParseEmptyObjectHandler> { |
| 1161 |
ParseEmptyObjectHandler() : step_(0) {} |
| 1162 |
|
| 1163 |
bool Default() { ADD_FAILURE(); return false; } |
| 1164 |
bool StartObject() { EXPECT_EQ(0u, step_); step_++; return true; } |
| 1165 |
bool EndObject(SizeType) { EXPECT_EQ(1u, step_); step_++; return true; } |
| 1166 |
|
| 1167 |
unsigned step_; |
| 1168 |
}; |
| 1169 |
|
| 1170 |
TEST(Reader, Parse_EmptyObject) { |
| 1171 |
StringStream s("{ } "); |
| 1172 |
ParseEmptyObjectHandler h; |
| 1173 |
Reader reader; |
| 1174 |
reader.Parse(s, h); |
| 1175 |
EXPECT_EQ(2u, h.step_); |
| 1176 |
} |
| 1177 |
|
| 1178 |
struct ParseMultipleRootHandler : BaseReaderHandler<UTF8<>, ParseMultipleRootHandler> { |
| 1179 |
ParseMultipleRootHandler() : step_(0) {} |
| 1180 |
|
| 1181 |
bool Default() { ADD_FAILURE(); return false; } |
| 1182 |
bool StartObject() { EXPECT_EQ(0u, step_); step_++; return true; } |
| 1183 |
bool EndObject(SizeType) { EXPECT_EQ(1u, step_); step_++; return true; } |
| 1184 |
bool StartArray() { EXPECT_EQ(2u, step_); step_++; return true; } |
| 1185 |
bool EndArray(SizeType) { EXPECT_EQ(3u, step_); step_++; return true; } |
| 1186 |
|
| 1187 |
unsigned step_; |
| 1188 |
}; |
| 1189 |
|
| 1190 |
template <unsigned parseFlags> |
| 1191 |
void TestMultipleRoot() { |
| 1192 |
StringStream s("{}[] a"); |
| 1193 |
ParseMultipleRootHandler h; |
| 1194 |
Reader reader; |
| 1195 |
EXPECT_TRUE(reader.Parse<parseFlags>(s, h)); |
| 1196 |
EXPECT_EQ(2u, h.step_); |
| 1197 |
EXPECT_TRUE(reader.Parse<parseFlags>(s, h)); |
| 1198 |
EXPECT_EQ(4u, h.step_); |
| 1199 |
EXPECT_EQ(' ', s.Take()); |
| 1200 |
EXPECT_EQ('a', s.Take()); |
| 1201 |
} |
| 1202 |
|
| 1203 |
TEST(Reader, Parse_MultipleRoot) { |
| 1204 |
TestMultipleRoot<kParseStopWhenDoneFlag>(); |
| 1205 |
} |
| 1206 |
|
| 1207 |
TEST(Reader, ParseIterative_MultipleRoot) { |
| 1208 |
TestMultipleRoot<kParseIterativeFlag | kParseStopWhenDoneFlag>(); |
| 1209 |
} |
| 1210 |
|
| 1211 |
template <unsigned parseFlags> |
| 1212 |
void TestInsituMultipleRoot() { |
| 1213 |
char* buffer = strdup("{}[] a"); |
| 1214 |
InsituStringStream s(buffer); |
| 1215 |
ParseMultipleRootHandler h; |
| 1216 |
Reader reader; |
| 1217 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag | parseFlags>(s, h)); |
| 1218 |
EXPECT_EQ(2u, h.step_); |
| 1219 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag | parseFlags>(s, h)); |
| 1220 |
EXPECT_EQ(4u, h.step_); |
| 1221 |
EXPECT_EQ(' ', s.Take()); |
| 1222 |
EXPECT_EQ('a', s.Take()); |
| 1223 |
free(buffer); |
| 1224 |
} |
| 1225 |
|
| 1226 |
TEST(Reader, ParseInsitu_MultipleRoot) { |
| 1227 |
TestInsituMultipleRoot<kParseStopWhenDoneFlag>(); |
| 1228 |
} |
| 1229 |
|
| 1230 |
TEST(Reader, ParseInsituIterative_MultipleRoot) { |
| 1231 |
TestInsituMultipleRoot<kParseIterativeFlag | kParseStopWhenDoneFlag>(); |
| 1232 |
} |
| 1233 |
|
| 1234 |
#define TEST_ERROR(errorCode, str, errorOffset) \ |
| 1235 |
{ \ |
| 1236 |
unsigned streamPos = errorOffset; \ |
| 1237 |
char buffer[1001]; \ |
| 1238 |
strncpy(buffer, str, 1000); \ |
| 1239 |
InsituStringStream s(buffer); \ |
| 1240 |
BaseReaderHandler<> h; \ |
| 1241 |
Reader reader; \ |
| 1242 |
EXPECT_FALSE(reader.Parse(s, h)); \ |
| 1243 |
EXPECT_EQ(errorCode, reader.GetParseErrorCode());\ |
| 1244 |
EXPECT_EQ(errorOffset, reader.GetErrorOffset());\ |
| 1245 |
EXPECT_EQ(streamPos, s.Tell());\ |
| 1246 |
} |
| 1247 |
|
| 1248 |
TEST(Reader, ParseDocument_Error) { |
| 1249 |
// The document is empty. |
| 1250 |
TEST_ERROR(kParseErrorDocumentEmpty, "", 0u); |
| 1251 |
TEST_ERROR(kParseErrorDocumentEmpty, " ", 1u); |
| 1252 |
TEST_ERROR(kParseErrorDocumentEmpty, " \n", 2u); |
| 1253 |
|
| 1254 |
// The document root must not follow by other values. |
| 1255 |
TEST_ERROR(kParseErrorDocumentRootNotSingular, "[] 0", 3u); |
| 1256 |
TEST_ERROR(kParseErrorDocumentRootNotSingular, "{} 0", 3u); |
| 1257 |
TEST_ERROR(kParseErrorDocumentRootNotSingular, "null []", 5u); |
| 1258 |
TEST_ERROR(kParseErrorDocumentRootNotSingular, "0 {}", 2u); |
| 1259 |
} |
| 1260 |
|
| 1261 |
TEST(Reader, ParseValue_Error) { |
| 1262 |
// Invalid value. |
| 1263 |
TEST_ERROR(kParseErrorValueInvalid, "nulL", 3u); |
| 1264 |
TEST_ERROR(kParseErrorValueInvalid, "truE", 3u); |
| 1265 |
TEST_ERROR(kParseErrorValueInvalid, "falsE", 4u); |
| 1266 |
TEST_ERROR(kParseErrorValueInvalid, "a]", 0u); |
| 1267 |
TEST_ERROR(kParseErrorValueInvalid, ".1", 0u); |
| 1268 |
} |
| 1269 |
|
| 1270 |
TEST(Reader, ParseObject_Error) { |
| 1271 |
// Missing a name for object member. |
| 1272 |
TEST_ERROR(kParseErrorObjectMissName, "{1}", 1u); |
| 1273 |
TEST_ERROR(kParseErrorObjectMissName, "{:1}", 1u); |
| 1274 |
TEST_ERROR(kParseErrorObjectMissName, "{null:1}", 1u); |
| 1275 |
TEST_ERROR(kParseErrorObjectMissName, "{true:1}", 1u); |
| 1276 |
TEST_ERROR(kParseErrorObjectMissName, "{false:1}", 1u); |
| 1277 |
TEST_ERROR(kParseErrorObjectMissName, "{1:1}", 1u); |
| 1278 |
TEST_ERROR(kParseErrorObjectMissName, "{[]:1}", 1u); |
| 1279 |
TEST_ERROR(kParseErrorObjectMissName, "{{}:1}", 1u); |
| 1280 |
TEST_ERROR(kParseErrorObjectMissName, "{xyz:1}", 1u); |
| 1281 |
|
| 1282 |
// Missing a colon after a name of object member. |
| 1283 |
TEST_ERROR(kParseErrorObjectMissColon, "{\"a\" 1}", 5u); |
| 1284 |
TEST_ERROR(kParseErrorObjectMissColon, "{\"a\",1}", 4u); |
| 1285 |
|
| 1286 |
// Must be a comma or '}' after an object member |
| 1287 |
TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{\"a\":1]", 6u); |
| 1288 |
|
| 1289 |
// Object cannot have a trailing comma (without kParseTrailingCommasFlag); |
| 1290 |
// an object member name must follow a comma |
| 1291 |
TEST_ERROR(kParseErrorObjectMissName, "{\"a\":1,}", 7u); |
| 1292 |
|
| 1293 |
// This tests that MemoryStream is checking the length in Peek(). |
| 1294 |
{ |
| 1295 |
MemoryStream ms("{\"a\"", 1); |
| 1296 |
BaseReaderHandler<> h; |
| 1297 |
Reader reader; |
| 1298 |
EXPECT_FALSE(reader.Parse<kParseStopWhenDoneFlag>(ms, h)); |
| 1299 |
EXPECT_EQ(kParseErrorObjectMissName, reader.GetParseErrorCode()); |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
#undef TEST_ERROR |
| 1304 |
|
| 1305 |
TEST(Reader, SkipWhitespace) { |
| 1306 |
StringStream ss(" A \t\tB\n \n\nC\r\r \rD \t\n\r E"); |
| 1307 |
const char* expected = "ABCDE"; |
| 1308 |
for (size_t i = 0; i < 5; i++) { |
| 1309 |
SkipWhitespace(ss); |
| 1310 |
EXPECT_EQ(expected[i], ss.Take()); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
// Test implementing a stream without copy stream optimization. |
| 1315 |
// Clone from GenericStringStream except that copy constructor is disabled. |
| 1316 |
template <typename Encoding> |
| 1317 |
class CustomStringStream { |
| 1318 |
public: |
| 1319 |
typedef typename Encoding::Ch Ch; |
| 1320 |
|
| 1321 |
CustomStringStream(const Ch *src) : src_(src), head_(src) {} |
| 1322 |
|
| 1323 |
Ch Peek() const { return *src_; } |
| 1324 |
Ch Take() { return *src_++; } |
| 1325 |
size_t Tell() const { return static_cast<size_t>(src_ - head_); } |
| 1326 |
|
| 1327 |
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } |
| 1328 |
void Put(Ch) { RAPIDJSON_ASSERT(false); } |
| 1329 |
void Flush() { RAPIDJSON_ASSERT(false); } |
| 1330 |
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } |
| 1331 |
|
| 1332 |
private: |
| 1333 |
// Prohibit copy constructor & assignment operator. |
| 1334 |
CustomStringStream(const CustomStringStream&); |
| 1335 |
CustomStringStream& operator=(const CustomStringStream&); |
| 1336 |
|
| 1337 |
const Ch* src_; //!< Current read position. |
| 1338 |
const Ch* head_; //!< Original head of the string. |
| 1339 |
}; |
| 1340 |
|
| 1341 |
// If the following code is compiled, it should generate compilation error as predicted. |
| 1342 |
// Because CustomStringStream<> is not copyable via making copy constructor private. |
| 1343 |
#if 0 |
| 1344 |
namespace rapidjson { |
| 1345 |
|
| 1346 |
template <typename Encoding> |
| 1347 |
struct StreamTraits<CustomStringStream<Encoding> > { |
| 1348 |
enum { copyOptimization = 1 }; |
| 1349 |
}; |
| 1350 |
|
| 1351 |
} // namespace rapidjson |
| 1352 |
#endif |
| 1353 |
|
| 1354 |
TEST(Reader, CustomStringStream) { |
| 1355 |
const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } "; |
| 1356 |
CustomStringStream<UTF8<char> > s(json); |
| 1357 |
ParseObjectHandler h; |
| 1358 |
Reader reader; |
| 1359 |
reader.Parse(s, h); |
| 1360 |
EXPECT_EQ(20u, h.step_); |
| 1361 |
} |
| 1362 |
|
| 1363 |
#include <sstream> |
| 1364 |
|
| 1365 |
class IStreamWrapper { |
| 1366 |
public: |
| 1367 |
typedef char Ch; |
| 1368 |
|
| 1369 |
IStreamWrapper(std::istream& is) : is_(is) {} |
| 1370 |
|
| 1371 |
Ch Peek() const { |
| 1372 |
int c = is_.peek(); |
| 1373 |
return c == std::char_traits<char>::eof() ? '\0' : static_cast<Ch>(c); |
| 1374 |
} |
| 1375 |
|
| 1376 |
Ch Take() { |
| 1377 |
int c = is_.get(); |
| 1378 |
return c == std::char_traits<char>::eof() ? '\0' : static_cast<Ch>(c); |
| 1379 |
} |
| 1380 |
|
| 1381 |
size_t Tell() const { return static_cast<size_t>(is_.tellg()); } |
| 1382 |
|
| 1383 |
Ch* PutBegin() { assert(false); return 0; } |
| 1384 |
void Put(Ch) { assert(false); } |
| 1385 |
void Flush() { assert(false); } |
| 1386 |
size_t PutEnd(Ch*) { assert(false); return 0; } |
| 1387 |
|
| 1388 |
private: |
| 1389 |
IStreamWrapper(const IStreamWrapper&); |
| 1390 |
IStreamWrapper& operator=(const IStreamWrapper&); |
| 1391 |
|
| 1392 |
std::istream& is_; |
| 1393 |
}; |
| 1394 |
|
| 1395 |
class WIStreamWrapper { |
| 1396 |
public: |
| 1397 |
typedef wchar_t Ch; |
| 1398 |
|
| 1399 |
WIStreamWrapper(std::wistream& is) : is_(is) {} |
| 1400 |
|
| 1401 |
Ch Peek() const { |
| 1402 |
unsigned c = is_.peek(); |
| 1403 |
return c == std::char_traits<wchar_t>::eof() ? Ch('\0') : static_cast<Ch>(c); |
| 1404 |
} |
| 1405 |
|
| 1406 |
Ch Take() { |
| 1407 |
unsigned c = is_.get(); |
| 1408 |
return c == std::char_traits<wchar_t>::eof() ? Ch('\0') : static_cast<Ch>(c); |
| 1409 |
} |
| 1410 |
|
| 1411 |
size_t Tell() const { return static_cast<size_t>(is_.tellg()); } |
| 1412 |
|
| 1413 |
Ch* PutBegin() { assert(false); return 0; } |
| 1414 |
void Put(Ch) { assert(false); } |
| 1415 |
void Flush() { assert(false); } |
| 1416 |
size_t PutEnd(Ch*) { assert(false); return 0; } |
| 1417 |
|
| 1418 |
private: |
| 1419 |
WIStreamWrapper(const WIStreamWrapper&); |
| 1420 |
WIStreamWrapper& operator=(const WIStreamWrapper&); |
| 1421 |
|
| 1422 |
std::wistream& is_; |
| 1423 |
}; |
| 1424 |
|
| 1425 |
TEST(Reader, Parse_IStreamWrapper_StringStream) { |
| 1426 |
const char* json = "[1,2,3,4]"; |
| 1427 |
|
| 1428 |
std::stringstream ss(json); |
| 1429 |
IStreamWrapper is(ss); |
| 1430 |
|
| 1431 |
Reader reader; |
| 1432 |
ParseArrayHandler<4> h; |
| 1433 |
reader.Parse(is, h); |
| 1434 |
EXPECT_FALSE(reader.HasParseError()); |
| 1435 |
} |
| 1436 |
|
| 1437 |
// Test iterative parsing. |
| 1438 |
|
| 1439 |
#define TESTERRORHANDLING(text, errorCode, offset)\ |
| 1440 |
{\ |
| 1441 |
unsigned streamPos = offset; \ |
| 1442 |
StringStream json(text); \ |
| 1443 |
BaseReaderHandler<> handler; \ |
| 1444 |
Reader reader; \ |
| 1445 |
reader.Parse<kParseIterativeFlag>(json, handler); \ |
| 1446 |
EXPECT_TRUE(reader.HasParseError()); \ |
| 1447 |
EXPECT_EQ(errorCode, reader.GetParseErrorCode()); \ |
| 1448 |
EXPECT_EQ(offset, reader.GetErrorOffset()); \ |
| 1449 |
EXPECT_EQ(streamPos, json.Tell()); \ |
| 1450 |
} |
| 1451 |
|
| 1452 |
TEST(Reader, IterativeParsing_ErrorHandling) { |
| 1453 |
TESTERRORHANDLING("{\"a\": a}", kParseErrorValueInvalid, 6u); |
| 1454 |
|
| 1455 |
TESTERRORHANDLING("", kParseErrorDocumentEmpty, 0u); |
| 1456 |
TESTERRORHANDLING("{}{}", kParseErrorDocumentRootNotSingular, 2u); |
| 1457 |
|
| 1458 |
TESTERRORHANDLING("{1}", kParseErrorObjectMissName, 1u); |
| 1459 |
TESTERRORHANDLING("{\"a\", 1}", kParseErrorObjectMissColon, 4u); |
| 1460 |
TESTERRORHANDLING("{\"a\"}", kParseErrorObjectMissColon, 4u); |
| 1461 |
TESTERRORHANDLING("{\"a\": 1", kParseErrorObjectMissCommaOrCurlyBracket, 7u); |
| 1462 |
TESTERRORHANDLING("[1 2 3]", kParseErrorArrayMissCommaOrSquareBracket, 3u); |
| 1463 |
TESTERRORHANDLING("{\"a: 1", kParseErrorStringMissQuotationMark, 6u); |
| 1464 |
TESTERRORHANDLING("{\"a\":}", kParseErrorValueInvalid, 5u); |
| 1465 |
TESTERRORHANDLING("{\"a\":]", kParseErrorValueInvalid, 5u); |
| 1466 |
TESTERRORHANDLING("[1,2,}", kParseErrorValueInvalid, 5u); |
| 1467 |
TESTERRORHANDLING("[}]", kParseErrorValueInvalid, 1u); |
| 1468 |
TESTERRORHANDLING("[,]", kParseErrorValueInvalid, 1u); |
| 1469 |
TESTERRORHANDLING("[1,,]", kParseErrorValueInvalid, 3u); |
| 1470 |
|
| 1471 |
// Trailing commas are not allowed without kParseTrailingCommasFlag |
| 1472 |
TESTERRORHANDLING("{\"a\": 1,}", kParseErrorObjectMissName, 8u); |
| 1473 |
TESTERRORHANDLING("[1,2,3,]", kParseErrorValueInvalid, 7u); |
| 1474 |
|
| 1475 |
// Any JSON value can be a valid root element in RFC7159. |
| 1476 |
TESTERRORHANDLING("\"ab", kParseErrorStringMissQuotationMark, 3u); |
| 1477 |
TESTERRORHANDLING("truE", kParseErrorValueInvalid, 3u); |
| 1478 |
TESTERRORHANDLING("False", kParseErrorValueInvalid, 0u); |
| 1479 |
TESTERRORHANDLING("true, false", kParseErrorDocumentRootNotSingular, 4u); |
| 1480 |
TESTERRORHANDLING("false, false", kParseErrorDocumentRootNotSingular, 5u); |
| 1481 |
TESTERRORHANDLING("nulL", kParseErrorValueInvalid, 3u); |
| 1482 |
TESTERRORHANDLING("null , null", kParseErrorDocumentRootNotSingular, 5u); |
| 1483 |
TESTERRORHANDLING("1a", kParseErrorDocumentRootNotSingular, 1u); |
| 1484 |
} |
| 1485 |
|
| 1486 |
template<typename Encoding = UTF8<> > |
| 1487 |
struct IterativeParsingReaderHandler { |
| 1488 |
typedef typename Encoding::Ch Ch; |
| 1489 |
|
| 1490 |
const static uint32_t LOG_NULL = 0x10000000; |
| 1491 |
const static uint32_t LOG_BOOL = 0x20000000; |
| 1492 |
const static uint32_t LOG_INT = 0x30000000; |
| 1493 |
const static uint32_t LOG_UINT = 0x40000000; |
| 1494 |
const static uint32_t LOG_INT64 = 0x50000000; |
| 1495 |
const static uint32_t LOG_UINT64 = 0x60000000; |
| 1496 |
const static uint32_t LOG_DOUBLE = 0x70000000; |
| 1497 |
const static uint32_t LOG_STRING = 0x80000000; |
| 1498 |
const static uint32_t LOG_STARTOBJECT = 0x90000000; |
| 1499 |
const static uint32_t LOG_KEY = 0xA0000000; |
| 1500 |
const static uint32_t LOG_ENDOBJECT = 0xB0000000; |
| 1501 |
const static uint32_t LOG_STARTARRAY = 0xC0000000; |
| 1502 |
const static uint32_t LOG_ENDARRAY = 0xD0000000; |
| 1503 |
|
| 1504 |
const static size_t LogCapacity = 256; |
| 1505 |
uint32_t Logs[LogCapacity]; |
| 1506 |
size_t LogCount; |
| 1507 |
|
| 1508 |
IterativeParsingReaderHandler() : LogCount(0) { |
| 1509 |
} |
| 1510 |
|
| 1511 |
bool Null() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_NULL; return true; } |
| 1512 |
|
| 1513 |
bool Bool(bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_BOOL; return true; } |
| 1514 |
|
| 1515 |
bool Int(int) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_INT; return true; } |
| 1516 |
|
| 1517 |
bool Uint(unsigned) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_INT; return true; } |
| 1518 |
|
| 1519 |
bool Int64(int64_t) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_INT64; return true; } |
| 1520 |
|
| 1521 |
bool Uint64(uint64_t) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_UINT64; return true; } |
| 1522 |
|
| 1523 |
bool Double(double) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_DOUBLE; return true; } |
| 1524 |
|
| 1525 |
bool RawNumber(const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STRING; return true; } |
| 1526 |
|
| 1527 |
bool String(const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STRING; return true; } |
| 1528 |
|
| 1529 |
bool StartObject() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STARTOBJECT; return true; } |
| 1530 |
|
| 1531 |
bool Key (const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_KEY; return true; } |
| 1532 |
|
| 1533 |
bool EndObject(SizeType c) { |
| 1534 |
RAPIDJSON_ASSERT(LogCount < LogCapacity); |
| 1535 |
RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0); |
| 1536 |
Logs[LogCount++] = LOG_ENDOBJECT | static_cast<uint32_t>(c); |
| 1537 |
return true; |
| 1538 |
} |
| 1539 |
|
| 1540 |
bool StartArray() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STARTARRAY; return true; } |
| 1541 |
|
| 1542 |
bool EndArray(SizeType c) { |
| 1543 |
RAPIDJSON_ASSERT(LogCount < LogCapacity); |
| 1544 |
RAPIDJSON_ASSERT((static_cast<uint32_t>(c) & 0xF0000000) == 0); |
| 1545 |
Logs[LogCount++] = LOG_ENDARRAY | static_cast<uint32_t>(c); |
| 1546 |
return true; |
| 1547 |
} |
| 1548 |
}; |
| 1549 |
|
| 1550 |
TEST(Reader, IterativeParsing_General) { |
| 1551 |
{ |
| 1552 |
StringStream is("[1, {\"k\": [1, 2]}, null, false, true, \"string\", 1.2]"); |
| 1553 |
Reader reader; |
| 1554 |
IterativeParsingReaderHandler<> handler; |
| 1555 |
|
| 1556 |
ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler); |
| 1557 |
|
| 1558 |
EXPECT_FALSE(r.IsError()); |
| 1559 |
EXPECT_FALSE(reader.HasParseError()); |
| 1560 |
|
| 1561 |
uint32_t e[] = { |
| 1562 |
handler.LOG_STARTARRAY, |
| 1563 |
handler.LOG_INT, |
| 1564 |
handler.LOG_STARTOBJECT, |
| 1565 |
handler.LOG_KEY, |
| 1566 |
handler.LOG_STARTARRAY, |
| 1567 |
handler.LOG_INT, |
| 1568 |
handler.LOG_INT, |
| 1569 |
handler.LOG_ENDARRAY | 2, |
| 1570 |
handler.LOG_ENDOBJECT | 1, |
| 1571 |
handler.LOG_NULL, |
| 1572 |
handler.LOG_BOOL, |
| 1573 |
handler.LOG_BOOL, |
| 1574 |
handler.LOG_STRING, |
| 1575 |
handler.LOG_DOUBLE, |
| 1576 |
handler.LOG_ENDARRAY | 7 |
| 1577 |
}; |
| 1578 |
|
| 1579 |
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount); |
| 1580 |
|
| 1581 |
for (size_t i = 0; i < handler.LogCount; ++i) { |
| 1582 |
EXPECT_EQ(e[i], handler.Logs[i]) << "i = " << i; |
| 1583 |
} |
| 1584 |
} |
| 1585 |
} |
| 1586 |
|
| 1587 |
TEST(Reader, IterativeParsing_Count) { |
| 1588 |
{ |
| 1589 |
StringStream is("[{}, {\"k\": 1}, [1], []]"); |
| 1590 |
Reader reader; |
| 1591 |
IterativeParsingReaderHandler<> handler; |
| 1592 |
|
| 1593 |
ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler); |
| 1594 |
|
| 1595 |
EXPECT_FALSE(r.IsError()); |
| 1596 |
EXPECT_FALSE(reader.HasParseError()); |
| 1597 |
|
| 1598 |
uint32_t e[] = { |
| 1599 |
handler.LOG_STARTARRAY, |
| 1600 |
handler.LOG_STARTOBJECT, |
| 1601 |
handler.LOG_ENDOBJECT | 0, |
| 1602 |
handler.LOG_STARTOBJECT, |
| 1603 |
handler.LOG_KEY, |
| 1604 |
handler.LOG_INT, |
| 1605 |
handler.LOG_ENDOBJECT | 1, |
| 1606 |
handler.LOG_STARTARRAY, |
| 1607 |
handler.LOG_INT, |
| 1608 |
handler.LOG_ENDARRAY | 1, |
| 1609 |
handler.LOG_STARTARRAY, |
| 1610 |
handler.LOG_ENDARRAY | 0, |
| 1611 |
handler.LOG_ENDARRAY | 4 |
| 1612 |
}; |
| 1613 |
|
| 1614 |
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount); |
| 1615 |
|
| 1616 |
for (size_t i = 0; i < handler.LogCount; ++i) { |
| 1617 |
EXPECT_EQ(e[i], handler.Logs[i]) << "i = " << i; |
| 1618 |
} |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
TEST(Reader, IterativePullParsing_General) { |
| 1623 |
{ |
| 1624 |
IterativeParsingReaderHandler<> handler; |
| 1625 |
uint32_t e[] = { |
| 1626 |
handler.LOG_STARTARRAY, |
| 1627 |
handler.LOG_INT, |
| 1628 |
handler.LOG_STARTOBJECT, |
| 1629 |
handler.LOG_KEY, |
| 1630 |
handler.LOG_STARTARRAY, |
| 1631 |
handler.LOG_INT, |
| 1632 |
handler.LOG_INT, |
| 1633 |
handler.LOG_ENDARRAY | 2, |
| 1634 |
handler.LOG_ENDOBJECT | 1, |
| 1635 |
handler.LOG_NULL, |
| 1636 |
handler.LOG_BOOL, |
| 1637 |
handler.LOG_BOOL, |
| 1638 |
handler.LOG_STRING, |
| 1639 |
handler.LOG_DOUBLE, |
| 1640 |
handler.LOG_ENDARRAY | 7 |
| 1641 |
}; |
| 1642 |
|
| 1643 |
StringStream is("[1, {\"k\": [1, 2]}, null, false, true, \"string\", 1.2]"); |
| 1644 |
Reader reader; |
| 1645 |
|
| 1646 |
reader.IterativeParseInit(); |
| 1647 |
while (!reader.IterativeParseComplete()) { |
| 1648 |
size_t oldLogCount = handler.LogCount; |
| 1649 |
EXPECT_TRUE(oldLogCount < sizeof(e) / sizeof(int)) << "overrun"; |
| 1650 |
|
| 1651 |
EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse fail"; |
| 1652 |
EXPECT_EQ(handler.LogCount, oldLogCount + 1) << "handler should be invoked exactly once each time"; |
| 1653 |
EXPECT_EQ(e[oldLogCount], handler.Logs[oldLogCount]) << "wrong event returned"; |
| 1654 |
} |
| 1655 |
|
| 1656 |
EXPECT_FALSE(reader.HasParseError()); |
| 1657 |
EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount) << "handler invoked wrong number of times"; |
| 1658 |
|
| 1659 |
// The handler should not be invoked when the JSON has been fully read, but it should not fail |
| 1660 |
size_t oldLogCount = handler.LogCount; |
| 1661 |
EXPECT_TRUE(reader.IterativeParseNext<kParseDefaultFlags>(is, handler)) << "parse-next past complete is allowed"; |
| 1662 |
EXPECT_EQ(handler.LogCount, oldLogCount) << "parse-next past complete should not invoke handler"; |
| 1663 |
EXPECT_FALSE(reader.HasParseError()) << "parse-next past complete should not generate parse error"; |
| 1664 |
} |
| 1665 |
} |
| 1666 |
|
| 1667 |
// Test iterative parsing on kParseErrorTermination. |
| 1668 |
struct HandlerTerminateAtStartObject : public IterativeParsingReaderHandler<> { |
| 1669 |
bool StartObject() { return false; } |
| 1670 |
}; |
| 1671 |
|
| 1672 |
struct HandlerTerminateAtStartArray : public IterativeParsingReaderHandler<> { |
| 1673 |
bool StartArray() { return false; } |
| 1674 |
}; |
| 1675 |
|
| 1676 |
struct HandlerTerminateAtEndObject : public IterativeParsingReaderHandler<> { |
| 1677 |
bool EndObject(SizeType) { return false; } |
| 1678 |
}; |
| 1679 |
|
| 1680 |
struct HandlerTerminateAtEndArray : public IterativeParsingReaderHandler<> { |
| 1681 |
bool EndArray(SizeType) { return false; } |
| 1682 |
}; |
| 1683 |
|
| 1684 |
TEST(Reader, IterativeParsing_ShortCircuit) { |
| 1685 |
{ |
| 1686 |
HandlerTerminateAtStartObject handler; |
| 1687 |
Reader reader; |
| 1688 |
StringStream is("[1, {}]"); |
| 1689 |
|
| 1690 |
ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler); |
| 1691 |
|
| 1692 |
EXPECT_TRUE(reader.HasParseError()); |
| 1693 |
EXPECT_EQ(kParseErrorTermination, r.Code()); |
| 1694 |
EXPECT_EQ(4u, r.Offset()); |
| 1695 |
} |
| 1696 |
|
| 1697 |
{ |
| 1698 |
HandlerTerminateAtStartArray handler; |
| 1699 |
Reader reader; |
| 1700 |
StringStream is("{\"a\": []}"); |
| 1701 |
|
| 1702 |
ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler); |
| 1703 |
|
| 1704 |
EXPECT_TRUE(reader.HasParseError()); |
| 1705 |
EXPECT_EQ(kParseErrorTermination, r.Code()); |
| 1706 |
EXPECT_EQ(6u, r.Offset()); |
| 1707 |
} |
| 1708 |
|
| 1709 |
{ |
| 1710 |
HandlerTerminateAtEndObject handler; |
| 1711 |
Reader reader; |
| 1712 |
StringStream is("[1, {}]"); |
| 1713 |
|
| 1714 |
ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler); |
| 1715 |
|
| 1716 |
EXPECT_TRUE(reader.HasParseError()); |
| 1717 |
EXPECT_EQ(kParseErrorTermination, r.Code()); |
| 1718 |
EXPECT_EQ(5u, r.Offset()); |
| 1719 |
} |
| 1720 |
|
| 1721 |
{ |
| 1722 |
HandlerTerminateAtEndArray handler; |
| 1723 |
Reader reader; |
| 1724 |
StringStream is("{\"a\": []}"); |
| 1725 |
|
| 1726 |
ParseResult r = reader.Parse<kParseIterativeFlag>(is, handler); |
| 1727 |
|
| 1728 |
EXPECT_TRUE(reader.HasParseError()); |
| 1729 |
EXPECT_EQ(kParseErrorTermination, r.Code()); |
| 1730 |
EXPECT_EQ(7u, r.Offset()); |
| 1731 |
} |
| 1732 |
} |
| 1733 |
|
| 1734 |
// For covering BaseReaderHandler default functions |
| 1735 |
TEST(Reader, BaseReaderHandler_Default) { |
| 1736 |
BaseReaderHandler<> h; |
| 1737 |
Reader reader; |
| 1738 |
StringStream is("[null, true, -1, 1, -1234567890123456789, 1234567890123456789, 3.14, \"s\", { \"a\" : 1 }]"); |
| 1739 |
EXPECT_TRUE(reader.Parse(is, h)); |
| 1740 |
} |
| 1741 |
|
| 1742 |
template <int e> |
| 1743 |
struct TerminateHandler { |
| 1744 |
bool Null() { return e != 0; } |
| 1745 |
bool Bool(bool) { return e != 1; } |
| 1746 |
bool Int(int) { return e != 2; } |
| 1747 |
bool Uint(unsigned) { return e != 3; } |
| 1748 |
bool Int64(int64_t) { return e != 4; } |
| 1749 |
bool Uint64(uint64_t) { return e != 5; } |
| 1750 |
bool Double(double) { return e != 6; } |
| 1751 |
bool RawNumber(const char*, SizeType, bool) { return e != 7; } |
| 1752 |
bool String(const char*, SizeType, bool) { return e != 8; } |
| 1753 |
bool StartObject() { return e != 9; } |
| 1754 |
bool Key(const char*, SizeType, bool) { return e != 10; } |
| 1755 |
bool EndObject(SizeType) { return e != 11; } |
| 1756 |
bool StartArray() { return e != 12; } |
| 1757 |
bool EndArray(SizeType) { return e != 13; } |
| 1758 |
}; |
| 1759 |
|
| 1760 |
#define TEST_TERMINATION(e, json)\ |
| 1761 |
{\ |
| 1762 |
Reader reader;\ |
| 1763 |
TerminateHandler<e> h;\ |
| 1764 |
StringStream is(json);\ |
| 1765 |
EXPECT_FALSE(reader.Parse(is, h));\ |
| 1766 |
EXPECT_EQ(kParseErrorTermination, reader.GetParseErrorCode());\ |
| 1767 |
} |
| 1768 |
|
| 1769 |
TEST(Reader, ParseTerminationByHandler) { |
| 1770 |
TEST_TERMINATION(0, "[null"); |
| 1771 |
TEST_TERMINATION(1, "[true"); |
| 1772 |
TEST_TERMINATION(1, "[false"); |
| 1773 |
TEST_TERMINATION(2, "[-1"); |
| 1774 |
TEST_TERMINATION(3, "[1"); |
| 1775 |
TEST_TERMINATION(4, "[-1234567890123456789"); |
| 1776 |
TEST_TERMINATION(5, "[1234567890123456789"); |
| 1777 |
TEST_TERMINATION(6, "[0.5]"); |
| 1778 |
// RawNumber() is never called |
| 1779 |
TEST_TERMINATION(8, "[\"a\""); |
| 1780 |
TEST_TERMINATION(9, "[{"); |
| 1781 |
TEST_TERMINATION(10, "[{\"a\""); |
| 1782 |
TEST_TERMINATION(11, "[{}"); |
| 1783 |
TEST_TERMINATION(11, "[{\"a\":1}"); // non-empty object |
| 1784 |
TEST_TERMINATION(12, "{\"a\":["); |
| 1785 |
TEST_TERMINATION(13, "{\"a\":[]"); |
| 1786 |
TEST_TERMINATION(13, "{\"a\":[1]"); // non-empty array |
| 1787 |
} |
| 1788 |
|
| 1789 |
TEST(Reader, ParseComments) { |
| 1790 |
const char* json = |
| 1791 |
"// Here is a one-line comment.\n" |
| 1792 |
"{// And here's another one\n" |
| 1793 |
" /*And here's an in-line one.*/\"hello\" : \"world\"," |
| 1794 |
" \"t\" :/* And one with '*' symbol*/true ," |
| 1795 |
"/* A multiline comment\n" |
| 1796 |
" goes here*/" |
| 1797 |
" \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3]" |
| 1798 |
"}/*And the last one to be sure */"; |
| 1799 |
|
| 1800 |
StringStream s(json); |
| 1801 |
ParseObjectHandler h; |
| 1802 |
Reader reader; |
| 1803 |
EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1804 |
EXPECT_EQ(20u, h.step_); |
| 1805 |
} |
| 1806 |
|
| 1807 |
TEST(Reader, ParseEmptyInlineComment) { |
| 1808 |
const char* json = "{/**/\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }"; |
| 1809 |
|
| 1810 |
StringStream s(json); |
| 1811 |
ParseObjectHandler h; |
| 1812 |
Reader reader; |
| 1813 |
EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1814 |
EXPECT_EQ(20u, h.step_); |
| 1815 |
} |
| 1816 |
|
| 1817 |
TEST(Reader, ParseEmptyOnelineComment) { |
| 1818 |
const char* json = "{//\n\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }"; |
| 1819 |
|
| 1820 |
StringStream s(json); |
| 1821 |
ParseObjectHandler h; |
| 1822 |
Reader reader; |
| 1823 |
EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1824 |
EXPECT_EQ(20u, h.step_); |
| 1825 |
} |
| 1826 |
|
| 1827 |
TEST(Reader, ParseMultipleCommentsInARow) { |
| 1828 |
const char* json = |
| 1829 |
"{/* first comment *//* second */\n" |
| 1830 |
"/* third */ /*fourth*/// last one\n" |
| 1831 |
"\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }"; |
| 1832 |
|
| 1833 |
StringStream s(json); |
| 1834 |
ParseObjectHandler h; |
| 1835 |
Reader reader; |
| 1836 |
EXPECT_TRUE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1837 |
EXPECT_EQ(20u, h.step_); |
| 1838 |
} |
| 1839 |
|
| 1840 |
TEST(Reader, InlineCommentsAreDisabledByDefault) { |
| 1841 |
{ |
| 1842 |
const char* json = "{/* Inline comment. */\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }"; |
| 1843 |
|
| 1844 |
StringStream s(json); |
| 1845 |
ParseObjectHandler h; |
| 1846 |
Reader reader; |
| 1847 |
EXPECT_FALSE(reader.Parse<kParseDefaultFlags>(s, h)); |
| 1848 |
} |
| 1849 |
|
| 1850 |
{ |
| 1851 |
const char* json = |
| 1852 |
"{\"hello\" : /* Multiline comment starts here\n" |
| 1853 |
" continues here\n" |
| 1854 |
" and ends here */\"world\", \"t\" :true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }"; |
| 1855 |
|
| 1856 |
StringStream s(json); |
| 1857 |
ParseObjectHandler h; |
| 1858 |
Reader reader; |
| 1859 |
EXPECT_FALSE(reader.Parse<kParseDefaultFlags>(s, h)); |
| 1860 |
} |
| 1861 |
} |
| 1862 |
|
| 1863 |
TEST(Reader, OnelineCommentsAreDisabledByDefault) { |
| 1864 |
const char* json = "{// One-line comment\n\"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }"; |
| 1865 |
|
| 1866 |
StringStream s(json); |
| 1867 |
ParseObjectHandler h; |
| 1868 |
Reader reader; |
| 1869 |
EXPECT_FALSE(reader.Parse<kParseDefaultFlags>(s, h)); |
| 1870 |
} |
| 1871 |
|
| 1872 |
TEST(Reader, EofAfterOneLineComment) { |
| 1873 |
const char* json = "{\"hello\" : \"world\" // EOF is here -->\0 \n}"; |
| 1874 |
|
| 1875 |
StringStream s(json); |
| 1876 |
ParseObjectHandler h; |
| 1877 |
Reader reader; |
| 1878 |
EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1879 |
EXPECT_EQ(kParseErrorObjectMissCommaOrCurlyBracket, reader.GetParseErrorCode()); |
| 1880 |
} |
| 1881 |
|
| 1882 |
TEST(Reader, IncompleteMultilineComment) { |
| 1883 |
const char* json = "{\"hello\" : \"world\" /* EOF is here -->\0 */}"; |
| 1884 |
|
| 1885 |
StringStream s(json); |
| 1886 |
ParseObjectHandler h; |
| 1887 |
Reader reader; |
| 1888 |
EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1889 |
EXPECT_EQ(kParseErrorUnspecificSyntaxError, reader.GetParseErrorCode()); |
| 1890 |
} |
| 1891 |
|
| 1892 |
TEST(Reader, IncompleteMultilineComment2) { |
| 1893 |
const char* json = "{\"hello\" : \"world\" /* *\0 */}"; |
| 1894 |
|
| 1895 |
StringStream s(json); |
| 1896 |
ParseObjectHandler h; |
| 1897 |
Reader reader; |
| 1898 |
EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1899 |
EXPECT_EQ(kParseErrorUnspecificSyntaxError, reader.GetParseErrorCode()); |
| 1900 |
} |
| 1901 |
|
| 1902 |
TEST(Reader, UnrecognizedComment) { |
| 1903 |
const char* json = "{\"hello\" : \"world\" /! }"; |
| 1904 |
|
| 1905 |
StringStream s(json); |
| 1906 |
ParseObjectHandler h; |
| 1907 |
Reader reader; |
| 1908 |
EXPECT_FALSE(reader.Parse<kParseCommentsFlag>(s, h)); |
| 1909 |
EXPECT_EQ(kParseErrorUnspecificSyntaxError, reader.GetParseErrorCode()); |
| 1910 |
} |
| 1911 |
|
| 1912 |
struct NumbersAsStringsHandler { |
| 1913 |
bool Null() { return true; } |
| 1914 |
bool Bool(bool) { return true; } |
| 1915 |
bool Int(int) { return true; } |
| 1916 |
bool Uint(unsigned) { return true; } |
| 1917 |
bool Int64(int64_t) { return true; } |
| 1918 |
bool Uint64(uint64_t) { return true; } |
| 1919 |
bool Double(double) { return true; } |
| 1920 |
// 'str' is not null-terminated |
| 1921 |
bool RawNumber(const char* str, SizeType length, bool) { |
| 1922 |
EXPECT_TRUE(str != 0); |
| 1923 |
EXPECT_TRUE(expected_len_ == length); |
| 1924 |
EXPECT_TRUE(strncmp(str, expected_, length) == 0); |
| 1925 |
return true; |
| 1926 |
} |
| 1927 |
bool String(const char*, SizeType, bool) { return true; } |
| 1928 |
bool StartObject() { return true; } |
| 1929 |
bool Key(const char*, SizeType, bool) { return true; } |
| 1930 |
bool EndObject(SizeType) { return true; } |
| 1931 |
bool StartArray() { return true; } |
| 1932 |
bool EndArray(SizeType) { return true; } |
| 1933 |
|
| 1934 |
NumbersAsStringsHandler(const char* expected) |
| 1935 |
: expected_(expected) |
| 1936 |
, expected_len_(strlen(expected)) {} |
| 1937 |
|
| 1938 |
const char* expected_; |
| 1939 |
size_t expected_len_; |
| 1940 |
}; |
| 1941 |
|
| 1942 |
TEST(Reader, NumbersAsStrings) { |
| 1943 |
{ |
| 1944 |
const char* json = "{ \"pi\": 3.1416 } "; |
| 1945 |
StringStream s(json); |
| 1946 |
NumbersAsStringsHandler h("3.1416"); |
| 1947 |
Reader reader; |
| 1948 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 1949 |
} |
| 1950 |
{ |
| 1951 |
char* json = StrDup("{ \"pi\": 3.1416 } "); |
| 1952 |
InsituStringStream s(json); |
| 1953 |
NumbersAsStringsHandler h("3.1416"); |
| 1954 |
Reader reader; |
| 1955 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h)); |
| 1956 |
free(json); |
| 1957 |
} |
| 1958 |
{ |
| 1959 |
const char* json = "{ \"gigabyte\": 1.0e9 } "; |
| 1960 |
StringStream s(json); |
| 1961 |
NumbersAsStringsHandler h("1.0e9"); |
| 1962 |
Reader reader; |
| 1963 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 1964 |
} |
| 1965 |
{ |
| 1966 |
char* json = StrDup("{ \"gigabyte\": 1.0e9 } "); |
| 1967 |
InsituStringStream s(json); |
| 1968 |
NumbersAsStringsHandler h("1.0e9"); |
| 1969 |
Reader reader; |
| 1970 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h)); |
| 1971 |
free(json); |
| 1972 |
} |
| 1973 |
{ |
| 1974 |
const char* json = "{ \"pi\": 314.159e-2 } "; |
| 1975 |
StringStream s(json); |
| 1976 |
NumbersAsStringsHandler h("314.159e-2"); |
| 1977 |
Reader reader; |
| 1978 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 1979 |
} |
| 1980 |
{ |
| 1981 |
char* json = StrDup("{ \"gigabyte\": 314.159e-2 } "); |
| 1982 |
InsituStringStream s(json); |
| 1983 |
NumbersAsStringsHandler h("314.159e-2"); |
| 1984 |
Reader reader; |
| 1985 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h)); |
| 1986 |
free(json); |
| 1987 |
} |
| 1988 |
{ |
| 1989 |
const char* json = "{ \"negative\": -1.54321 } "; |
| 1990 |
StringStream s(json); |
| 1991 |
NumbersAsStringsHandler h("-1.54321"); |
| 1992 |
Reader reader; |
| 1993 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 1994 |
} |
| 1995 |
{ |
| 1996 |
char* json = StrDup("{ \"negative\": -1.54321 } "); |
| 1997 |
InsituStringStream s(json); |
| 1998 |
NumbersAsStringsHandler h("-1.54321"); |
| 1999 |
Reader reader; |
| 2000 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag>(s, h)); |
| 2001 |
free(json); |
| 2002 |
} |
| 2003 |
{ |
| 2004 |
const char* json = "{ \"pi\": 314.159e-2 } "; |
| 2005 |
std::stringstream ss(json); |
| 2006 |
IStreamWrapper s(ss); |
| 2007 |
NumbersAsStringsHandler h("314.159e-2"); |
| 2008 |
Reader reader; |
| 2009 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2010 |
} |
| 2011 |
{ |
| 2012 |
char n1e319[321]; // '1' followed by 319 '0' |
| 2013 |
n1e319[0] = '1'; |
| 2014 |
for (int i = 1; i < 320; i++) |
| 2015 |
n1e319[i] = '0'; |
| 2016 |
n1e319[320] = '\0'; |
| 2017 |
StringStream s(n1e319); |
| 2018 |
NumbersAsStringsHandler h(n1e319); |
| 2019 |
Reader reader; |
| 2020 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2021 |
} |
| 2022 |
} |
| 2023 |
|
| 2024 |
struct NumbersAsStringsHandlerWChar_t { |
| 2025 |
bool Null() { return true; } |
| 2026 |
bool Bool(bool) { return true; } |
| 2027 |
bool Int(int) { return true; } |
| 2028 |
bool Uint(unsigned) { return true; } |
| 2029 |
bool Int64(int64_t) { return true; } |
| 2030 |
bool Uint64(uint64_t) { return true; } |
| 2031 |
bool Double(double) { return true; } |
| 2032 |
// 'str' is not null-terminated |
| 2033 |
bool RawNumber(const wchar_t* str, SizeType length, bool) { |
| 2034 |
EXPECT_TRUE(str != 0); |
| 2035 |
EXPECT_TRUE(expected_len_ == length); |
| 2036 |
EXPECT_TRUE(wcsncmp(str, expected_, length) == 0); |
| 2037 |
return true; |
| 2038 |
} |
| 2039 |
bool String(const wchar_t*, SizeType, bool) { return true; } |
| 2040 |
bool StartObject() { return true; } |
| 2041 |
bool Key(const wchar_t*, SizeType, bool) { return true; } |
| 2042 |
bool EndObject(SizeType) { return true; } |
| 2043 |
bool StartArray() { return true; } |
| 2044 |
bool EndArray(SizeType) { return true; } |
| 2045 |
|
| 2046 |
NumbersAsStringsHandlerWChar_t(const wchar_t* expected) |
| 2047 |
: expected_(expected) |
| 2048 |
, expected_len_(wcslen(expected)) {} |
| 2049 |
|
| 2050 |
const wchar_t* expected_; |
| 2051 |
size_t expected_len_; |
| 2052 |
}; |
| 2053 |
|
| 2054 |
TEST(Reader, NumbersAsStringsWChar_t) { |
| 2055 |
{ |
| 2056 |
const wchar_t* json = L"{ \"pi\": 3.1416 } "; |
| 2057 |
GenericStringStream<UTF16<> > s(json); |
| 2058 |
NumbersAsStringsHandlerWChar_t h(L"3.1416"); |
| 2059 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2060 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2061 |
} |
| 2062 |
{ |
| 2063 |
wchar_t* json = StrDup(L"{ \"pi\": 3.1416 } "); |
| 2064 |
GenericInsituStringStream<UTF16<> > s(json); |
| 2065 |
NumbersAsStringsHandlerWChar_t h(L"3.1416"); |
| 2066 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2067 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h)); |
| 2068 |
free(json); |
| 2069 |
} |
| 2070 |
{ |
| 2071 |
const wchar_t* json = L"{ \"gigabyte\": 1.0e9 } "; |
| 2072 |
GenericStringStream<UTF16<> > s(json); |
| 2073 |
NumbersAsStringsHandlerWChar_t h(L"1.0e9"); |
| 2074 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2075 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2076 |
} |
| 2077 |
{ |
| 2078 |
wchar_t* json = StrDup(L"{ \"gigabyte\": 1.0e9 } "); |
| 2079 |
GenericInsituStringStream<UTF16<> > s(json); |
| 2080 |
NumbersAsStringsHandlerWChar_t h(L"1.0e9"); |
| 2081 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2082 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h)); |
| 2083 |
free(json); |
| 2084 |
} |
| 2085 |
{ |
| 2086 |
const wchar_t* json = L"{ \"pi\": 314.159e-2 } "; |
| 2087 |
GenericStringStream<UTF16<> > s(json); |
| 2088 |
NumbersAsStringsHandlerWChar_t h(L"314.159e-2"); |
| 2089 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2090 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2091 |
} |
| 2092 |
{ |
| 2093 |
wchar_t* json = StrDup(L"{ \"gigabyte\": 314.159e-2 } "); |
| 2094 |
GenericInsituStringStream<UTF16<> > s(json); |
| 2095 |
NumbersAsStringsHandlerWChar_t h(L"314.159e-2"); |
| 2096 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2097 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h)); |
| 2098 |
free(json); |
| 2099 |
} |
| 2100 |
{ |
| 2101 |
const wchar_t* json = L"{ \"negative\": -1.54321 } "; |
| 2102 |
GenericStringStream<UTF16<> > s(json); |
| 2103 |
NumbersAsStringsHandlerWChar_t h(L"-1.54321"); |
| 2104 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2105 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2106 |
} |
| 2107 |
{ |
| 2108 |
wchar_t* json = StrDup(L"{ \"negative\": -1.54321 } "); |
| 2109 |
GenericInsituStringStream<UTF16<> > s(json); |
| 2110 |
NumbersAsStringsHandlerWChar_t h(L"-1.54321"); |
| 2111 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2112 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag | kParseNumbersAsStringsFlag>(s, h)); |
| 2113 |
free(json); |
| 2114 |
} |
| 2115 |
{ |
| 2116 |
const wchar_t* json = L"{ \"pi\": 314.159e-2 } "; |
| 2117 |
std::wstringstream ss(json); |
| 2118 |
WIStreamWrapper s(ss); |
| 2119 |
NumbersAsStringsHandlerWChar_t h(L"314.159e-2"); |
| 2120 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2121 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2122 |
} |
| 2123 |
{ |
| 2124 |
wchar_t n1e319[321]; // '1' followed by 319 '0' |
| 2125 |
n1e319[0] = L'1'; |
| 2126 |
for(int i = 1; i < 320; i++) |
| 2127 |
n1e319[i] = L'0'; |
| 2128 |
n1e319[320] = L'\0'; |
| 2129 |
GenericStringStream<UTF16<> > s(n1e319); |
| 2130 |
NumbersAsStringsHandlerWChar_t h(n1e319); |
| 2131 |
GenericReader<UTF16<>, UTF16<> > reader; |
| 2132 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag>(s, h)); |
| 2133 |
} |
| 2134 |
} |
| 2135 |
|
| 2136 |
template <unsigned extraFlags> |
| 2137 |
void TestTrailingCommas() { |
| 2138 |
{ |
| 2139 |
StringStream s("[1,2,3,]"); |
| 2140 |
ParseArrayHandler<3> h; |
| 2141 |
Reader reader; |
| 2142 |
EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h)); |
| 2143 |
EXPECT_EQ(5u, h.step_); |
| 2144 |
} |
| 2145 |
{ |
| 2146 |
const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false," |
| 2147 |
"\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3],}"; |
| 2148 |
StringStream s(json); |
| 2149 |
ParseObjectHandler h; |
| 2150 |
Reader reader; |
| 2151 |
EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h)); |
| 2152 |
EXPECT_EQ(20u, h.step_); |
| 2153 |
} |
| 2154 |
{ |
| 2155 |
// whitespace around trailing commas |
| 2156 |
const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false," |
| 2157 |
"\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3\n,\n]\n,\n} "; |
| 2158 |
StringStream s(json); |
| 2159 |
ParseObjectHandler h; |
| 2160 |
Reader reader; |
| 2161 |
EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h)); |
| 2162 |
EXPECT_EQ(20u, h.step_); |
| 2163 |
} |
| 2164 |
{ |
| 2165 |
// comments around trailing commas |
| 2166 |
const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null," |
| 2167 |
"\"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3/*test*/,/*test*/]/*test*/,/*test*/}"; |
| 2168 |
StringStream s(json); |
| 2169 |
ParseObjectHandler h; |
| 2170 |
Reader reader; |
| 2171 |
EXPECT_TRUE(reader.Parse<extraFlags|kParseTrailingCommasFlag|kParseCommentsFlag>(s, h)); |
| 2172 |
EXPECT_EQ(20u, h.step_); |
| 2173 |
} |
| 2174 |
} |
| 2175 |
|
| 2176 |
TEST(Reader, TrailingCommas) { |
| 2177 |
TestTrailingCommas<kParseNoFlags>(); |
| 2178 |
} |
| 2179 |
|
| 2180 |
TEST(Reader, TrailingCommasIterative) { |
| 2181 |
TestTrailingCommas<kParseIterativeFlag>(); |
| 2182 |
} |
| 2183 |
|
| 2184 |
template <unsigned extraFlags> |
| 2185 |
void TestMultipleTrailingCommaErrors() { |
| 2186 |
// only a single trailing comma is allowed. |
| 2187 |
{ |
| 2188 |
StringStream s("[1,2,3,,]"); |
| 2189 |
ParseArrayHandler<3> h; |
| 2190 |
Reader reader; |
| 2191 |
ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h); |
| 2192 |
EXPECT_TRUE(reader.HasParseError()); |
| 2193 |
EXPECT_EQ(kParseErrorValueInvalid, r.Code()); |
| 2194 |
EXPECT_EQ(7u, r.Offset()); |
| 2195 |
} |
| 2196 |
{ |
| 2197 |
const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false," |
| 2198 |
"\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3,],,}"; |
| 2199 |
StringStream s(json); |
| 2200 |
ParseObjectHandler h; |
| 2201 |
Reader reader; |
| 2202 |
ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h); |
| 2203 |
EXPECT_TRUE(reader.HasParseError()); |
| 2204 |
EXPECT_EQ(kParseErrorObjectMissName, r.Code()); |
| 2205 |
EXPECT_EQ(95u, r.Offset()); |
| 2206 |
} |
| 2207 |
} |
| 2208 |
|
| 2209 |
TEST(Reader, MultipleTrailingCommaErrors) { |
| 2210 |
TestMultipleTrailingCommaErrors<kParseNoFlags>(); |
| 2211 |
} |
| 2212 |
|
| 2213 |
TEST(Reader, MultipleTrailingCommaErrorsIterative) { |
| 2214 |
TestMultipleTrailingCommaErrors<kParseIterativeFlag>(); |
| 2215 |
} |
| 2216 |
|
| 2217 |
template <unsigned extraFlags> |
| 2218 |
void TestEmptyExceptForCommaErrors() { |
| 2219 |
// not allowed even with trailing commas enabled; the |
| 2220 |
// trailing comma must follow a value. |
| 2221 |
{ |
| 2222 |
StringStream s("[,]"); |
| 2223 |
ParseArrayHandler<3> h; |
| 2224 |
Reader reader; |
| 2225 |
ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h); |
| 2226 |
EXPECT_TRUE(reader.HasParseError()); |
| 2227 |
EXPECT_EQ(kParseErrorValueInvalid, r.Code()); |
| 2228 |
EXPECT_EQ(1u, r.Offset()); |
| 2229 |
} |
| 2230 |
{ |
| 2231 |
StringStream s("{,}"); |
| 2232 |
ParseObjectHandler h; |
| 2233 |
Reader reader; |
| 2234 |
ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h); |
| 2235 |
EXPECT_TRUE(reader.HasParseError()); |
| 2236 |
EXPECT_EQ(kParseErrorObjectMissName, r.Code()); |
| 2237 |
EXPECT_EQ(1u, r.Offset()); |
| 2238 |
} |
| 2239 |
} |
| 2240 |
|
| 2241 |
TEST(Reader, EmptyExceptForCommaErrors) { |
| 2242 |
TestEmptyExceptForCommaErrors<kParseNoFlags>(); |
| 2243 |
} |
| 2244 |
|
| 2245 |
TEST(Reader, EmptyExceptForCommaErrorsIterative) { |
| 2246 |
TestEmptyExceptForCommaErrors<kParseIterativeFlag>(); |
| 2247 |
} |
| 2248 |
|
| 2249 |
template <unsigned extraFlags> |
| 2250 |
void TestTrailingCommaHandlerTermination() { |
| 2251 |
{ |
| 2252 |
HandlerTerminateAtEndArray h; |
| 2253 |
Reader reader; |
| 2254 |
StringStream s("[1,2,3,]"); |
| 2255 |
ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h); |
| 2256 |
EXPECT_TRUE(reader.HasParseError()); |
| 2257 |
EXPECT_EQ(kParseErrorTermination, r.Code()); |
| 2258 |
EXPECT_EQ(7u, r.Offset()); |
| 2259 |
} |
| 2260 |
{ |
| 2261 |
HandlerTerminateAtEndObject h; |
| 2262 |
Reader reader; |
| 2263 |
StringStream s("{\"t\": true, \"f\": false,}"); |
| 2264 |
ParseResult r = reader.Parse<extraFlags|kParseTrailingCommasFlag>(s, h); |
| 2265 |
EXPECT_TRUE(reader.HasParseError()); |
| 2266 |
EXPECT_EQ(kParseErrorTermination, r.Code()); |
| 2267 |
EXPECT_EQ(23u, r.Offset()); |
| 2268 |
} |
| 2269 |
} |
| 2270 |
|
| 2271 |
TEST(Reader, TrailingCommaHandlerTermination) { |
| 2272 |
TestTrailingCommaHandlerTermination<kParseNoFlags>(); |
| 2273 |
} |
| 2274 |
|
| 2275 |
TEST(Reader, TrailingCommaHandlerTerminationIterative) { |
| 2276 |
TestTrailingCommaHandlerTermination<kParseIterativeFlag>(); |
| 2277 |
} |
| 2278 |
|
| 2279 |
TEST(Reader, ParseNanAndInfinity) { |
| 2280 |
#define TEST_NAN_INF(str, x) \ |
| 2281 |
{ \ |
| 2282 |
{ \ |
| 2283 |
StringStream s(str); \ |
| 2284 |
ParseDoubleHandler h; \ |
| 2285 |
Reader reader; \ |
| 2286 |
ASSERT_EQ(kParseErrorNone, reader.Parse<kParseNanAndInfFlag>(s, h).Code()); \ |
| 2287 |
EXPECT_EQ(1u, h.step_); \ |
| 2288 |
internal::Double e(x), a(h.actual_); \ |
| 2289 |
EXPECT_EQ(e.IsNan(), a.IsNan()); \ |
| 2290 |
EXPECT_EQ(e.IsInf(), a.IsInf()); \ |
| 2291 |
if (!e.IsNan()) \ |
| 2292 |
EXPECT_EQ(e.Sign(), a.Sign()); \ |
| 2293 |
} \ |
| 2294 |
{ \ |
| 2295 |
const char* json = "{ \"naninfdouble\": " str " } "; \ |
| 2296 |
StringStream s(json); \ |
| 2297 |
NumbersAsStringsHandler h(str); \ |
| 2298 |
Reader reader; \ |
| 2299 |
EXPECT_TRUE(reader.Parse<kParseNumbersAsStringsFlag|kParseNanAndInfFlag>(s, h)); \ |
| 2300 |
} \ |
| 2301 |
{ \ |
| 2302 |
char* json = StrDup("{ \"naninfdouble\": " str " } "); \ |
| 2303 |
InsituStringStream s(json); \ |
| 2304 |
NumbersAsStringsHandler h(str); \ |
| 2305 |
Reader reader; \ |
| 2306 |
EXPECT_TRUE(reader.Parse<kParseInsituFlag|kParseNumbersAsStringsFlag|kParseNanAndInfFlag>(s, h)); \ |
| 2307 |
free(json); \ |
| 2308 |
} \ |
| 2309 |
} |
| 2310 |
#define TEST_NAN_INF_ERROR(errorCode, str, errorOffset) \ |
| 2311 |
{ \ |
| 2312 |
unsigned streamPos = errorOffset; \ |
| 2313 |
char buffer[1001]; \ |
| 2314 |
strncpy(buffer, str, 1000); \ |
| 2315 |
InsituStringStream s(buffer); \ |
| 2316 |
BaseReaderHandler<> h; \ |
| 2317 |
Reader reader; \ |
| 2318 |
EXPECT_FALSE(reader.Parse<kParseNanAndInfFlag>(s, h)); \ |
| 2319 |
EXPECT_EQ(errorCode, reader.GetParseErrorCode());\ |
| 2320 |
EXPECT_EQ(errorOffset, reader.GetErrorOffset());\ |
| 2321 |
EXPECT_EQ(streamPos, s.Tell());\ |
| 2322 |
} |
| 2323 |
|
| 2324 |
double nan = std::numeric_limits<double>::quiet_NaN(); |
| 2325 |
double inf = std::numeric_limits<double>::infinity(); |
| 2326 |
|
| 2327 |
TEST_NAN_INF("NaN", nan); |
| 2328 |
TEST_NAN_INF("-NaN", nan); |
| 2329 |
TEST_NAN_INF("Inf", inf); |
| 2330 |
TEST_NAN_INF("Infinity", inf); |
| 2331 |
TEST_NAN_INF("-Inf", -inf); |
| 2332 |
TEST_NAN_INF("-Infinity", -inf); |
| 2333 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NInf", 1u); |
| 2334 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NaInf", 2u); |
| 2335 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "INan", 1u); |
| 2336 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "InNan", 2u); |
| 2337 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1u); |
| 2338 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1u); |
| 2339 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1u); |
| 2340 |
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-Infinty", 6u); |
| 2341 |
|
| 2342 |
#undef TEST_NAN_INF_ERROR |
| 2343 |
#undef TEST_NAN_INF |
| 2344 |
} |
| 2345 |
|
| 2346 |
TEST(Reader, EscapedApostrophe) { |
| 2347 |
const char json[] = " { \"foo\": \"bar\\'buzz\" } "; |
| 2348 |
|
| 2349 |
BaseReaderHandler<> h; |
| 2350 |
|
| 2351 |
{ |
| 2352 |
StringStream s(json); |
| 2353 |
Reader reader; |
| 2354 |
ParseResult r = reader.Parse<kParseNoFlags>(s, h); |
| 2355 |
EXPECT_TRUE(reader.HasParseError()); |
| 2356 |
EXPECT_EQ(kParseErrorStringEscapeInvalid, r.Code()); |
| 2357 |
EXPECT_EQ(14u, r.Offset()); |
| 2358 |
} |
| 2359 |
|
| 2360 |
{ |
| 2361 |
StringStream s(json); |
| 2362 |
Reader reader; |
| 2363 |
ParseResult r = reader.Parse<kParseEscapedApostropheFlag>(s, h); |
| 2364 |
EXPECT_FALSE(reader.HasParseError()); |
| 2365 |
EXPECT_EQ(kParseErrorNone, r.Code()); |
| 2366 |
EXPECT_EQ(0u, r.Offset()); |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
RAPIDJSON_DIAG_POP |