valuetest.cpp 16.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "unittest.h"
#include "rapidjson/document.h"

using namespace rapidjson;

TEST(Value, default_constructor) {
	Value x;
	EXPECT_EQ(kNullType, x.GetType());
	EXPECT_TRUE(x.IsNull());

	//std::cout << "sizeof(Value): " << sizeof(x) << std::endl;
}

// Should not pass compilation
//TEST(Value, copy_constructor) {
//	Value x(1234);
//	Value y = x;
//}

TEST(Value, assignment_operator) {
	Value x(1234);
	Value y;
	y = x;
	EXPECT_TRUE(x.IsNull());	// move semantic
	EXPECT_EQ(1234, y.GetInt());
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
TEST(Value, CopyFrom)
{
	// use CrtAllocator to explicitly malloc/free any memory
	// comment this line to use the default Allocator instead
	typedef GenericValue<UTF8<>,CrtAllocator> Value;

	Value::AllocatorType a;
	Value v1(1234);
	Value v2(v1,a); // deep copy constructor
	EXPECT_TRUE(v1.GetType() == v2.GetType());
	EXPECT_EQ(v1.GetInt(), v2.GetInt());

	v1.SetString("foo");
	v2.CopyFrom(v1,a);
	EXPECT_TRUE(v1.GetType() == v2.GetType());
	EXPECT_STREQ(v1.GetString(), v2.GetString());
	EXPECT_EQ(v1.GetString(), v2.GetString()); // string NOT copied

	v1.SetArray().PushBack(1234,a);
	v2.CopyFrom(v1,a);
	EXPECT_TRUE(v2.IsArray());
	EXPECT_EQ(v1.Size(), v2.Size());

	v1.PushBack(Value().SetString("foo",a),a); // push string copy
	EXPECT_TRUE(v1.Size() != v2.Size());
	v2.CopyFrom(v1,a);
	EXPECT_TRUE(v1.Size() == v2.Size());
	EXPECT_STREQ(v1[1].GetString(), v2[1].GetString());
	EXPECT_NE(v1[1].GetString(), v2[1].GetString()); // string got copied
}
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

TEST(Value, Null) {
	// Default constructor
	Value x;
	EXPECT_EQ(kNullType, x.GetType());
	EXPECT_TRUE(x.IsNull());

	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsNumber());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	// Constructor with type
	Value y(kNullType);
	EXPECT_TRUE(y.IsNull());

	// SetNull();
	Value z(true);
	z.SetNull();
	EXPECT_TRUE(z.IsNull());
}

TEST(Value, True) {
	// Constructor with bool
	Value x(true);
	EXPECT_EQ(kTrueType, x.GetType());
	EXPECT_TRUE(x.GetBool());
	EXPECT_TRUE(x.IsBool());
	EXPECT_TRUE(x.IsTrue());

	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsNumber());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	// Constructor with type
	Value y(kTrueType);
	EXPECT_TRUE(y.IsTrue());

	// SetBool()
	Value z;
	z.SetBool(true);
	EXPECT_TRUE(z.IsTrue());
}

TEST(Value, False) {
	// Constructor with bool
	Value x(false);
	EXPECT_EQ(kFalseType, x.GetType());
	EXPECT_TRUE(x.IsBool());
	EXPECT_TRUE(x.IsFalse());

	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.GetBool());
	//EXPECT_FALSE((bool)x);
	EXPECT_FALSE(x.IsNumber());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	// Constructor with type
	Value y(kFalseType);
	EXPECT_TRUE(y.IsFalse());

	// SetBool()
	Value z;
	z.SetBool(false);
	EXPECT_TRUE(z.IsFalse());
}

TEST(Value, Int) {
	// Constructor with int
	Value x(1234);
	EXPECT_EQ(kNumberType, x.GetType());
	EXPECT_EQ(1234, x.GetInt());
138
	EXPECT_EQ(1234u, x.GetUint());
139
	EXPECT_EQ(1234, x.GetInt64());
140
	EXPECT_EQ(1234u, x.GetUint64());
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	EXPECT_EQ(1234, x.GetDouble());
	//EXPECT_EQ(1234, (int)x);
	//EXPECT_EQ(1234, (unsigned)x);
	//EXPECT_EQ(1234, (int64_t)x);
	//EXPECT_EQ(1234, (uint64_t)x);
	//EXPECT_EQ(1234, (double)x);
	EXPECT_TRUE(x.IsNumber());
	EXPECT_TRUE(x.IsInt());
	EXPECT_TRUE(x.IsUint());
	EXPECT_TRUE(x.IsInt64());
	EXPECT_TRUE(x.IsUint64());

	EXPECT_FALSE(x.IsDouble());
	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsBool());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	Value nx(-1234);
	EXPECT_EQ(-1234, nx.GetInt());
	EXPECT_EQ(-1234, nx.GetInt64());
	EXPECT_TRUE(nx.IsInt());
	EXPECT_TRUE(nx.IsInt64());
	EXPECT_FALSE(nx.IsUint());
	EXPECT_FALSE(nx.IsUint64());

	// Constructor with type
	Value y(kNumberType);
	EXPECT_TRUE(y.IsNumber());
	EXPECT_TRUE(y.IsInt());
	EXPECT_EQ(0, y.GetInt());

	// SetInt()
	Value z;
	z.SetInt(1234);
	EXPECT_EQ(1234, z.GetInt());

	// operator=(int)
	z = 5678;
	EXPECT_EQ(5678, z.GetInt());
}

TEST(Value, Uint) {
	// Constructor with int
	Value x(1234u);
	EXPECT_EQ(kNumberType, x.GetType());
190
	EXPECT_EQ(1234, x.GetInt());
191
	EXPECT_EQ(1234u, x.GetUint());
192
	EXPECT_EQ(1234, x.GetInt64());
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	EXPECT_EQ(1234u, x.GetUint64());
	EXPECT_TRUE(x.IsNumber());
	EXPECT_TRUE(x.IsInt());
	EXPECT_TRUE(x.IsUint());
	EXPECT_TRUE(x.IsInt64());
	EXPECT_TRUE(x.IsUint64());
	EXPECT_EQ(1234.0, x.GetDouble());	// Number can always be cast as double but !IsDouble().

	EXPECT_FALSE(x.IsDouble());
	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsBool());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	// SetUint()
	Value z;
	z.SetUint(1234);
213
	EXPECT_EQ(1234u, z.GetUint());
214 215 216

	// operator=(unsigned)
	z = 5678u;
217
	EXPECT_EQ(5678u, z.GetUint());
218 219 220 221

	z = 2147483648u;	// 2^31, cannot cast as int
	EXPECT_EQ(2147483648u, z.GetUint());
	EXPECT_FALSE(z.IsInt());
222
	EXPECT_TRUE(z.IsInt64());	// Issue 41: Incorrect parsing of unsigned int number types
223 224 225 226
}

TEST(Value, Int64) {
	// Constructor with int
227
	Value x(int64_t(1234LL));
228 229
	EXPECT_EQ(kNumberType, x.GetType());
	EXPECT_EQ(1234, x.GetInt());
230
	EXPECT_EQ(1234u, x.GetUint());
231
	EXPECT_EQ(1234, x.GetInt64());
232
	EXPECT_EQ(1234u, x.GetUint64());
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	EXPECT_TRUE(x.IsNumber());
	EXPECT_TRUE(x.IsInt());
	EXPECT_TRUE(x.IsUint());
	EXPECT_TRUE(x.IsInt64());
	EXPECT_TRUE(x.IsUint64());

	EXPECT_FALSE(x.IsDouble());
	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsBool());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

248
	Value nx(int64_t(-1234LL));
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	EXPECT_EQ(-1234, nx.GetInt());
	EXPECT_EQ(-1234, nx.GetInt64());
	EXPECT_TRUE(nx.IsInt());
	EXPECT_TRUE(nx.IsInt64());
	EXPECT_FALSE(nx.IsUint());
	EXPECT_FALSE(nx.IsUint64());

	// SetInt64()
	Value z;
	z.SetInt64(1234);
	EXPECT_EQ(1234, z.GetInt64());

	z.SetInt64(2147483648LL);	// 2^31, cannot cast as int
	EXPECT_FALSE(z.IsInt());
	EXPECT_TRUE(z.IsUint());

	z.SetInt64(4294967296LL);	// 2^32, cannot cast as uint
	EXPECT_FALSE(z.IsInt());
	EXPECT_FALSE(z.IsUint());
}

TEST(Value, Uint64) {
	// Constructor with int
272
	Value x(uint64_t(1234LL));
273 274
	EXPECT_EQ(kNumberType, x.GetType());
	EXPECT_EQ(1234, x.GetInt());
275
	EXPECT_EQ(1234u, x.GetUint());
276
	EXPECT_EQ(1234, x.GetInt64());
277
	EXPECT_EQ(1234u, x.GetUint64());
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	EXPECT_TRUE(x.IsNumber());
	EXPECT_TRUE(x.IsInt());
	EXPECT_TRUE(x.IsUint());
	EXPECT_TRUE(x.IsInt64());
	EXPECT_TRUE(x.IsUint64());

	EXPECT_FALSE(x.IsDouble());
	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsBool());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	// SetUint64()
	Value z;
	z.SetUint64(1234);
296
	EXPECT_EQ(1234u, z.GetUint64());
297 298 299 300 301 302 303 304 305 306 307 308 309

	z.SetUint64(2147483648LL);	// 2^31, cannot cast as int
	EXPECT_FALSE(z.IsInt());
	EXPECT_TRUE(z.IsUint());
	EXPECT_TRUE(z.IsInt64());

	z.SetUint64(4294967296LL);	// 2^32, cannot cast as uint
	EXPECT_FALSE(z.IsInt());
	EXPECT_FALSE(z.IsUint());
	EXPECT_TRUE(z.IsInt64());

	z.SetUint64(9223372036854775808uLL);	// 2^63 cannot cast as int64
	EXPECT_FALSE(z.IsInt64());
310 311 312

	// Issue 48
	EXPECT_EQ(9223372036854775808uLL, z.GetUint64());
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
}

TEST(Value, Double) {
	// Constructor with double
	Value x(12.34);
	EXPECT_EQ(kNumberType, x.GetType());
	EXPECT_EQ(12.34, x.GetDouble());
	EXPECT_TRUE(x.IsNumber());
	EXPECT_TRUE(x.IsDouble());

	EXPECT_FALSE(x.IsInt());
	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsBool());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	// SetDouble()
	Value z;
	z.SetDouble(12.34);
	EXPECT_EQ(12.34, z.GetDouble());

	z = 56.78;
	EXPECT_EQ(56.78, z.GetDouble());
}

TEST(Value, String) {
	// Constructor with const string
	Value x("Hello", 5);
	EXPECT_EQ(kStringType, x.GetType());
	EXPECT_TRUE(x.IsString());
	EXPECT_STREQ("Hello", x.GetString());
347
	EXPECT_EQ(5u, x.GetStringLength());
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

	EXPECT_FALSE(x.IsNumber());
	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsBool());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsObject());
	EXPECT_FALSE(x.IsArray());

	// Constructor with copy string
	MemoryPoolAllocator<> allocator;
	Value c(x.GetString(), x.GetStringLength(), allocator);
	//x.SetString("World");
	x.SetString("World", 5);
	EXPECT_STREQ("Hello", c.GetString());
363
	EXPECT_EQ(5u, c.GetStringLength());
364 365 366 367 368

	// Constructor with type
	Value y(kStringType);
	EXPECT_TRUE(y.IsString());
	EXPECT_EQ(0, y.GetString());
369
	EXPECT_EQ(0u, y.GetStringLength());
370 371 372 373 374 375

	// SetConsttring()
	Value z;
	//z.SetString("Hello");
	z.SetString("Hello", 5);
	EXPECT_STREQ("Hello", z.GetString());
376
	EXPECT_EQ(5u, z.GetStringLength());
377 378 379 380 381 382 383

	// SetString()
	char s[] = "World";
	Value w;
	w.SetString(s, (SizeType)strlen(s), allocator);
	s[0] = '\0';
	EXPECT_STREQ("World", w.GetString());
384
	EXPECT_EQ(5u, w.GetStringLength());
385 386 387 388 389 390 391 392 393 394
}

TEST(Value, Array) {
	Value x(kArrayType);
	const Value& y = x;
	Value::AllocatorType allocator;

	EXPECT_EQ(kArrayType, x.GetType());
	EXPECT_TRUE(x.IsArray());
	EXPECT_TRUE(x.Empty());
395
	EXPECT_EQ(0u, x.Size());
396 397
	EXPECT_TRUE(y.IsArray());
	EXPECT_TRUE(y.Empty());
398
	EXPECT_EQ(0u, y.Size());
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

	EXPECT_FALSE(x.IsNull());
	EXPECT_FALSE(x.IsBool());
	EXPECT_FALSE(x.IsFalse());
	EXPECT_FALSE(x.IsTrue());
	EXPECT_FALSE(x.IsString());
	EXPECT_FALSE(x.IsObject());

	// PushBack()
	Value v;
	x.PushBack(v, allocator);
	v.SetBool(true);
	x.PushBack(v, allocator);
	v.SetBool(false);
	x.PushBack(v, allocator);
	v.SetInt(123);
	x.PushBack(v, allocator);

	EXPECT_FALSE(x.Empty());
418
	EXPECT_EQ(4u, x.Size());
419
	EXPECT_FALSE(y.Empty());
420
	EXPECT_EQ(4u, y.Size());
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	EXPECT_TRUE(x[SizeType(0)].IsNull());
	EXPECT_TRUE(x[1u].IsTrue());
	EXPECT_TRUE(x[2u].IsFalse());
	EXPECT_TRUE(x[3u].IsInt());
	EXPECT_EQ(123, x[3u].GetInt());
	EXPECT_TRUE(y[SizeType(0)].IsNull());
	EXPECT_TRUE(y[1u].IsTrue());
	EXPECT_TRUE(y[2u].IsFalse());
	EXPECT_TRUE(y[3u].IsInt());
	EXPECT_EQ(123, y[3u].GetInt());

	// iterator
	Value::ValueIterator itr = x.Begin();
	EXPECT_TRUE(itr != x.End());
	EXPECT_TRUE(itr->IsNull());
	++itr;
	EXPECT_TRUE(itr != x.End());
	EXPECT_TRUE(itr->IsTrue());
	++itr;
	EXPECT_TRUE(itr != x.End());
	EXPECT_TRUE(itr->IsFalse());
	++itr;
	EXPECT_TRUE(itr != x.End());
	EXPECT_TRUE(itr->IsInt());
	EXPECT_EQ(123, itr->GetInt());

	// const iterator
	Value::ConstValueIterator citr = y.Begin();
	EXPECT_TRUE(citr != y.End());
	EXPECT_TRUE(citr->IsNull());
	++citr;
	EXPECT_TRUE(citr != y.End());
	EXPECT_TRUE(citr->IsTrue());
	++citr;
	EXPECT_TRUE(citr != y.End());
	EXPECT_TRUE(citr->IsFalse());
	++citr;
	EXPECT_TRUE(citr != y.End());
	EXPECT_TRUE(citr->IsInt());
	EXPECT_EQ(123, citr->GetInt());

	// PopBack()
	x.PopBack();
464
	EXPECT_EQ(3u, x.Size());
465 466 467 468 469 470 471
	EXPECT_TRUE(y[SizeType(0)].IsNull());
	EXPECT_TRUE(y[1].IsTrue());
	EXPECT_TRUE(y[2].IsFalse());

	// Clear()
	x.Clear();
	EXPECT_TRUE(x.Empty());
472
	EXPECT_EQ(0u, x.Size());
473
	EXPECT_TRUE(y.Empty());
474
	EXPECT_EQ(0u, y.Size());
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

	// SetArray()
	Value z;
	z.SetArray();
	EXPECT_TRUE(z.IsArray());
	EXPECT_TRUE(z.Empty());
}

TEST(Value, Object) {
	Value x(kObjectType);
	const Value& y = x;	// const version
	Value::AllocatorType allocator;

	EXPECT_EQ(kObjectType, x.GetType());
	EXPECT_TRUE(x.IsObject());
	EXPECT_EQ(kObjectType, y.GetType());
	EXPECT_TRUE(y.IsObject());

	// AddMember()
	Value name("A", 1);
	Value value("Apple", 5);
	x.AddMember(name, value, allocator);
	//name.SetString("B");
	name.SetString("B", 1);
	//value.SetString("Banana");
	value.SetString("Banana", 6);
	x.AddMember(name, value, allocator);

503 504 505 506 507 508
	// Tests a member with null character
	const Value C0D("C\0D", 3);
	name.SetString(C0D.GetString(), 3);
	value.SetString("CherryD", 7);
	x.AddMember(name, value, allocator);

509 510 511 512 513 514
	// HasMember()
	EXPECT_TRUE(x.HasMember("A"));
	EXPECT_TRUE(x.HasMember("B"));
	EXPECT_TRUE(y.HasMember("A"));
	EXPECT_TRUE(y.HasMember("B"));

515 516 517 518
	name.SetString("C\0D", 3);
	EXPECT_TRUE(x.HasMember(name));
	EXPECT_TRUE(y.HasMember(name));

519 520 521
	// operator[]
	EXPECT_STREQ("Apple", x["A"].GetString());
	EXPECT_STREQ("Banana", x["B"].GetString());
522
	EXPECT_STREQ("CherryD", x[C0D].GetString());
523 524 525 526

	// const operator[]
	EXPECT_STREQ("Apple", y["A"].GetString());
	EXPECT_STREQ("Banana", y["B"].GetString());
527
	EXPECT_STREQ("CherryD", y[C0D].GetString());
528 529 530 531 532 533 534 535 536 537 538

	// member iterator
	Value::MemberIterator itr = x.MemberBegin(); 
	EXPECT_TRUE(itr != x.MemberEnd());
	EXPECT_STREQ("A", itr->name.GetString());
	EXPECT_STREQ("Apple", itr->value.GetString());
	++itr;
	EXPECT_TRUE(itr != x.MemberEnd());
	EXPECT_STREQ("B", itr->name.GetString());
	EXPECT_STREQ("Banana", itr->value.GetString());
	++itr;
539 540 541 542
	EXPECT_TRUE(itr != x.MemberEnd());
	EXPECT_TRUE(memcmp(itr->name.GetString(), "C\0D", 4) == 0);
	EXPECT_STREQ("CherryD", itr->value.GetString());
	++itr;
543 544 545 546 547 548 549 550 551 552 553 554
	EXPECT_FALSE(itr != x.MemberEnd());

	// const member iterator
	Value::ConstMemberIterator citr = y.MemberBegin(); 
	EXPECT_TRUE(citr != y.MemberEnd());
	EXPECT_STREQ("A", citr->name.GetString());
	EXPECT_STREQ("Apple", citr->value.GetString());
	++citr;
	EXPECT_TRUE(citr != y.MemberEnd());
	EXPECT_STREQ("B", citr->name.GetString());
	EXPECT_STREQ("Banana", citr->value.GetString());
	++citr;
555 556 557 558
	EXPECT_TRUE(citr != y.MemberEnd());
	EXPECT_TRUE(memcmp(citr->name.GetString(), "C\0D", 4) == 0);
	EXPECT_STREQ("CherryD", citr->value.GetString());
	++citr;
559 560 561 562 563 564 565 566 567
	EXPECT_FALSE(citr != y.MemberEnd());

	// RemoveMember()
	x.RemoveMember("A");
	EXPECT_FALSE(x.HasMember("A"));

	x.RemoveMember("B");
	EXPECT_FALSE(x.HasMember("B"));

568 569 570
	x.RemoveMember(name);
	EXPECT_FALSE(x.HasMember(name));

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	EXPECT_TRUE(x.MemberBegin() == x.MemberEnd());

	// SetObject()
	Value z;
	z.SetObject();
	EXPECT_TRUE(z.IsObject());
}

TEST(Value, BigNestedArray) {
	MemoryPoolAllocator<> allocator;
	Value x(kArrayType);
	static const SizeType  n = 200;

	for (SizeType i = 0; i < n; i++) {
		Value y(kArrayType);
		for (SizeType  j = 0; j < n; j++) {
			Value number((int)(i * n + j));
			y.PushBack(number, allocator);
		}
		x.PushBack(y, allocator);
	}

	for (SizeType i = 0; i < n; i++)
		for (SizeType j = 0; j < n; j++) {
			EXPECT_TRUE(x[i][j].IsInt());
			EXPECT_EQ((int)(i * n + j), x[i][j].GetInt());
		}
}

TEST(Value, BigNestedObject) {
	MemoryPoolAllocator<> allocator;
	Value x(kObjectType);
	static const SizeType n = 200;

	for (SizeType i = 0; i < n; i++) {
		char name1[10];
		sprintf(name1, "%d", i);

		Value name(name1, (SizeType)strlen(name1), allocator);
		Value object(kObjectType);

		for (SizeType j = 0; j < n; j++) {
			char name2[10];
			sprintf(name2, "%d", j);

			Value name(name2, (SizeType)strlen(name2), allocator);
			Value number((int)(i * n + j));
			object.AddMember(name, number, allocator);
		}

		x.AddMember(name, object, allocator);
	}

	for (SizeType i = 0; i < n; i++) {
		char name1[10];
		sprintf(name1, "%d", i);
		
		for (SizeType j = 0; j < n; j++) {
			char name2[10];
			sprintf(name2, "%d", j);
			x[name1];
			EXPECT_EQ((int)(i * n + j), x[name1][name2].GetInt());
		}
	}
}
636 637 638 639 640 641 642 643 644 645 646 647 648

// Issue 18: Error removing last element of object
// http://code.google.com/p/rapidjson/issues/detail?id=18
TEST(Value, RemoveLastElement) {
	rapidjson::Document doc;
	rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
	rapidjson::Value objVal(rapidjson::kObjectType);        
	objVal.AddMember("var1", 123, allocator);       
	objVal.AddMember("var2", "444", allocator);
	objVal.AddMember("var3", 555, allocator);
	EXPECT_TRUE(objVal.HasMember("var3"));
	objVal.RemoveMember("var3");    // Assertion here in r61
	EXPECT_FALSE(objVal.HasMember("var3"));
649
}
650 651 652 653 654 655 656 657 658 659 660 661

// Issue 38:	Segmentation fault with CrtAllocator
TEST(Document, CrtAllocator) {
	typedef GenericValue<UTF8<>, CrtAllocator> V;

	V::AllocatorType allocator;
	V o(kObjectType);
	o.AddMember("x", 1, allocator);	// Should not call destructor on uninitialized name/value of newly allocated members.

	V a(kArrayType);
	a.PushBack(1, allocator);	// Should not call destructor on uninitialized Value of newly allocated elements.
}