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

Side by Side Diff: base/containers/flat_map_unittest.cc

Issue 2776793002: Make flat containers stable, allow constructing from vector. (Closed)
Patch Set: Merge Created 3 years, 8 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/containers/flat_map.h" 5 #include "base/containers/flat_map.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/containers/container_test_utils.h" 10 #include "base/containers/container_test_utils.h"
(...skipping 18 matching lines...) Expand all
29 Map::const_iterator cit; 29 Map::const_iterator cit;
30 30
31 // We do not declare operator< because clang complains that it's unused. 31 // We do not declare operator< because clang complains that it's unused.
32 }; 32 };
33 33
34 A a; 34 A a;
35 } 35 }
36 36
37 TEST(FlatMap, RangeConstructor) { 37 TEST(FlatMap, RangeConstructor) {
38 flat_map<int, int>::value_type input_vals[] = { 38 flat_map<int, int>::value_type input_vals[] = {
39 {1, 1}, {1, 1}, {1, 1}, {2, 2}, {2, 2}, {2, 2}, {3, 3}, {3, 3}, {3, 3}}; 39 {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}};
40 40
41 flat_map<int, int> cont(std::begin(input_vals), std::end(input_vals)); 41 flat_map<int, int> first(std::begin(input_vals), std::end(input_vals),
42 EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2), 42 KEEP_FIRST_OF_DUPES);
43 EXPECT_THAT(first, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 1),
44 std::make_pair(3, 1)));
45
46 flat_map<int, int> last(std::begin(input_vals), std::end(input_vals),
47 KEEP_LAST_OF_DUPES);
48 EXPECT_THAT(last, ElementsAre(std::make_pair(1, 3), std::make_pair(2, 3),
43 std::make_pair(3, 3))); 49 std::make_pair(3, 3)));
44 } 50 }
45 51
46 TEST(FlatMap, MoveConstructor) { 52 TEST(FlatMap, MoveConstructor) {
47 using pair = std::pair<MoveOnlyInt, MoveOnlyInt>; 53 using pair = std::pair<MoveOnlyInt, MoveOnlyInt>;
48 54
49 flat_map<MoveOnlyInt, MoveOnlyInt> original; 55 flat_map<MoveOnlyInt, MoveOnlyInt> original;
50 original.insert(pair(MoveOnlyInt(1), MoveOnlyInt(1))); 56 original.insert(pair(MoveOnlyInt(1), MoveOnlyInt(1)));
51 original.insert(pair(MoveOnlyInt(2), MoveOnlyInt(2))); 57 original.insert(pair(MoveOnlyInt(2), MoveOnlyInt(2)));
52 original.insert(pair(MoveOnlyInt(3), MoveOnlyInt(3))); 58 original.insert(pair(MoveOnlyInt(3), MoveOnlyInt(3)));
53 original.insert(pair(MoveOnlyInt(4), MoveOnlyInt(4))); 59 original.insert(pair(MoveOnlyInt(4), MoveOnlyInt(4)));
54 60
55 flat_map<MoveOnlyInt, MoveOnlyInt> moved(std::move(original)); 61 flat_map<MoveOnlyInt, MoveOnlyInt> moved(std::move(original));
56 62
57 EXPECT_EQ(1U, moved.count(MoveOnlyInt(1))); 63 EXPECT_EQ(1U, moved.count(MoveOnlyInt(1)));
58 EXPECT_EQ(1U, moved.count(MoveOnlyInt(2))); 64 EXPECT_EQ(1U, moved.count(MoveOnlyInt(2)));
59 EXPECT_EQ(1U, moved.count(MoveOnlyInt(3))); 65 EXPECT_EQ(1U, moved.count(MoveOnlyInt(3)));
60 EXPECT_EQ(1U, moved.count(MoveOnlyInt(4))); 66 EXPECT_EQ(1U, moved.count(MoveOnlyInt(4)));
61 } 67 }
62 68
69 TEST(FlatMap, VectorConstructor) {
70 using IntPair = std::pair<int, int>;
71 using IntMap = flat_map<int, int>;
72 {
73 std::vector<IntPair> vect{{1, 1}, {1, 2}, {2, 1}};
74 IntMap map(std::move(vect), KEEP_FIRST_OF_DUPES);
75 EXPECT_THAT(map, ElementsAre(IntPair(1, 1), IntPair(2, 1)));
76 }
77 {
78 std::vector<IntPair> vect{{1, 1}, {1, 2}, {2, 1}};
79 IntMap map(std::move(vect), KEEP_LAST_OF_DUPES);
80 EXPECT_THAT(map, ElementsAre(IntPair(1, 2), IntPair(2, 1)));
81 }
82 }
83
63 TEST(FlatMap, InitializerListConstructor) { 84 TEST(FlatMap, InitializerListConstructor) {
64 flat_map<int, int> cont{{1, 1}, {2, 2}, {3, 3}, {4, 4}, 85 {
65 {5, 5}, {6, 6}, {10, 10}, {8, 8}}; 86 flat_map<int, int> cont(
66 EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2), 87 {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {1, 2}, {10, 10}, {8, 8}},
67 std::make_pair(3, 3), std::make_pair(4, 4), 88 KEEP_FIRST_OF_DUPES);
68 std::make_pair(5, 5), std::make_pair(6, 6), 89 EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2),
69 std::make_pair(8, 8), std::make_pair(10, 10))); 90 std::make_pair(3, 3), std::make_pair(4, 4),
91 std::make_pair(5, 5), std::make_pair(8, 8),
92 std::make_pair(10, 10)));
93 }
94 {
95 flat_map<int, int> cont(
96 {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {1, 2}, {10, 10}, {8, 8}},
97 KEEP_LAST_OF_DUPES);
98 EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 2), std::make_pair(2, 2),
99 std::make_pair(3, 3), std::make_pair(4, 4),
100 std::make_pair(5, 5), std::make_pair(8, 8),
101 std::make_pair(10, 10)));
102 }
70 } 103 }
71 104
72 TEST(FlatMap, InsertFindSize) { 105 TEST(FlatMap, InsertFindSize) {
73 base::flat_map<int, int> s; 106 base::flat_map<int, int> s;
74 s.insert(std::make_pair(1, 1)); 107 s.insert(std::make_pair(1, 1));
75 s.insert(std::make_pair(1, 1)); 108 s.insert(std::make_pair(1, 1));
76 s.insert(std::make_pair(2, 2)); 109 s.insert(std::make_pair(2, 2));
77 110
78 EXPECT_EQ(2u, s.size()); 111 EXPECT_EQ(2u, s.size());
79 EXPECT_EQ(std::make_pair(1, 1), *s.find(1)); 112 EXPECT_EQ(std::make_pair(1, 1), *s.find(1));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 // The returned mapped reference should refer into the map. 164 // The returned mapped reference should refer into the map.
132 s = 22; 165 s = 22;
133 EXPECT_EQ(22, m[MoveOnlyInt(1)]); 166 EXPECT_EQ(22, m[MoveOnlyInt(1)]);
134 167
135 // Overwrite existing elements. 168 // Overwrite existing elements.
136 m[MoveOnlyInt(1)] = 44; 169 m[MoveOnlyInt(1)] = 44;
137 EXPECT_EQ(44, m[MoveOnlyInt(1)]); 170 EXPECT_EQ(44, m[MoveOnlyInt(1)]);
138 } 171 }
139 172
140 } // namespace base 173 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698