pointertest.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/pointer.h"
  16. #include "rapidjson/stringbuffer.h"
  17. #include "rapidjson/ostreamwrapper.h"
  18. #include <sstream>
  19. #include <map>
  20. #include <algorithm>
  21. using namespace rapidjson;
  22. static const char kJson[] = "{\n"
  23. " \"foo\":[\"bar\", \"baz\"],\n"
  24. " \"\" : 0,\n"
  25. " \"a/b\" : 1,\n"
  26. " \"c%d\" : 2,\n"
  27. " \"e^f\" : 3,\n"
  28. " \"g|h\" : 4,\n"
  29. " \"i\\\\j\" : 5,\n"
  30. " \"k\\\"l\" : 6,\n"
  31. " \" \" : 7,\n"
  32. " \"m~n\" : 8\n"
  33. "}";
  34. TEST(Pointer, DefaultConstructor) {
  35. Pointer p;
  36. EXPECT_TRUE(p.IsValid());
  37. EXPECT_EQ(0u, p.GetTokenCount());
  38. }
  39. TEST(Pointer, Parse) {
  40. {
  41. Pointer p("");
  42. EXPECT_TRUE(p.IsValid());
  43. EXPECT_EQ(0u, p.GetTokenCount());
  44. }
  45. {
  46. Pointer p("/");
  47. EXPECT_TRUE(p.IsValid());
  48. EXPECT_EQ(1u, p.GetTokenCount());
  49. EXPECT_EQ(0u, p.GetTokens()[0].length);
  50. EXPECT_STREQ("", p.GetTokens()[0].name);
  51. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  52. }
  53. {
  54. Pointer p("/foo");
  55. EXPECT_TRUE(p.IsValid());
  56. EXPECT_EQ(1u, p.GetTokenCount());
  57. EXPECT_EQ(3u, p.GetTokens()[0].length);
  58. EXPECT_STREQ("foo", p.GetTokens()[0].name);
  59. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  60. }
  61. #if RAPIDJSON_HAS_STDSTRING
  62. {
  63. Pointer p(std::string("/foo"));
  64. EXPECT_TRUE(p.IsValid());
  65. EXPECT_EQ(1u, p.GetTokenCount());
  66. EXPECT_EQ(3u, p.GetTokens()[0].length);
  67. EXPECT_STREQ("foo", p.GetTokens()[0].name);
  68. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  69. }
  70. #endif
  71. {
  72. Pointer p("/foo/0");
  73. EXPECT_TRUE(p.IsValid());
  74. EXPECT_EQ(2u, p.GetTokenCount());
  75. EXPECT_EQ(3u, p.GetTokens()[0].length);
  76. EXPECT_STREQ("foo", p.GetTokens()[0].name);
  77. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  78. EXPECT_EQ(1u, p.GetTokens()[1].length);
  79. EXPECT_STREQ("0", p.GetTokens()[1].name);
  80. EXPECT_EQ(0u, p.GetTokens()[1].index);
  81. }
  82. {
  83. // Unescape ~1
  84. Pointer p("/a~1b");
  85. EXPECT_TRUE(p.IsValid());
  86. EXPECT_EQ(1u, p.GetTokenCount());
  87. EXPECT_EQ(3u, p.GetTokens()[0].length);
  88. EXPECT_STREQ("a/b", p.GetTokens()[0].name);
  89. }
  90. {
  91. // Unescape ~0
  92. Pointer p("/m~0n");
  93. EXPECT_TRUE(p.IsValid());
  94. EXPECT_EQ(1u, p.GetTokenCount());
  95. EXPECT_EQ(3u, p.GetTokens()[0].length);
  96. EXPECT_STREQ("m~n", p.GetTokens()[0].name);
  97. }
  98. {
  99. // empty name
  100. Pointer p("/");
  101. EXPECT_TRUE(p.IsValid());
  102. EXPECT_EQ(1u, p.GetTokenCount());
  103. EXPECT_EQ(0u, p.GetTokens()[0].length);
  104. EXPECT_STREQ("", p.GetTokens()[0].name);
  105. }
  106. {
  107. // empty and non-empty name
  108. Pointer p("//a");
  109. EXPECT_TRUE(p.IsValid());
  110. EXPECT_EQ(2u, p.GetTokenCount());
  111. EXPECT_EQ(0u, p.GetTokens()[0].length);
  112. EXPECT_STREQ("", p.GetTokens()[0].name);
  113. EXPECT_EQ(1u, p.GetTokens()[1].length);
  114. EXPECT_STREQ("a", p.GetTokens()[1].name);
  115. }
  116. {
  117. // Null characters
  118. Pointer p("/\0\0", 3);
  119. EXPECT_TRUE(p.IsValid());
  120. EXPECT_EQ(1u, p.GetTokenCount());
  121. EXPECT_EQ(2u, p.GetTokens()[0].length);
  122. EXPECT_EQ('\0', p.GetTokens()[0].name[0]);
  123. EXPECT_EQ('\0', p.GetTokens()[0].name[1]);
  124. EXPECT_EQ('\0', p.GetTokens()[0].name[2]);
  125. }
  126. {
  127. // Valid index
  128. Pointer p("/123");
  129. EXPECT_TRUE(p.IsValid());
  130. EXPECT_EQ(1u, p.GetTokenCount());
  131. EXPECT_STREQ("123", p.GetTokens()[0].name);
  132. EXPECT_EQ(123u, p.GetTokens()[0].index);
  133. }
  134. {
  135. // Invalid index (with leading zero)
  136. Pointer p("/01");
  137. EXPECT_TRUE(p.IsValid());
  138. EXPECT_EQ(1u, p.GetTokenCount());
  139. EXPECT_STREQ("01", p.GetTokens()[0].name);
  140. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  141. }
  142. if (sizeof(SizeType) == 4) {
  143. // Invalid index (overflow)
  144. Pointer p("/4294967296");
  145. EXPECT_TRUE(p.IsValid());
  146. EXPECT_EQ(1u, p.GetTokenCount());
  147. EXPECT_STREQ("4294967296", p.GetTokens()[0].name);
  148. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  149. }
  150. {
  151. // kPointerParseErrorTokenMustBeginWithSolidus
  152. Pointer p(" ");
  153. EXPECT_FALSE(p.IsValid());
  154. EXPECT_EQ(kPointerParseErrorTokenMustBeginWithSolidus, p.GetParseErrorCode());
  155. EXPECT_EQ(0u, p.GetParseErrorOffset());
  156. }
  157. {
  158. // kPointerParseErrorInvalidEscape
  159. Pointer p("/~");
  160. EXPECT_FALSE(p.IsValid());
  161. EXPECT_EQ(kPointerParseErrorInvalidEscape, p.GetParseErrorCode());
  162. EXPECT_EQ(2u, p.GetParseErrorOffset());
  163. }
  164. {
  165. // kPointerParseErrorInvalidEscape
  166. Pointer p("/~2");
  167. EXPECT_FALSE(p.IsValid());
  168. EXPECT_EQ(kPointerParseErrorInvalidEscape, p.GetParseErrorCode());
  169. EXPECT_EQ(2u, p.GetParseErrorOffset());
  170. }
  171. }
  172. TEST(Pointer, Parse_URIFragment) {
  173. {
  174. Pointer p("#");
  175. EXPECT_TRUE(p.IsValid());
  176. EXPECT_EQ(0u, p.GetTokenCount());
  177. }
  178. {
  179. Pointer p("#/foo");
  180. EXPECT_TRUE(p.IsValid());
  181. EXPECT_EQ(1u, p.GetTokenCount());
  182. EXPECT_EQ(3u, p.GetTokens()[0].length);
  183. EXPECT_STREQ("foo", p.GetTokens()[0].name);
  184. }
  185. {
  186. Pointer p("#/foo/0");
  187. EXPECT_TRUE(p.IsValid());
  188. EXPECT_EQ(2u, p.GetTokenCount());
  189. EXPECT_EQ(3u, p.GetTokens()[0].length);
  190. EXPECT_STREQ("foo", p.GetTokens()[0].name);
  191. EXPECT_EQ(1u, p.GetTokens()[1].length);
  192. EXPECT_STREQ("0", p.GetTokens()[1].name);
  193. EXPECT_EQ(0u, p.GetTokens()[1].index);
  194. }
  195. {
  196. // Unescape ~1
  197. Pointer p("#/a~1b");
  198. EXPECT_TRUE(p.IsValid());
  199. EXPECT_EQ(1u, p.GetTokenCount());
  200. EXPECT_EQ(3u, p.GetTokens()[0].length);
  201. EXPECT_STREQ("a/b", p.GetTokens()[0].name);
  202. }
  203. {
  204. // Unescape ~0
  205. Pointer p("#/m~0n");
  206. EXPECT_TRUE(p.IsValid());
  207. EXPECT_EQ(1u, p.GetTokenCount());
  208. EXPECT_EQ(3u, p.GetTokens()[0].length);
  209. EXPECT_STREQ("m~n", p.GetTokens()[0].name);
  210. }
  211. {
  212. // empty name
  213. Pointer p("#/");
  214. EXPECT_TRUE(p.IsValid());
  215. EXPECT_EQ(1u, p.GetTokenCount());
  216. EXPECT_EQ(0u, p.GetTokens()[0].length);
  217. EXPECT_STREQ("", p.GetTokens()[0].name);
  218. }
  219. {
  220. // empty and non-empty name
  221. Pointer p("#//a");
  222. EXPECT_TRUE(p.IsValid());
  223. EXPECT_EQ(2u, p.GetTokenCount());
  224. EXPECT_EQ(0u, p.GetTokens()[0].length);
  225. EXPECT_STREQ("", p.GetTokens()[0].name);
  226. EXPECT_EQ(1u, p.GetTokens()[1].length);
  227. EXPECT_STREQ("a", p.GetTokens()[1].name);
  228. }
  229. {
  230. // Null characters
  231. Pointer p("#/%00%00");
  232. EXPECT_TRUE(p.IsValid());
  233. EXPECT_EQ(1u, p.GetTokenCount());
  234. EXPECT_EQ(2u, p.GetTokens()[0].length);
  235. EXPECT_EQ('\0', p.GetTokens()[0].name[0]);
  236. EXPECT_EQ('\0', p.GetTokens()[0].name[1]);
  237. EXPECT_EQ('\0', p.GetTokens()[0].name[2]);
  238. }
  239. {
  240. // Percentage Escapes
  241. EXPECT_STREQ("c%d", Pointer("#/c%25d").GetTokens()[0].name);
  242. EXPECT_STREQ("e^f", Pointer("#/e%5Ef").GetTokens()[0].name);
  243. EXPECT_STREQ("g|h", Pointer("#/g%7Ch").GetTokens()[0].name);
  244. EXPECT_STREQ("i\\j", Pointer("#/i%5Cj").GetTokens()[0].name);
  245. EXPECT_STREQ("k\"l", Pointer("#/k%22l").GetTokens()[0].name);
  246. EXPECT_STREQ(" ", Pointer("#/%20").GetTokens()[0].name);
  247. }
  248. {
  249. // Valid index
  250. Pointer p("#/123");
  251. EXPECT_TRUE(p.IsValid());
  252. EXPECT_EQ(1u, p.GetTokenCount());
  253. EXPECT_STREQ("123", p.GetTokens()[0].name);
  254. EXPECT_EQ(123u, p.GetTokens()[0].index);
  255. }
  256. {
  257. // Invalid index (with leading zero)
  258. Pointer p("#/01");
  259. EXPECT_TRUE(p.IsValid());
  260. EXPECT_EQ(1u, p.GetTokenCount());
  261. EXPECT_STREQ("01", p.GetTokens()[0].name);
  262. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  263. }
  264. if (sizeof(SizeType) == 4) {
  265. // Invalid index (overflow)
  266. Pointer p("#/4294967296");
  267. EXPECT_TRUE(p.IsValid());
  268. EXPECT_EQ(1u, p.GetTokenCount());
  269. EXPECT_STREQ("4294967296", p.GetTokens()[0].name);
  270. EXPECT_EQ(kPointerInvalidIndex, p.GetTokens()[0].index);
  271. }
  272. {
  273. // Decode UTF-8 perecent encoding to UTF-8
  274. Pointer p("#/%C2%A2");
  275. EXPECT_TRUE(p.IsValid());
  276. EXPECT_EQ(1u, p.GetTokenCount());
  277. EXPECT_STREQ("\xC2\xA2", p.GetTokens()[0].name);
  278. }
  279. {
  280. // Decode UTF-8 perecent encoding to UTF-16
  281. GenericPointer<GenericValue<UTF16<> > > p(L"#/%C2%A2");
  282. EXPECT_TRUE(p.IsValid());
  283. EXPECT_EQ(1u, p.GetTokenCount());
  284. EXPECT_EQ(static_cast<UTF16<>::Ch>(0x00A2), p.GetTokens()[0].name[0]);
  285. EXPECT_EQ(1u, p.GetTokens()[0].length);
  286. }
  287. {
  288. // Decode UTF-8 perecent encoding to UTF-16
  289. GenericPointer<GenericValue<UTF16<> > > p(L"#/%E2%82%AC");
  290. EXPECT_TRUE(p.IsValid());
  291. EXPECT_EQ(1u, p.GetTokenCount());
  292. EXPECT_EQ(static_cast<UTF16<>::Ch>(0x20AC), p.GetTokens()[0].name[0]);
  293. EXPECT_EQ(1u, p.GetTokens()[0].length);
  294. }
  295. {
  296. // kPointerParseErrorTokenMustBeginWithSolidus
  297. Pointer p("# ");
  298. EXPECT_FALSE(p.IsValid());
  299. EXPECT_EQ(kPointerParseErrorTokenMustBeginWithSolidus, p.GetParseErrorCode());
  300. EXPECT_EQ(1u, p.GetParseErrorOffset());
  301. }
  302. {
  303. // kPointerParseErrorInvalidEscape
  304. Pointer p("#/~");
  305. EXPECT_FALSE(p.IsValid());
  306. EXPECT_EQ(kPointerParseErrorInvalidEscape, p.GetParseErrorCode());
  307. EXPECT_EQ(3u, p.GetParseErrorOffset());
  308. }
  309. {
  310. // kPointerParseErrorInvalidEscape
  311. Pointer p("#/~2");
  312. EXPECT_FALSE(p.IsValid());
  313. EXPECT_EQ(kPointerParseErrorInvalidEscape, p.GetParseErrorCode());
  314. EXPECT_EQ(3u, p.GetParseErrorOffset());
  315. }
  316. {
  317. // kPointerParseErrorInvalidPercentEncoding
  318. Pointer p("#/%");
  319. EXPECT_FALSE(p.IsValid());
  320. EXPECT_EQ(kPointerParseErrorInvalidPercentEncoding, p.GetParseErrorCode());
  321. EXPECT_EQ(2u, p.GetParseErrorOffset());
  322. }
  323. {
  324. // kPointerParseErrorInvalidPercentEncoding (invalid hex)
  325. Pointer p("#/%g0");
  326. EXPECT_FALSE(p.IsValid());
  327. EXPECT_EQ(kPointerParseErrorInvalidPercentEncoding, p.GetParseErrorCode());
  328. EXPECT_EQ(2u, p.GetParseErrorOffset());
  329. }
  330. {
  331. // kPointerParseErrorInvalidPercentEncoding (invalid hex)
  332. Pointer p("#/%0g");
  333. EXPECT_FALSE(p.IsValid());
  334. EXPECT_EQ(kPointerParseErrorInvalidPercentEncoding, p.GetParseErrorCode());
  335. EXPECT_EQ(2u, p.GetParseErrorOffset());
  336. }
  337. {
  338. // kPointerParseErrorInvalidPercentEncoding (incomplete UTF-8 sequence)
  339. Pointer p("#/%C2");
  340. EXPECT_FALSE(p.IsValid());
  341. EXPECT_EQ(kPointerParseErrorInvalidPercentEncoding, p.GetParseErrorCode());
  342. EXPECT_EQ(2u, p.GetParseErrorOffset());
  343. }
  344. {
  345. // kPointerParseErrorCharacterMustPercentEncode
  346. Pointer p("#/ ");
  347. EXPECT_FALSE(p.IsValid());
  348. EXPECT_EQ(kPointerParseErrorCharacterMustPercentEncode, p.GetParseErrorCode());
  349. EXPECT_EQ(2u, p.GetParseErrorOffset());
  350. }
  351. {
  352. // kPointerParseErrorCharacterMustPercentEncode
  353. Pointer p("#/\n");
  354. EXPECT_FALSE(p.IsValid());
  355. EXPECT_EQ(kPointerParseErrorCharacterMustPercentEncode, p.GetParseErrorCode());
  356. EXPECT_EQ(2u, p.GetParseErrorOffset());
  357. }
  358. }
  359. TEST(Pointer, Stringify) {
  360. // Test by roundtrip
  361. const char* sources[] = {
  362. "",
  363. "/foo",
  364. "/foo/0",
  365. "/",
  366. "/a~1b",
  367. "/c%d",
  368. "/e^f",
  369. "/g|h",
  370. "/i\\j",
  371. "/k\"l",
  372. "/ ",
  373. "/m~0n",
  374. "/\xC2\xA2",
  375. "/\xE2\x82\xAC",
  376. "/\xF0\x9D\x84\x9E"
  377. };
  378. for (size_t i = 0; i < sizeof(sources) / sizeof(sources[0]); i++) {
  379. Pointer p(sources[i]);
  380. StringBuffer s;
  381. EXPECT_TRUE(p.Stringify(s));
  382. EXPECT_STREQ(sources[i], s.GetString());
  383. // Stringify to URI fragment
  384. StringBuffer s2;
  385. EXPECT_TRUE(p.StringifyUriFragment(s2));
  386. Pointer p2(s2.GetString(), s2.GetSize());
  387. EXPECT_TRUE(p2.IsValid());
  388. EXPECT_TRUE(p == p2);
  389. }
  390. {
  391. // Strigify to URI fragment with an invalid UTF-8 sequence
  392. Pointer p("/\xC2");
  393. StringBuffer s;
  394. EXPECT_FALSE(p.StringifyUriFragment(s));
  395. }
  396. }
  397. // Construct a Pointer with static tokens, no dynamic allocation involved.
  398. #define NAME(s) { s, static_cast<SizeType>(sizeof(s) / sizeof(s[0]) - 1), kPointerInvalidIndex }
  399. #define INDEX(i) { #i, static_cast<SizeType>(sizeof(#i) - 1), i }
  400. static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(0) }; // equivalent to "/foo/0"
  401. #undef NAME
  402. #undef INDEX
  403. TEST(Pointer, ConstructorWithToken) {
  404. Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0]));
  405. EXPECT_TRUE(p.IsValid());
  406. EXPECT_EQ(2u, p.GetTokenCount());
  407. EXPECT_EQ(3u, p.GetTokens()[0].length);
  408. EXPECT_STREQ("foo", p.GetTokens()[0].name);
  409. EXPECT_EQ(1u, p.GetTokens()[1].length);
  410. EXPECT_STREQ("0", p.GetTokens()[1].name);
  411. EXPECT_EQ(0u, p.GetTokens()[1].index);
  412. }
  413. TEST(Pointer, CopyConstructor) {
  414. {
  415. CrtAllocator allocator;
  416. Pointer p("/foo/0", &allocator);
  417. Pointer q(p);
  418. EXPECT_TRUE(q.IsValid());
  419. EXPECT_EQ(2u, q.GetTokenCount());
  420. EXPECT_EQ(3u, q.GetTokens()[0].length);
  421. EXPECT_STREQ("foo", q.GetTokens()[0].name);
  422. EXPECT_EQ(1u, q.GetTokens()[1].length);
  423. EXPECT_STREQ("0", q.GetTokens()[1].name);
  424. EXPECT_EQ(0u, q.GetTokens()[1].index);
  425. EXPECT_EQ(&p.GetAllocator(), &q.GetAllocator());
  426. }
  427. // Static tokens
  428. {
  429. Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0]));
  430. Pointer q(p);
  431. EXPECT_TRUE(q.IsValid());
  432. EXPECT_EQ(2u, q.GetTokenCount());
  433. EXPECT_EQ(3u, q.GetTokens()[0].length);
  434. EXPECT_STREQ("foo", q.GetTokens()[0].name);
  435. EXPECT_EQ(1u, q.GetTokens()[1].length);
  436. EXPECT_STREQ("0", q.GetTokens()[1].name);
  437. EXPECT_EQ(0u, q.GetTokens()[1].index);
  438. }
  439. }
  440. TEST(Pointer, Assignment) {
  441. {
  442. CrtAllocator allocator;
  443. Pointer p("/foo/0", &allocator);
  444. Pointer q;
  445. q = p;
  446. EXPECT_TRUE(q.IsValid());
  447. EXPECT_EQ(2u, q.GetTokenCount());
  448. EXPECT_EQ(3u, q.GetTokens()[0].length);
  449. EXPECT_STREQ("foo", q.GetTokens()[0].name);
  450. EXPECT_EQ(1u, q.GetTokens()[1].length);
  451. EXPECT_STREQ("0", q.GetTokens()[1].name);
  452. EXPECT_EQ(0u, q.GetTokens()[1].index);
  453. EXPECT_NE(&p.GetAllocator(), &q.GetAllocator());
  454. q = static_cast<const Pointer &>(q);
  455. EXPECT_TRUE(q.IsValid());
  456. EXPECT_EQ(2u, q.GetTokenCount());
  457. EXPECT_EQ(3u, q.GetTokens()[0].length);
  458. EXPECT_STREQ("foo", q.GetTokens()[0].name);
  459. EXPECT_EQ(1u, q.GetTokens()[1].length);
  460. EXPECT_STREQ("0", q.GetTokens()[1].name);
  461. EXPECT_EQ(0u, q.GetTokens()[1].index);
  462. EXPECT_NE(&p.GetAllocator(), &q.GetAllocator());
  463. }
  464. // Static tokens
  465. {
  466. Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0]));
  467. Pointer q;
  468. q = p;
  469. EXPECT_TRUE(q.IsValid());
  470. EXPECT_EQ(2u, q.GetTokenCount());
  471. EXPECT_EQ(3u, q.GetTokens()[0].length);
  472. EXPECT_STREQ("foo", q.GetTokens()[0].name);
  473. EXPECT_EQ(1u, q.GetTokens()[1].length);
  474. EXPECT_STREQ("0", q.GetTokens()[1].name);
  475. EXPECT_EQ(0u, q.GetTokens()[1].index);
  476. }
  477. }
  478. TEST(Pointer, Swap) {
  479. Pointer p("/foo/0");
  480. Pointer q(&p.GetAllocator());
  481. q.Swap(p);
  482. EXPECT_EQ(&q.GetAllocator(), &p.GetAllocator());
  483. EXPECT_TRUE(p.IsValid());
  484. EXPECT_TRUE(q.IsValid());
  485. EXPECT_EQ(0u, p.GetTokenCount());
  486. EXPECT_EQ(2u, q.GetTokenCount());
  487. EXPECT_EQ(3u, q.GetTokens()[0].length);
  488. EXPECT_STREQ("foo", q.GetTokens()[0].name);
  489. EXPECT_EQ(1u, q.GetTokens()[1].length);
  490. EXPECT_STREQ("0", q.GetTokens()[1].name);
  491. EXPECT_EQ(0u, q.GetTokens()[1].index);
  492. // std::swap compatibility
  493. std::swap(p, q);
  494. EXPECT_EQ(&p.GetAllocator(), &q.GetAllocator());
  495. EXPECT_TRUE(q.IsValid());
  496. EXPECT_TRUE(p.IsValid());
  497. EXPECT_EQ(0u, q.GetTokenCount());
  498. EXPECT_EQ(2u, p.GetTokenCount());
  499. EXPECT_EQ(3u, p.GetTokens()[0].length);
  500. EXPECT_STREQ("foo", p.GetTokens()[0].name);
  501. EXPECT_EQ(1u, p.GetTokens()[1].length);
  502. EXPECT_STREQ("0", p.GetTokens()[1].name);
  503. EXPECT_EQ(0u, p.GetTokens()[1].index);
  504. }
  505. TEST(Pointer, Append) {
  506. {
  507. Pointer p;
  508. Pointer q = p.Append("foo");
  509. EXPECT_TRUE(Pointer("/foo") == q);
  510. q = q.Append(1234);
  511. EXPECT_TRUE(Pointer("/foo/1234") == q);
  512. q = q.Append("");
  513. EXPECT_TRUE(Pointer("/foo/1234/") == q);
  514. }
  515. {
  516. Pointer p;
  517. Pointer q = p.Append(Value("foo").Move());
  518. EXPECT_TRUE(Pointer("/foo") == q);
  519. q = q.Append(Value(1234).Move());
  520. EXPECT_TRUE(Pointer("/foo/1234") == q);
  521. q = q.Append(Value(kStringType).Move());
  522. EXPECT_TRUE(Pointer("/foo/1234/") == q);
  523. }
  524. #if RAPIDJSON_HAS_STDSTRING
  525. {
  526. Pointer p;
  527. Pointer q = p.Append(std::string("foo"));
  528. EXPECT_TRUE(Pointer("/foo") == q);
  529. }
  530. #endif
  531. }
  532. TEST(Pointer, Equality) {
  533. EXPECT_TRUE(Pointer("/foo/0") == Pointer("/foo/0"));
  534. EXPECT_FALSE(Pointer("/foo/0") == Pointer("/foo/1"));
  535. EXPECT_FALSE(Pointer("/foo/0") == Pointer("/foo/0/1"));
  536. EXPECT_FALSE(Pointer("/foo/0") == Pointer("a"));
  537. EXPECT_FALSE(Pointer("a") == Pointer("a")); // Invalid always not equal
  538. }
  539. TEST(Pointer, Inequality) {
  540. EXPECT_FALSE(Pointer("/foo/0") != Pointer("/foo/0"));
  541. EXPECT_TRUE(Pointer("/foo/0") != Pointer("/foo/1"));
  542. EXPECT_TRUE(Pointer("/foo/0") != Pointer("/foo/0/1"));
  543. EXPECT_TRUE(Pointer("/foo/0") != Pointer("a"));
  544. EXPECT_TRUE(Pointer("a") != Pointer("a")); // Invalid always not equal
  545. }
  546. TEST(Pointer, Create) {
  547. Document d;
  548. {
  549. Value* v = &Pointer("").Create(d, d.GetAllocator());
  550. EXPECT_EQ(&d, v);
  551. }
  552. {
  553. Value* v = &Pointer("/foo").Create(d, d.GetAllocator());
  554. EXPECT_EQ(&d["foo"], v);
  555. }
  556. {
  557. Value* v = &Pointer("/foo/0").Create(d, d.GetAllocator());
  558. EXPECT_EQ(&d["foo"][0], v);
  559. }
  560. {
  561. Value* v = &Pointer("/foo/-").Create(d, d.GetAllocator());
  562. EXPECT_EQ(&d["foo"][1], v);
  563. }
  564. {
  565. Value* v = &Pointer("/foo/-/-").Create(d, d.GetAllocator());
  566. // "foo/-" is a newly created null value x.
  567. // "foo/-/-" finds that x is not an array, it converts x to empty object
  568. // and treats - as "-" member name
  569. EXPECT_EQ(&d["foo"][2]["-"], v);
  570. }
  571. {
  572. // Document with no allocator
  573. Value* v = &Pointer("/foo/-").Create(d);
  574. EXPECT_EQ(&d["foo"][3], v);
  575. }
  576. {
  577. // Value (not document) must give allocator
  578. Value* v = &Pointer("/-").Create(d["foo"], d.GetAllocator());
  579. EXPECT_EQ(&d["foo"][4], v);
  580. }
  581. }
  582. TEST(Pointer, Get) {
  583. Document d;
  584. d.Parse(kJson);
  585. EXPECT_EQ(&d, Pointer("").Get(d));
  586. EXPECT_EQ(&d["foo"], Pointer("/foo").Get(d));
  587. EXPECT_EQ(&d["foo"][0], Pointer("/foo/0").Get(d));
  588. EXPECT_EQ(&d[""], Pointer("/").Get(d));
  589. EXPECT_EQ(&d["a/b"], Pointer("/a~1b").Get(d));
  590. EXPECT_EQ(&d["c%d"], Pointer("/c%d").Get(d));
  591. EXPECT_EQ(&d["e^f"], Pointer("/e^f").Get(d));
  592. EXPECT_EQ(&d["g|h"], Pointer("/g|h").Get(d));
  593. EXPECT_EQ(&d["i\\j"], Pointer("/i\\j").Get(d));
  594. EXPECT_EQ(&d["k\"l"], Pointer("/k\"l").Get(d));
  595. EXPECT_EQ(&d[" "], Pointer("/ ").Get(d));
  596. EXPECT_EQ(&d["m~n"], Pointer("/m~0n").Get(d));
  597. EXPECT_TRUE(Pointer("/abc").Get(d) == 0);
  598. size_t unresolvedTokenIndex;
  599. EXPECT_TRUE(Pointer("/foo/2").Get(d, &unresolvedTokenIndex) == 0); // Out of boundary
  600. EXPECT_EQ(1u, unresolvedTokenIndex);
  601. EXPECT_TRUE(Pointer("/foo/a").Get(d, &unresolvedTokenIndex) == 0); // "/foo" is an array, cannot query by "a"
  602. EXPECT_EQ(1u, unresolvedTokenIndex);
  603. EXPECT_TRUE(Pointer("/foo/0/0").Get(d, &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
  604. EXPECT_EQ(2u, unresolvedTokenIndex);
  605. EXPECT_TRUE(Pointer("/foo/0/a").Get(d, &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
  606. EXPECT_EQ(2u, unresolvedTokenIndex);
  607. Pointer::Token tokens[] = { { "foo ...", 3, kPointerInvalidIndex } };
  608. EXPECT_EQ(&d["foo"], Pointer(tokens, 1).Get(d));
  609. }
  610. TEST(Pointer, GetWithDefault) {
  611. Document d;
  612. d.Parse(kJson);
  613. // Value version
  614. Document::AllocatorType& a = d.GetAllocator();
  615. const Value v("qux");
  616. EXPECT_TRUE(Value("bar") == Pointer("/foo/0").GetWithDefault(d, v, a));
  617. EXPECT_TRUE(Value("baz") == Pointer("/foo/1").GetWithDefault(d, v, a));
  618. EXPECT_TRUE(Value("qux") == Pointer("/foo/2").GetWithDefault(d, v, a));
  619. EXPECT_TRUE(Value("last") == Pointer("/foo/-").GetWithDefault(d, Value("last").Move(), a));
  620. EXPECT_STREQ("last", d["foo"][3].GetString());
  621. EXPECT_TRUE(Pointer("/foo/null").GetWithDefault(d, Value().Move(), a).IsNull());
  622. EXPECT_TRUE(Pointer("/foo/null").GetWithDefault(d, "x", a).IsNull());
  623. // Generic version
  624. EXPECT_EQ(-1, Pointer("/foo/int").GetWithDefault(d, -1, a).GetInt());
  625. EXPECT_EQ(-1, Pointer("/foo/int").GetWithDefault(d, -2, a).GetInt());
  626. EXPECT_EQ(0x87654321, Pointer("/foo/uint").GetWithDefault(d, 0x87654321, a).GetUint());
  627. EXPECT_EQ(0x87654321, Pointer("/foo/uint").GetWithDefault(d, 0x12345678, a).GetUint());
  628. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  629. EXPECT_EQ(i64, Pointer("/foo/int64").GetWithDefault(d, i64, a).GetInt64());
  630. EXPECT_EQ(i64, Pointer("/foo/int64").GetWithDefault(d, i64 + 1, a).GetInt64());
  631. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  632. EXPECT_EQ(u64, Pointer("/foo/uint64").GetWithDefault(d, u64, a).GetUint64());
  633. EXPECT_EQ(u64, Pointer("/foo/uint64").GetWithDefault(d, u64 - 1, a).GetUint64());
  634. EXPECT_TRUE(Pointer("/foo/true").GetWithDefault(d, true, a).IsTrue());
  635. EXPECT_TRUE(Pointer("/foo/true").GetWithDefault(d, false, a).IsTrue());
  636. EXPECT_TRUE(Pointer("/foo/false").GetWithDefault(d, false, a).IsFalse());
  637. EXPECT_TRUE(Pointer("/foo/false").GetWithDefault(d, true, a).IsFalse());
  638. // StringRef version
  639. EXPECT_STREQ("Hello", Pointer("/foo/hello").GetWithDefault(d, "Hello", a).GetString());
  640. // Copy string version
  641. {
  642. char buffer[256];
  643. strcpy(buffer, "World");
  644. EXPECT_STREQ("World", Pointer("/foo/world").GetWithDefault(d, buffer, a).GetString());
  645. memset(buffer, 0, sizeof(buffer));
  646. }
  647. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  648. #if RAPIDJSON_HAS_STDSTRING
  649. EXPECT_STREQ("C++", Pointer("/foo/C++").GetWithDefault(d, std::string("C++"), a).GetString());
  650. #endif
  651. }
  652. TEST(Pointer, GetWithDefault_NoAllocator) {
  653. Document d;
  654. d.Parse(kJson);
  655. // Value version
  656. const Value v("qux");
  657. EXPECT_TRUE(Value("bar") == Pointer("/foo/0").GetWithDefault(d, v));
  658. EXPECT_TRUE(Value("baz") == Pointer("/foo/1").GetWithDefault(d, v));
  659. EXPECT_TRUE(Value("qux") == Pointer("/foo/2").GetWithDefault(d, v));
  660. EXPECT_TRUE(Value("last") == Pointer("/foo/-").GetWithDefault(d, Value("last").Move()));
  661. EXPECT_STREQ("last", d["foo"][3].GetString());
  662. EXPECT_TRUE(Pointer("/foo/null").GetWithDefault(d, Value().Move()).IsNull());
  663. EXPECT_TRUE(Pointer("/foo/null").GetWithDefault(d, "x").IsNull());
  664. // Generic version
  665. EXPECT_EQ(-1, Pointer("/foo/int").GetWithDefault(d, -1).GetInt());
  666. EXPECT_EQ(-1, Pointer("/foo/int").GetWithDefault(d, -2).GetInt());
  667. EXPECT_EQ(0x87654321, Pointer("/foo/uint").GetWithDefault(d, 0x87654321).GetUint());
  668. EXPECT_EQ(0x87654321, Pointer("/foo/uint").GetWithDefault(d, 0x12345678).GetUint());
  669. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  670. EXPECT_EQ(i64, Pointer("/foo/int64").GetWithDefault(d, i64).GetInt64());
  671. EXPECT_EQ(i64, Pointer("/foo/int64").GetWithDefault(d, i64 + 1).GetInt64());
  672. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  673. EXPECT_EQ(u64, Pointer("/foo/uint64").GetWithDefault(d, u64).GetUint64());
  674. EXPECT_EQ(u64, Pointer("/foo/uint64").GetWithDefault(d, u64 - 1).GetUint64());
  675. EXPECT_TRUE(Pointer("/foo/true").GetWithDefault(d, true).IsTrue());
  676. EXPECT_TRUE(Pointer("/foo/true").GetWithDefault(d, false).IsTrue());
  677. EXPECT_TRUE(Pointer("/foo/false").GetWithDefault(d, false).IsFalse());
  678. EXPECT_TRUE(Pointer("/foo/false").GetWithDefault(d, true).IsFalse());
  679. // StringRef version
  680. EXPECT_STREQ("Hello", Pointer("/foo/hello").GetWithDefault(d, "Hello").GetString());
  681. // Copy string version
  682. {
  683. char buffer[256];
  684. strcpy(buffer, "World");
  685. EXPECT_STREQ("World", Pointer("/foo/world").GetWithDefault(d, buffer).GetString());
  686. memset(buffer, 0, sizeof(buffer));
  687. }
  688. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  689. #if RAPIDJSON_HAS_STDSTRING
  690. EXPECT_STREQ("C++", Pointer("/foo/C++").GetWithDefault(d, std::string("C++")).GetString());
  691. #endif
  692. }
  693. TEST(Pointer, Set) {
  694. Document d;
  695. d.Parse(kJson);
  696. Document::AllocatorType& a = d.GetAllocator();
  697. // Value version
  698. Pointer("/foo/0").Set(d, Value(123).Move(), a);
  699. EXPECT_EQ(123, d["foo"][0].GetInt());
  700. Pointer("/foo/-").Set(d, Value(456).Move(), a);
  701. EXPECT_EQ(456, d["foo"][2].GetInt());
  702. Pointer("/foo/null").Set(d, Value().Move(), a);
  703. EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull());
  704. // Const Value version
  705. const Value foo(d["foo"], a);
  706. Pointer("/clone").Set(d, foo, a);
  707. EXPECT_EQ(foo, *GetValueByPointer(d, "/clone"));
  708. // Generic version
  709. Pointer("/foo/int").Set(d, -1, a);
  710. EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt());
  711. Pointer("/foo/uint").Set(d, 0x87654321, a);
  712. EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint());
  713. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  714. Pointer("/foo/int64").Set(d, i64, a);
  715. EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64());
  716. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  717. Pointer("/foo/uint64").Set(d, u64, a);
  718. EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64());
  719. Pointer("/foo/true").Set(d, true, a);
  720. EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue());
  721. Pointer("/foo/false").Set(d, false, a);
  722. EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse());
  723. // StringRef version
  724. Pointer("/foo/hello").Set(d, "Hello", a);
  725. EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString());
  726. // Copy string version
  727. {
  728. char buffer[256];
  729. strcpy(buffer, "World");
  730. Pointer("/foo/world").Set(d, buffer, a);
  731. memset(buffer, 0, sizeof(buffer));
  732. }
  733. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  734. #if RAPIDJSON_HAS_STDSTRING
  735. Pointer("/foo/c++").Set(d, std::string("C++"), a);
  736. EXPECT_STREQ("C++", GetValueByPointer(d, "/foo/c++")->GetString());
  737. #endif
  738. }
  739. TEST(Pointer, Set_NoAllocator) {
  740. Document d;
  741. d.Parse(kJson);
  742. // Value version
  743. Pointer("/foo/0").Set(d, Value(123).Move());
  744. EXPECT_EQ(123, d["foo"][0].GetInt());
  745. Pointer("/foo/-").Set(d, Value(456).Move());
  746. EXPECT_EQ(456, d["foo"][2].GetInt());
  747. Pointer("/foo/null").Set(d, Value().Move());
  748. EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull());
  749. // Const Value version
  750. const Value foo(d["foo"], d.GetAllocator());
  751. Pointer("/clone").Set(d, foo);
  752. EXPECT_EQ(foo, *GetValueByPointer(d, "/clone"));
  753. // Generic version
  754. Pointer("/foo/int").Set(d, -1);
  755. EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt());
  756. Pointer("/foo/uint").Set(d, 0x87654321);
  757. EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint());
  758. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  759. Pointer("/foo/int64").Set(d, i64);
  760. EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64());
  761. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  762. Pointer("/foo/uint64").Set(d, u64);
  763. EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64());
  764. Pointer("/foo/true").Set(d, true);
  765. EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue());
  766. Pointer("/foo/false").Set(d, false);
  767. EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse());
  768. // StringRef version
  769. Pointer("/foo/hello").Set(d, "Hello");
  770. EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString());
  771. // Copy string version
  772. {
  773. char buffer[256];
  774. strcpy(buffer, "World");
  775. Pointer("/foo/world").Set(d, buffer);
  776. memset(buffer, 0, sizeof(buffer));
  777. }
  778. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  779. #if RAPIDJSON_HAS_STDSTRING
  780. Pointer("/foo/c++").Set(d, std::string("C++"));
  781. EXPECT_STREQ("C++", GetValueByPointer(d, "/foo/c++")->GetString());
  782. #endif
  783. }
  784. TEST(Pointer, Swap_Value) {
  785. Document d;
  786. d.Parse(kJson);
  787. Document::AllocatorType& a = d.GetAllocator();
  788. Pointer("/foo/0").Swap(d, *Pointer("/foo/1").Get(d), a);
  789. EXPECT_STREQ("baz", d["foo"][0].GetString());
  790. EXPECT_STREQ("bar", d["foo"][1].GetString());
  791. }
  792. TEST(Pointer, Swap_Value_NoAllocator) {
  793. Document d;
  794. d.Parse(kJson);
  795. Pointer("/foo/0").Swap(d, *Pointer("/foo/1").Get(d));
  796. EXPECT_STREQ("baz", d["foo"][0].GetString());
  797. EXPECT_STREQ("bar", d["foo"][1].GetString());
  798. }
  799. TEST(Pointer, Erase) {
  800. Document d;
  801. d.Parse(kJson);
  802. EXPECT_FALSE(Pointer("").Erase(d));
  803. EXPECT_FALSE(Pointer("/nonexist").Erase(d));
  804. EXPECT_FALSE(Pointer("/nonexist/nonexist").Erase(d));
  805. EXPECT_FALSE(Pointer("/foo/nonexist").Erase(d));
  806. EXPECT_FALSE(Pointer("/foo/nonexist/nonexist").Erase(d));
  807. EXPECT_FALSE(Pointer("/foo/0/nonexist").Erase(d));
  808. EXPECT_FALSE(Pointer("/foo/0/nonexist/nonexist").Erase(d));
  809. EXPECT_FALSE(Pointer("/foo/2/nonexist").Erase(d));
  810. EXPECT_TRUE(Pointer("/foo/0").Erase(d));
  811. EXPECT_EQ(1u, d["foo"].Size());
  812. EXPECT_STREQ("baz", d["foo"][0].GetString());
  813. EXPECT_TRUE(Pointer("/foo/0").Erase(d));
  814. EXPECT_TRUE(d["foo"].Empty());
  815. EXPECT_TRUE(Pointer("/foo").Erase(d));
  816. EXPECT_TRUE(Pointer("/foo").Get(d) == 0);
  817. Pointer("/a/0/b/0").Create(d);
  818. EXPECT_TRUE(Pointer("/a/0/b/0").Get(d) != 0);
  819. EXPECT_TRUE(Pointer("/a/0/b/0").Erase(d));
  820. EXPECT_TRUE(Pointer("/a/0/b/0").Get(d) == 0);
  821. EXPECT_TRUE(Pointer("/a/0/b").Get(d) != 0);
  822. EXPECT_TRUE(Pointer("/a/0/b").Erase(d));
  823. EXPECT_TRUE(Pointer("/a/0/b").Get(d) == 0);
  824. EXPECT_TRUE(Pointer("/a/0").Get(d) != 0);
  825. EXPECT_TRUE(Pointer("/a/0").Erase(d));
  826. EXPECT_TRUE(Pointer("/a/0").Get(d) == 0);
  827. EXPECT_TRUE(Pointer("/a").Get(d) != 0);
  828. EXPECT_TRUE(Pointer("/a").Erase(d));
  829. EXPECT_TRUE(Pointer("/a").Get(d) == 0);
  830. }
  831. TEST(Pointer, CreateValueByPointer) {
  832. Document d;
  833. Document::AllocatorType& a = d.GetAllocator();
  834. {
  835. Value& v = CreateValueByPointer(d, Pointer("/foo/0"), a);
  836. EXPECT_EQ(&d["foo"][0], &v);
  837. }
  838. {
  839. Value& v = CreateValueByPointer(d, "/foo/1", a);
  840. EXPECT_EQ(&d["foo"][1], &v);
  841. }
  842. }
  843. TEST(Pointer, CreateValueByPointer_NoAllocator) {
  844. Document d;
  845. {
  846. Value& v = CreateValueByPointer(d, Pointer("/foo/0"));
  847. EXPECT_EQ(&d["foo"][0], &v);
  848. }
  849. {
  850. Value& v = CreateValueByPointer(d, "/foo/1");
  851. EXPECT_EQ(&d["foo"][1], &v);
  852. }
  853. }
  854. TEST(Pointer, GetValueByPointer) {
  855. Document d;
  856. d.Parse(kJson);
  857. EXPECT_EQ(&d["foo"][0], GetValueByPointer(d, Pointer("/foo/0")));
  858. EXPECT_EQ(&d["foo"][0], GetValueByPointer(d, "/foo/0"));
  859. size_t unresolvedTokenIndex;
  860. EXPECT_TRUE(GetValueByPointer(d, "/foo/2", &unresolvedTokenIndex) == 0); // Out of boundary
  861. EXPECT_EQ(1u, unresolvedTokenIndex);
  862. EXPECT_TRUE(GetValueByPointer(d, "/foo/a", &unresolvedTokenIndex) == 0); // "/foo" is an array, cannot query by "a"
  863. EXPECT_EQ(1u, unresolvedTokenIndex);
  864. EXPECT_TRUE(GetValueByPointer(d, "/foo/0/0", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
  865. EXPECT_EQ(2u, unresolvedTokenIndex);
  866. EXPECT_TRUE(GetValueByPointer(d, "/foo/0/a", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
  867. EXPECT_EQ(2u, unresolvedTokenIndex);
  868. // const version
  869. const Value& v = d;
  870. EXPECT_EQ(&d["foo"][0], GetValueByPointer(v, Pointer("/foo/0")));
  871. EXPECT_EQ(&d["foo"][0], GetValueByPointer(v, "/foo/0"));
  872. EXPECT_TRUE(GetValueByPointer(v, "/foo/2", &unresolvedTokenIndex) == 0); // Out of boundary
  873. EXPECT_EQ(1u, unresolvedTokenIndex);
  874. EXPECT_TRUE(GetValueByPointer(v, "/foo/a", &unresolvedTokenIndex) == 0); // "/foo" is an array, cannot query by "a"
  875. EXPECT_EQ(1u, unresolvedTokenIndex);
  876. EXPECT_TRUE(GetValueByPointer(v, "/foo/0/0", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
  877. EXPECT_EQ(2u, unresolvedTokenIndex);
  878. EXPECT_TRUE(GetValueByPointer(v, "/foo/0/a", &unresolvedTokenIndex) == 0); // "/foo/0" is an string, cannot further query
  879. EXPECT_EQ(2u, unresolvedTokenIndex);
  880. }
  881. TEST(Pointer, GetValueByPointerWithDefault_Pointer) {
  882. Document d;
  883. d.Parse(kJson);
  884. Document::AllocatorType& a = d.GetAllocator();
  885. const Value v("qux");
  886. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, Pointer("/foo/0"), v, a));
  887. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, Pointer("/foo/0"), v, a));
  888. EXPECT_TRUE(Value("baz") == GetValueByPointerWithDefault(d, Pointer("/foo/1"), v, a));
  889. EXPECT_TRUE(Value("qux") == GetValueByPointerWithDefault(d, Pointer("/foo/2"), v, a));
  890. EXPECT_TRUE(Value("last") == GetValueByPointerWithDefault(d, Pointer("/foo/-"), Value("last").Move(), a));
  891. EXPECT_STREQ("last", d["foo"][3].GetString());
  892. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/null"), Value().Move(), a).IsNull());
  893. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/null"), "x", a).IsNull());
  894. // Generic version
  895. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, Pointer("/foo/int"), -1, a).GetInt());
  896. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, Pointer("/foo/int"), -2, a).GetInt());
  897. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, Pointer("/foo/uint"), 0x87654321, a).GetUint());
  898. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, Pointer("/foo/uint"), 0x12345678, a).GetUint());
  899. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  900. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, Pointer("/foo/int64"), i64, a).GetInt64());
  901. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, Pointer("/foo/int64"), i64 + 1, a).GetInt64());
  902. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  903. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, Pointer("/foo/uint64"), u64, a).GetUint64());
  904. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, Pointer("/foo/uint64"), u64 - 1, a).GetUint64());
  905. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/true"), true, a).IsTrue());
  906. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/true"), false, a).IsTrue());
  907. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/false"), false, a).IsFalse());
  908. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/false"), true, a).IsFalse());
  909. // StringRef version
  910. EXPECT_STREQ("Hello", GetValueByPointerWithDefault(d, Pointer("/foo/hello"), "Hello", a).GetString());
  911. // Copy string version
  912. {
  913. char buffer[256];
  914. strcpy(buffer, "World");
  915. EXPECT_STREQ("World", GetValueByPointerWithDefault(d, Pointer("/foo/world"), buffer, a).GetString());
  916. memset(buffer, 0, sizeof(buffer));
  917. }
  918. EXPECT_STREQ("World", GetValueByPointer(d, Pointer("/foo/world"))->GetString());
  919. #if RAPIDJSON_HAS_STDSTRING
  920. EXPECT_STREQ("C++", GetValueByPointerWithDefault(d, Pointer("/foo/C++"), std::string("C++"), a).GetString());
  921. #endif
  922. }
  923. TEST(Pointer, GetValueByPointerWithDefault_String) {
  924. Document d;
  925. d.Parse(kJson);
  926. Document::AllocatorType& a = d.GetAllocator();
  927. const Value v("qux");
  928. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, "/foo/0", v, a));
  929. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, "/foo/0", v, a));
  930. EXPECT_TRUE(Value("baz") == GetValueByPointerWithDefault(d, "/foo/1", v, a));
  931. EXPECT_TRUE(Value("qux") == GetValueByPointerWithDefault(d, "/foo/2", v, a));
  932. EXPECT_TRUE(Value("last") == GetValueByPointerWithDefault(d, "/foo/-", Value("last").Move(), a));
  933. EXPECT_STREQ("last", d["foo"][3].GetString());
  934. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/null", Value().Move(), a).IsNull());
  935. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/null", "x", a).IsNull());
  936. // Generic version
  937. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, "/foo/int", -1, a).GetInt());
  938. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, "/foo/int", -2, a).GetInt());
  939. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, "/foo/uint", 0x87654321, a).GetUint());
  940. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, "/foo/uint", 0x12345678, a).GetUint());
  941. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  942. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, "/foo/int64", i64, a).GetInt64());
  943. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, "/foo/int64", i64 + 1, a).GetInt64());
  944. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  945. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, "/foo/uint64", u64, a).GetUint64());
  946. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, "/foo/uint64", u64 - 1, a).GetUint64());
  947. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/true", true, a).IsTrue());
  948. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/true", false, a).IsTrue());
  949. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/false", false, a).IsFalse());
  950. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/false", true, a).IsFalse());
  951. // StringRef version
  952. EXPECT_STREQ("Hello", GetValueByPointerWithDefault(d, "/foo/hello", "Hello", a).GetString());
  953. // Copy string version
  954. {
  955. char buffer[256];
  956. strcpy(buffer, "World");
  957. EXPECT_STREQ("World", GetValueByPointerWithDefault(d, "/foo/world", buffer, a).GetString());
  958. memset(buffer, 0, sizeof(buffer));
  959. }
  960. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  961. #if RAPIDJSON_HAS_STDSTRING
  962. EXPECT_STREQ("C++", GetValueByPointerWithDefault(d, "/foo/C++", std::string("C++"), a).GetString());
  963. #endif
  964. }
  965. TEST(Pointer, GetValueByPointerWithDefault_Pointer_NoAllocator) {
  966. Document d;
  967. d.Parse(kJson);
  968. const Value v("qux");
  969. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, Pointer("/foo/0"), v));
  970. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, Pointer("/foo/0"), v));
  971. EXPECT_TRUE(Value("baz") == GetValueByPointerWithDefault(d, Pointer("/foo/1"), v));
  972. EXPECT_TRUE(Value("qux") == GetValueByPointerWithDefault(d, Pointer("/foo/2"), v));
  973. EXPECT_TRUE(Value("last") == GetValueByPointerWithDefault(d, Pointer("/foo/-"), Value("last").Move()));
  974. EXPECT_STREQ("last", d["foo"][3].GetString());
  975. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/null"), Value().Move()).IsNull());
  976. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/null"), "x").IsNull());
  977. // Generic version
  978. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, Pointer("/foo/int"), -1).GetInt());
  979. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, Pointer("/foo/int"), -2).GetInt());
  980. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, Pointer("/foo/uint"), 0x87654321).GetUint());
  981. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, Pointer("/foo/uint"), 0x12345678).GetUint());
  982. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  983. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, Pointer("/foo/int64"), i64).GetInt64());
  984. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, Pointer("/foo/int64"), i64 + 1).GetInt64());
  985. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  986. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, Pointer("/foo/uint64"), u64).GetUint64());
  987. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, Pointer("/foo/uint64"), u64 - 1).GetUint64());
  988. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/true"), true).IsTrue());
  989. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/true"), false).IsTrue());
  990. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/false"), false).IsFalse());
  991. EXPECT_TRUE(GetValueByPointerWithDefault(d, Pointer("/foo/false"), true).IsFalse());
  992. // StringRef version
  993. EXPECT_STREQ("Hello", GetValueByPointerWithDefault(d, Pointer("/foo/hello"), "Hello").GetString());
  994. // Copy string version
  995. {
  996. char buffer[256];
  997. strcpy(buffer, "World");
  998. EXPECT_STREQ("World", GetValueByPointerWithDefault(d, Pointer("/foo/world"), buffer).GetString());
  999. memset(buffer, 0, sizeof(buffer));
  1000. }
  1001. EXPECT_STREQ("World", GetValueByPointer(d, Pointer("/foo/world"))->GetString());
  1002. #if RAPIDJSON_HAS_STDSTRING
  1003. EXPECT_STREQ("C++", GetValueByPointerWithDefault(d, Pointer("/foo/C++"), std::string("C++")).GetString());
  1004. #endif
  1005. }
  1006. TEST(Pointer, GetValueByPointerWithDefault_String_NoAllocator) {
  1007. Document d;
  1008. d.Parse(kJson);
  1009. const Value v("qux");
  1010. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, "/foo/0", v));
  1011. EXPECT_TRUE(Value("bar") == GetValueByPointerWithDefault(d, "/foo/0", v));
  1012. EXPECT_TRUE(Value("baz") == GetValueByPointerWithDefault(d, "/foo/1", v));
  1013. EXPECT_TRUE(Value("qux") == GetValueByPointerWithDefault(d, "/foo/2", v));
  1014. EXPECT_TRUE(Value("last") == GetValueByPointerWithDefault(d, "/foo/-", Value("last").Move()));
  1015. EXPECT_STREQ("last", d["foo"][3].GetString());
  1016. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/null", Value().Move()).IsNull());
  1017. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/null", "x").IsNull());
  1018. // Generic version
  1019. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, "/foo/int", -1).GetInt());
  1020. EXPECT_EQ(-1, GetValueByPointerWithDefault(d, "/foo/int", -2).GetInt());
  1021. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, "/foo/uint", 0x87654321).GetUint());
  1022. EXPECT_EQ(0x87654321, GetValueByPointerWithDefault(d, "/foo/uint", 0x12345678).GetUint());
  1023. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  1024. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, "/foo/int64", i64).GetInt64());
  1025. EXPECT_EQ(i64, GetValueByPointerWithDefault(d, "/foo/int64", i64 + 1).GetInt64());
  1026. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  1027. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, "/foo/uint64", u64).GetUint64());
  1028. EXPECT_EQ(u64, GetValueByPointerWithDefault(d, "/foo/uint64", u64 - 1).GetUint64());
  1029. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/true", true).IsTrue());
  1030. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/true", false).IsTrue());
  1031. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/false", false).IsFalse());
  1032. EXPECT_TRUE(GetValueByPointerWithDefault(d, "/foo/false", true).IsFalse());
  1033. // StringRef version
  1034. EXPECT_STREQ("Hello", GetValueByPointerWithDefault(d, "/foo/hello", "Hello").GetString());
  1035. // Copy string version
  1036. {
  1037. char buffer[256];
  1038. strcpy(buffer, "World");
  1039. EXPECT_STREQ("World", GetValueByPointerWithDefault(d, "/foo/world", buffer).GetString());
  1040. memset(buffer, 0, sizeof(buffer));
  1041. }
  1042. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  1043. #if RAPIDJSON_HAS_STDSTRING
  1044. EXPECT_STREQ("C++", GetValueByPointerWithDefault(d, Pointer("/foo/C++"), std::string("C++")).GetString());
  1045. #endif
  1046. }
  1047. TEST(Pointer, SetValueByPointer_Pointer) {
  1048. Document d;
  1049. d.Parse(kJson);
  1050. Document::AllocatorType& a = d.GetAllocator();
  1051. // Value version
  1052. SetValueByPointer(d, Pointer("/foo/0"), Value(123).Move(), a);
  1053. EXPECT_EQ(123, d["foo"][0].GetInt());
  1054. SetValueByPointer(d, Pointer("/foo/null"), Value().Move(), a);
  1055. EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull());
  1056. // Const Value version
  1057. const Value foo(d["foo"], d.GetAllocator());
  1058. SetValueByPointer(d, Pointer("/clone"), foo, a);
  1059. EXPECT_EQ(foo, *GetValueByPointer(d, "/clone"));
  1060. // Generic version
  1061. SetValueByPointer(d, Pointer("/foo/int"), -1, a);
  1062. EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt());
  1063. SetValueByPointer(d, Pointer("/foo/uint"), 0x87654321, a);
  1064. EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint());
  1065. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  1066. SetValueByPointer(d, Pointer("/foo/int64"), i64, a);
  1067. EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64());
  1068. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  1069. SetValueByPointer(d, Pointer("/foo/uint64"), u64, a);
  1070. EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64());
  1071. SetValueByPointer(d, Pointer("/foo/true"), true, a);
  1072. EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue());
  1073. SetValueByPointer(d, Pointer("/foo/false"), false, a);
  1074. EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse());
  1075. // StringRef version
  1076. SetValueByPointer(d, Pointer("/foo/hello"), "Hello", a);
  1077. EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString());
  1078. // Copy string version
  1079. {
  1080. char buffer[256];
  1081. strcpy(buffer, "World");
  1082. SetValueByPointer(d, Pointer("/foo/world"), buffer, a);
  1083. memset(buffer, 0, sizeof(buffer));
  1084. }
  1085. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  1086. #if RAPIDJSON_HAS_STDSTRING
  1087. SetValueByPointer(d, Pointer("/foo/c++"), std::string("C++"), a);
  1088. EXPECT_STREQ("C++", GetValueByPointer(d, "/foo/c++")->GetString());
  1089. #endif
  1090. }
  1091. TEST(Pointer, SetValueByPointer_String) {
  1092. Document d;
  1093. d.Parse(kJson);
  1094. Document::AllocatorType& a = d.GetAllocator();
  1095. // Value version
  1096. SetValueByPointer(d, "/foo/0", Value(123).Move(), a);
  1097. EXPECT_EQ(123, d["foo"][0].GetInt());
  1098. SetValueByPointer(d, "/foo/null", Value().Move(), a);
  1099. EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull());
  1100. // Const Value version
  1101. const Value foo(d["foo"], d.GetAllocator());
  1102. SetValueByPointer(d, "/clone", foo, a);
  1103. EXPECT_EQ(foo, *GetValueByPointer(d, "/clone"));
  1104. // Generic version
  1105. SetValueByPointer(d, "/foo/int", -1, a);
  1106. EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt());
  1107. SetValueByPointer(d, "/foo/uint", 0x87654321, a);
  1108. EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint());
  1109. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  1110. SetValueByPointer(d, "/foo/int64", i64, a);
  1111. EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64());
  1112. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  1113. SetValueByPointer(d, "/foo/uint64", u64, a);
  1114. EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64());
  1115. SetValueByPointer(d, "/foo/true", true, a);
  1116. EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue());
  1117. SetValueByPointer(d, "/foo/false", false, a);
  1118. EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse());
  1119. // StringRef version
  1120. SetValueByPointer(d, "/foo/hello", "Hello", a);
  1121. EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString());
  1122. // Copy string version
  1123. {
  1124. char buffer[256];
  1125. strcpy(buffer, "World");
  1126. SetValueByPointer(d, "/foo/world", buffer, a);
  1127. memset(buffer, 0, sizeof(buffer));
  1128. }
  1129. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  1130. #if RAPIDJSON_HAS_STDSTRING
  1131. SetValueByPointer(d, "/foo/c++", std::string("C++"), a);
  1132. EXPECT_STREQ("C++", GetValueByPointer(d, "/foo/c++")->GetString());
  1133. #endif
  1134. }
  1135. TEST(Pointer, SetValueByPointer_Pointer_NoAllocator) {
  1136. Document d;
  1137. d.Parse(kJson);
  1138. // Value version
  1139. SetValueByPointer(d, Pointer("/foo/0"), Value(123).Move());
  1140. EXPECT_EQ(123, d["foo"][0].GetInt());
  1141. SetValueByPointer(d, Pointer("/foo/null"), Value().Move());
  1142. EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull());
  1143. // Const Value version
  1144. const Value foo(d["foo"], d.GetAllocator());
  1145. SetValueByPointer(d, Pointer("/clone"), foo);
  1146. EXPECT_EQ(foo, *GetValueByPointer(d, "/clone"));
  1147. // Generic version
  1148. SetValueByPointer(d, Pointer("/foo/int"), -1);
  1149. EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt());
  1150. SetValueByPointer(d, Pointer("/foo/uint"), 0x87654321);
  1151. EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint());
  1152. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  1153. SetValueByPointer(d, Pointer("/foo/int64"), i64);
  1154. EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64());
  1155. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  1156. SetValueByPointer(d, Pointer("/foo/uint64"), u64);
  1157. EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64());
  1158. SetValueByPointer(d, Pointer("/foo/true"), true);
  1159. EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue());
  1160. SetValueByPointer(d, Pointer("/foo/false"), false);
  1161. EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse());
  1162. // StringRef version
  1163. SetValueByPointer(d, Pointer("/foo/hello"), "Hello");
  1164. EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString());
  1165. // Copy string version
  1166. {
  1167. char buffer[256];
  1168. strcpy(buffer, "World");
  1169. SetValueByPointer(d, Pointer("/foo/world"), buffer);
  1170. memset(buffer, 0, sizeof(buffer));
  1171. }
  1172. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  1173. #if RAPIDJSON_HAS_STDSTRING
  1174. SetValueByPointer(d, Pointer("/foo/c++"), std::string("C++"));
  1175. EXPECT_STREQ("C++", GetValueByPointer(d, "/foo/c++")->GetString());
  1176. #endif
  1177. }
  1178. TEST(Pointer, SetValueByPointer_String_NoAllocator) {
  1179. Document d;
  1180. d.Parse(kJson);
  1181. // Value version
  1182. SetValueByPointer(d, "/foo/0", Value(123).Move());
  1183. EXPECT_EQ(123, d["foo"][0].GetInt());
  1184. SetValueByPointer(d, "/foo/null", Value().Move());
  1185. EXPECT_TRUE(GetValueByPointer(d, "/foo/null")->IsNull());
  1186. // Const Value version
  1187. const Value foo(d["foo"], d.GetAllocator());
  1188. SetValueByPointer(d, "/clone", foo);
  1189. EXPECT_EQ(foo, *GetValueByPointer(d, "/clone"));
  1190. // Generic version
  1191. SetValueByPointer(d, "/foo/int", -1);
  1192. EXPECT_EQ(-1, GetValueByPointer(d, "/foo/int")->GetInt());
  1193. SetValueByPointer(d, "/foo/uint", 0x87654321);
  1194. EXPECT_EQ(0x87654321, GetValueByPointer(d, "/foo/uint")->GetUint());
  1195. const int64_t i64 = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 0));
  1196. SetValueByPointer(d, "/foo/int64", i64);
  1197. EXPECT_EQ(i64, GetValueByPointer(d, "/foo/int64")->GetInt64());
  1198. const uint64_t u64 = RAPIDJSON_UINT64_C2(0xFFFFFFFFF, 0xFFFFFFFFF);
  1199. SetValueByPointer(d, "/foo/uint64", u64);
  1200. EXPECT_EQ(u64, GetValueByPointer(d, "/foo/uint64")->GetUint64());
  1201. SetValueByPointer(d, "/foo/true", true);
  1202. EXPECT_TRUE(GetValueByPointer(d, "/foo/true")->IsTrue());
  1203. SetValueByPointer(d, "/foo/false", false);
  1204. EXPECT_TRUE(GetValueByPointer(d, "/foo/false")->IsFalse());
  1205. // StringRef version
  1206. SetValueByPointer(d, "/foo/hello", "Hello");
  1207. EXPECT_STREQ("Hello", GetValueByPointer(d, "/foo/hello")->GetString());
  1208. // Copy string version
  1209. {
  1210. char buffer[256];
  1211. strcpy(buffer, "World");
  1212. SetValueByPointer(d, "/foo/world", buffer);
  1213. memset(buffer, 0, sizeof(buffer));
  1214. }
  1215. EXPECT_STREQ("World", GetValueByPointer(d, "/foo/world")->GetString());
  1216. #if RAPIDJSON_HAS_STDSTRING
  1217. SetValueByPointer(d, "/foo/c++", std::string("C++"));
  1218. EXPECT_STREQ("C++", GetValueByPointer(d, "/foo/c++")->GetString());
  1219. #endif
  1220. }
  1221. TEST(Pointer, SwapValueByPointer) {
  1222. Document d;
  1223. d.Parse(kJson);
  1224. Document::AllocatorType& a = d.GetAllocator();
  1225. SwapValueByPointer(d, Pointer("/foo/0"), *GetValueByPointer(d, "/foo/1"), a);
  1226. EXPECT_STREQ("baz", d["foo"][0].GetString());
  1227. EXPECT_STREQ("bar", d["foo"][1].GetString());
  1228. SwapValueByPointer(d, "/foo/0", *GetValueByPointer(d, "/foo/1"), a);
  1229. EXPECT_STREQ("bar", d["foo"][0].GetString());
  1230. EXPECT_STREQ("baz", d["foo"][1].GetString());
  1231. }
  1232. TEST(Pointer, SwapValueByPointer_NoAllocator) {
  1233. Document d;
  1234. d.Parse(kJson);
  1235. SwapValueByPointer(d, Pointer("/foo/0"), *GetValueByPointer(d, "/foo/1"));
  1236. EXPECT_STREQ("baz", d["foo"][0].GetString());
  1237. EXPECT_STREQ("bar", d["foo"][1].GetString());
  1238. SwapValueByPointer(d, "/foo/0", *GetValueByPointer(d, "/foo/1"));
  1239. EXPECT_STREQ("bar", d["foo"][0].GetString());
  1240. EXPECT_STREQ("baz", d["foo"][1].GetString());
  1241. }
  1242. TEST(Pointer, EraseValueByPointer_Pointer) {
  1243. Document d;
  1244. d.Parse(kJson);
  1245. EXPECT_FALSE(EraseValueByPointer(d, Pointer("")));
  1246. EXPECT_FALSE(Pointer("/foo/nonexist").Erase(d));
  1247. EXPECT_TRUE(EraseValueByPointer(d, Pointer("/foo/0")));
  1248. EXPECT_EQ(1u, d["foo"].Size());
  1249. EXPECT_STREQ("baz", d["foo"][0].GetString());
  1250. EXPECT_TRUE(EraseValueByPointer(d, Pointer("/foo/0")));
  1251. EXPECT_TRUE(d["foo"].Empty());
  1252. EXPECT_TRUE(EraseValueByPointer(d, Pointer("/foo")));
  1253. EXPECT_TRUE(Pointer("/foo").Get(d) == 0);
  1254. }
  1255. TEST(Pointer, EraseValueByPointer_String) {
  1256. Document d;
  1257. d.Parse(kJson);
  1258. EXPECT_FALSE(EraseValueByPointer(d, ""));
  1259. EXPECT_FALSE(Pointer("/foo/nonexist").Erase(d));
  1260. EXPECT_TRUE(EraseValueByPointer(d, "/foo/0"));
  1261. EXPECT_EQ(1u, d["foo"].Size());
  1262. EXPECT_STREQ("baz", d["foo"][0].GetString());
  1263. EXPECT_TRUE(EraseValueByPointer(d, "/foo/0"));
  1264. EXPECT_TRUE(d["foo"].Empty());
  1265. EXPECT_TRUE(EraseValueByPointer(d, "/foo"));
  1266. EXPECT_TRUE(Pointer("/foo").Get(d) == 0);
  1267. }
  1268. TEST(Pointer, Ambiguity) {
  1269. {
  1270. Document d;
  1271. d.Parse("{\"0\" : [123]}");
  1272. EXPECT_EQ(123, Pointer("/0/0").Get(d)->GetInt());
  1273. Pointer("/0/a").Set(d, 456); // Change array [123] to object {456}
  1274. EXPECT_EQ(456, Pointer("/0/a").Get(d)->GetInt());
  1275. }
  1276. {
  1277. Document d;
  1278. EXPECT_FALSE(d.Parse("[{\"0\": 123}]").HasParseError());
  1279. EXPECT_EQ(123, Pointer("/0/0").Get(d)->GetInt());
  1280. Pointer("/0/1").Set(d, 456); // 1 is treated as "1" to index object
  1281. EXPECT_EQ(123, Pointer("/0/0").Get(d)->GetInt());
  1282. EXPECT_EQ(456, Pointer("/0/1").Get(d)->GetInt());
  1283. }
  1284. }
  1285. TEST(Pointer, LessThan) {
  1286. static const struct {
  1287. const char *str;
  1288. bool valid;
  1289. } pointers[] = {
  1290. { "/a/b", true },
  1291. { "/a", true },
  1292. { "/d/1", true },
  1293. { "/d/2/z", true },
  1294. { "/d/2/3", true },
  1295. { "/d/2", true },
  1296. { "/a/c", true },
  1297. { "/e/f~g", false },
  1298. { "/d/2/zz", true },
  1299. { "/d/1", true },
  1300. { "/d/2/z", true },
  1301. { "/e/f~~g", false },
  1302. { "/e/f~0g", true },
  1303. { "/e/f~1g", true },
  1304. { "/e/f.g", true },
  1305. { "", true }
  1306. };
  1307. static const char *ordered_pointers[] = {
  1308. "",
  1309. "/a",
  1310. "/a/b",
  1311. "/a/c",
  1312. "/d/1",
  1313. "/d/1",
  1314. "/d/2",
  1315. "/e/f.g",
  1316. "/e/f~1g",
  1317. "/e/f~0g",
  1318. "/d/2/3",
  1319. "/d/2/z",
  1320. "/d/2/z",
  1321. "/d/2/zz",
  1322. NULL, // was invalid "/e/f~g"
  1323. NULL // was invalid "/e/f~~g"
  1324. };
  1325. typedef MemoryPoolAllocator<> AllocatorType;
  1326. typedef GenericPointer<Value, AllocatorType> PointerType;
  1327. typedef std::multimap<PointerType, size_t> PointerMap;
  1328. PointerMap map;
  1329. PointerMap::iterator it;
  1330. AllocatorType allocator;
  1331. size_t i;
  1332. EXPECT_EQ(sizeof(pointers) / sizeof(pointers[0]),
  1333. sizeof(ordered_pointers) / sizeof(ordered_pointers[0]));
  1334. for (i = 0; i < sizeof(pointers) / sizeof(pointers[0]); ++i) {
  1335. it = map.insert(PointerMap::value_type(PointerType(pointers[i].str, &allocator), i));
  1336. if (!it->first.IsValid()) {
  1337. EXPECT_EQ(++it, map.end());
  1338. }
  1339. }
  1340. for (i = 0, it = map.begin(); it != map.end(); ++it, ++i) {
  1341. EXPECT_TRUE(it->second < sizeof(pointers) / sizeof(pointers[0]));
  1342. EXPECT_EQ(it->first.IsValid(), pointers[it->second].valid);
  1343. EXPECT_TRUE(i < sizeof(ordered_pointers) / sizeof(ordered_pointers[0]));
  1344. EXPECT_EQ(it->first.IsValid(), !!ordered_pointers[i]);
  1345. if (it->first.IsValid()) {
  1346. std::stringstream ss;
  1347. OStreamWrapper os(ss);
  1348. EXPECT_TRUE(it->first.Stringify(os));
  1349. EXPECT_EQ(ss.str(), pointers[it->second].str);
  1350. EXPECT_EQ(ss.str(), ordered_pointers[i]);
  1351. }
  1352. }
  1353. }
  1354. // https://github.com/Tencent/rapidjson/issues/483
  1355. namespace myjson {
  1356. class MyAllocator
  1357. {
  1358. public:
  1359. static const bool kNeedFree = true;
  1360. void * Malloc(size_t _size) { return malloc(_size); }
  1361. void * Realloc(void *_org_p, size_t _org_size, size_t _new_size) { (void)_org_size; return realloc(_org_p, _new_size); }
  1362. static void Free(void *_p) { return free(_p); }
  1363. };
  1364. typedef rapidjson::GenericDocument<
  1365. rapidjson::UTF8<>,
  1366. rapidjson::MemoryPoolAllocator< MyAllocator >,
  1367. MyAllocator
  1368. > Document;
  1369. typedef rapidjson::GenericPointer<
  1370. ::myjson::Document::ValueType,
  1371. MyAllocator
  1372. > Pointer;
  1373. typedef ::myjson::Document::ValueType Value;
  1374. }
  1375. TEST(Pointer, Issue483) {
  1376. std::string mystr, path;
  1377. myjson::Document document;
  1378. myjson::Value value(rapidjson::kStringType);
  1379. value.SetString(mystr.c_str(), static_cast<SizeType>(mystr.length()), document.GetAllocator());
  1380. myjson::Pointer(path.c_str()).Set(document, value, document.GetAllocator());
  1381. }