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 <unordered_map> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "mojo/public/cpp/bindings/array.h" | 12 #include "mojo/public/cpp/bindings/array.h" |
12 #include "mojo/public/cpp/bindings/string.h" | 13 #include "mojo/public/cpp/bindings/string.h" |
13 #include "mojo/public/cpp/bindings/tests/container_test_util.h" | 14 #include "mojo/public/cpp/bindings/tests/container_test_util.h" |
14 #include "mojo/public/cpp/bindings/tests/map_common_test.h" | 15 #include "mojo/public/cpp/bindings/tests/map_common_test.h" |
| 16 #include "mojo/public/interfaces/bindings/tests/rect.mojom.h" |
| 17 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace mojo { | 20 namespace mojo { |
18 namespace test { | 21 namespace test { |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 using MapTest = testing::Test; | 25 using MapTest = testing::Test; |
23 | 26 |
24 MAP_COMMON_TEST(Map, NullAndEmpty) | 27 MAP_COMMON_TEST(Map, NullAndEmpty) |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 ASSERT_NE(mojo_map.end(), mojo_map.find(123)); | 218 ASSERT_NE(mojo_map.end(), mojo_map.find(123)); |
216 | 219 |
217 std::map<int32_t, MoveOnlyType> map2(mojo_map.PassStorage()); | 220 std::map<int32_t, MoveOnlyType> map2(mojo_map.PassStorage()); |
218 ASSERT_EQ(1u, map2.size()); | 221 ASSERT_EQ(1u, map2.size()); |
219 ASSERT_NE(map2.end(), map2.find(123)); | 222 ASSERT_NE(map2.end(), map2.find(123)); |
220 | 223 |
221 ASSERT_EQ(0u, mojo_map.size()); | 224 ASSERT_EQ(0u, mojo_map.size()); |
222 ASSERT_TRUE(mojo_map.is_null()); | 225 ASSERT_TRUE(mojo_map.is_null()); |
223 } | 226 } |
224 | 227 |
| 228 static RectPtr MakeRect(int32_t x, int32_t y, int32_t width, int32_t height) { |
| 229 RectPtr rect_ptr = Rect::New(); |
| 230 rect_ptr->x = x; |
| 231 rect_ptr->y = y; |
| 232 rect_ptr->width = width; |
| 233 rect_ptr->height = height; |
| 234 return rect_ptr; |
| 235 } |
| 236 |
| 237 TEST_F(MapTest, StructKey) { |
| 238 std::unordered_map<RectPtr, int32_t> map; |
| 239 map.insert(std::make_pair(MakeRect(1, 2, 3, 4), 123)); |
| 240 |
| 241 RectPtr key = MakeRect(1, 2, 3, 4); |
| 242 ASSERT_NE(map.end(), map.find(key)); |
| 243 ASSERT_EQ(123, map.find(key)->second); |
| 244 |
| 245 map.erase(key); |
| 246 ASSERT_EQ(0u, map.size()); |
| 247 } |
| 248 |
| 249 static ContainsHashablePtr MakeContainsHashablePtr(RectPtr rect_ptr) { |
| 250 ContainsHashablePtr ptr = ContainsHashable::New(); |
| 251 // TODO(tibell): Move rect_ptr |
| 252 return ptr; |
| 253 } |
| 254 |
| 255 // TODO(tibell): Check why typemap doesn't fire. |
| 256 TEST_F(MapTest, TypemappedStructKey) { |
| 257 std::unordered_map<ContainsHashablePtr, int32_t> map; |
| 258 map.insert( |
| 259 std::make_pair(MakeContainsHashablePtr(MakeRect(1, 2, 3, 4)), 123)); |
| 260 |
| 261 ContainsHashablePtr key = MakeContainsHashablePtr(MakeRect(1, 2, 3, 4)); |
| 262 ASSERT_NE(map.end(), map.find(key)); |
| 263 ASSERT_EQ(123, map.find(key)->second); |
| 264 |
| 265 map.erase(key); |
| 266 ASSERT_EQ(0u, map.size()); |
| 267 } |
| 268 |
225 } // namespace | 269 } // namespace |
226 } // namespace test | 270 } // namespace test |
227 } // namespace mojo | 271 } // namespace mojo |
OLD | NEW |