OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <cmath> |
5 #include <limits> | 6 #include <limits> |
6 | 7 |
| 8 #include "base/basictypes.h" |
7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
8 #include "base/string16.h" | 10 #include "base/string16.h" |
9 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 12 #include "base/values.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 | 16 |
15 TEST(ValuesTest, Basic) { | 17 TEST(ValuesTest, Basic) { |
16 // Test basic dictionary getting/setting | 18 // Test basic dictionary getting/setting |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 ASSERT_EQ(std::string("Froogle"), bookmark_name); | 54 ASSERT_EQ(std::string("Froogle"), bookmark_name); |
53 std::string bookmark_url; | 55 std::string bookmark_url; |
54 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url)); | 56 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url)); |
55 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url); | 57 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url); |
56 } | 58 } |
57 | 59 |
58 TEST(ValuesTest, List) { | 60 TEST(ValuesTest, List) { |
59 scoped_ptr<ListValue> mixed_list(new ListValue()); | 61 scoped_ptr<ListValue> mixed_list(new ListValue()); |
60 mixed_list->Set(0, Value::CreateBooleanValue(true)); | 62 mixed_list->Set(0, Value::CreateBooleanValue(true)); |
61 mixed_list->Set(1, Value::CreateIntegerValue(42)); | 63 mixed_list->Set(1, Value::CreateIntegerValue(42)); |
62 mixed_list->Set(2, Value::CreateDoubleValue(88.8)); | 64 mixed_list->Set(2, Value::CreateInteger64Value(std::pow(2, 42))); |
63 mixed_list->Set(3, Value::CreateStringValue("foo")); | 65 mixed_list->Set(3, Value::CreateDoubleValue(88.8)); |
64 ASSERT_EQ(4u, mixed_list->GetSize()); | 66 mixed_list->Set(4, Value::CreateStringValue("foo")); |
| 67 ASSERT_EQ(5u, mixed_list->GetSize()); |
65 | 68 |
66 Value *value = NULL; | 69 Value *value = NULL; |
67 bool bool_value = false; | 70 bool bool_value = false; |
68 int int_value = 0; | 71 int int_value = 0; |
| 72 int64 int64_value = 0; |
69 double double_value = 0.0; | 73 double double_value = 0.0; |
70 std::string string_value; | 74 std::string string_value; |
71 | 75 |
72 ASSERT_FALSE(mixed_list->Get(4, &value)); | 76 ASSERT_FALSE(mixed_list->Get(5, &value)); |
73 | 77 |
74 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value)); | 78 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value)); |
75 ASSERT_EQ(0, int_value); | 79 ASSERT_EQ(0, int_value); |
| 80 ASSERT_FALSE(mixed_list->GetInteger64(0, &int64_value)); |
| 81 ASSERT_EQ(0, int64_value); |
76 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value)); | 82 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value)); |
77 ASSERT_FALSE(bool_value); | 83 ASSERT_FALSE(bool_value); |
78 ASSERT_FALSE(mixed_list->GetString(2, &string_value)); | 84 ASSERT_FALSE(mixed_list->GetBoolean(2, &bool_value)); |
| 85 ASSERT_FALSE(bool_value); |
| 86 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value)); |
| 87 ASSERT_FALSE(int_value); |
| 88 // Implicit conversion from int64 to double is not allowed, even if the |
| 89 // double can represent the integer. |
| 90 ASSERT_FALSE(mixed_list->GetDouble(2, &double_value)); |
| 91 ASSERT_FALSE(double_value); |
| 92 ASSERT_FALSE(mixed_list->GetString(3, &string_value)); |
79 ASSERT_EQ("", string_value); | 93 ASSERT_EQ("", string_value); |
80 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value)); | 94 ASSERT_FALSE(mixed_list->GetInteger(3, &int_value)); |
81 ASSERT_EQ(0, int_value); | 95 ASSERT_EQ(0, int_value); |
82 ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value)); | 96 ASSERT_FALSE(mixed_list->GetInteger64(3, &int64_value)); |
| 97 ASSERT_EQ(0, int64_value); |
| 98 ASSERT_FALSE(mixed_list->GetBoolean(4, &bool_value)); |
83 ASSERT_FALSE(bool_value); | 99 ASSERT_FALSE(bool_value); |
84 | 100 |
85 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value)); | 101 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value)); |
86 ASSERT_TRUE(bool_value); | 102 ASSERT_TRUE(bool_value); |
87 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value)); | 103 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value)); |
88 ASSERT_EQ(42, int_value); | 104 ASSERT_EQ(42, int_value); |
89 // implicit conversion from Integer to Double should be possible. | 105 // Implicit conversion from Integer to Double should be possible. |
90 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value)); | 106 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value)); |
91 ASSERT_EQ(42, double_value); | 107 ASSERT_EQ(42, double_value); |
92 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value)); | 108 ASSERT_TRUE(mixed_list->GetInteger64(1, &int64_value)); |
| 109 ASSERT_EQ(42, int64_value); |
| 110 ASSERT_TRUE(mixed_list->GetInteger64(2, &int64_value)); |
| 111 ASSERT_EQ(std::pow(2, 42), int64_value); |
| 112 ASSERT_TRUE(mixed_list->GetDouble(3, &double_value)); |
93 ASSERT_EQ(88.8, double_value); | 113 ASSERT_EQ(88.8, double_value); |
94 ASSERT_TRUE(mixed_list->GetString(3, &string_value)); | 114 ASSERT_TRUE(mixed_list->GetString(4, &string_value)); |
95 ASSERT_EQ("foo", string_value); | 115 ASSERT_EQ("foo", string_value); |
96 | 116 |
97 // Try searching in the mixed list. | 117 // Try searching in the mixed list. |
98 scoped_ptr<Value> sought_value(Value::CreateIntegerValue(42)); | 118 scoped_ptr<Value> sought_value(Value::CreateIntegerValue(42)); |
99 scoped_ptr<Value> not_found_value(Value::CreateBooleanValue(false)); | 119 scoped_ptr<Value> not_found_value(Value::CreateBooleanValue(false)); |
100 | 120 |
101 ASSERT_NE(mixed_list->end(), mixed_list->Find(*sought_value)); | 121 ASSERT_NE(mixed_list->end(), mixed_list->Find(*sought_value)); |
102 ASSERT_TRUE((*mixed_list->Find(*sought_value))->GetAsInteger(&int_value)); | 122 ASSERT_TRUE((*mixed_list->Find(*sought_value))->GetAsInteger(&int_value)); |
103 ASSERT_EQ(42, int_value); | 123 ASSERT_EQ(42, int_value); |
104 ASSERT_EQ(mixed_list->end(), mixed_list->Find(*not_found_value)); | 124 ASSERT_EQ(mixed_list->end(), mixed_list->Find(*not_found_value)); |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 } | 355 } |
336 | 356 |
337 TEST(ValuesTest, DeepCopy) { | 357 TEST(ValuesTest, DeepCopy) { |
338 DictionaryValue original_dict; | 358 DictionaryValue original_dict; |
339 Value* original_null = Value::CreateNullValue(); | 359 Value* original_null = Value::CreateNullValue(); |
340 original_dict.Set("null", original_null); | 360 original_dict.Set("null", original_null); |
341 FundamentalValue* original_bool = Value::CreateBooleanValue(true); | 361 FundamentalValue* original_bool = Value::CreateBooleanValue(true); |
342 original_dict.Set("bool", original_bool); | 362 original_dict.Set("bool", original_bool); |
343 FundamentalValue* original_int = Value::CreateIntegerValue(42); | 363 FundamentalValue* original_int = Value::CreateIntegerValue(42); |
344 original_dict.Set("int", original_int); | 364 original_dict.Set("int", original_int); |
| 365 FundamentalValue* original_int64 = |
| 366 Value::CreateInteger64Value(std::pow(2, 42)); |
| 367 original_dict.Set("int64", original_int64); |
345 FundamentalValue* original_double = Value::CreateDoubleValue(3.14); | 368 FundamentalValue* original_double = Value::CreateDoubleValue(3.14); |
346 original_dict.Set("double", original_double); | 369 original_dict.Set("double", original_double); |
347 StringValue* original_string = Value::CreateStringValue("hello"); | 370 StringValue* original_string = Value::CreateStringValue("hello"); |
348 original_dict.Set("string", original_string); | 371 original_dict.Set("string", original_string); |
349 StringValue* original_string16 = | 372 StringValue* original_string16 = |
350 Value::CreateStringValue(ASCIIToUTF16("hello16")); | 373 Value::CreateStringValue(ASCIIToUTF16("hello16")); |
351 original_dict.Set("string16", original_string16); | 374 original_dict.Set("string16", original_string16); |
352 | 375 |
353 char* original_buffer = new char[42]; | 376 char* original_buffer = new char[42]; |
354 memset(original_buffer, '!', 42); | 377 memset(original_buffer, '!', 42); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 410 |
388 Value* copy_int = NULL; | 411 Value* copy_int = NULL; |
389 ASSERT_TRUE(copy_dict->Get("int", ©_int)); | 412 ASSERT_TRUE(copy_dict->Get("int", ©_int)); |
390 ASSERT_TRUE(copy_int); | 413 ASSERT_TRUE(copy_int); |
391 ASSERT_NE(copy_int, original_int); | 414 ASSERT_NE(copy_int, original_int); |
392 ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER)); | 415 ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER)); |
393 int copy_int_value = 0; | 416 int copy_int_value = 0; |
394 ASSERT_TRUE(copy_int->GetAsInteger(©_int_value)); | 417 ASSERT_TRUE(copy_int->GetAsInteger(©_int_value)); |
395 ASSERT_EQ(42, copy_int_value); | 418 ASSERT_EQ(42, copy_int_value); |
396 | 419 |
| 420 Value* copy_int64 = NULL; |
| 421 ASSERT_TRUE(copy_dict->Get("int64", ©_int64)); |
| 422 ASSERT_TRUE(copy_int64); |
| 423 ASSERT_NE(copy_int64, original_int64); |
| 424 ASSERT_TRUE(copy_int64->IsType(Value::TYPE_INTEGER64)); |
| 425 int64 copy_int64_value = 0; |
| 426 ASSERT_TRUE(copy_int64->GetAsInteger64(©_int64_value)); |
| 427 ASSERT_EQ(std::pow(2, 42), copy_int64_value); |
| 428 |
397 Value* copy_double = NULL; | 429 Value* copy_double = NULL; |
398 ASSERT_TRUE(copy_dict->Get("double", ©_double)); | 430 ASSERT_TRUE(copy_dict->Get("double", ©_double)); |
399 ASSERT_TRUE(copy_double); | 431 ASSERT_TRUE(copy_double); |
400 ASSERT_NE(copy_double, original_double); | 432 ASSERT_NE(copy_double, original_double); |
401 ASSERT_TRUE(copy_double->IsType(Value::TYPE_DOUBLE)); | 433 ASSERT_TRUE(copy_double->IsType(Value::TYPE_DOUBLE)); |
402 double copy_double_value = 0; | 434 double copy_double_value = 0; |
403 ASSERT_TRUE(copy_double->GetAsDouble(©_double_value)); | 435 ASSERT_TRUE(copy_double->GetAsDouble(©_double_value)); |
404 ASSERT_EQ(3.14, copy_double_value); | 436 ASSERT_EQ(3.14, copy_double_value); |
405 | 437 |
406 Value* copy_string = NULL; | 438 Value* copy_string = NULL; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 | 515 |
484 Value* boolean = Value::CreateBooleanValue(false); | 516 Value* boolean = Value::CreateBooleanValue(false); |
485 EXPECT_FALSE(null1->Equals(boolean)); | 517 EXPECT_FALSE(null1->Equals(boolean)); |
486 delete null1; | 518 delete null1; |
487 delete null2; | 519 delete null2; |
488 delete boolean; | 520 delete boolean; |
489 | 521 |
490 DictionaryValue dv; | 522 DictionaryValue dv; |
491 dv.SetBoolean("a", false); | 523 dv.SetBoolean("a", false); |
492 dv.SetInteger("b", 2); | 524 dv.SetInteger("b", 2); |
| 525 dv.SetInteger64("b64", std::pow(2, 42)); |
493 dv.SetDouble("c", 2.5); | 526 dv.SetDouble("c", 2.5); |
494 dv.SetString("d1", "string"); | 527 dv.SetString("d1", "string"); |
495 dv.SetString("d2", ASCIIToUTF16("http://google.com")); | 528 dv.SetString("d2", ASCIIToUTF16("http://google.com")); |
496 dv.Set("e", Value::CreateNullValue()); | 529 dv.Set("e", Value::CreateNullValue()); |
497 | 530 |
498 scoped_ptr<DictionaryValue> copy; | 531 scoped_ptr<DictionaryValue> copy; |
499 copy.reset(dv.DeepCopy()); | 532 copy.reset(dv.DeepCopy()); |
500 EXPECT_TRUE(dv.Equals(copy.get())); | 533 EXPECT_TRUE(dv.Equals(copy.get())); |
501 | 534 |
502 ListValue* list = new ListValue; | 535 ListValue* list = new ListValue; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 } | 576 } |
544 | 577 |
545 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { | 578 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { |
546 DictionaryValue original_dict; | 579 DictionaryValue original_dict; |
547 Value* original_null = Value::CreateNullValue(); | 580 Value* original_null = Value::CreateNullValue(); |
548 original_dict.Set("null", original_null); | 581 original_dict.Set("null", original_null); |
549 FundamentalValue* original_bool = Value::CreateBooleanValue(true); | 582 FundamentalValue* original_bool = Value::CreateBooleanValue(true); |
550 original_dict.Set("bool", original_bool); | 583 original_dict.Set("bool", original_bool); |
551 FundamentalValue* original_int = Value::CreateIntegerValue(42); | 584 FundamentalValue* original_int = Value::CreateIntegerValue(42); |
552 original_dict.Set("int", original_int); | 585 original_dict.Set("int", original_int); |
| 586 FundamentalValue* original_int64 = |
| 587 Value::CreateInteger64Value(std::pow(2, 42)); |
| 588 original_dict.Set("int64", original_int64); |
553 FundamentalValue* original_double = Value::CreateDoubleValue(3.14); | 589 FundamentalValue* original_double = Value::CreateDoubleValue(3.14); |
554 original_dict.Set("double", original_double); | 590 original_dict.Set("double", original_double); |
555 StringValue* original_string = Value::CreateStringValue("hello"); | 591 StringValue* original_string = Value::CreateStringValue("hello"); |
556 original_dict.Set("string", original_string); | 592 original_dict.Set("string", original_string); |
557 StringValue* original_string16 = | 593 StringValue* original_string16 = |
558 Value::CreateStringValue(ASCIIToUTF16("hello16")); | 594 Value::CreateStringValue(ASCIIToUTF16("hello16")); |
559 original_dict.Set("string16", original_string16); | 595 original_dict.Set("string16", original_string16); |
560 | 596 |
561 char* original_buffer = new char[42]; | 597 char* original_buffer = new char[42]; |
562 memset(original_buffer, '!', 42); | 598 memset(original_buffer, '!', 42); |
563 BinaryValue* original_binary = BinaryValue::Create(original_buffer, 42); | 599 BinaryValue* original_binary = BinaryValue::Create(original_buffer, 42); |
564 original_dict.Set("binary", original_binary); | 600 original_dict.Set("binary", original_binary); |
565 | 601 |
566 ListValue* original_list = new ListValue(); | 602 ListValue* original_list = new ListValue(); |
567 FundamentalValue* original_list_element_0 = Value::CreateIntegerValue(0); | 603 FundamentalValue* original_list_element_0 = Value::CreateIntegerValue(0); |
568 original_list->Append(original_list_element_0); | 604 original_list->Append(original_list_element_0); |
569 FundamentalValue* original_list_element_1 = Value::CreateIntegerValue(1); | 605 FundamentalValue* original_list_element_1 = Value::CreateIntegerValue(1); |
570 original_list->Append(original_list_element_1); | 606 original_list->Append(original_list_element_1); |
571 original_dict.Set("list", original_list); | 607 original_dict.Set("list", original_list); |
572 | 608 |
573 Value* original_dict_value = &original_dict; | 609 Value* original_dict_value = &original_dict; |
574 Value* original_bool_value = original_bool; | 610 Value* original_bool_value = original_bool; |
575 Value* original_int_value = original_int; | 611 Value* original_int_value = original_int; |
| 612 Value* original_int64_value = original_int64; |
576 Value* original_double_value = original_double; | 613 Value* original_double_value = original_double; |
577 Value* original_string_value = original_string; | 614 Value* original_string_value = original_string; |
578 Value* original_string16_value = original_string16; | 615 Value* original_string16_value = original_string16; |
579 Value* original_binary_value = original_binary; | 616 Value* original_binary_value = original_binary; |
580 Value* original_list_value = original_list; | 617 Value* original_list_value = original_list; |
581 | 618 |
582 scoped_ptr<Value> copy_dict_value(original_dict_value->DeepCopy()); | 619 scoped_ptr<Value> copy_dict_value(original_dict_value->DeepCopy()); |
583 scoped_ptr<Value> copy_bool_value(original_bool_value->DeepCopy()); | 620 scoped_ptr<Value> copy_bool_value(original_bool_value->DeepCopy()); |
584 scoped_ptr<Value> copy_int_value(original_int_value->DeepCopy()); | 621 scoped_ptr<Value> copy_int_value(original_int_value->DeepCopy()); |
| 622 scoped_ptr<Value> copy_int64_value(original_int64_value->DeepCopy()); |
585 scoped_ptr<Value> copy_double_value(original_double_value->DeepCopy()); | 623 scoped_ptr<Value> copy_double_value(original_double_value->DeepCopy()); |
586 scoped_ptr<Value> copy_string_value(original_string_value->DeepCopy()); | 624 scoped_ptr<Value> copy_string_value(original_string_value->DeepCopy()); |
587 scoped_ptr<Value> copy_string16_value(original_string16_value->DeepCopy()); | 625 scoped_ptr<Value> copy_string16_value(original_string16_value->DeepCopy()); |
588 scoped_ptr<Value> copy_binary_value(original_binary_value->DeepCopy()); | 626 scoped_ptr<Value> copy_binary_value(original_binary_value->DeepCopy()); |
589 scoped_ptr<Value> copy_list_value(original_list_value->DeepCopy()); | 627 scoped_ptr<Value> copy_list_value(original_list_value->DeepCopy()); |
590 | 628 |
591 EXPECT_TRUE(original_dict_value->Equals(copy_dict_value.get())); | 629 EXPECT_TRUE(original_dict_value->Equals(copy_dict_value.get())); |
592 EXPECT_TRUE(original_bool_value->Equals(copy_bool_value.get())); | 630 EXPECT_TRUE(original_bool_value->Equals(copy_bool_value.get())); |
593 EXPECT_TRUE(original_int_value->Equals(copy_int_value.get())); | 631 EXPECT_TRUE(original_int_value->Equals(copy_int_value.get())); |
| 632 EXPECT_TRUE(original_int64_value->Equals(copy_int64_value.get())); |
594 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get())); | 633 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get())); |
595 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get())); | 634 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get())); |
596 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get())); | 635 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get())); |
597 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get())); | 636 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get())); |
598 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get())); | 637 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get())); |
599 } | 638 } |
600 | 639 |
601 TEST(ValuesTest, RemoveEmptyChildren) { | 640 TEST(ValuesTest, RemoveEmptyChildren) { |
602 scoped_ptr<DictionaryValue> root(new DictionaryValue); | 641 scoped_ptr<DictionaryValue> root(new DictionaryValue); |
603 // Remove empty lists and dictionaries. | 642 // Remove empty lists and dictionaries. |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 seen2 = true; | 788 seen2 = true; |
750 } else { | 789 } else { |
751 ADD_FAILURE(); | 790 ADD_FAILURE(); |
752 } | 791 } |
753 } | 792 } |
754 EXPECT_TRUE(seen1); | 793 EXPECT_TRUE(seen1); |
755 EXPECT_TRUE(seen2); | 794 EXPECT_TRUE(seen2); |
756 } | 795 } |
757 | 796 |
758 } // namespace base | 797 } // namespace base |
OLD | NEW |