| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/values.h" | 5 #include "base/values.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace base { | 21 namespace base { |
| 22 | 22 |
| 23 TEST(ValuesTest, TestNothrow) { | 23 TEST(ValuesTest, TestNothrow) { |
| 24 static_assert(std::is_nothrow_move_constructible<Value>::value, | 24 static_assert(std::is_nothrow_move_constructible<Value>::value, |
| 25 "IsNothrowMoveConstructible"); | 25 "IsNothrowMoveConstructible"); |
| 26 static_assert(std::is_nothrow_default_constructible<Value>::value, | 26 static_assert(std::is_nothrow_default_constructible<Value>::value, |
| 27 "IsNothrowDefaultConstructible"); | 27 "IsNothrowDefaultConstructible"); |
| 28 static_assert(std::is_nothrow_constructible<Value, std::string&&>::value, | 28 static_assert(std::is_nothrow_constructible<Value, std::string&&>::value, |
| 29 "IsNothrowMoveConstructibleFromString"); | 29 "IsNothrowMoveConstructibleFromString"); |
| 30 static_assert( | 30 static_assert( |
| 31 std::is_nothrow_constructible<Value, std::vector<char>&&>::value, | 31 std::is_nothrow_constructible<Value, Value::BlobStorage&&>::value, |
| 32 "IsNothrowMoveConstructibleFromBlob"); | 32 "IsNothrowMoveConstructibleFromBlob"); |
| 33 static_assert(std::is_nothrow_move_assignable<Value>::value, | 33 static_assert(std::is_nothrow_move_assignable<Value>::value, |
| 34 "IsNothrowMoveAssignable"); | 34 "IsNothrowMoveAssignable"); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Group of tests for the value constructors. | 37 // Group of tests for the value constructors. |
| 38 TEST(ValuesTest, ConstructBool) { | 38 TEST(ValuesTest, ConstructBool) { |
| 39 Value true_value(true); | 39 Value true_value(true); |
| 40 EXPECT_EQ(Value::Type::BOOLEAN, true_value.type()); | 40 EXPECT_EQ(Value::Type::BOOLEAN, true_value.type()); |
| 41 EXPECT_TRUE(true_value.GetBool()); | 41 EXPECT_TRUE(true_value.GetBool()); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 93 } |
| 94 | 94 |
| 95 TEST(ValuesTest, ConstructStringFromStringPiece) { | 95 TEST(ValuesTest, ConstructStringFromStringPiece) { |
| 96 StringPiece str = "foobar"; | 96 StringPiece str = "foobar"; |
| 97 Value value(str); | 97 Value value(str); |
| 98 EXPECT_EQ(Value::Type::STRING, value.type()); | 98 EXPECT_EQ(Value::Type::STRING, value.type()); |
| 99 EXPECT_EQ("foobar", value.GetString()); | 99 EXPECT_EQ("foobar", value.GetString()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 TEST(ValuesTest, ConstructBinary) { | 102 TEST(ValuesTest, ConstructBinary) { |
| 103 Value value(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2})); | 103 Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2})); |
| 104 EXPECT_EQ(Value::Type::BINARY, value.type()); | 104 EXPECT_EQ(Value::Type::BINARY, value.type()); |
| 105 EXPECT_EQ(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}), value.GetBlob()); | 105 EXPECT_EQ(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}), |
| 106 value.GetBlob()); |
| 106 } | 107 } |
| 107 | 108 |
| 108 TEST(ValuesTest, ConstructDict) { | 109 TEST(ValuesTest, ConstructDict) { |
| 109 DictionaryValue value; | 110 DictionaryValue value; |
| 110 EXPECT_EQ(Value::Type::DICTIONARY, value.type()); | 111 EXPECT_EQ(Value::Type::DICTIONARY, value.type()); |
| 111 } | 112 } |
| 112 | 113 |
| 113 TEST(ValuesTest, ConstructList) { | 114 TEST(ValuesTest, ConstructList) { |
| 114 ListValue value; | 115 ListValue value; |
| 115 EXPECT_EQ(Value::Type::LIST, value.type()); | 116 EXPECT_EQ(Value::Type::LIST, value.type()); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 EXPECT_EQ(value.GetString(), copied_value.GetString()); | 174 EXPECT_EQ(value.GetString(), copied_value.GetString()); |
| 174 | 175 |
| 175 Value blank; | 176 Value blank; |
| 176 | 177 |
| 177 blank = value; | 178 blank = value; |
| 178 EXPECT_EQ(value.type(), blank.type()); | 179 EXPECT_EQ(value.type(), blank.type()); |
| 179 EXPECT_EQ(value.GetString(), blank.GetString()); | 180 EXPECT_EQ(value.GetString(), blank.GetString()); |
| 180 } | 181 } |
| 181 | 182 |
| 182 TEST(ValuesTest, CopyBinary) { | 183 TEST(ValuesTest, CopyBinary) { |
| 183 Value value(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2})); | 184 Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2})); |
| 184 Value copied_value(value); | 185 Value copied_value(value); |
| 185 EXPECT_EQ(value.type(), copied_value.type()); | 186 EXPECT_EQ(value.type(), copied_value.type()); |
| 186 EXPECT_EQ(value.GetBlob(), copied_value.GetBlob()); | 187 EXPECT_EQ(value.GetBlob(), copied_value.GetBlob()); |
| 187 | 188 |
| 188 Value blank; | 189 Value blank; |
| 189 | 190 |
| 190 blank = value; | 191 blank = value; |
| 191 EXPECT_EQ(value.type(), blank.type()); | 192 EXPECT_EQ(value.type(), blank.type()); |
| 192 EXPECT_EQ(value.GetBlob(), blank.GetBlob()); | 193 EXPECT_EQ(value.GetBlob(), blank.GetBlob()); |
| 193 } | 194 } |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 EXPECT_EQ("foobar", moved_value.GetString()); | 293 EXPECT_EQ("foobar", moved_value.GetString()); |
| 293 | 294 |
| 294 Value blank; | 295 Value blank; |
| 295 | 296 |
| 296 blank = Value("foobar"); | 297 blank = Value("foobar"); |
| 297 EXPECT_EQ(Value::Type::STRING, blank.type()); | 298 EXPECT_EQ(Value::Type::STRING, blank.type()); |
| 298 EXPECT_EQ("foobar", blank.GetString()); | 299 EXPECT_EQ("foobar", blank.GetString()); |
| 299 } | 300 } |
| 300 | 301 |
| 301 TEST(ValuesTest, MoveBinary) { | 302 TEST(ValuesTest, MoveBinary) { |
| 302 const std::vector<char> buffer = {0xF, 0x0, 0x0, 0xB, 0xA, 0x2}; | 303 const Value::BlobStorage buffer = {0xF, 0x0, 0x0, 0xB, 0xA, 0x2}; |
| 303 Value value(buffer); | 304 Value value(buffer); |
| 304 Value moved_value(std::move(value)); | 305 Value moved_value(std::move(value)); |
| 305 EXPECT_EQ(Value::Type::BINARY, moved_value.type()); | 306 EXPECT_EQ(Value::Type::BINARY, moved_value.type()); |
| 306 EXPECT_EQ(buffer, moved_value.GetBlob()); | 307 EXPECT_EQ(buffer, moved_value.GetBlob()); |
| 307 | 308 |
| 308 Value blank; | 309 Value blank; |
| 309 | 310 |
| 310 blank = Value(buffer); | 311 blank = Value(buffer); |
| 311 EXPECT_EQ(Value::Type::BINARY, blank.type()); | 312 EXPECT_EQ(Value::Type::BINARY, blank.type()); |
| 312 EXPECT_EQ(buffer, blank.GetBlob()); | 313 EXPECT_EQ(buffer, blank.GetBlob()); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value)); | 443 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value)); |
| 443 } | 444 } |
| 444 | 445 |
| 445 TEST(ValuesTest, BinaryValue) { | 446 TEST(ValuesTest, BinaryValue) { |
| 446 // Default constructor creates a BinaryValue with a buffer of size 0. | 447 // Default constructor creates a BinaryValue with a buffer of size 0. |
| 447 auto binary = MakeUnique<Value>(Value::Type::BINARY); | 448 auto binary = MakeUnique<Value>(Value::Type::BINARY); |
| 448 ASSERT_TRUE(binary.get()); | 449 ASSERT_TRUE(binary.get()); |
| 449 ASSERT_EQ(0U, binary->GetSize()); | 450 ASSERT_EQ(0U, binary->GetSize()); |
| 450 | 451 |
| 451 // Test the common case of a non-empty buffer | 452 // Test the common case of a non-empty buffer |
| 452 std::vector<char> buffer(15); | 453 Value::BlobStorage buffer(15); |
| 453 char* original_buffer = buffer.data(); | 454 char* original_buffer = buffer.data(); |
| 454 binary.reset(new Value(std::move(buffer))); | 455 binary.reset(new Value(std::move(buffer))); |
| 455 ASSERT_TRUE(binary.get()); | 456 ASSERT_TRUE(binary.get()); |
| 456 ASSERT_TRUE(binary->GetBuffer()); | 457 ASSERT_TRUE(binary->GetBuffer()); |
| 457 ASSERT_EQ(original_buffer, binary->GetBuffer()); | 458 ASSERT_EQ(original_buffer, binary->GetBuffer()); |
| 458 ASSERT_EQ(15U, binary->GetSize()); | 459 ASSERT_EQ(15U, binary->GetSize()); |
| 459 | 460 |
| 460 char stack_buffer[42]; | 461 char stack_buffer[42]; |
| 461 memset(stack_buffer, '!', 42); | 462 memset(stack_buffer, '!', 42); |
| 462 binary = Value::CreateWithCopiedBuffer(stack_buffer, 42); | 463 binary = Value::CreateWithCopiedBuffer(stack_buffer, 42); |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 std::unique_ptr<Value> scoped_double(new Value(3.14)); | 667 std::unique_ptr<Value> scoped_double(new Value(3.14)); |
| 667 Value* original_double = scoped_double.get(); | 668 Value* original_double = scoped_double.get(); |
| 668 original_dict.Set("double", std::move(scoped_double)); | 669 original_dict.Set("double", std::move(scoped_double)); |
| 669 std::unique_ptr<Value> scoped_string(new Value("hello")); | 670 std::unique_ptr<Value> scoped_string(new Value("hello")); |
| 670 Value* original_string = scoped_string.get(); | 671 Value* original_string = scoped_string.get(); |
| 671 original_dict.Set("string", std::move(scoped_string)); | 672 original_dict.Set("string", std::move(scoped_string)); |
| 672 std::unique_ptr<Value> scoped_string16(new Value(ASCIIToUTF16("hello16"))); | 673 std::unique_ptr<Value> scoped_string16(new Value(ASCIIToUTF16("hello16"))); |
| 673 Value* original_string16 = scoped_string16.get(); | 674 Value* original_string16 = scoped_string16.get(); |
| 674 original_dict.Set("string16", std::move(scoped_string16)); | 675 original_dict.Set("string16", std::move(scoped_string16)); |
| 675 | 676 |
| 676 std::vector<char> original_buffer(42, '!'); | 677 Value::BlobStorage original_buffer(42, '!'); |
| 677 std::unique_ptr<Value> scoped_binary(new Value(std::move(original_buffer))); | 678 std::unique_ptr<Value> scoped_binary(new Value(std::move(original_buffer))); |
| 678 Value* original_binary = scoped_binary.get(); | 679 Value* original_binary = scoped_binary.get(); |
| 679 original_dict.Set("binary", std::move(scoped_binary)); | 680 original_dict.Set("binary", std::move(scoped_binary)); |
| 680 | 681 |
| 681 std::unique_ptr<ListValue> scoped_list(new ListValue()); | 682 std::unique_ptr<ListValue> scoped_list(new ListValue()); |
| 682 Value* original_list = scoped_list.get(); | 683 Value* original_list = scoped_list.get(); |
| 683 std::unique_ptr<Value> scoped_list_element_0(new Value(0)); | 684 std::unique_ptr<Value> scoped_list_element_0(new Value(0)); |
| 684 Value* original_list_element_0 = scoped_list_element_0.get(); | 685 Value* original_list_element_0 = scoped_list_element_0.get(); |
| 685 scoped_list->Append(std::move(scoped_list_element_0)); | 686 scoped_list->Append(std::move(scoped_list_element_0)); |
| 686 std::unique_ptr<Value> scoped_list_element_1(new Value(1)); | 687 std::unique_ptr<Value> scoped_list_element_1(new Value(1)); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 Value string1("1"); | 911 Value string1("1"); |
| 911 Value string2("2"); | 912 Value string2("2"); |
| 912 EXPECT_FALSE(string1 == string2); | 913 EXPECT_FALSE(string1 == string2); |
| 913 EXPECT_NE(string1, string2); | 914 EXPECT_NE(string1, string2); |
| 914 EXPECT_LT(string1, string2); | 915 EXPECT_LT(string1, string2); |
| 915 EXPECT_FALSE(string1 > string2); | 916 EXPECT_FALSE(string1 > string2); |
| 916 EXPECT_LE(string1, string2); | 917 EXPECT_LE(string1, string2); |
| 917 EXPECT_FALSE(string1 >= string2); | 918 EXPECT_FALSE(string1 >= string2); |
| 918 | 919 |
| 919 // Test Binary Values. | 920 // Test Binary Values. |
| 920 Value binary1(std::vector<char>{0x01}); | 921 Value binary1(Value::BlobStorage{0x01}); |
| 921 Value binary2(std::vector<char>{0x02}); | 922 Value binary2(Value::BlobStorage{0x02}); |
| 922 EXPECT_FALSE(binary1 == binary2); | 923 EXPECT_FALSE(binary1 == binary2); |
| 923 EXPECT_NE(binary1, binary2); | 924 EXPECT_NE(binary1, binary2); |
| 924 EXPECT_LT(binary1, binary2); | 925 EXPECT_LT(binary1, binary2); |
| 925 EXPECT_FALSE(binary1 > binary2); | 926 EXPECT_FALSE(binary1 > binary2); |
| 926 EXPECT_LE(binary1, binary2); | 927 EXPECT_LE(binary1, binary2); |
| 927 EXPECT_FALSE(binary1 >= binary2); | 928 EXPECT_FALSE(binary1 >= binary2); |
| 928 | 929 |
| 929 // Test Empty List Values. | 930 // Test Empty List Values. |
| 930 ListValue null_list1; | 931 ListValue null_list1; |
| 931 ListValue null_list2; | 932 ListValue null_list2; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 999 std::unique_ptr<Value> scoped_double(new Value(3.14)); | 1000 std::unique_ptr<Value> scoped_double(new Value(3.14)); |
| 1000 Value* original_double = scoped_double.get(); | 1001 Value* original_double = scoped_double.get(); |
| 1001 original_dict.Set("double", std::move(scoped_double)); | 1002 original_dict.Set("double", std::move(scoped_double)); |
| 1002 std::unique_ptr<Value> scoped_string(new Value("hello")); | 1003 std::unique_ptr<Value> scoped_string(new Value("hello")); |
| 1003 Value* original_string = scoped_string.get(); | 1004 Value* original_string = scoped_string.get(); |
| 1004 original_dict.Set("string", std::move(scoped_string)); | 1005 original_dict.Set("string", std::move(scoped_string)); |
| 1005 std::unique_ptr<Value> scoped_string16(new Value(ASCIIToUTF16("hello16"))); | 1006 std::unique_ptr<Value> scoped_string16(new Value(ASCIIToUTF16("hello16"))); |
| 1006 Value* original_string16 = scoped_string16.get(); | 1007 Value* original_string16 = scoped_string16.get(); |
| 1007 original_dict.Set("string16", std::move(scoped_string16)); | 1008 original_dict.Set("string16", std::move(scoped_string16)); |
| 1008 | 1009 |
| 1009 std::vector<char> original_buffer(42, '!'); | 1010 Value::BlobStorage original_buffer(42, '!'); |
| 1010 std::unique_ptr<Value> scoped_binary(new Value(std::move(original_buffer))); | 1011 std::unique_ptr<Value> scoped_binary(new Value(std::move(original_buffer))); |
| 1011 Value* original_binary = scoped_binary.get(); | 1012 Value* original_binary = scoped_binary.get(); |
| 1012 original_dict.Set("binary", std::move(scoped_binary)); | 1013 original_dict.Set("binary", std::move(scoped_binary)); |
| 1013 | 1014 |
| 1014 std::unique_ptr<ListValue> scoped_list(new ListValue()); | 1015 std::unique_ptr<ListValue> scoped_list(new ListValue()); |
| 1015 Value* original_list = scoped_list.get(); | 1016 Value* original_list = scoped_list.get(); |
| 1016 std::unique_ptr<Value> scoped_list_element_0(new Value(0)); | 1017 std::unique_ptr<Value> scoped_list_element_0(new Value(0)); |
| 1017 scoped_list->Append(std::move(scoped_list_element_0)); | 1018 scoped_list->Append(std::move(scoped_list_element_0)); |
| 1018 std::unique_ptr<Value> scoped_list_element_1(new Value(1)); | 1019 std::unique_ptr<Value> scoped_list_element_1(new Value(1)); |
| 1019 scoped_list->Append(std::move(scoped_list_element_1)); | 1020 scoped_list->Append(std::move(scoped_list_element_1)); |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1517 EXPECT_FALSE(main_list.GetList(7, NULL)); | 1518 EXPECT_FALSE(main_list.GetList(7, NULL)); |
| 1518 } | 1519 } |
| 1519 | 1520 |
| 1520 TEST(ValuesTest, SelfSwap) { | 1521 TEST(ValuesTest, SelfSwap) { |
| 1521 base::Value test(1); | 1522 base::Value test(1); |
| 1522 std::swap(test, test); | 1523 std::swap(test, test); |
| 1523 EXPECT_TRUE(test.GetInt() == 1); | 1524 EXPECT_TRUE(test.GetInt() == 1); |
| 1524 } | 1525 } |
| 1525 | 1526 |
| 1526 } // namespace base | 1527 } // namespace base |
| OLD | NEW |