| 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 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 Array<int32_t> array(1); | 85 Array<int32_t> array(1); |
| 86 array[0] = kStringIntData[i].int_data; | 86 array[0] = kStringIntData[i].int_data; |
| 87 map[key] = std::move(array); | 87 map[key] = std::move(array); |
| 88 EXPECT_TRUE(map); | 88 EXPECT_TRUE(map); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // We now read back that data, to test the behavior of operator[]. | 91 // We now read back that data, to test the behavior of operator[]. |
| 92 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 92 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 93 auto it = map.find(kStringIntData[i].string_data); | 93 auto it = map.find(kStringIntData[i].string_data); |
| 94 ASSERT_TRUE(it != map.end()); | 94 ASSERT_TRUE(it != map.end()); |
| 95 ASSERT_EQ(1u, it.GetValue().size()); | 95 ASSERT_EQ(1u, it->second.size()); |
| 96 EXPECT_EQ(kStringIntData[i].int_data, it.GetValue()[0]); | 96 EXPECT_EQ(kStringIntData[i].int_data, it->second[0]); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 TEST_F(MapTest, ConstructedFromArray) { | 100 TEST_F(MapTest, ConstructedFromArray) { |
| 101 Array<String> keys(kStringIntDataSize); | 101 Array<String> keys(kStringIntDataSize); |
| 102 Array<int> values(kStringIntDataSize); | 102 Array<int> values(kStringIntDataSize); |
| 103 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 103 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 104 keys[i] = kStringIntData[i].string_data; | 104 keys[i] = kStringIntData[i].string_data; |
| 105 values[i] = kStringIntData[i].int_data; | 105 values[i] = kStringIntData[i].int_data; |
| 106 } | 106 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 Map<String, Array<String>> m; | 259 Map<String, Array<String>> m; |
| 260 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 260 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 261 Array<String> s; | 261 Array<String> s; |
| 262 s.push_back(kStringIntData[i].string_data); | 262 s.push_back(kStringIntData[i].string_data); |
| 263 m.insert(kStringIntData[i].string_data, std::move(s)); | 263 m.insert(kStringIntData[i].string_data, std::move(s)); |
| 264 } | 264 } |
| 265 | 265 |
| 266 Map<String, Array<String>> m2 = m.Clone(); | 266 Map<String, Array<String>> m2 = m.Clone(); |
| 267 | 267 |
| 268 for (auto it = m2.begin(); it != m2.end(); ++it) { | 268 for (auto it = m2.begin(); it != m2.end(); ++it) { |
| 269 ASSERT_EQ(1u, it.GetValue().size()); | 269 ASSERT_EQ(1u, it->second.size()); |
| 270 EXPECT_EQ(it.GetKey(), it.GetValue().at(0)); | 270 EXPECT_EQ(it->first, it->second.at(0)); |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 | 273 |
| 274 TEST_F(MapTest, ArrayOfMap) { | 274 TEST_F(MapTest, ArrayOfMap) { |
| 275 { | 275 { |
| 276 Array<Map<int32_t, int8_t>> array(1); | 276 Array<Map<int32_t, int8_t>> array(1); |
| 277 array[0].insert(1, 42); | 277 array[0].insert(1, 42); |
| 278 | 278 |
| 279 size_t size = GetSerializedSize_(array); | 279 size_t size = GetSerializedSize_(array); |
| 280 FixedBufferForTesting buf(size); | 280 FixedBufferForTesting buf(size); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 309 Array<Map<String, Array<bool>>> deserialized_array; | 309 Array<Map<String, Array<bool>>> deserialized_array; |
| 310 Deserialize_(data, &deserialized_array, nullptr); | 310 Deserialize_(data, &deserialized_array, nullptr); |
| 311 | 311 |
| 312 ASSERT_EQ(1u, deserialized_array.size()); | 312 ASSERT_EQ(1u, deserialized_array.size()); |
| 313 ASSERT_EQ(1u, deserialized_array[0].size()); | 313 ASSERT_EQ(1u, deserialized_array[0].size()); |
| 314 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); | 314 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); |
| 315 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); | 315 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); |
| 316 } | 316 } |
| 317 } | 317 } |
| 318 | 318 |
| 319 TEST_F(MapTest, MoveFromAndToSTLMap_Copyable) { |
| 320 std::map<int32_t, CopyableType> map1; |
| 321 map1.insert(std::make_pair(123, CopyableType())); |
| 322 map1[123].ResetCopied(); |
| 323 |
| 324 Map<int32_t, CopyableType> mojo_map(std::move(map1)); |
| 325 ASSERT_EQ(1u, mojo_map.size()); |
| 326 ASSERT_NE(mojo_map.end(), mojo_map.find(123)); |
| 327 ASSERT_FALSE(mojo_map[123].copied()); |
| 328 |
| 329 std::map<int32_t, CopyableType> map2(mojo_map.PassStorage()); |
| 330 ASSERT_EQ(1u, map2.size()); |
| 331 ASSERT_NE(map2.end(), map2.find(123)); |
| 332 ASSERT_FALSE(map2[123].copied()); |
| 333 |
| 334 ASSERT_EQ(0u, mojo_map.size()); |
| 335 ASSERT_TRUE(mojo_map.is_null()); |
| 336 } |
| 337 |
| 338 TEST_F(MapTest, MoveFromAndToSTLMap_MoveOnly) { |
| 339 std::map<int32_t, MoveOnlyType> map1; |
| 340 map1.insert(std::make_pair(123, MoveOnlyType())); |
| 341 |
| 342 Map<int32_t, MoveOnlyType> mojo_map(std::move(map1)); |
| 343 ASSERT_EQ(1u, mojo_map.size()); |
| 344 ASSERT_NE(mojo_map.end(), mojo_map.find(123)); |
| 345 |
| 346 std::map<int32_t, MoveOnlyType> map2(mojo_map.PassStorage()); |
| 347 ASSERT_EQ(1u, map2.size()); |
| 348 ASSERT_NE(map2.end(), map2.find(123)); |
| 349 |
| 350 ASSERT_EQ(0u, mojo_map.size()); |
| 351 ASSERT_TRUE(mojo_map.is_null()); |
| 352 } |
| 353 |
| 319 } // namespace | 354 } // namespace |
| 320 } // namespace test | 355 } // namespace test |
| 321 } // namespace mojo | 356 } // namespace mojo |
| OLD | NEW |