readertest.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #include "unittest.h"
  2. #define private public // For testing private members
  3. #include "rapidjson/reader.h"
  4. using namespace rapidjson;
  5. template<bool expect>
  6. struct ParseBoolHandler : BaseReaderHandler<> {
  7. ParseBoolHandler() : step_(0) {}
  8. void Default() { FAIL(); }
  9. void Bool(bool b) { EXPECT_EQ(expect, b); ++step_; }
  10. unsigned step_;
  11. };
  12. TEST(Reader, ParseTrue) {
  13. StringStream s("true");
  14. ParseBoolHandler<true> h;
  15. Reader reader;
  16. reader.ParseTrue<0>(s, h);
  17. EXPECT_EQ(1u, h.step_);
  18. }
  19. TEST(Reader, ParseFalse) {
  20. StringStream s("false");
  21. ParseBoolHandler<false> h;
  22. Reader reader;
  23. reader.ParseFalse<0>(s, h);
  24. EXPECT_EQ(1u, h.step_);
  25. }
  26. struct ParseIntHandler : BaseReaderHandler<> {
  27. ParseIntHandler() : step_(0) {}
  28. void Default() { FAIL(); }
  29. void Int(int i) { actual_ = i; step_++; }
  30. unsigned step_;
  31. int actual_;
  32. };
  33. struct ParseUintHandler : BaseReaderHandler<> {
  34. ParseUintHandler() : step_(0) {}
  35. void Default() { FAIL(); }
  36. void Uint(unsigned i) { actual_ = i; step_++; }
  37. unsigned step_;
  38. unsigned actual_;
  39. };
  40. struct ParseInt64Handler : BaseReaderHandler<> {
  41. ParseInt64Handler() : step_(0) {}
  42. void Default() { FAIL(); }
  43. void Int64(int64_t i) { actual_ = i; step_++; }
  44. unsigned step_;
  45. int64_t actual_;
  46. };
  47. struct ParseUint64Handler : BaseReaderHandler<> {
  48. ParseUint64Handler() : step_(0) {}
  49. void Default() { FAIL(); }
  50. void Uint64(uint64_t i) { actual_ = i; step_++; }
  51. unsigned step_;
  52. uint64_t actual_;
  53. };
  54. struct ParseDoubleHandler : BaseReaderHandler<> {
  55. ParseDoubleHandler() : step_(0) {}
  56. void Default() { FAIL(); }
  57. void Double(double d) { actual_ = d; step_++; }
  58. unsigned step_;
  59. double actual_;
  60. };
  61. TEST(Reader, ParseNumberHandler) {
  62. #define TEST_NUMBER(Handler, str, x) \
  63. { \
  64. StringStream s(str); \
  65. Handler h; \
  66. Reader reader; \
  67. reader.ParseNumber<0>(s, h); \
  68. EXPECT_EQ(1u, h.step_); \
  69. EXPECT_EQ(double(x), h.actual_); \
  70. }
  71. #define TEST_DOUBLE(str, x) \
  72. { \
  73. StringStream s(str); \
  74. ParseDoubleHandler h; \
  75. Reader reader; \
  76. reader.ParseNumber<0>(s, h); \
  77. EXPECT_EQ(1u, h.step_); \
  78. EXPECT_DOUBLE_EQ(x, h.actual_); \
  79. }
  80. TEST_NUMBER(ParseUintHandler, "0", 0);
  81. TEST_NUMBER(ParseUintHandler, "123", 123);
  82. TEST_NUMBER(ParseUintHandler, "2147483648", 2147483648u); // 2^31 - 1 (cannot be stored in int)
  83. TEST_NUMBER(ParseUintHandler, "4294967295", 4294967295u);
  84. TEST_NUMBER(ParseIntHandler, "-123", -123);
  85. TEST_NUMBER(ParseIntHandler, "-2147483648", -2147483648LL); // -2^31 (min of int)
  86. TEST_NUMBER(ParseUint64Handler, "4294967296", 4294967296ULL); // 2^32 (max of unsigned + 1, force to use uint64_t)
  87. TEST_NUMBER(ParseUint64Handler, "18446744073709551615", 18446744073709551615ULL); // 2^64 - 1 (max of uint64_t)
  88. TEST_NUMBER(ParseInt64Handler, "-2147483649", -2147483649LL); // -2^31 -1 (min of int - 1, force to use int64_t)
  89. TEST_NUMBER(ParseInt64Handler, "-9223372036854775808", (-9223372036854775807LL - 1)); // -2^63 (min of int64_t)
  90. TEST_DOUBLE("0.0", 0.0);
  91. TEST_DOUBLE("1.0", 1.0);
  92. TEST_DOUBLE("-1.0", -1.0);
  93. TEST_DOUBLE("1.5", 1.5);
  94. TEST_DOUBLE("-1.5", -1.5);
  95. TEST_DOUBLE("3.1416", 3.1416);
  96. TEST_DOUBLE("1E10", 1E10);
  97. TEST_DOUBLE("1e10", 1e10);
  98. TEST_DOUBLE("1E+10", 1E+10);
  99. TEST_DOUBLE("1E-10", 1E-10);
  100. TEST_DOUBLE("-1E10", -1E10);
  101. TEST_DOUBLE("-1e10", -1e10);
  102. TEST_DOUBLE("-1E+10", -1E+10);
  103. TEST_DOUBLE("-1E-10", -1E-10);
  104. TEST_DOUBLE("1.234E+10", 1.234E+10);
  105. TEST_DOUBLE("1.234E-10", 1.234E-10);
  106. TEST_DOUBLE("1.79769e+308", 1.79769e+308);
  107. //TEST_DOUBLE("2.22507e-308", 2.22507e-308); // TODO: underflow
  108. TEST_DOUBLE("-1.79769e+308", -1.79769e+308);
  109. //TEST_DOUBLE("-2.22507e-308", -2.22507e-308); // TODO: underflow
  110. TEST_DOUBLE("18446744073709551616", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double)
  111. TEST_DOUBLE("-9223372036854775809", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double)
  112. {
  113. char n1e308[310]; // '1' followed by 308 '0'
  114. n1e308[0] = '1';
  115. for (int i = 1; i < 309; i++)
  116. n1e308[i] = '0';
  117. n1e308[309] = '\0';
  118. TEST_DOUBLE(n1e308, 1E308);
  119. }
  120. #undef TEST_NUMBER
  121. #undef TEST_DOUBLE
  122. }
  123. TEST(Reader, ParseNumberHandler_Error) {
  124. #define TEST_NUMBER_ERROR(str) \
  125. { \
  126. char buffer[1001]; \
  127. sprintf(buffer, "[%s]", str); \
  128. InsituStringStream s(buffer); \
  129. BaseReaderHandler<> h; \
  130. Reader reader; \
  131. EXPECT_FALSE(reader.Parse<0>(s, h)); \
  132. }
  133. TEST_NUMBER_ERROR("a"); // At least one digit in integer part
  134. TEST_NUMBER_ERROR(".1"); // At least one digit in integer part
  135. {
  136. char n1e309[311]; // '1' followed by 309 '0'
  137. n1e309[0] = '1';
  138. for (int i = 1; i < 310; i++)
  139. n1e309[i] = '0';
  140. n1e309[310] = '\0';
  141. TEST_NUMBER_ERROR(n1e309); // Number too big to store in double
  142. }
  143. TEST_NUMBER_ERROR("1."); // At least one digit in fraction part
  144. TEST_NUMBER_ERROR("1e309"); // Number too big to store in double
  145. TEST_NUMBER_ERROR("1e_"); // At least one digit in exponent
  146. #undef TEST_NUMBER_ERROR
  147. }
  148. template <typename Encoding>
  149. struct ParseStringHandler : BaseReaderHandler<Encoding> {
  150. ParseStringHandler() : str_(0), length_(0) {}
  151. ~ParseStringHandler() { EXPECT_TRUE(str_ != 0); if (copy_) free(const_cast<typename Encoding::Ch*>(str_)); }
  152. void Default() { FAIL(); }
  153. void String(const typename Encoding::Ch* str, size_t length, bool copy) {
  154. EXPECT_EQ(0, str_);
  155. if (copy) {
  156. str_ = (typename Encoding::Ch*)malloc((length + 1) * sizeof(typename Encoding::Ch));
  157. memcpy((void*)str_, str, (length + 1) * sizeof(typename Encoding::Ch));
  158. }
  159. else
  160. str_ = str;
  161. length_ = length;
  162. copy_ = copy;
  163. }
  164. const typename Encoding::Ch* str_;
  165. size_t length_;
  166. bool copy_;
  167. };
  168. TEST(Reader, ParseString) {
  169. #define TEST_STRING(Encoding, e, x) \
  170. { \
  171. Encoding::Ch* buffer = StrDup(x); \
  172. GenericInsituStringStream<Encoding> is(buffer); \
  173. ParseStringHandler<Encoding> h; \
  174. GenericReader<Encoding, Encoding> reader; \
  175. reader.ParseString<kParseInsituFlag | kParseValidateEncodingFlag>(is, h); \
  176. EXPECT_EQ(0, StrCmp<Encoding::Ch>(e, h.str_)); \
  177. EXPECT_EQ(StrLen(e), h.length_); \
  178. free(buffer); \
  179. GenericStringStream<Encoding> s(x); \
  180. ParseStringHandler<Encoding> h2; \
  181. GenericReader<Encoding, Encoding> reader2; \
  182. reader2.ParseString<0>(s, h2); \
  183. EXPECT_EQ(0, StrCmp<Encoding::Ch>(e, h2.str_)); \
  184. EXPECT_EQ(StrLen(e), h2.length_); \
  185. }
  186. // String constant L"\xXX" can only specify character code in bytes, which is not endianness-neutral.
  187. // And old compiler does not support u"" and U"" string literal. So here specify string literal by array of Ch.
  188. #define ARRAY(...) { __VA_ARGS__ }
  189. #define TEST_STRINGARRAY(Encoding, array, x) \
  190. { \
  191. static const Encoding::Ch e[] = array; \
  192. TEST_STRING(Encoding, e, x); \
  193. }
  194. #define TEST_STRINGARRAY2(Encoding, earray, xarray) \
  195. { \
  196. static const Encoding::Ch e[] = earray; \
  197. static const Encoding::Ch x[] = xarray; \
  198. TEST_STRING(Encoding, e, x); \
  199. }
  200. TEST_STRING(UTF8<>, "", "\"\"");
  201. TEST_STRING(UTF8<>, "Hello", "\"Hello\"");
  202. TEST_STRING(UTF8<>, "Hello\nWorld", "\"Hello\\nWorld\"");
  203. TEST_STRING(UTF8<>, "\"\\/\b\f\n\r\t", "\"\\\"\\\\/\\b\\f\\n\\r\\t\"");
  204. TEST_STRING(UTF8<>, "\x24", "\"\\u0024\""); // Dollar sign U+0024
  205. TEST_STRING(UTF8<>, "\xC2\xA2", "\"\\u00A2\""); // Cents sign U+00A2
  206. TEST_STRING(UTF8<>, "\xE2\x82\xAC", "\"\\u20AC\""); // Euro sign U+20AC
  207. TEST_STRING(UTF8<>, "\xF0\x9D\x84\x9E", "\"\\uD834\\uDD1E\""); // G clef sign U+1D11E
  208. // UTF16
  209. TEST_STRING(UTF16<>, L"", L"\"\"");
  210. TEST_STRING(UTF16<>, L"Hello", L"\"Hello\"");
  211. TEST_STRING(UTF16<>, L"Hello\nWorld", L"\"Hello\\nWorld\"");
  212. TEST_STRING(UTF16<>, L"\"\\/\b\f\n\r\t", L"\"\\\"\\\\/\\b\\f\\n\\r\\t\"");
  213. TEST_STRINGARRAY(UTF16<>, ARRAY(0x0024, 0x0000), L"\"\\u0024\"");
  214. TEST_STRINGARRAY(UTF16<>, ARRAY(0x00A2, 0x0000), L"\"\\u00A2\""); // Cents sign U+00A2
  215. TEST_STRINGARRAY(UTF16<>, ARRAY(0x20AC, 0x0000), L"\"\\u20AC\""); // Euro sign U+20AC
  216. TEST_STRINGARRAY(UTF16<>, ARRAY(0xD834, 0xDD1E, 0x0000), L"\"\\uD834\\uDD1E\""); // G clef sign U+1D11E
  217. // UTF32
  218. TEST_STRINGARRAY2(UTF32<>, ARRAY('\0'), ARRAY('\"', '\"', '\0'));
  219. TEST_STRINGARRAY2(UTF32<>, ARRAY('H', 'e', 'l', 'l', 'o', '\0'), ARRAY('\"', 'H', 'e', 'l', 'l', 'o', '\"', '\0'));
  220. TEST_STRINGARRAY2(UTF32<>, 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'));
  221. TEST_STRINGARRAY2(UTF32<>, ARRAY('\"', '\\', '/', '\b', '\f', '\n', '\r', '\t', '\0'), ARRAY('\"', '\\', '\"', '\\', '\\', '/', '\\', 'b', '\\', 'f', '\\', 'n', '\\', 'r', '\\', 't', '\"', '\0'));
  222. TEST_STRINGARRAY2(UTF32<>, ARRAY(0x00024, 0x0000), ARRAY('\"', '\\', 'u', '0', '0', '2', '4', '\"', '\0'));
  223. TEST_STRINGARRAY2(UTF32<>, ARRAY(0x000A2, 0x0000), ARRAY('\"', '\\', 'u', '0', '0', 'A', '2', '\"', '\0')); // Cents sign U+00A2
  224. TEST_STRINGARRAY2(UTF32<>, ARRAY(0x020AC, 0x0000), ARRAY('\"', '\\', 'u', '2', '0', 'A', 'C', '\"', '\0')); // Euro sign U+20AC
  225. TEST_STRINGARRAY2(UTF32<>, ARRAY(0x1D11E, 0x0000), ARRAY('\"', '\\', 'u', 'D', '8', '3', '4', '\\', 'u', 'D', 'D', '1', 'E', '\"', '\0')); // G clef sign U+1D11E
  226. #undef TEST_STRINGARRAY
  227. #undef ARRAY
  228. #undef TEST_STRING
  229. // Support of null character in string
  230. {
  231. StringStream s("\"Hello\\u0000World\"");
  232. const char e[] = "Hello\0World";
  233. ParseStringHandler<UTF8<> > h;
  234. Reader reader;
  235. reader.ParseString<0>(s, h);
  236. EXPECT_EQ(0, memcmp(e, h.str_, h.length_ + 1));
  237. EXPECT_EQ(11u, h.length_);
  238. }
  239. }
  240. TEST(Reader, ParseString_Transcoding) {
  241. const char* x = "\"Hello\"";
  242. const wchar_t* e = L"Hello";
  243. GenericStringStream<UTF8<> > is(x);
  244. GenericReader<UTF8<>, UTF16<> > reader;
  245. ParseStringHandler<UTF16<> > h;
  246. reader.ParseString<0>(is, h);
  247. EXPECT_EQ(0, StrCmp<UTF16<>::Ch>(e, h.str_));
  248. EXPECT_EQ(StrLen(e), h.length_);
  249. }
  250. TEST(Reader, ParseString_NonDestructive) {
  251. StringStream s("\"Hello\\nWorld\"");
  252. ParseStringHandler<UTF8<> > h;
  253. Reader reader;
  254. reader.ParseString<0>(s, h);
  255. EXPECT_EQ(0, StrCmp("Hello\nWorld", h.str_));
  256. EXPECT_EQ(11u, h.length_);
  257. }
  258. bool TestString(const char* str) {
  259. StringStream s(str);
  260. BaseReaderHandler<> h;
  261. Reader reader;
  262. return reader.Parse<kParseValidateEncodingFlag>(s, h);
  263. }
  264. TEST(Reader, ParseString_Error) {
  265. #define ARRAY(...) { __VA_ARGS__ }
  266. #define TEST_STRINGARRAY_ERROR(Encoding, array) \
  267. { \
  268. static const Encoding::Ch e[] = array; \
  269. EXPECT_FALSE(TestString(e)); \
  270. }
  271. EXPECT_FALSE(TestString("[\"\\a\"]")); // Unknown escape character
  272. EXPECT_FALSE(TestString("[\"\\uABCG\"]")); // Incorrect hex digit after \\u escape
  273. EXPECT_FALSE(TestString("[\"\\uD800X\"]")); // Missing the second \\u in surrogate pair
  274. EXPECT_FALSE(TestString("[\"\\uD800\\uFFFF\"]")); // The second \\u in surrogate pair is invalid
  275. EXPECT_FALSE(TestString("[\"Test]")); // lacks ending quotation before the end of string
  276. // http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
  277. // 3 Malformed sequences
  278. // 3.1 Unexpected continuation bytes
  279. {
  280. char e[] = { '[', '\"', 0, '\"', ']', '\0' };
  281. for (unsigned char c = 0x80u; c <= 0xBFu; c++) {
  282. e[2] = c;
  283. bool b = TestString(e);
  284. EXPECT_FALSE(b);
  285. if (b)
  286. std::cout << (unsigned)(unsigned char)c << std::endl;
  287. }
  288. }
  289. // 3.2 Lonely start characters, 3.5 Impossible bytes
  290. {
  291. char e[] = { '[', '\"', 0, ' ', '\"', ']', '\0' };
  292. for (unsigned c = 0xC0u; c <= 0xFFu; c++) {
  293. e[2] = (char)c;
  294. EXPECT_FALSE(TestString(e));
  295. }
  296. }
  297. // 4 Overlong sequences
  298. // 4.1 Examples of an overlong ASCII character
  299. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xC0u, 0xAFu, '\"', ']', '\0'));
  300. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xE0u, 0x80u, 0xAFu, '\"', ']', '\0'));
  301. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0xAFu, '\"', ']', '\0'));
  302. // 4.2 Maximum overlong sequences
  303. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xC1u, 0xBFu, '\"', ']', '\0'));
  304. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xE0u, 0x9Fu, 0xBFu, '\"', ']', '\0'));
  305. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xF0u, 0x8Fu, 0xBFu, 0xBFu, '\"', ']', '\0'));
  306. // 4.3 Overlong representation of the NUL character
  307. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xC0u, 0x80u, '\"', ']', '\0'));
  308. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xE0u, 0x80u, 0x80u, '\"', ']', '\0'));
  309. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0x80u, '\"', ']', '\0'));
  310. // 5 Illegal code positions
  311. // 5.1 Single UTF-16 surrogates
  312. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xEDu, 0xA0u, 0x80u, '\"', ']', '\0'));
  313. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xEDu, 0xADu, 0xBFu, '\"', ']', '\0'));
  314. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xEDu, 0xAEu, 0x80u, '\"', ']', '\0'));
  315. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xEDu, 0xAFu, 0xBFu, '\"', ']', '\0'));
  316. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xEDu, 0xB0u, 0x80u, '\"', ']', '\0'));
  317. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xEDu, 0xBEu, 0x80u, '\"', ']', '\0'));
  318. TEST_STRINGARRAY_ERROR(UTF8<>, ARRAY('[', '\"', 0xEDu, 0xBFu, 0xBFu, '\"', ']', '\0'));
  319. #undef ARRAY
  320. #undef TEST_STRINGARRAY_ERROR
  321. }
  322. template <unsigned count>
  323. struct ParseArrayHandler : BaseReaderHandler<> {
  324. ParseArrayHandler() : step_(0) {}
  325. void Default() { FAIL(); }
  326. void Uint(unsigned i) { EXPECT_EQ(step_, i); step_++; }
  327. void StartArray() { EXPECT_EQ(0u, step_); step_++; }
  328. void EndArray(SizeType) { step_++; }
  329. unsigned step_;
  330. };
  331. TEST(Reader, ParseEmptyArray) {
  332. char *json = StrDup("[ ] ");
  333. InsituStringStream s(json);
  334. ParseArrayHandler<0> h;
  335. Reader reader;
  336. reader.ParseArray<0>(s, h);
  337. EXPECT_EQ(2u, h.step_);
  338. free(json);
  339. }
  340. TEST(Reader, ParseArray) {
  341. char *json = StrDup("[1, 2, 3, 4]");
  342. InsituStringStream s(json);
  343. ParseArrayHandler<4> h;
  344. Reader reader;
  345. reader.ParseArray<0>(s, h);
  346. EXPECT_EQ(6u, h.step_);
  347. free(json);
  348. }
  349. TEST(Reader, ParseArray_Error) {
  350. #define TEST_ARRAY_ERROR(str) \
  351. { \
  352. char buffer[1001]; \
  353. strncpy(buffer, str, 1000); \
  354. InsituStringStream s(buffer); \
  355. BaseReaderHandler<> h; \
  356. GenericReader<UTF8<>, UTF8<>, CrtAllocator> reader; \
  357. EXPECT_FALSE(reader.Parse<0>(s, h)); \
  358. }
  359. // Must be a comma or ']' after an array element.
  360. TEST_ARRAY_ERROR("[");
  361. TEST_ARRAY_ERROR("[}");
  362. TEST_ARRAY_ERROR("[1 2]");
  363. #undef TEST_ARRAY_ERROR
  364. }
  365. struct ParseObjectHandler : BaseReaderHandler<> {
  366. ParseObjectHandler() : step_(0) {}
  367. void Null() { EXPECT_EQ(8u, step_); step_++; }
  368. void Bool(bool b) {
  369. switch(step_) {
  370. case 4: EXPECT_TRUE(b); step_++; break;
  371. case 6: EXPECT_FALSE(b); step_++; break;
  372. default: FAIL();
  373. }
  374. }
  375. void Int(int i) {
  376. switch(step_) {
  377. case 10: EXPECT_EQ(123, i); step_++; break;
  378. case 15: EXPECT_EQ(1, i); step_++; break;
  379. case 16: EXPECT_EQ(2, i); step_++; break;
  380. case 17: EXPECT_EQ(3, i); step_++; break;
  381. default: FAIL();
  382. }
  383. }
  384. void Uint(unsigned i) { Int(i); }
  385. void Double(double d) { EXPECT_EQ(12u, step_); EXPECT_EQ(3.1416, d); step_++; }
  386. void String(const char* str, size_t, bool) {
  387. switch(step_) {
  388. case 1: EXPECT_STREQ("hello", str); step_++; break;
  389. case 2: EXPECT_STREQ("world", str); step_++; break;
  390. case 3: EXPECT_STREQ("t", str); step_++; break;
  391. case 5: EXPECT_STREQ("f", str); step_++; break;
  392. case 7: EXPECT_STREQ("n", str); step_++; break;
  393. case 9: EXPECT_STREQ("i", str); step_++; break;
  394. case 11: EXPECT_STREQ("pi", str); step_++; break;
  395. case 13: EXPECT_STREQ("a", str); step_++; break;
  396. default: FAIL();
  397. }
  398. }
  399. void StartObject() { EXPECT_EQ(0u, step_); step_++; }
  400. void EndObject(SizeType memberCount) { EXPECT_EQ(19u, step_); EXPECT_EQ(7u, memberCount); step_++;}
  401. void StartArray() { EXPECT_EQ(14u, step_); step_++; }
  402. void EndArray(SizeType elementCount) { EXPECT_EQ(18u, step_); EXPECT_EQ(3u, elementCount); step_++;}
  403. unsigned step_;
  404. };
  405. TEST(Reader, ParseObject) {
  406. const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } ";
  407. // Insitu
  408. {
  409. char* json2 = StrDup(json);
  410. InsituStringStream s(json2);
  411. ParseObjectHandler h;
  412. Reader reader;
  413. reader.ParseObject<kParseInsituFlag>(s, h);
  414. EXPECT_EQ(20u, h.step_);
  415. free(json2);
  416. }
  417. // Normal
  418. {
  419. StringStream s(json);
  420. ParseObjectHandler h;
  421. Reader reader;
  422. reader.ParseObject<0>(s, h);
  423. EXPECT_EQ(20u, h.step_);
  424. }
  425. }
  426. struct ParseEmptyObjectHandler : BaseReaderHandler<> {
  427. ParseEmptyObjectHandler() : step_(0) {}
  428. void Default() { FAIL(); }
  429. void StartObject() { EXPECT_EQ(0u, step_); step_++; }
  430. void EndObject(SizeType) { EXPECT_EQ(1u, step_); step_++; }
  431. unsigned step_;
  432. };
  433. TEST(Reader, Parse_EmptyObject) {
  434. StringStream s("{ } ");
  435. ParseEmptyObjectHandler h;
  436. Reader reader;
  437. reader.ParseObject<0>(s, h);
  438. EXPECT_EQ(2u, h.step_);
  439. }
  440. TEST(Reader, ParseObject_Error) {
  441. #define TEST_OBJECT_ERROR(str) \
  442. { \
  443. char buffer[1001]; \
  444. strncpy(buffer, str, 1000); \
  445. InsituStringStream s(buffer); \
  446. BaseReaderHandler<> h; \
  447. GenericReader<UTF8<>, UTF8<>, CrtAllocator> reader; \
  448. EXPECT_FALSE(reader.Parse<0>(s, h)); \
  449. }
  450. // Name of an object member must be a string
  451. TEST_OBJECT_ERROR("{null:1}");
  452. TEST_OBJECT_ERROR("{true:1}");
  453. TEST_OBJECT_ERROR("{false:1}");
  454. TEST_OBJECT_ERROR("{1:1}");
  455. TEST_OBJECT_ERROR("{[]:1}");
  456. TEST_OBJECT_ERROR("{{}:1}");
  457. TEST_OBJECT_ERROR("{xyz:1}");
  458. // There must be a colon after the name of object member
  459. TEST_OBJECT_ERROR("{\"a\" 1}");
  460. TEST_OBJECT_ERROR("{\"a\",1}");
  461. // Must be a comma or '}' after an object member
  462. TEST_OBJECT_ERROR("{]");
  463. TEST_OBJECT_ERROR("{\"a\":1]");
  464. #undef TEST_OBJECT_ERROR
  465. }
  466. TEST(Reader, Parse_Error) {
  467. #define TEST_ERROR(str) \
  468. { \
  469. char buffer[1001]; \
  470. strncpy(buffer, str, 1000); \
  471. InsituStringStream s(buffer); \
  472. BaseReaderHandler<> h; \
  473. Reader reader; \
  474. EXPECT_FALSE(reader.Parse<0>(s, h)); \
  475. }
  476. // Text only contains white space(s)
  477. TEST_ERROR("");
  478. TEST_ERROR(" ");
  479. TEST_ERROR(" \n");
  480. // Expect either an object or array at root
  481. TEST_ERROR("null");
  482. TEST_ERROR("true");
  483. TEST_ERROR("false");
  484. TEST_ERROR("\"s\"");
  485. TEST_ERROR("0");
  486. // Nothing should follow the root object or array
  487. TEST_ERROR("[] 0");
  488. TEST_ERROR("{} 0");
  489. // Invalid value
  490. TEST_ERROR("nulL");
  491. TEST_ERROR("truE");
  492. TEST_ERROR("falsE");
  493. #undef TEST_ERROR
  494. }