| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/public/cpp/bindings/map.h" | 5 #include "mojo/public/cpp/bindings/map.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "mojo/public/cpp/bindings/array.h" | 11 #include "mojo/public/cpp/bindings/array.h" |
| 12 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/serialization.h" | |
| 15 #include "mojo/public/cpp/bindings/lib/validate_params.h" | |
| 16 #include "mojo/public/cpp/bindings/string.h" | 12 #include "mojo/public/cpp/bindings/string.h" |
| 17 #include "mojo/public/cpp/bindings/tests/container_test_util.h" | 13 #include "mojo/public/cpp/bindings/tests/container_test_util.h" |
| 14 #include "mojo/public/cpp/bindings/tests/map_common_test.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 16 |
| 20 namespace mojo { | 17 namespace mojo { |
| 21 namespace test { | 18 namespace test { |
| 22 | 19 |
| 23 namespace { | 20 namespace { |
| 24 | 21 |
| 25 using mojo::internal::Array_Data; | |
| 26 using mojo::internal::ArrayValidateParams; | |
| 27 using mojo::internal::FixedBufferForTesting; | |
| 28 using mojo::internal::Map_Data; | |
| 29 using mojo::internal::String_Data; | |
| 30 | |
| 31 struct StringIntData { | |
| 32 const char* string_data; | |
| 33 int int_data; | |
| 34 } kStringIntData[] = { | |
| 35 {"one", 1}, | |
| 36 {"two", 2}, | |
| 37 {"three", 3}, | |
| 38 {"four", 4}, | |
| 39 }; | |
| 40 | |
| 41 const size_t kStringIntDataSize = 4; | |
| 42 | |
| 43 using MapTest = testing::Test; | 22 using MapTest = testing::Test; |
| 44 | 23 |
| 45 // Tests null and empty maps. | 24 MAP_COMMON_TEST(Map, NullAndEmpty) |
| 46 TEST_F(MapTest, NullAndEmpty) { | 25 MAP_COMMON_TEST(Map, InsertWorks) |
| 47 Map<char, char> map0; | 26 MAP_COMMON_TEST(Map, TestIndexOperator) |
| 48 EXPECT_TRUE(map0.empty()); | 27 MAP_COMMON_TEST(Map, TestIndexOperatorAsRValue) |
| 49 EXPECT_FALSE(map0.is_null()); | 28 MAP_COMMON_TEST(Map, TestIndexOperatorMoveOnly) |
| 50 map0 = nullptr; | 29 MAP_COMMON_TEST(Map, MapArrayClone) |
| 51 EXPECT_TRUE(map0.is_null()); | 30 MAP_COMMON_TEST(Map, ArrayOfMap) |
| 52 EXPECT_FALSE(map0.empty()); | |
| 53 | |
| 54 Map<char, char> map1(nullptr); | |
| 55 EXPECT_TRUE(map1.is_null()); | |
| 56 EXPECT_FALSE(map1.empty()); | |
| 57 map1.SetToEmpty(); | |
| 58 EXPECT_TRUE(map1.empty()); | |
| 59 EXPECT_FALSE(map1.is_null()); | |
| 60 } | |
| 61 | |
| 62 // Tests that basic Map operations work. | |
| 63 TEST_F(MapTest, InsertWorks) { | |
| 64 Map<String, int> map; | |
| 65 for (size_t i = 0; i < kStringIntDataSize; ++i) | |
| 66 map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data); | |
| 67 | |
| 68 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 69 EXPECT_EQ(kStringIntData[i].int_data, | |
| 70 map.at(kStringIntData[i].string_data)); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 TEST_F(MapTest, TestIndexOperator) { | |
| 75 Map<String, int> map; | |
| 76 for (size_t i = 0; i < kStringIntDataSize; ++i) | |
| 77 map[kStringIntData[i].string_data] = kStringIntData[i].int_data; | |
| 78 | |
| 79 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 80 EXPECT_EQ(kStringIntData[i].int_data, | |
| 81 map.at(kStringIntData[i].string_data)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 TEST_F(MapTest, TestIndexOperatorAsRValue) { | |
| 86 Map<String, int> map; | |
| 87 for (size_t i = 0; i < kStringIntDataSize; ++i) | |
| 88 map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data); | |
| 89 | |
| 90 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 91 EXPECT_EQ(kStringIntData[i].int_data, map[kStringIntData[i].string_data]); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 TEST_F(MapTest, TestIndexOperatorMoveOnly) { | |
| 96 ASSERT_EQ(0u, MoveOnlyType::num_instances()); | |
| 97 mojo::Map<mojo::String, mojo::Array<int32_t>> map; | |
| 98 std::vector<MoveOnlyType*> value_ptrs; | |
| 99 | |
| 100 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 101 const char* key = kStringIntData[i].string_data; | |
| 102 Array<int32_t> array(1); | |
| 103 array[0] = kStringIntData[i].int_data; | |
| 104 map[key] = std::move(array); | |
| 105 EXPECT_TRUE(map); | |
| 106 } | |
| 107 | |
| 108 // We now read back that data, to test the behavior of operator[]. | |
| 109 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 110 auto it = map.find(kStringIntData[i].string_data); | |
| 111 ASSERT_TRUE(it != map.end()); | |
| 112 ASSERT_EQ(1u, it->second.size()); | |
| 113 EXPECT_EQ(kStringIntData[i].int_data, it->second[0]); | |
| 114 } | |
| 115 } | |
| 116 | 31 |
| 117 TEST_F(MapTest, ConstructedFromArray) { | 32 TEST_F(MapTest, ConstructedFromArray) { |
| 118 Array<String> keys(kStringIntDataSize); | 33 Array<String> keys(kStringIntDataSize); |
| 119 Array<int> values(kStringIntDataSize); | 34 Array<int> values(kStringIntDataSize); |
| 120 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 35 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 121 keys[i] = kStringIntData[i].string_data; | 36 keys[i] = kStringIntData[i].string_data; |
| 122 values[i] = kStringIntData[i].int_data; | 37 values[i] = kStringIntData[i].int_data; |
| 123 } | 38 } |
| 124 | 39 |
| 125 Map<String, int> map(std::move(keys), std::move(values)); | 40 Map<String, int> map(std::move(keys), std::move(values)); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 | 180 |
| 266 std::map<std::string, int> stl_map = | 181 std::map<std::string, int> stl_map = |
| 267 mojo_map.To<std::map<std::string, int>>(); | 182 mojo_map.To<std::map<std::string, int>>(); |
| 268 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 183 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 269 auto it = stl_map.find(kStringIntData[i].string_data); | 184 auto it = stl_map.find(kStringIntData[i].string_data); |
| 270 ASSERT_TRUE(it != stl_map.end()); | 185 ASSERT_TRUE(it != stl_map.end()); |
| 271 EXPECT_EQ(kStringIntData[i].int_data, it->second); | 186 EXPECT_EQ(kStringIntData[i].int_data, it->second); |
| 272 } | 187 } |
| 273 } | 188 } |
| 274 | 189 |
| 275 TEST_F(MapTest, MapArrayClone) { | |
| 276 Map<String, Array<String>> m; | |
| 277 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 278 Array<String> s; | |
| 279 s.push_back(kStringIntData[i].string_data); | |
| 280 m.insert(kStringIntData[i].string_data, std::move(s)); | |
| 281 } | |
| 282 | |
| 283 Map<String, Array<String>> m2 = m.Clone(); | |
| 284 | |
| 285 for (auto it = m2.begin(); it != m2.end(); ++it) { | |
| 286 ASSERT_EQ(1u, it->second.size()); | |
| 287 EXPECT_EQ(it->first, it->second.at(0)); | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 TEST_F(MapTest, ArrayOfMap) { | |
| 292 { | |
| 293 Array<Map<int32_t, int8_t>> array(1); | |
| 294 array[0].insert(1, 42); | |
| 295 | |
| 296 mojo::internal::SerializationContext context; | |
| 297 size_t size = | |
| 298 mojo::internal::PrepareToSerialize<Array<Map<int32_t, int8_t>>>( | |
| 299 array, &context); | |
| 300 FixedBufferForTesting buf(size); | |
| 301 Array_Data<Map_Data<int32_t, int8_t>*>* data; | |
| 302 ArrayValidateParams validate_params( | |
| 303 0, false, new ArrayValidateParams(0, false, nullptr)); | |
| 304 mojo::internal::Serialize<Array<Map<int32_t, int8_t>>>( | |
| 305 array, &buf, &data, &validate_params, &context); | |
| 306 | |
| 307 Array<Map<int32_t, int8_t>> deserialized_array; | |
| 308 mojo::internal::Deserialize<Array<Map<int32_t, int8_t>>>( | |
| 309 data, &deserialized_array, &context); | |
| 310 | |
| 311 ASSERT_EQ(1u, deserialized_array.size()); | |
| 312 ASSERT_EQ(1u, deserialized_array[0].size()); | |
| 313 ASSERT_EQ(42, deserialized_array[0].at(1)); | |
| 314 } | |
| 315 | |
| 316 { | |
| 317 Array<Map<String, Array<bool>>> array(1); | |
| 318 Array<bool> map_value(2); | |
| 319 map_value[0] = false; | |
| 320 map_value[1] = true; | |
| 321 array[0].insert("hello world", std::move(map_value)); | |
| 322 | |
| 323 mojo::internal::SerializationContext context; | |
| 324 size_t size = | |
| 325 mojo::internal::PrepareToSerialize<Array<Map<String, Array<bool>>>>( | |
| 326 array, &context); | |
| 327 FixedBufferForTesting buf(size); | |
| 328 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data; | |
| 329 ArrayValidateParams validate_params( | |
| 330 0, false, new ArrayValidateParams( | |
| 331 0, false, new ArrayValidateParams(0, false, nullptr))); | |
| 332 mojo::internal::Serialize<Array<Map<String, Array<bool>>>>( | |
| 333 array, &buf, &data, &validate_params, &context); | |
| 334 | |
| 335 Array<Map<String, Array<bool>>> deserialized_array; | |
| 336 mojo::internal::Deserialize<Array<Map<String, Array<bool>>>>( | |
| 337 data, &deserialized_array, &context); | |
| 338 | |
| 339 ASSERT_EQ(1u, deserialized_array.size()); | |
| 340 ASSERT_EQ(1u, deserialized_array[0].size()); | |
| 341 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); | |
| 342 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 TEST_F(MapTest, MoveFromAndToSTLMap_Copyable) { | 190 TEST_F(MapTest, MoveFromAndToSTLMap_Copyable) { |
| 347 std::map<int32_t, CopyableType> map1; | 191 std::map<int32_t, CopyableType> map1; |
| 348 map1.insert(std::make_pair(123, CopyableType())); | 192 map1.insert(std::make_pair(123, CopyableType())); |
| 349 map1[123].ResetCopied(); | 193 map1[123].ResetCopied(); |
| 350 | 194 |
| 351 Map<int32_t, CopyableType> mojo_map(std::move(map1)); | 195 Map<int32_t, CopyableType> mojo_map(std::move(map1)); |
| 352 ASSERT_EQ(1u, mojo_map.size()); | 196 ASSERT_EQ(1u, mojo_map.size()); |
| 353 ASSERT_NE(mojo_map.end(), mojo_map.find(123)); | 197 ASSERT_NE(mojo_map.end(), mojo_map.find(123)); |
| 354 ASSERT_FALSE(mojo_map[123].copied()); | 198 ASSERT_FALSE(mojo_map[123].copied()); |
| 355 | 199 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 374 ASSERT_EQ(1u, map2.size()); | 218 ASSERT_EQ(1u, map2.size()); |
| 375 ASSERT_NE(map2.end(), map2.find(123)); | 219 ASSERT_NE(map2.end(), map2.find(123)); |
| 376 | 220 |
| 377 ASSERT_EQ(0u, mojo_map.size()); | 221 ASSERT_EQ(0u, mojo_map.size()); |
| 378 ASSERT_TRUE(mojo_map.is_null()); | 222 ASSERT_TRUE(mojo_map.is_null()); |
| 379 } | 223 } |
| 380 | 224 |
| 381 } // namespace | 225 } // namespace |
| 382 } // namespace test | 226 } // namespace test |
| 383 } // namespace mojo | 227 } // namespace mojo |
| OLD | NEW |