Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: mojo/public/cpp/bindings/tests/map_unittest.cc

Issue 2339413004: Allow Mojo structs as map keys (Closed)
Patch Set: Fix merge problem in BUILD.gn file Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/cpp/bindings/tests/rect_chromium.h"
17 #include "mojo/public/interfaces/bindings/tests/rect.mojom.h"
18 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
15 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
16 20
17 namespace mojo { 21 namespace mojo {
18 namespace test { 22 namespace test {
19 23
20 namespace { 24 namespace {
21 25
22 using MapTest = testing::Test; 26 using MapTest = testing::Test;
23 27
24 MAP_COMMON_TEST(Map, NullAndEmpty) 28 MAP_COMMON_TEST(Map, NullAndEmpty)
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 ASSERT_NE(mojo_map.end(), mojo_map.find(123)); 219 ASSERT_NE(mojo_map.end(), mojo_map.find(123));
216 220
217 std::map<int32_t, MoveOnlyType> map2(mojo_map.PassStorage()); 221 std::map<int32_t, MoveOnlyType> map2(mojo_map.PassStorage());
218 ASSERT_EQ(1u, map2.size()); 222 ASSERT_EQ(1u, map2.size());
219 ASSERT_NE(map2.end(), map2.find(123)); 223 ASSERT_NE(map2.end(), map2.find(123));
220 224
221 ASSERT_EQ(0u, mojo_map.size()); 225 ASSERT_EQ(0u, mojo_map.size());
222 ASSERT_TRUE(mojo_map.is_null()); 226 ASSERT_TRUE(mojo_map.is_null());
223 } 227 }
224 228
229 static RectPtr MakeRect(int32_t x, int32_t y, int32_t width, int32_t height) {
230 RectPtr rect_ptr = Rect::New();
231 rect_ptr->x = x;
232 rect_ptr->y = y;
233 rect_ptr->width = width;
234 rect_ptr->height = height;
235 return rect_ptr;
236 }
237
238 TEST_F(MapTest, StructKey) {
239 std::unordered_map<RectPtr, int32_t> map;
240 map.insert(std::make_pair(MakeRect(1, 2, 3, 4), 123));
241
242 RectPtr key = MakeRect(1, 2, 3, 4);
243 ASSERT_NE(map.end(), map.find(key));
244 ASSERT_EQ(123, map.find(key)->second);
245
246 map.erase(key);
247 ASSERT_EQ(0u, map.size());
248 }
249
250 static ContainsHashablePtr MakeContainsHashablePtr(RectChromium rect) {
251 ContainsHashablePtr ptr = ContainsHashable::New();
252 ptr->rect = rect;
253 return ptr;
254 }
255
256 TEST_F(MapTest, TypemappedStructKey) {
257 std::unordered_map<ContainsHashablePtr, int32_t> map;
258 map.insert(
259 std::make_pair(MakeContainsHashablePtr(RectChromium(1, 2, 3, 4)), 123));
260
261 ContainsHashablePtr key = MakeContainsHashablePtr(RectChromium(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698