valuetest.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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/document.h"
  16. #include <algorithm>
  17. #ifdef __clang__
  18. RAPIDJSON_DIAG_PUSH
  19. RAPIDJSON_DIAG_OFF(c++98-compat)
  20. #endif
  21. using namespace rapidjson;
  22. TEST(Value, Size) {
  23. if (sizeof(SizeType) == 4) {
  24. #if RAPIDJSON_48BITPOINTER_OPTIMIZATION
  25. EXPECT_EQ(16, sizeof(Value));
  26. #elif RAPIDJSON_64BIT
  27. EXPECT_EQ(24, sizeof(Value));
  28. #else
  29. EXPECT_EQ(16, sizeof(Value));
  30. #endif
  31. }
  32. }
  33. TEST(Value, DefaultConstructor) {
  34. Value x;
  35. EXPECT_EQ(kNullType, x.GetType());
  36. EXPECT_TRUE(x.IsNull());
  37. //std::cout << "sizeof(Value): " << sizeof(x) << std::endl;
  38. }
  39. // Should not pass compilation
  40. //TEST(Value, copy_constructor) {
  41. // Value x(1234);
  42. // Value y = x;
  43. //}
  44. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  45. #if 0 // Many old compiler does not support these. Turn it off temporaily.
  46. #include <type_traits>
  47. TEST(Value, Traits) {
  48. typedef GenericValue<UTF8<>, CrtAllocator> Value;
  49. static_assert(std::is_constructible<Value>::value, "");
  50. static_assert(std::is_default_constructible<Value>::value, "");
  51. #ifndef _MSC_VER
  52. static_assert(!std::is_copy_constructible<Value>::value, "");
  53. #endif
  54. static_assert(std::is_move_constructible<Value>::value, "");
  55. #ifndef _MSC_VER
  56. static_assert(std::is_nothrow_constructible<Value>::value, "");
  57. static_assert(std::is_nothrow_default_constructible<Value>::value, "");
  58. static_assert(!std::is_nothrow_copy_constructible<Value>::value, "");
  59. static_assert(std::is_nothrow_move_constructible<Value>::value, "");
  60. #endif
  61. static_assert(std::is_assignable<Value,Value>::value, "");
  62. #ifndef _MSC_VER
  63. static_assert(!std::is_copy_assignable<Value>::value, "");
  64. #endif
  65. static_assert(std::is_move_assignable<Value>::value, "");
  66. #ifndef _MSC_VER
  67. static_assert(std::is_nothrow_assignable<Value, Value>::value, "");
  68. #endif
  69. static_assert(!std::is_nothrow_copy_assignable<Value>::value, "");
  70. #ifndef _MSC_VER
  71. static_assert(std::is_nothrow_move_assignable<Value>::value, "");
  72. #endif
  73. static_assert(std::is_destructible<Value>::value, "");
  74. #ifndef _MSC_VER
  75. static_assert(std::is_nothrow_destructible<Value>::value, "");
  76. #endif
  77. }
  78. #endif
  79. TEST(Value, MoveConstructor) {
  80. typedef GenericValue<UTF8<>, CrtAllocator> V;
  81. V::AllocatorType allocator;
  82. V x((V(kArrayType)));
  83. x.Reserve(4u, allocator);
  84. x.PushBack(1, allocator).PushBack(2, allocator).PushBack(3, allocator).PushBack(4, allocator);
  85. EXPECT_TRUE(x.IsArray());
  86. EXPECT_EQ(4u, x.Size());
  87. // Value y(x); // does not compile (!is_copy_constructible)
  88. V y(std::move(x));
  89. EXPECT_TRUE(x.IsNull());
  90. EXPECT_TRUE(y.IsArray());
  91. EXPECT_EQ(4u, y.Size());
  92. // Value z = y; // does not compile (!is_copy_assignable)
  93. V z = std::move(y);
  94. EXPECT_TRUE(y.IsNull());
  95. EXPECT_TRUE(z.IsArray());
  96. EXPECT_EQ(4u, z.Size());
  97. }
  98. #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
  99. TEST(Value, AssignmentOperator) {
  100. Value x(1234);
  101. Value y;
  102. y = x;
  103. EXPECT_TRUE(x.IsNull()); // move semantic
  104. EXPECT_EQ(1234, y.GetInt());
  105. y = 5678;
  106. EXPECT_TRUE(y.IsInt());
  107. EXPECT_EQ(5678, y.GetInt());
  108. x = "Hello";
  109. EXPECT_TRUE(x.IsString());
  110. EXPECT_STREQ(x.GetString(),"Hello");
  111. y = StringRef(x.GetString(),x.GetStringLength());
  112. EXPECT_TRUE(y.IsString());
  113. EXPECT_EQ(y.GetString(),x.GetString());
  114. EXPECT_EQ(y.GetStringLength(),x.GetStringLength());
  115. static char mstr[] = "mutable";
  116. // y = mstr; // should not compile
  117. y = StringRef(mstr);
  118. EXPECT_TRUE(y.IsString());
  119. EXPECT_EQ(y.GetString(),mstr);
  120. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  121. // C++11 move assignment
  122. x = Value("World");
  123. EXPECT_TRUE(x.IsString());
  124. EXPECT_STREQ("World", x.GetString());
  125. x = std::move(y);
  126. EXPECT_TRUE(y.IsNull());
  127. EXPECT_TRUE(x.IsString());
  128. EXPECT_EQ(x.GetString(), mstr);
  129. y = std::move(Value().SetInt(1234));
  130. EXPECT_TRUE(y.IsInt());
  131. EXPECT_EQ(1234, y);
  132. #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
  133. }
  134. template <typename A, typename B>
  135. void TestEqual(const A& a, const B& b) {
  136. EXPECT_TRUE (a == b);
  137. EXPECT_FALSE(a != b);
  138. EXPECT_TRUE (b == a);
  139. EXPECT_FALSE(b != a);
  140. }
  141. template <typename A, typename B>
  142. void TestUnequal(const A& a, const B& b) {
  143. EXPECT_FALSE(a == b);
  144. EXPECT_TRUE (a != b);
  145. EXPECT_FALSE(b == a);
  146. EXPECT_TRUE (b != a);
  147. }
  148. TEST(Value, EqualtoOperator) {
  149. Value::AllocatorType allocator;
  150. Value x(kObjectType);
  151. x.AddMember("hello", "world", allocator)
  152. .AddMember("t", Value(true).Move(), allocator)
  153. .AddMember("f", Value(false).Move(), allocator)
  154. .AddMember("n", Value(kNullType).Move(), allocator)
  155. .AddMember("i", 123, allocator)
  156. .AddMember("pi", 3.14, allocator)
  157. .AddMember("a", Value(kArrayType).Move().PushBack(1, allocator).PushBack(2, allocator).PushBack(3, allocator), allocator);
  158. // Test templated operator==() and operator!=()
  159. TestEqual(x["hello"], "world");
  160. const char* cc = "world";
  161. TestEqual(x["hello"], cc);
  162. char* c = strdup("world");
  163. TestEqual(x["hello"], c);
  164. free(c);
  165. TestEqual(x["t"], true);
  166. TestEqual(x["f"], false);
  167. TestEqual(x["i"], 123);
  168. TestEqual(x["pi"], 3.14);
  169. // Test operator==() (including different allocators)
  170. CrtAllocator crtAllocator;
  171. GenericValue<UTF8<>, CrtAllocator> y;
  172. GenericDocument<UTF8<>, CrtAllocator> z(&crtAllocator);
  173. y.CopyFrom(x, crtAllocator);
  174. z.CopyFrom(y, z.GetAllocator());
  175. TestEqual(x, y);
  176. TestEqual(y, z);
  177. TestEqual(z, x);
  178. // Swapping member order should be fine.
  179. EXPECT_TRUE(y.RemoveMember("t"));
  180. TestUnequal(x, y);
  181. TestUnequal(z, y);
  182. EXPECT_TRUE(z.RemoveMember("t"));
  183. TestUnequal(x, z);
  184. TestEqual(y, z);
  185. y.AddMember("t", false, crtAllocator);
  186. z.AddMember("t", false, z.GetAllocator());
  187. TestUnequal(x, y);
  188. TestUnequal(z, x);
  189. y["t"] = true;
  190. z["t"] = true;
  191. TestEqual(x, y);
  192. TestEqual(y, z);
  193. TestEqual(z, x);
  194. // Swapping element order is not OK
  195. x["a"][0].Swap(x["a"][1]);
  196. TestUnequal(x, y);
  197. x["a"][0].Swap(x["a"][1]);
  198. TestEqual(x, y);
  199. // Array of different size
  200. x["a"].PushBack(4, allocator);
  201. TestUnequal(x, y);
  202. x["a"].PopBack();
  203. TestEqual(x, y);
  204. // Issue #129: compare Uint64
  205. x.SetUint64(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF0));
  206. y.SetUint64(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF));
  207. TestUnequal(x, y);
  208. }
  209. template <typename Value>
  210. void TestCopyFrom() {
  211. typename Value::AllocatorType a;
  212. Value v1(1234);
  213. Value v2(v1, a); // deep copy constructor
  214. EXPECT_TRUE(v1.GetType() == v2.GetType());
  215. EXPECT_EQ(v1.GetInt(), v2.GetInt());
  216. v1.SetString("foo");
  217. v2.CopyFrom(v1, a);
  218. EXPECT_TRUE(v1.GetType() == v2.GetType());
  219. EXPECT_STREQ(v1.GetString(), v2.GetString());
  220. EXPECT_EQ(v1.GetString(), v2.GetString()); // string NOT copied
  221. v1.SetString("bar", a); // copy string
  222. v2.CopyFrom(v1, a);
  223. EXPECT_TRUE(v1.GetType() == v2.GetType());
  224. EXPECT_STREQ(v1.GetString(), v2.GetString());
  225. EXPECT_NE(v1.GetString(), v2.GetString()); // string copied
  226. v1.SetArray().PushBack(1234, a);
  227. v2.CopyFrom(v1, a);
  228. EXPECT_TRUE(v2.IsArray());
  229. EXPECT_EQ(v1.Size(), v2.Size());
  230. v1.PushBack(Value().SetString("foo", a), a); // push string copy
  231. EXPECT_TRUE(v1.Size() != v2.Size());
  232. v2.CopyFrom(v1, a);
  233. EXPECT_TRUE(v1.Size() == v2.Size());
  234. EXPECT_STREQ(v1[1].GetString(), v2[1].GetString());
  235. EXPECT_NE(v1[1].GetString(), v2[1].GetString()); // string got copied
  236. }
  237. TEST(Value, CopyFrom) {
  238. TestCopyFrom<Value>();
  239. TestCopyFrom<GenericValue<UTF8<>, CrtAllocator> >();
  240. }
  241. TEST(Value, Swap) {
  242. Value v1(1234);
  243. Value v2(kObjectType);
  244. EXPECT_EQ(&v1, &v1.Swap(v2));
  245. EXPECT_TRUE(v1.IsObject());
  246. EXPECT_TRUE(v2.IsInt());
  247. EXPECT_EQ(1234, v2.GetInt());
  248. // testing std::swap compatibility
  249. using std::swap;
  250. swap(v1, v2);
  251. EXPECT_TRUE(v1.IsInt());
  252. EXPECT_TRUE(v2.IsObject());
  253. }
  254. TEST(Value, Null) {
  255. // Default constructor
  256. Value x;
  257. EXPECT_EQ(kNullType, x.GetType());
  258. EXPECT_TRUE(x.IsNull());
  259. EXPECT_FALSE(x.IsTrue());
  260. EXPECT_FALSE(x.IsFalse());
  261. EXPECT_FALSE(x.IsNumber());
  262. EXPECT_FALSE(x.IsString());
  263. EXPECT_FALSE(x.IsObject());
  264. EXPECT_FALSE(x.IsArray());
  265. // Constructor with type
  266. Value y(kNullType);
  267. EXPECT_TRUE(y.IsNull());
  268. // SetNull();
  269. Value z(true);
  270. z.SetNull();
  271. EXPECT_TRUE(z.IsNull());
  272. }
  273. TEST(Value, True) {
  274. // Constructor with bool
  275. Value x(true);
  276. EXPECT_EQ(kTrueType, x.GetType());
  277. EXPECT_TRUE(x.GetBool());
  278. EXPECT_TRUE(x.IsBool());
  279. EXPECT_TRUE(x.IsTrue());
  280. EXPECT_FALSE(x.IsNull());
  281. EXPECT_FALSE(x.IsFalse());
  282. EXPECT_FALSE(x.IsNumber());
  283. EXPECT_FALSE(x.IsString());
  284. EXPECT_FALSE(x.IsObject());
  285. EXPECT_FALSE(x.IsArray());
  286. // Constructor with type
  287. Value y(kTrueType);
  288. EXPECT_TRUE(y.IsTrue());
  289. // SetBool()
  290. Value z;
  291. z.SetBool(true);
  292. EXPECT_TRUE(z.IsTrue());
  293. // Templated functions
  294. EXPECT_TRUE(z.Is<bool>());
  295. EXPECT_TRUE(z.Get<bool>());
  296. EXPECT_FALSE(z.Set<bool>(false).Get<bool>());
  297. EXPECT_TRUE(z.Set(true).Get<bool>());
  298. }
  299. TEST(Value, False) {
  300. // Constructor with bool
  301. Value x(false);
  302. EXPECT_EQ(kFalseType, x.GetType());
  303. EXPECT_TRUE(x.IsBool());
  304. EXPECT_TRUE(x.IsFalse());
  305. EXPECT_FALSE(x.IsNull());
  306. EXPECT_FALSE(x.IsTrue());
  307. EXPECT_FALSE(x.GetBool());
  308. //EXPECT_FALSE((bool)x);
  309. EXPECT_FALSE(x.IsNumber());
  310. EXPECT_FALSE(x.IsString());
  311. EXPECT_FALSE(x.IsObject());
  312. EXPECT_FALSE(x.IsArray());
  313. // Constructor with type
  314. Value y(kFalseType);
  315. EXPECT_TRUE(y.IsFalse());
  316. // SetBool()
  317. Value z;
  318. z.SetBool(false);
  319. EXPECT_TRUE(z.IsFalse());
  320. }
  321. TEST(Value, Int) {
  322. // Constructor with int
  323. Value x(1234);
  324. EXPECT_EQ(kNumberType, x.GetType());
  325. EXPECT_EQ(1234, x.GetInt());
  326. EXPECT_EQ(1234u, x.GetUint());
  327. EXPECT_EQ(1234, x.GetInt64());
  328. EXPECT_EQ(1234u, x.GetUint64());
  329. EXPECT_NEAR(1234.0, x.GetDouble(), 0.0);
  330. //EXPECT_EQ(1234, (int)x);
  331. //EXPECT_EQ(1234, (unsigned)x);
  332. //EXPECT_EQ(1234, (int64_t)x);
  333. //EXPECT_EQ(1234, (uint64_t)x);
  334. //EXPECT_EQ(1234, (double)x);
  335. EXPECT_TRUE(x.IsNumber());
  336. EXPECT_TRUE(x.IsInt());
  337. EXPECT_TRUE(x.IsUint());
  338. EXPECT_TRUE(x.IsInt64());
  339. EXPECT_TRUE(x.IsUint64());
  340. EXPECT_FALSE(x.IsDouble());
  341. EXPECT_FALSE(x.IsFloat());
  342. EXPECT_FALSE(x.IsNull());
  343. EXPECT_FALSE(x.IsBool());
  344. EXPECT_FALSE(x.IsFalse());
  345. EXPECT_FALSE(x.IsTrue());
  346. EXPECT_FALSE(x.IsString());
  347. EXPECT_FALSE(x.IsObject());
  348. EXPECT_FALSE(x.IsArray());
  349. Value nx(-1234);
  350. EXPECT_EQ(-1234, nx.GetInt());
  351. EXPECT_EQ(-1234, nx.GetInt64());
  352. EXPECT_TRUE(nx.IsInt());
  353. EXPECT_TRUE(nx.IsInt64());
  354. EXPECT_FALSE(nx.IsUint());
  355. EXPECT_FALSE(nx.IsUint64());
  356. // Constructor with type
  357. Value y(kNumberType);
  358. EXPECT_TRUE(y.IsNumber());
  359. EXPECT_TRUE(y.IsInt());
  360. EXPECT_EQ(0, y.GetInt());
  361. // SetInt()
  362. Value z;
  363. z.SetInt(1234);
  364. EXPECT_EQ(1234, z.GetInt());
  365. // operator=(int)
  366. z = 5678;
  367. EXPECT_EQ(5678, z.GetInt());
  368. // Templated functions
  369. EXPECT_TRUE(z.Is<int>());
  370. EXPECT_EQ(5678, z.Get<int>());
  371. EXPECT_EQ(5679, z.Set(5679).Get<int>());
  372. EXPECT_EQ(5680, z.Set<int>(5680).Get<int>());
  373. }
  374. TEST(Value, Uint) {
  375. // Constructor with int
  376. Value x(1234u);
  377. EXPECT_EQ(kNumberType, x.GetType());
  378. EXPECT_EQ(1234, x.GetInt());
  379. EXPECT_EQ(1234u, x.GetUint());
  380. EXPECT_EQ(1234, x.GetInt64());
  381. EXPECT_EQ(1234u, x.GetUint64());
  382. EXPECT_TRUE(x.IsNumber());
  383. EXPECT_TRUE(x.IsInt());
  384. EXPECT_TRUE(x.IsUint());
  385. EXPECT_TRUE(x.IsInt64());
  386. EXPECT_TRUE(x.IsUint64());
  387. EXPECT_NEAR(1234.0, x.GetDouble(), 0.0); // Number can always be cast as double but !IsDouble().
  388. EXPECT_FALSE(x.IsDouble());
  389. EXPECT_FALSE(x.IsFloat());
  390. EXPECT_FALSE(x.IsNull());
  391. EXPECT_FALSE(x.IsBool());
  392. EXPECT_FALSE(x.IsFalse());
  393. EXPECT_FALSE(x.IsTrue());
  394. EXPECT_FALSE(x.IsString());
  395. EXPECT_FALSE(x.IsObject());
  396. EXPECT_FALSE(x.IsArray());
  397. // SetUint()
  398. Value z;
  399. z.SetUint(1234);
  400. EXPECT_EQ(1234u, z.GetUint());
  401. // operator=(unsigned)
  402. z = 5678u;
  403. EXPECT_EQ(5678u, z.GetUint());
  404. z = 2147483648u; // 2^31, cannot cast as int
  405. EXPECT_EQ(2147483648u, z.GetUint());
  406. EXPECT_FALSE(z.IsInt());
  407. EXPECT_TRUE(z.IsInt64()); // Issue 41: Incorrect parsing of unsigned int number types
  408. // Templated functions
  409. EXPECT_TRUE(z.Is<unsigned>());
  410. EXPECT_EQ(2147483648u, z.Get<unsigned>());
  411. EXPECT_EQ(2147483649u, z.Set(2147483649u).Get<unsigned>());
  412. EXPECT_EQ(2147483650u, z.Set<unsigned>(2147483650u).Get<unsigned>());
  413. }
  414. TEST(Value, Int64) {
  415. // Constructor with int
  416. Value x(int64_t(1234));
  417. EXPECT_EQ(kNumberType, x.GetType());
  418. EXPECT_EQ(1234, x.GetInt());
  419. EXPECT_EQ(1234u, x.GetUint());
  420. EXPECT_EQ(1234, x.GetInt64());
  421. EXPECT_EQ(1234u, x.GetUint64());
  422. EXPECT_TRUE(x.IsNumber());
  423. EXPECT_TRUE(x.IsInt());
  424. EXPECT_TRUE(x.IsUint());
  425. EXPECT_TRUE(x.IsInt64());
  426. EXPECT_TRUE(x.IsUint64());
  427. EXPECT_FALSE(x.IsDouble());
  428. EXPECT_FALSE(x.IsFloat());
  429. EXPECT_FALSE(x.IsNull());
  430. EXPECT_FALSE(x.IsBool());
  431. EXPECT_FALSE(x.IsFalse());
  432. EXPECT_FALSE(x.IsTrue());
  433. EXPECT_FALSE(x.IsString());
  434. EXPECT_FALSE(x.IsObject());
  435. EXPECT_FALSE(x.IsArray());
  436. Value nx(int64_t(-1234));
  437. EXPECT_EQ(-1234, nx.GetInt());
  438. EXPECT_EQ(-1234, nx.GetInt64());
  439. EXPECT_TRUE(nx.IsInt());
  440. EXPECT_TRUE(nx.IsInt64());
  441. EXPECT_FALSE(nx.IsUint());
  442. EXPECT_FALSE(nx.IsUint64());
  443. // SetInt64()
  444. Value z;
  445. z.SetInt64(1234);
  446. EXPECT_EQ(1234, z.GetInt64());
  447. z.SetInt64(2147483648u); // 2^31, cannot cast as int
  448. EXPECT_FALSE(z.IsInt());
  449. EXPECT_TRUE(z.IsUint());
  450. EXPECT_NEAR(2147483648.0, z.GetDouble(), 0.0);
  451. z.SetInt64(int64_t(4294967295u) + 1); // 2^32, cannot cast as uint
  452. EXPECT_FALSE(z.IsInt());
  453. EXPECT_FALSE(z.IsUint());
  454. EXPECT_NEAR(4294967296.0, z.GetDouble(), 0.0);
  455. z.SetInt64(-int64_t(2147483648u) - 1); // -2^31-1, cannot cast as int
  456. EXPECT_FALSE(z.IsInt());
  457. EXPECT_NEAR(-2147483649.0, z.GetDouble(), 0.0);
  458. int64_t i = static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x80000000, 00000000));
  459. z.SetInt64(i);
  460. EXPECT_DOUBLE_EQ(-9223372036854775808.0, z.GetDouble());
  461. // Templated functions
  462. EXPECT_TRUE(z.Is<int64_t>());
  463. EXPECT_EQ(i, z.Get<int64_t>());
  464. #if 0 // signed integer underflow is undefined behaviour
  465. EXPECT_EQ(i - 1, z.Set(i - 1).Get<int64_t>());
  466. EXPECT_EQ(i - 2, z.Set<int64_t>(i - 2).Get<int64_t>());
  467. #endif
  468. }
  469. TEST(Value, Uint64) {
  470. // Constructor with int
  471. Value x(uint64_t(1234));
  472. EXPECT_EQ(kNumberType, x.GetType());
  473. EXPECT_EQ(1234, x.GetInt());
  474. EXPECT_EQ(1234u, x.GetUint());
  475. EXPECT_EQ(1234, x.GetInt64());
  476. EXPECT_EQ(1234u, x.GetUint64());
  477. EXPECT_TRUE(x.IsNumber());
  478. EXPECT_TRUE(x.IsInt());
  479. EXPECT_TRUE(x.IsUint());
  480. EXPECT_TRUE(x.IsInt64());
  481. EXPECT_TRUE(x.IsUint64());
  482. EXPECT_FALSE(x.IsDouble());
  483. EXPECT_FALSE(x.IsFloat());
  484. EXPECT_FALSE(x.IsNull());
  485. EXPECT_FALSE(x.IsBool());
  486. EXPECT_FALSE(x.IsFalse());
  487. EXPECT_FALSE(x.IsTrue());
  488. EXPECT_FALSE(x.IsString());
  489. EXPECT_FALSE(x.IsObject());
  490. EXPECT_FALSE(x.IsArray());
  491. // SetUint64()
  492. Value z;
  493. z.SetUint64(1234);
  494. EXPECT_EQ(1234u, z.GetUint64());
  495. z.SetUint64(uint64_t(2147483648u)); // 2^31, cannot cast as int
  496. EXPECT_FALSE(z.IsInt());
  497. EXPECT_TRUE(z.IsUint());
  498. EXPECT_TRUE(z.IsInt64());
  499. z.SetUint64(uint64_t(4294967295u) + 1); // 2^32, cannot cast as uint
  500. EXPECT_FALSE(z.IsInt());
  501. EXPECT_FALSE(z.IsUint());
  502. EXPECT_TRUE(z.IsInt64());
  503. uint64_t u = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
  504. z.SetUint64(u); // 2^63 cannot cast as int64
  505. EXPECT_FALSE(z.IsInt64());
  506. EXPECT_EQ(u, z.GetUint64()); // Issue 48
  507. EXPECT_DOUBLE_EQ(9223372036854775808.0, z.GetDouble());
  508. // Templated functions
  509. EXPECT_TRUE(z.Is<uint64_t>());
  510. EXPECT_EQ(u, z.Get<uint64_t>());
  511. EXPECT_EQ(u + 1, z.Set(u + 1).Get<uint64_t>());
  512. EXPECT_EQ(u + 2, z.Set<uint64_t>(u + 2).Get<uint64_t>());
  513. }
  514. TEST(Value, Double) {
  515. // Constructor with double
  516. Value x(12.34);
  517. EXPECT_EQ(kNumberType, x.GetType());
  518. EXPECT_NEAR(12.34, x.GetDouble(), 0.0);
  519. EXPECT_TRUE(x.IsNumber());
  520. EXPECT_TRUE(x.IsDouble());
  521. EXPECT_FALSE(x.IsInt());
  522. EXPECT_FALSE(x.IsNull());
  523. EXPECT_FALSE(x.IsBool());
  524. EXPECT_FALSE(x.IsFalse());
  525. EXPECT_FALSE(x.IsTrue());
  526. EXPECT_FALSE(x.IsString());
  527. EXPECT_FALSE(x.IsObject());
  528. EXPECT_FALSE(x.IsArray());
  529. // SetDouble()
  530. Value z;
  531. z.SetDouble(12.34);
  532. EXPECT_NEAR(12.34, z.GetDouble(), 0.0);
  533. z = 56.78;
  534. EXPECT_NEAR(56.78, z.GetDouble(), 0.0);
  535. // Templated functions
  536. EXPECT_TRUE(z.Is<double>());
  537. EXPECT_EQ(56.78, z.Get<double>());
  538. EXPECT_EQ(57.78, z.Set(57.78).Get<double>());
  539. EXPECT_EQ(58.78, z.Set<double>(58.78).Get<double>());
  540. }
  541. TEST(Value, Float) {
  542. // Constructor with double
  543. Value x(12.34f);
  544. EXPECT_EQ(kNumberType, x.GetType());
  545. EXPECT_NEAR(12.34f, x.GetFloat(), 0.0);
  546. EXPECT_TRUE(x.IsNumber());
  547. EXPECT_TRUE(x.IsDouble());
  548. EXPECT_TRUE(x.IsFloat());
  549. EXPECT_FALSE(x.IsInt());
  550. EXPECT_FALSE(x.IsNull());
  551. EXPECT_FALSE(x.IsBool());
  552. EXPECT_FALSE(x.IsFalse());
  553. EXPECT_FALSE(x.IsTrue());
  554. EXPECT_FALSE(x.IsString());
  555. EXPECT_FALSE(x.IsObject());
  556. EXPECT_FALSE(x.IsArray());
  557. // SetFloat()
  558. Value z;
  559. z.SetFloat(12.34f);
  560. EXPECT_NEAR(12.34f, z.GetFloat(), 0.0f);
  561. // Issue 573
  562. z.SetInt(0);
  563. EXPECT_EQ(0.0f, z.GetFloat());
  564. z = 56.78f;
  565. EXPECT_NEAR(56.78f, z.GetFloat(), 0.0f);
  566. // Templated functions
  567. EXPECT_TRUE(z.Is<float>());
  568. EXPECT_EQ(56.78f, z.Get<float>());
  569. EXPECT_EQ(57.78f, z.Set(57.78f).Get<float>());
  570. EXPECT_EQ(58.78f, z.Set<float>(58.78f).Get<float>());
  571. }
  572. TEST(Value, IsLosslessDouble) {
  573. EXPECT_TRUE(Value(0.0).IsLosslessDouble());
  574. EXPECT_TRUE(Value(12.34).IsLosslessDouble());
  575. EXPECT_TRUE(Value(-123).IsLosslessDouble());
  576. EXPECT_TRUE(Value(2147483648u).IsLosslessDouble());
  577. EXPECT_TRUE(Value(-static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x40000000, 0x00000000))).IsLosslessDouble());
  578. #if !(defined(_MSC_VER) && _MSC_VER < 1800) // VC2010 has problem
  579. EXPECT_TRUE(Value(RAPIDJSON_UINT64_C2(0xA0000000, 0x00000000)).IsLosslessDouble());
  580. #endif
  581. EXPECT_FALSE(Value(static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble()); // INT64_MAX
  582. EXPECT_FALSE(Value(-static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble()); // -INT64_MAX
  583. EXPECT_TRUE(Value(-static_cast<int64_t>(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF)) - 1).IsLosslessDouble()); // INT64_MIN
  584. EXPECT_FALSE(Value(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF)).IsLosslessDouble()); // UINT64_MAX
  585. EXPECT_TRUE(Value(3.4028234e38f).IsLosslessDouble()); // FLT_MAX
  586. EXPECT_TRUE(Value(-3.4028234e38f).IsLosslessDouble()); // -FLT_MAX
  587. EXPECT_TRUE(Value(1.17549435e-38f).IsLosslessDouble()); // FLT_MIN
  588. EXPECT_TRUE(Value(-1.17549435e-38f).IsLosslessDouble()); // -FLT_MIN
  589. EXPECT_TRUE(Value(1.7976931348623157e+308).IsLosslessDouble()); // DBL_MAX
  590. EXPECT_TRUE(Value(-1.7976931348623157e+308).IsLosslessDouble()); // -DBL_MAX
  591. EXPECT_TRUE(Value(2.2250738585072014e-308).IsLosslessDouble()); // DBL_MIN
  592. EXPECT_TRUE(Value(-2.2250738585072014e-308).IsLosslessDouble()); // -DBL_MIN
  593. }
  594. TEST(Value, IsLosslessFloat) {
  595. EXPECT_TRUE(Value(12.25).IsLosslessFloat());
  596. EXPECT_TRUE(Value(-123).IsLosslessFloat());
  597. EXPECT_TRUE(Value(2147483648u).IsLosslessFloat());
  598. EXPECT_TRUE(Value(3.4028234e38f).IsLosslessFloat());
  599. EXPECT_TRUE(Value(-3.4028234e38f).IsLosslessFloat());
  600. EXPECT_FALSE(Value(3.4028235e38).IsLosslessFloat());
  601. EXPECT_FALSE(Value(0.3).IsLosslessFloat());
  602. }
  603. TEST(Value, String) {
  604. // Construction with const string
  605. Value x("Hello", 5); // literal
  606. EXPECT_EQ(kStringType, x.GetType());
  607. EXPECT_TRUE(x.IsString());
  608. EXPECT_STREQ("Hello", x.GetString());
  609. EXPECT_EQ(5u, x.GetStringLength());
  610. EXPECT_FALSE(x.IsNumber());
  611. EXPECT_FALSE(x.IsNull());
  612. EXPECT_FALSE(x.IsBool());
  613. EXPECT_FALSE(x.IsFalse());
  614. EXPECT_FALSE(x.IsTrue());
  615. EXPECT_FALSE(x.IsObject());
  616. EXPECT_FALSE(x.IsArray());
  617. static const char cstr[] = "World"; // const array
  618. Value(cstr).Swap(x);
  619. EXPECT_TRUE(x.IsString());
  620. EXPECT_EQ(x.GetString(), cstr);
  621. EXPECT_EQ(x.GetStringLength(), sizeof(cstr)-1);
  622. static char mstr[] = "Howdy"; // non-const array
  623. // Value(mstr).Swap(x); // should not compile
  624. Value(StringRef(mstr)).Swap(x);
  625. EXPECT_TRUE(x.IsString());
  626. EXPECT_EQ(x.GetString(), mstr);
  627. EXPECT_EQ(x.GetStringLength(), sizeof(mstr)-1);
  628. strncpy(mstr,"Hello", sizeof(mstr));
  629. EXPECT_STREQ(x.GetString(), "Hello");
  630. const char* pstr = cstr;
  631. //Value(pstr).Swap(x); // should not compile
  632. Value(StringRef(pstr)).Swap(x);
  633. EXPECT_TRUE(x.IsString());
  634. EXPECT_EQ(x.GetString(), cstr);
  635. EXPECT_EQ(x.GetStringLength(), sizeof(cstr)-1);
  636. char* mpstr = mstr;
  637. Value(StringRef(mpstr,sizeof(mstr)-1)).Swap(x);
  638. EXPECT_TRUE(x.IsString());
  639. EXPECT_EQ(x.GetString(), mstr);
  640. EXPECT_EQ(x.GetStringLength(), 5u);
  641. EXPECT_STREQ(x.GetString(), "Hello");
  642. // Constructor with copy string
  643. MemoryPoolAllocator<> allocator;
  644. Value c(x.GetString(), x.GetStringLength(), allocator);
  645. EXPECT_NE(x.GetString(), c.GetString());
  646. EXPECT_EQ(x.GetStringLength(), c.GetStringLength());
  647. EXPECT_STREQ(x.GetString(), c.GetString());
  648. //x.SetString("World");
  649. x.SetString("World", 5);
  650. EXPECT_STREQ("Hello", c.GetString());
  651. EXPECT_EQ(5u, c.GetStringLength());
  652. // Constructor with type
  653. Value y(kStringType);
  654. EXPECT_TRUE(y.IsString());
  655. EXPECT_STREQ("", y.GetString()); // Empty string should be "" instead of 0 (issue 226)
  656. EXPECT_EQ(0u, y.GetStringLength());
  657. // SetConsttring()
  658. Value z;
  659. z.SetString("Hello");
  660. EXPECT_TRUE(x.IsString());
  661. z.SetString("Hello", 5);
  662. EXPECT_STREQ("Hello", z.GetString());
  663. EXPECT_STREQ("Hello", z.GetString());
  664. EXPECT_EQ(5u, z.GetStringLength());
  665. z.SetString("Hello");
  666. EXPECT_TRUE(z.IsString());
  667. EXPECT_STREQ("Hello", z.GetString());
  668. //z.SetString(mstr); // should not compile
  669. //z.SetString(pstr); // should not compile
  670. z.SetString(StringRef(mstr));
  671. EXPECT_TRUE(z.IsString());
  672. EXPECT_STREQ(z.GetString(), mstr);
  673. z.SetString(cstr);
  674. EXPECT_TRUE(z.IsString());
  675. EXPECT_EQ(cstr, z.GetString());
  676. z = cstr;
  677. EXPECT_TRUE(z.IsString());
  678. EXPECT_EQ(cstr, z.GetString());
  679. // SetString()
  680. char s[] = "World";
  681. Value w;
  682. w.SetString(s, static_cast<SizeType>(strlen(s)), allocator);
  683. s[0] = '\0';
  684. EXPECT_STREQ("World", w.GetString());
  685. EXPECT_EQ(5u, w.GetStringLength());
  686. // templated functions
  687. EXPECT_TRUE(z.Is<const char*>());
  688. EXPECT_STREQ(cstr, z.Get<const char*>());
  689. EXPECT_STREQ("Apple", z.Set<const char*>("Apple").Get<const char*>());
  690. #if RAPIDJSON_HAS_STDSTRING
  691. {
  692. std::string str = "Hello World";
  693. str[5] = '\0';
  694. EXPECT_STREQ(str.data(),"Hello"); // embedded '\0'
  695. EXPECT_EQ(str.size(), 11u);
  696. // no copy
  697. Value vs0(StringRef(str));
  698. EXPECT_TRUE(vs0.IsString());
  699. EXPECT_EQ(vs0.GetString(), str.data());
  700. EXPECT_EQ(vs0.GetStringLength(), str.size());
  701. TestEqual(vs0, str);
  702. // do copy
  703. Value vs1(str, allocator);
  704. EXPECT_TRUE(vs1.IsString());
  705. EXPECT_NE(vs1.GetString(), str.data());
  706. EXPECT_NE(vs1.GetString(), str); // not equal due to embedded '\0'
  707. EXPECT_EQ(vs1.GetStringLength(), str.size());
  708. TestEqual(vs1, str);
  709. // SetString
  710. str = "World";
  711. vs0.SetNull().SetString(str, allocator);
  712. EXPECT_TRUE(vs0.IsString());
  713. EXPECT_STREQ(vs0.GetString(), str.c_str());
  714. EXPECT_EQ(vs0.GetStringLength(), str.size());
  715. TestEqual(str, vs0);
  716. TestUnequal(str, vs1);
  717. // vs1 = str; // should not compile
  718. vs1 = StringRef(str);
  719. TestEqual(str, vs1);
  720. TestEqual(vs0, vs1);
  721. // Templated function.
  722. EXPECT_TRUE(vs0.Is<std::string>());
  723. EXPECT_EQ(str, vs0.Get<std::string>());
  724. vs0.Set<std::string>(std::string("Apple"), allocator);
  725. EXPECT_EQ(std::string("Apple"), vs0.Get<std::string>());
  726. vs0.Set(std::string("Orange"), allocator);
  727. EXPECT_EQ(std::string("Orange"), vs0.Get<std::string>());
  728. }
  729. #endif // RAPIDJSON_HAS_STDSTRING
  730. }
  731. // Issue 226: Value of string type should not point to NULL
  732. TEST(Value, SetStringNullException) {
  733. Value v;
  734. EXPECT_THROW(v.SetString(0, 0), AssertException);
  735. }
  736. template <typename T, typename Allocator>
  737. static void TestArray(T& x, Allocator& allocator) {
  738. const T& y = x;
  739. // PushBack()
  740. Value v;
  741. x.PushBack(v, allocator);
  742. v.SetBool(true);
  743. x.PushBack(v, allocator);
  744. v.SetBool(false);
  745. x.PushBack(v, allocator);
  746. v.SetInt(123);
  747. x.PushBack(v, allocator);
  748. //x.PushBack((const char*)"foo", allocator); // should not compile
  749. x.PushBack("foo", allocator);
  750. EXPECT_FALSE(x.Empty());
  751. EXPECT_EQ(5u, x.Size());
  752. EXPECT_FALSE(y.Empty());
  753. EXPECT_EQ(5u, y.Size());
  754. EXPECT_TRUE(x[SizeType(0)].IsNull());
  755. EXPECT_TRUE(x[1].IsTrue());
  756. EXPECT_TRUE(x[2].IsFalse());
  757. EXPECT_TRUE(x[3].IsInt());
  758. EXPECT_EQ(123, x[3].GetInt());
  759. EXPECT_TRUE(y[SizeType(0)].IsNull());
  760. EXPECT_TRUE(y[1].IsTrue());
  761. EXPECT_TRUE(y[2].IsFalse());
  762. EXPECT_TRUE(y[3].IsInt());
  763. EXPECT_EQ(123, y[3].GetInt());
  764. EXPECT_TRUE(y[4].IsString());
  765. EXPECT_STREQ("foo", y[4].GetString());
  766. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  767. // PushBack(GenericValue&&, Allocator&);
  768. {
  769. Value y2(kArrayType);
  770. y2.PushBack(Value(true), allocator);
  771. y2.PushBack(std::move(Value(kArrayType).PushBack(Value(1), allocator).PushBack("foo", allocator)), allocator);
  772. EXPECT_EQ(2u, y2.Size());
  773. EXPECT_TRUE(y2[0].IsTrue());
  774. EXPECT_TRUE(y2[1].IsArray());
  775. EXPECT_EQ(2u, y2[1].Size());
  776. EXPECT_TRUE(y2[1][0].IsInt());
  777. EXPECT_TRUE(y2[1][1].IsString());
  778. }
  779. #endif
  780. // iterator
  781. typename T::ValueIterator itr = x.Begin();
  782. EXPECT_TRUE(itr != x.End());
  783. EXPECT_TRUE(itr->IsNull());
  784. ++itr;
  785. EXPECT_TRUE(itr != x.End());
  786. EXPECT_TRUE(itr->IsTrue());
  787. ++itr;
  788. EXPECT_TRUE(itr != x.End());
  789. EXPECT_TRUE(itr->IsFalse());
  790. ++itr;
  791. EXPECT_TRUE(itr != x.End());
  792. EXPECT_TRUE(itr->IsInt());
  793. EXPECT_EQ(123, itr->GetInt());
  794. ++itr;
  795. EXPECT_TRUE(itr != x.End());
  796. EXPECT_TRUE(itr->IsString());
  797. EXPECT_STREQ("foo", itr->GetString());
  798. // const iterator
  799. typename T::ConstValueIterator citr = y.Begin();
  800. EXPECT_TRUE(citr != y.End());
  801. EXPECT_TRUE(citr->IsNull());
  802. ++citr;
  803. EXPECT_TRUE(citr != y.End());
  804. EXPECT_TRUE(citr->IsTrue());
  805. ++citr;
  806. EXPECT_TRUE(citr != y.End());
  807. EXPECT_TRUE(citr->IsFalse());
  808. ++citr;
  809. EXPECT_TRUE(citr != y.End());
  810. EXPECT_TRUE(citr->IsInt());
  811. EXPECT_EQ(123, citr->GetInt());
  812. ++citr;
  813. EXPECT_TRUE(citr != y.End());
  814. EXPECT_TRUE(citr->IsString());
  815. EXPECT_STREQ("foo", citr->GetString());
  816. // PopBack()
  817. x.PopBack();
  818. EXPECT_EQ(4u, x.Size());
  819. EXPECT_TRUE(y[SizeType(0)].IsNull());
  820. EXPECT_TRUE(y[1].IsTrue());
  821. EXPECT_TRUE(y[2].IsFalse());
  822. EXPECT_TRUE(y[3].IsInt());
  823. // Clear()
  824. x.Clear();
  825. EXPECT_TRUE(x.Empty());
  826. EXPECT_EQ(0u, x.Size());
  827. EXPECT_TRUE(y.Empty());
  828. EXPECT_EQ(0u, y.Size());
  829. // Erase(ValueIterator)
  830. // Use array of array to ensure removed elements' destructor is called.
  831. // [[0],[1],[2],...]
  832. for (int i = 0; i < 10; i++)
  833. x.PushBack(Value(kArrayType).PushBack(i, allocator).Move(), allocator);
  834. // Erase the first
  835. itr = x.Erase(x.Begin());
  836. EXPECT_EQ(x.Begin(), itr);
  837. EXPECT_EQ(9u, x.Size());
  838. for (int i = 0; i < 9; i++)
  839. EXPECT_EQ(i + 1, x[static_cast<SizeType>(i)][0].GetInt());
  840. // Ease the last
  841. itr = x.Erase(x.End() - 1);
  842. EXPECT_EQ(x.End(), itr);
  843. EXPECT_EQ(8u, x.Size());
  844. for (int i = 0; i < 8; i++)
  845. EXPECT_EQ(i + 1, x[static_cast<SizeType>(i)][0].GetInt());
  846. // Erase the middle
  847. itr = x.Erase(x.Begin() + 4);
  848. EXPECT_EQ(x.Begin() + 4, itr);
  849. EXPECT_EQ(7u, x.Size());
  850. for (int i = 0; i < 4; i++)
  851. EXPECT_EQ(i + 1, x[static_cast<SizeType>(i)][0].GetInt());
  852. for (int i = 4; i < 7; i++)
  853. EXPECT_EQ(i + 2, x[static_cast<SizeType>(i)][0].GetInt());
  854. // Erase(ValueIterator, ValueIterator)
  855. // Exhaustive test with all 0 <= first < n, first <= last <= n cases
  856. const unsigned n = 10;
  857. for (unsigned first = 0; first < n; first++) {
  858. for (unsigned last = first; last <= n; last++) {
  859. x.Clear();
  860. for (unsigned i = 0; i < n; i++)
  861. x.PushBack(Value(kArrayType).PushBack(i, allocator).Move(), allocator);
  862. itr = x.Erase(x.Begin() + first, x.Begin() + last);
  863. if (last == n)
  864. EXPECT_EQ(x.End(), itr);
  865. else
  866. EXPECT_EQ(x.Begin() + first, itr);
  867. size_t removeCount = last - first;
  868. EXPECT_EQ(n - removeCount, x.Size());
  869. for (unsigned i = 0; i < first; i++)
  870. EXPECT_EQ(i, x[i][0].GetUint());
  871. for (unsigned i = first; i < n - removeCount; i++)
  872. EXPECT_EQ(i + removeCount, x[static_cast<SizeType>(i)][0].GetUint());
  873. }
  874. }
  875. }
  876. TEST(Value, Array) {
  877. Value x(kArrayType);
  878. const Value& y = x;
  879. Value::AllocatorType allocator;
  880. EXPECT_EQ(kArrayType, x.GetType());
  881. EXPECT_TRUE(x.IsArray());
  882. EXPECT_TRUE(x.Empty());
  883. EXPECT_EQ(0u, x.Size());
  884. EXPECT_TRUE(y.IsArray());
  885. EXPECT_TRUE(y.Empty());
  886. EXPECT_EQ(0u, y.Size());
  887. EXPECT_FALSE(x.IsNull());
  888. EXPECT_FALSE(x.IsBool());
  889. EXPECT_FALSE(x.IsFalse());
  890. EXPECT_FALSE(x.IsTrue());
  891. EXPECT_FALSE(x.IsString());
  892. EXPECT_FALSE(x.IsObject());
  893. TestArray(x, allocator);
  894. // Working in gcc without C++11, but VS2013 cannot compile. To be diagnosed.
  895. // http://en.wikipedia.org/wiki/Erase-remove_idiom
  896. x.Clear();
  897. for (int i = 0; i < 10; i++)
  898. if (i % 2 == 0)
  899. x.PushBack(i, allocator);
  900. else
  901. x.PushBack(Value(kNullType).Move(), allocator);
  902. const Value null(kNullType);
  903. x.Erase(std::remove(x.Begin(), x.End(), null), x.End());
  904. EXPECT_EQ(5u, x.Size());
  905. for (int i = 0; i < 5; i++)
  906. EXPECT_EQ(i * 2, x[static_cast<SizeType>(i)]);
  907. // SetArray()
  908. Value z;
  909. z.SetArray();
  910. EXPECT_TRUE(z.IsArray());
  911. EXPECT_TRUE(z.Empty());
  912. }
  913. TEST(Value, ArrayHelper) {
  914. Value::AllocatorType allocator;
  915. {
  916. Value x(kArrayType);
  917. Value::Array a = x.GetArray();
  918. TestArray(a, allocator);
  919. }
  920. {
  921. Value x(kArrayType);
  922. Value::Array a = x.GetArray();
  923. a.PushBack(1, allocator);
  924. Value::Array a2(a); // copy constructor
  925. EXPECT_EQ(1, a2.Size());
  926. Value::Array a3 = a;
  927. EXPECT_EQ(1, a3.Size());
  928. Value::ConstArray y = static_cast<const Value&>(x).GetArray();
  929. (void)y;
  930. // y.PushBack(1, allocator); // should not compile
  931. // Templated functions
  932. x.Clear();
  933. EXPECT_TRUE(x.Is<Value::Array>());
  934. EXPECT_TRUE(x.Is<Value::ConstArray>());
  935. a.PushBack(1, allocator);
  936. EXPECT_EQ(1, x.Get<Value::Array>()[0].GetInt());
  937. EXPECT_EQ(1, x.Get<Value::ConstArray>()[0].GetInt());
  938. Value x2;
  939. x2.Set<Value::Array>(a);
  940. EXPECT_TRUE(x.IsArray()); // IsArray() is invariant after moving.
  941. EXPECT_EQ(1, x2.Get<Value::Array>()[0].GetInt());
  942. }
  943. {
  944. Value y(kArrayType);
  945. y.PushBack(123, allocator);
  946. Value x(y.GetArray()); // Construct value form array.
  947. EXPECT_TRUE(x.IsArray());
  948. EXPECT_EQ(123, x[0].GetInt());
  949. EXPECT_TRUE(y.IsArray()); // Invariant
  950. EXPECT_TRUE(y.Empty());
  951. }
  952. {
  953. Value x(kArrayType);
  954. Value y(kArrayType);
  955. y.PushBack(123, allocator);
  956. x.PushBack(y.GetArray(), allocator); // Implicit constructor to convert Array to GenericValue
  957. EXPECT_EQ(1, x.Size());
  958. EXPECT_EQ(123, x[0][0].GetInt());
  959. EXPECT_TRUE(y.IsArray());
  960. EXPECT_TRUE(y.Empty());
  961. }
  962. }
  963. #if RAPIDJSON_HAS_CXX11_RANGE_FOR
  964. TEST(Value, ArrayHelperRangeFor) {
  965. Value::AllocatorType allocator;
  966. Value x(kArrayType);
  967. for (int i = 0; i < 10; i++)
  968. x.PushBack(i, allocator);
  969. {
  970. int i = 0;
  971. for (auto& v : x.GetArray()) {
  972. EXPECT_EQ(i, v.GetInt());
  973. i++;
  974. }
  975. EXPECT_EQ(i, 10);
  976. }
  977. {
  978. int i = 0;
  979. for (const auto& v : const_cast<const Value&>(x).GetArray()) {
  980. EXPECT_EQ(i, v.GetInt());
  981. i++;
  982. }
  983. EXPECT_EQ(i, 10);
  984. }
  985. // Array a = x.GetArray();
  986. // Array ca = const_cast<const Value&>(x).GetArray();
  987. }
  988. #endif
  989. template <typename T, typename Allocator>
  990. static void TestObject(T& x, Allocator& allocator) {
  991. const T& y = x; // const version
  992. // AddMember()
  993. x.AddMember("A", "Apple", allocator);
  994. EXPECT_FALSE(x.ObjectEmpty());
  995. EXPECT_EQ(1u, x.MemberCount());
  996. Value value("Banana", 6);
  997. x.AddMember("B", "Banana", allocator);
  998. EXPECT_EQ(2u, x.MemberCount());
  999. // AddMember<T>(StringRefType, T, Allocator)
  1000. {
  1001. Value o(kObjectType);
  1002. o.AddMember("true", true, allocator);
  1003. o.AddMember("false", false, allocator);
  1004. o.AddMember("int", -1, allocator);
  1005. o.AddMember("uint", 1u, allocator);
  1006. o.AddMember("int64", int64_t(-4294967296), allocator);
  1007. o.AddMember("uint64", uint64_t(4294967296), allocator);
  1008. o.AddMember("double", 3.14, allocator);
  1009. o.AddMember("string", "Jelly", allocator);
  1010. EXPECT_TRUE(o["true"].GetBool());
  1011. EXPECT_FALSE(o["false"].GetBool());
  1012. EXPECT_EQ(-1, o["int"].GetInt());
  1013. EXPECT_EQ(1u, o["uint"].GetUint());
  1014. EXPECT_EQ(int64_t(-4294967296), o["int64"].GetInt64());
  1015. EXPECT_EQ(uint64_t(4294967296), o["uint64"].GetUint64());
  1016. EXPECT_STREQ("Jelly",o["string"].GetString());
  1017. EXPECT_EQ(8u, o.MemberCount());
  1018. }
  1019. // AddMember<T>(Value&, T, Allocator)
  1020. {
  1021. Value o(kObjectType);
  1022. Value n("s");
  1023. o.AddMember(n, "string", allocator);
  1024. EXPECT_EQ(1u, o.MemberCount());
  1025. Value count("#");
  1026. o.AddMember(count, o.MemberCount(), allocator);
  1027. EXPECT_EQ(2u, o.MemberCount());
  1028. }
  1029. #if RAPIDJSON_HAS_STDSTRING
  1030. {
  1031. // AddMember(StringRefType, const std::string&, Allocator)
  1032. Value o(kObjectType);
  1033. o.AddMember("b", std::string("Banana"), allocator);
  1034. EXPECT_STREQ("Banana", o["b"].GetString());
  1035. // RemoveMember(const std::string&)
  1036. o.RemoveMember(std::string("b"));
  1037. EXPECT_TRUE(o.ObjectEmpty());
  1038. }
  1039. #endif
  1040. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  1041. // AddMember(GenericValue&&, ...) variants
  1042. {
  1043. Value o(kObjectType);
  1044. o.AddMember(Value("true"), Value(true), allocator);
  1045. o.AddMember(Value("false"), Value(false).Move(), allocator); // value is lvalue ref
  1046. o.AddMember(Value("int").Move(), Value(-1), allocator); // name is lvalue ref
  1047. o.AddMember("uint", std::move(Value().SetUint(1u)), allocator); // name is literal, value is rvalue
  1048. EXPECT_TRUE(o["true"].GetBool());
  1049. EXPECT_FALSE(o["false"].GetBool());
  1050. EXPECT_EQ(-1, o["int"].GetInt());
  1051. EXPECT_EQ(1u, o["uint"].GetUint());
  1052. EXPECT_EQ(4u, o.MemberCount());
  1053. }
  1054. #endif
  1055. // Tests a member with null character
  1056. Value name;
  1057. const Value C0D("C\0D", 3);
  1058. name.SetString(C0D.GetString(), 3);
  1059. value.SetString("CherryD", 7);
  1060. x.AddMember(name, value, allocator);
  1061. // HasMember()
  1062. EXPECT_TRUE(x.HasMember("A"));
  1063. EXPECT_TRUE(x.HasMember("B"));
  1064. EXPECT_TRUE(y.HasMember("A"));
  1065. EXPECT_TRUE(y.HasMember("B"));
  1066. #if RAPIDJSON_HAS_STDSTRING
  1067. EXPECT_TRUE(x.HasMember(std::string("A")));
  1068. #endif
  1069. name.SetString("C\0D");
  1070. EXPECT_TRUE(x.HasMember(name));
  1071. EXPECT_TRUE(y.HasMember(name));
  1072. GenericValue<UTF8<>, CrtAllocator> othername("A");
  1073. EXPECT_TRUE(x.HasMember(othername));
  1074. EXPECT_TRUE(y.HasMember(othername));
  1075. othername.SetString("C\0D");
  1076. EXPECT_TRUE(x.HasMember(othername));
  1077. EXPECT_TRUE(y.HasMember(othername));
  1078. // operator[]
  1079. EXPECT_STREQ("Apple", x["A"].GetString());
  1080. EXPECT_STREQ("Banana", x["B"].GetString());
  1081. EXPECT_STREQ("CherryD", x[C0D].GetString());
  1082. EXPECT_STREQ("CherryD", x[othername].GetString());
  1083. EXPECT_THROW(x["nonexist"], AssertException);
  1084. // const operator[]
  1085. EXPECT_STREQ("Apple", y["A"].GetString());
  1086. EXPECT_STREQ("Banana", y["B"].GetString());
  1087. EXPECT_STREQ("CherryD", y[C0D].GetString());
  1088. #if RAPIDJSON_HAS_STDSTRING
  1089. EXPECT_STREQ("Apple", x["A"].GetString());
  1090. EXPECT_STREQ("Apple", y[std::string("A")].GetString());
  1091. #endif
  1092. // member iterator
  1093. Value::MemberIterator itr = x.MemberBegin();
  1094. EXPECT_TRUE(itr != x.MemberEnd());
  1095. EXPECT_STREQ("A", itr->name.GetString());
  1096. EXPECT_STREQ("Apple", itr->value.GetString());
  1097. ++itr;
  1098. EXPECT_TRUE(itr != x.MemberEnd());
  1099. EXPECT_STREQ("B", itr->name.GetString());
  1100. EXPECT_STREQ("Banana", itr->value.GetString());
  1101. ++itr;
  1102. EXPECT_TRUE(itr != x.MemberEnd());
  1103. EXPECT_TRUE(memcmp(itr->name.GetString(), "C\0D", 4) == 0);
  1104. EXPECT_STREQ("CherryD", itr->value.GetString());
  1105. ++itr;
  1106. EXPECT_FALSE(itr != x.MemberEnd());
  1107. // const member iterator
  1108. Value::ConstMemberIterator citr = y.MemberBegin();
  1109. EXPECT_TRUE(citr != y.MemberEnd());
  1110. EXPECT_STREQ("A", citr->name.GetString());
  1111. EXPECT_STREQ("Apple", citr->value.GetString());
  1112. ++citr;
  1113. EXPECT_TRUE(citr != y.MemberEnd());
  1114. EXPECT_STREQ("B", citr->name.GetString());
  1115. EXPECT_STREQ("Banana", citr->value.GetString());
  1116. ++citr;
  1117. EXPECT_TRUE(citr != y.MemberEnd());
  1118. EXPECT_TRUE(memcmp(citr->name.GetString(), "C\0D", 4) == 0);
  1119. EXPECT_STREQ("CherryD", citr->value.GetString());
  1120. ++citr;
  1121. EXPECT_FALSE(citr != y.MemberEnd());
  1122. // member iterator conversions/relations
  1123. itr = x.MemberBegin();
  1124. citr = x.MemberBegin(); // const conversion
  1125. TestEqual(itr, citr);
  1126. EXPECT_TRUE(itr < x.MemberEnd());
  1127. EXPECT_FALSE(itr > y.MemberEnd());
  1128. EXPECT_TRUE(citr < x.MemberEnd());
  1129. EXPECT_FALSE(citr > y.MemberEnd());
  1130. ++citr;
  1131. TestUnequal(itr, citr);
  1132. EXPECT_FALSE(itr < itr);
  1133. EXPECT_TRUE(itr < citr);
  1134. EXPECT_FALSE(itr > itr);
  1135. EXPECT_TRUE(citr > itr);
  1136. EXPECT_EQ(1, citr - x.MemberBegin());
  1137. EXPECT_EQ(0, itr - y.MemberBegin());
  1138. itr += citr - x.MemberBegin();
  1139. EXPECT_EQ(1, itr - y.MemberBegin());
  1140. TestEqual(citr, itr);
  1141. EXPECT_TRUE(itr <= citr);
  1142. EXPECT_TRUE(citr <= itr);
  1143. itr++;
  1144. EXPECT_TRUE(itr >= citr);
  1145. EXPECT_FALSE(citr >= itr);
  1146. // RemoveMember()
  1147. EXPECT_TRUE(x.RemoveMember("A"));
  1148. EXPECT_FALSE(x.HasMember("A"));
  1149. EXPECT_TRUE(x.RemoveMember("B"));
  1150. EXPECT_FALSE(x.HasMember("B"));
  1151. EXPECT_FALSE(x.RemoveMember("nonexist"));
  1152. EXPECT_TRUE(x.RemoveMember(othername));
  1153. EXPECT_FALSE(x.HasMember(name));
  1154. EXPECT_TRUE(x.MemberBegin() == x.MemberEnd());
  1155. // EraseMember(ConstMemberIterator)
  1156. // Use array members to ensure removed elements' destructor is called.
  1157. // { "a": [0], "b": [1],[2],...]
  1158. const char keys[][2] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
  1159. for (int i = 0; i < 10; i++)
  1160. x.AddMember(keys[i], Value(kArrayType).PushBack(i, allocator), allocator);
  1161. // MemberCount, iterator difference
  1162. EXPECT_EQ(x.MemberCount(), SizeType(x.MemberEnd() - x.MemberBegin()));
  1163. // Erase the first
  1164. itr = x.EraseMember(x.MemberBegin());
  1165. EXPECT_FALSE(x.HasMember(keys[0]));
  1166. EXPECT_EQ(x.MemberBegin(), itr);
  1167. EXPECT_EQ(9u, x.MemberCount());
  1168. for (; itr != x.MemberEnd(); ++itr) {
  1169. size_t i = static_cast<size_t>((itr - x.MemberBegin())) + 1;
  1170. EXPECT_STREQ(itr->name.GetString(), keys[i]);
  1171. EXPECT_EQ(i, itr->value[0].GetInt());
  1172. }
  1173. // Erase the last
  1174. itr = x.EraseMember(x.MemberEnd() - 1);
  1175. EXPECT_FALSE(x.HasMember(keys[9]));
  1176. EXPECT_EQ(x.MemberEnd(), itr);
  1177. EXPECT_EQ(8u, x.MemberCount());
  1178. for (; itr != x.MemberEnd(); ++itr) {
  1179. size_t i = static_cast<size_t>(itr - x.MemberBegin()) + 1;
  1180. EXPECT_STREQ(itr->name.GetString(), keys[i]);
  1181. EXPECT_EQ(i, itr->value[0].GetInt());
  1182. }
  1183. // Erase the middle
  1184. itr = x.EraseMember(x.MemberBegin() + 4);
  1185. EXPECT_FALSE(x.HasMember(keys[5]));
  1186. EXPECT_EQ(x.MemberBegin() + 4, itr);
  1187. EXPECT_EQ(7u, x.MemberCount());
  1188. for (; itr != x.MemberEnd(); ++itr) {
  1189. size_t i = static_cast<size_t>(itr - x.MemberBegin());
  1190. i += (i < 4) ? 1 : 2;
  1191. EXPECT_STREQ(itr->name.GetString(), keys[i]);
  1192. EXPECT_EQ(i, itr->value[0].GetInt());
  1193. }
  1194. // EraseMember(ConstMemberIterator, ConstMemberIterator)
  1195. // Exhaustive test with all 0 <= first < n, first <= last <= n cases
  1196. const unsigned n = 10;
  1197. for (unsigned first = 0; first < n; first++) {
  1198. for (unsigned last = first; last <= n; last++) {
  1199. x.RemoveAllMembers();
  1200. for (unsigned i = 0; i < n; i++)
  1201. x.AddMember(keys[i], Value(kArrayType).PushBack(i, allocator), allocator);
  1202. itr = x.EraseMember(x.MemberBegin() + static_cast<int>(first), x.MemberBegin() + static_cast<int>(last));
  1203. if (last == n)
  1204. EXPECT_EQ(x.MemberEnd(), itr);
  1205. else
  1206. EXPECT_EQ(x.MemberBegin() + static_cast<int>(first), itr);
  1207. size_t removeCount = last - first;
  1208. EXPECT_EQ(n - removeCount, x.MemberCount());
  1209. for (unsigned i = 0; i < first; i++)
  1210. EXPECT_EQ(i, x[keys[i]][0].GetUint());
  1211. for (unsigned i = first; i < n - removeCount; i++)
  1212. EXPECT_EQ(i + removeCount, x[keys[i+removeCount]][0].GetUint());
  1213. }
  1214. }
  1215. // RemoveAllMembers()
  1216. x.RemoveAllMembers();
  1217. EXPECT_TRUE(x.ObjectEmpty());
  1218. EXPECT_EQ(0u, x.MemberCount());
  1219. }
  1220. TEST(Value, Object) {
  1221. Value x(kObjectType);
  1222. const Value& y = x; // const version
  1223. Value::AllocatorType allocator;
  1224. EXPECT_EQ(kObjectType, x.GetType());
  1225. EXPECT_TRUE(x.IsObject());
  1226. EXPECT_TRUE(x.ObjectEmpty());
  1227. EXPECT_EQ(0u, x.MemberCount());
  1228. EXPECT_EQ(kObjectType, y.GetType());
  1229. EXPECT_TRUE(y.IsObject());
  1230. EXPECT_TRUE(y.ObjectEmpty());
  1231. EXPECT_EQ(0u, y.MemberCount());
  1232. TestObject(x, allocator);
  1233. // SetObject()
  1234. Value z;
  1235. z.SetObject();
  1236. EXPECT_TRUE(z.IsObject());
  1237. }
  1238. TEST(Value, ObjectHelper) {
  1239. Value::AllocatorType allocator;
  1240. {
  1241. Value x(kObjectType);
  1242. Value::Object o = x.GetObject();
  1243. TestObject(o, allocator);
  1244. }
  1245. {
  1246. Value x(kObjectType);
  1247. Value::Object o = x.GetObject();
  1248. o.AddMember("1", 1, allocator);
  1249. Value::Object o2(o); // copy constructor
  1250. EXPECT_EQ(1, o2.MemberCount());
  1251. Value::Object o3 = o;
  1252. EXPECT_EQ(1, o3.MemberCount());
  1253. Value::ConstObject y = static_cast<const Value&>(x).GetObject();
  1254. (void)y;
  1255. // y.AddMember("1", 1, allocator); // should not compile
  1256. // Templated functions
  1257. x.RemoveAllMembers();
  1258. EXPECT_TRUE(x.Is<Value::Object>());
  1259. EXPECT_TRUE(x.Is<Value::ConstObject>());
  1260. o.AddMember("1", 1, allocator);
  1261. EXPECT_EQ(1, x.Get<Value::Object>()["1"].GetInt());
  1262. EXPECT_EQ(1, x.Get<Value::ConstObject>()["1"].GetInt());
  1263. Value x2;
  1264. x2.Set<Value::Object>(o);
  1265. EXPECT_TRUE(x.IsObject()); // IsObject() is invariant after moving
  1266. EXPECT_EQ(1, x2.Get<Value::Object>()["1"].GetInt());
  1267. }
  1268. {
  1269. Value x(kObjectType);
  1270. x.AddMember("a", "apple", allocator);
  1271. Value y(x.GetObject());
  1272. EXPECT_STREQ("apple", y["a"].GetString());
  1273. EXPECT_TRUE(x.IsObject()); // Invariant
  1274. }
  1275. {
  1276. Value x(kObjectType);
  1277. x.AddMember("a", "apple", allocator);
  1278. Value y(kObjectType);
  1279. y.AddMember("fruits", x.GetObject(), allocator);
  1280. EXPECT_STREQ("apple", y["fruits"]["a"].GetString());
  1281. EXPECT_TRUE(x.IsObject()); // Invariant
  1282. }
  1283. }
  1284. #if RAPIDJSON_HAS_CXX11_RANGE_FOR
  1285. TEST(Value, ObjectHelperRangeFor) {
  1286. Value::AllocatorType allocator;
  1287. Value x(kObjectType);
  1288. for (int i = 0; i < 10; i++) {
  1289. char name[10];
  1290. Value n(name, static_cast<SizeType>(sprintf(name, "%d", i)), allocator);
  1291. x.AddMember(n, i, allocator);
  1292. }
  1293. {
  1294. int i = 0;
  1295. for (auto& m : x.GetObject()) {
  1296. char name[10];
  1297. sprintf(name, "%d", i);
  1298. EXPECT_STREQ(name, m.name.GetString());
  1299. EXPECT_EQ(i, m.value.GetInt());
  1300. i++;
  1301. }
  1302. EXPECT_EQ(i, 10);
  1303. }
  1304. {
  1305. int i = 0;
  1306. for (const auto& m : const_cast<const Value&>(x).GetObject()) {
  1307. char name[10];
  1308. sprintf(name, "%d", i);
  1309. EXPECT_STREQ(name, m.name.GetString());
  1310. EXPECT_EQ(i, m.value.GetInt());
  1311. i++;
  1312. }
  1313. EXPECT_EQ(i, 10);
  1314. }
  1315. // Object a = x.GetObject();
  1316. // Object ca = const_cast<const Value&>(x).GetObject();
  1317. }
  1318. #endif
  1319. TEST(Value, EraseMember_String) {
  1320. Value::AllocatorType allocator;
  1321. Value x(kObjectType);
  1322. x.AddMember("A", "Apple", allocator);
  1323. x.AddMember("B", "Banana", allocator);
  1324. EXPECT_TRUE(x.EraseMember("B"));
  1325. EXPECT_FALSE(x.HasMember("B"));
  1326. EXPECT_FALSE(x.EraseMember("nonexist"));
  1327. GenericValue<UTF8<>, CrtAllocator> othername("A");
  1328. EXPECT_TRUE(x.EraseMember(othername));
  1329. EXPECT_FALSE(x.HasMember("A"));
  1330. EXPECT_TRUE(x.MemberBegin() == x.MemberEnd());
  1331. }
  1332. TEST(Value, BigNestedArray) {
  1333. MemoryPoolAllocator<> allocator;
  1334. Value x(kArrayType);
  1335. static const SizeType n = 200;
  1336. for (SizeType i = 0; i < n; i++) {
  1337. Value y(kArrayType);
  1338. for (SizeType j = 0; j < n; j++) {
  1339. Value number(static_cast<int>(i * n + j));
  1340. y.PushBack(number, allocator);
  1341. }
  1342. x.PushBack(y, allocator);
  1343. }
  1344. for (SizeType i = 0; i < n; i++)
  1345. for (SizeType j = 0; j < n; j++) {
  1346. EXPECT_TRUE(x[i][j].IsInt());
  1347. EXPECT_EQ(static_cast<int>(i * n + j), x[i][j].GetInt());
  1348. }
  1349. }
  1350. TEST(Value, BigNestedObject) {
  1351. MemoryPoolAllocator<> allocator;
  1352. Value x(kObjectType);
  1353. static const SizeType n = 200;
  1354. for (SizeType i = 0; i < n; i++) {
  1355. char name1[10];
  1356. sprintf(name1, "%d", i);
  1357. // Value name(name1); // should not compile
  1358. Value name(name1, static_cast<SizeType>(strlen(name1)), allocator);
  1359. Value object(kObjectType);
  1360. for (SizeType j = 0; j < n; j++) {
  1361. char name2[10];
  1362. sprintf(name2, "%d", j);
  1363. Value name3(name2, static_cast<SizeType>(strlen(name2)), allocator);
  1364. Value number(static_cast<int>(i * n + j));
  1365. object.AddMember(name3, number, allocator);
  1366. }
  1367. // x.AddMember(name1, object, allocator); // should not compile
  1368. x.AddMember(name, object, allocator);
  1369. }
  1370. for (SizeType i = 0; i < n; i++) {
  1371. char name1[10];
  1372. sprintf(name1, "%d", i);
  1373. for (SizeType j = 0; j < n; j++) {
  1374. char name2[10];
  1375. sprintf(name2, "%d", j);
  1376. x[name1];
  1377. EXPECT_EQ(static_cast<int>(i * n + j), x[name1][name2].GetInt());
  1378. }
  1379. }
  1380. }
  1381. // Issue 18: Error removing last element of object
  1382. // http://code.google.com/p/rapidjson/issues/detail?id=18
  1383. TEST(Value, RemoveLastElement) {
  1384. rapidjson::Document doc;
  1385. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  1386. rapidjson::Value objVal(rapidjson::kObjectType);
  1387. objVal.AddMember("var1", 123, allocator);
  1388. objVal.AddMember("var2", "444", allocator);
  1389. objVal.AddMember("var3", 555, allocator);
  1390. EXPECT_TRUE(objVal.HasMember("var3"));
  1391. objVal.RemoveMember("var3"); // Assertion here in r61
  1392. EXPECT_FALSE(objVal.HasMember("var3"));
  1393. }
  1394. // Issue 38: Segmentation fault with CrtAllocator
  1395. TEST(Document, CrtAllocator) {
  1396. typedef GenericValue<UTF8<>, CrtAllocator> V;
  1397. V::AllocatorType allocator;
  1398. V o(kObjectType);
  1399. o.AddMember("x", 1, allocator); // Should not call destructor on uninitialized name/value of newly allocated members.
  1400. V a(kArrayType);
  1401. a.PushBack(1, allocator); // Should not call destructor on uninitialized Value of newly allocated elements.
  1402. }
  1403. static void TestShortStringOptimization(const char* str) {
  1404. const rapidjson::SizeType len = static_cast<rapidjson::SizeType>(strlen(str));
  1405. rapidjson::Document doc;
  1406. rapidjson::Value val;
  1407. val.SetString(str, len, doc.GetAllocator());
  1408. EXPECT_EQ(val.GetStringLength(), len);
  1409. EXPECT_STREQ(val.GetString(), str);
  1410. }
  1411. TEST(Value, AllocateShortString) {
  1412. TestShortStringOptimization(""); // edge case: empty string
  1413. TestShortStringOptimization("12345678"); // regular case for short strings: 8 chars
  1414. TestShortStringOptimization("12345678901"); // edge case: 11 chars in 32-bit mode (=> short string)
  1415. TestShortStringOptimization("123456789012"); // edge case: 12 chars in 32-bit mode (=> regular string)
  1416. TestShortStringOptimization("123456789012345"); // edge case: 15 chars in 64-bit mode (=> short string)
  1417. TestShortStringOptimization("1234567890123456"); // edge case: 16 chars in 64-bit mode (=> regular string)
  1418. }
  1419. template <int e>
  1420. struct TerminateHandler {
  1421. bool Null() { return e != 0; }
  1422. bool Bool(bool) { return e != 1; }
  1423. bool Int(int) { return e != 2; }
  1424. bool Uint(unsigned) { return e != 3; }
  1425. bool Int64(int64_t) { return e != 4; }
  1426. bool Uint64(uint64_t) { return e != 5; }
  1427. bool Double(double) { return e != 6; }
  1428. bool RawNumber(const char*, SizeType, bool) { return e != 7; }
  1429. bool String(const char*, SizeType, bool) { return e != 8; }
  1430. bool StartObject() { return e != 9; }
  1431. bool Key(const char*, SizeType, bool) { return e != 10; }
  1432. bool EndObject(SizeType) { return e != 11; }
  1433. bool StartArray() { return e != 12; }
  1434. bool EndArray(SizeType) { return e != 13; }
  1435. };
  1436. #define TEST_TERMINATION(e, json)\
  1437. {\
  1438. Document d; \
  1439. EXPECT_FALSE(d.Parse(json).HasParseError()); \
  1440. Reader reader; \
  1441. TerminateHandler<e> h;\
  1442. EXPECT_FALSE(d.Accept(h));\
  1443. }
  1444. TEST(Value, AcceptTerminationByHandler) {
  1445. TEST_TERMINATION(0, "[null]");
  1446. TEST_TERMINATION(1, "[true]");
  1447. TEST_TERMINATION(1, "[false]");
  1448. TEST_TERMINATION(2, "[-1]");
  1449. TEST_TERMINATION(3, "[2147483648]");
  1450. TEST_TERMINATION(4, "[-1234567890123456789]");
  1451. TEST_TERMINATION(5, "[9223372036854775808]");
  1452. TEST_TERMINATION(6, "[0.5]");
  1453. // RawNumber() is never called
  1454. TEST_TERMINATION(8, "[\"a\"]");
  1455. TEST_TERMINATION(9, "[{}]");
  1456. TEST_TERMINATION(10, "[{\"a\":1}]");
  1457. TEST_TERMINATION(11, "[{}]");
  1458. TEST_TERMINATION(12, "{\"a\":[]}");
  1459. TEST_TERMINATION(13, "{\"a\":[]}");
  1460. }
  1461. struct ValueIntComparer {
  1462. bool operator()(const Value& lhs, const Value& rhs) const {
  1463. return lhs.GetInt() < rhs.GetInt();
  1464. }
  1465. };
  1466. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  1467. TEST(Value, Sorting) {
  1468. Value::AllocatorType allocator;
  1469. Value a(kArrayType);
  1470. a.PushBack(5, allocator);
  1471. a.PushBack(1, allocator);
  1472. a.PushBack(3, allocator);
  1473. std::sort(a.Begin(), a.End(), ValueIntComparer());
  1474. EXPECT_EQ(1, a[0].GetInt());
  1475. EXPECT_EQ(3, a[1].GetInt());
  1476. EXPECT_EQ(5, a[2].GetInt());
  1477. }
  1478. #endif
  1479. // http://stackoverflow.com/questions/35222230/
  1480. static void MergeDuplicateKey(Value& v, Value::AllocatorType& a) {
  1481. if (v.IsObject()) {
  1482. // Convert all key:value into key:[value]
  1483. for (Value::MemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
  1484. itr->value = Value(kArrayType).Move().PushBack(itr->value, a);
  1485. // Merge arrays if key is duplicated
  1486. for (Value::MemberIterator itr = v.MemberBegin(); itr != v.MemberEnd();) {
  1487. Value::MemberIterator itr2 = v.FindMember(itr->name);
  1488. if (itr != itr2) {
  1489. itr2->value.PushBack(itr->value[0], a);
  1490. itr = v.EraseMember(itr);
  1491. }
  1492. else
  1493. ++itr;
  1494. }
  1495. // Convert key:[values] back to key:value if there is only one value
  1496. for (Value::MemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) {
  1497. if (itr->value.Size() == 1)
  1498. itr->value = itr->value[0];
  1499. MergeDuplicateKey(itr->value, a); // Recursion on the value
  1500. }
  1501. }
  1502. else if (v.IsArray())
  1503. for (Value::ValueIterator itr = v.Begin(); itr != v.End(); ++itr)
  1504. MergeDuplicateKey(*itr, a);
  1505. }
  1506. TEST(Value, MergeDuplicateKey) {
  1507. Document d;
  1508. d.Parse(
  1509. "{"
  1510. " \"key1\": {"
  1511. " \"a\": \"asdf\","
  1512. " \"b\": \"foo\","
  1513. " \"b\": \"bar\","
  1514. " \"c\": \"fdas\""
  1515. " }"
  1516. "}");
  1517. Document d2;
  1518. d2.Parse(
  1519. "{"
  1520. " \"key1\": {"
  1521. " \"a\": \"asdf\","
  1522. " \"b\": ["
  1523. " \"foo\","
  1524. " \"bar\""
  1525. " ],"
  1526. " \"c\": \"fdas\""
  1527. " }"
  1528. "}");
  1529. EXPECT_NE(d2, d);
  1530. MergeDuplicateKey(d, d.GetAllocator());
  1531. EXPECT_EQ(d2, d);
  1532. }
  1533. #ifdef __clang__
  1534. RAPIDJSON_DIAG_POP
  1535. #endif