OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/small_map.h" | 5 #include "base/containers/small_map.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <functional> | 10 #include <functional> |
11 #include <map> | 11 #include <map> |
| 12 #include <unordered_map> |
12 | 13 |
13 #include "base/containers/hash_tables.h" | |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 TEST(SmallMap, General) { | 19 TEST(SmallMap, General) { |
20 small_map<hash_map<int, int>> m; | 20 small_map<std::unordered_map<int, int>> m; |
21 | 21 |
22 EXPECT_TRUE(m.empty()); | 22 EXPECT_TRUE(m.empty()); |
23 | 23 |
24 m[0] = 5; | 24 m[0] = 5; |
25 | 25 |
26 EXPECT_FALSE(m.empty()); | 26 EXPECT_FALSE(m.empty()); |
27 EXPECT_EQ(m.size(), 1u); | 27 EXPECT_EQ(m.size(), 1u); |
28 | 28 |
29 m[9] = 2; | 29 m[9] = 2; |
30 | 30 |
31 EXPECT_FALSE(m.empty()); | 31 EXPECT_FALSE(m.empty()); |
32 EXPECT_EQ(m.size(), 2u); | 32 EXPECT_EQ(m.size(), 2u); |
33 | 33 |
34 EXPECT_EQ(m[9], 2); | 34 EXPECT_EQ(m[9], 2); |
35 EXPECT_EQ(m[0], 5); | 35 EXPECT_EQ(m[0], 5); |
36 EXPECT_FALSE(m.UsingFullMap()); | 36 EXPECT_FALSE(m.UsingFullMap()); |
37 | 37 |
38 small_map<hash_map<int, int>>::iterator iter(m.begin()); | 38 small_map<std::unordered_map<int, int>>::iterator iter(m.begin()); |
39 ASSERT_TRUE(iter != m.end()); | 39 ASSERT_TRUE(iter != m.end()); |
40 EXPECT_EQ(iter->first, 0); | 40 EXPECT_EQ(iter->first, 0); |
41 EXPECT_EQ(iter->second, 5); | 41 EXPECT_EQ(iter->second, 5); |
42 ++iter; | 42 ++iter; |
43 ASSERT_TRUE(iter != m.end()); | 43 ASSERT_TRUE(iter != m.end()); |
44 EXPECT_EQ((*iter).first, 9); | 44 EXPECT_EQ((*iter).first, 9); |
45 EXPECT_EQ((*iter).second, 2); | 45 EXPECT_EQ((*iter).second, 2); |
46 ++iter; | 46 ++iter; |
47 EXPECT_TRUE(iter == m.end()); | 47 EXPECT_TRUE(iter == m.end()); |
48 | 48 |
(...skipping 10 matching lines...) Expand all Loading... |
59 EXPECT_FALSE(m.empty()); | 59 EXPECT_FALSE(m.empty()); |
60 EXPECT_TRUE(m.UsingFullMap()); | 60 EXPECT_TRUE(m.UsingFullMap()); |
61 | 61 |
62 iter = m.begin(); | 62 iter = m.begin(); |
63 for (int i = 0; i < 5; i++) { | 63 for (int i = 0; i < 5; i++) { |
64 EXPECT_TRUE(iter != m.end()); | 64 EXPECT_TRUE(iter != m.end()); |
65 ++iter; | 65 ++iter; |
66 } | 66 } |
67 EXPECT_TRUE(iter == m.end()); | 67 EXPECT_TRUE(iter == m.end()); |
68 | 68 |
69 const small_map<hash_map<int, int>>& ref = m; | 69 const small_map<std::unordered_map<int, int>>& ref = m; |
70 EXPECT_TRUE(ref.find(1234) != m.end()); | 70 EXPECT_TRUE(ref.find(1234) != m.end()); |
71 EXPECT_TRUE(ref.find(5678) == m.end()); | 71 EXPECT_TRUE(ref.find(5678) == m.end()); |
72 } | 72 } |
73 | 73 |
74 TEST(SmallMap, PostFixIteratorIncrement) { | 74 TEST(SmallMap, PostFixIteratorIncrement) { |
75 small_map<hash_map<int, int>> m; | 75 small_map<std::unordered_map<int, int>> m; |
76 m[0] = 5; | 76 m[0] = 5; |
77 m[2] = 3; | 77 m[2] = 3; |
78 | 78 |
79 { | 79 { |
80 small_map<hash_map<int, int>>::iterator iter(m.begin()); | 80 small_map<std::unordered_map<int, int>>::iterator iter(m.begin()); |
81 small_map<hash_map<int, int>>::iterator last(iter++); | 81 small_map<std::unordered_map<int, int>>::iterator last(iter++); |
82 ++last; | 82 ++last; |
83 EXPECT_TRUE(last == iter); | 83 EXPECT_TRUE(last == iter); |
84 } | 84 } |
85 | 85 |
86 { | 86 { |
87 small_map<hash_map<int, int>>::const_iterator iter(m.begin()); | 87 small_map<std::unordered_map<int, int>>::const_iterator iter(m.begin()); |
88 small_map<hash_map<int, int>>::const_iterator last(iter++); | 88 small_map<std::unordered_map<int, int>>::const_iterator last(iter++); |
89 ++last; | 89 ++last; |
90 EXPECT_TRUE(last == iter); | 90 EXPECT_TRUE(last == iter); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 // Based on the General testcase. | 94 // Based on the General testcase. |
95 TEST(SmallMap, CopyConstructor) { | 95 TEST(SmallMap, CopyConstructor) { |
96 small_map<hash_map<int, int>> src; | 96 small_map<std::unordered_map<int, int>> src; |
97 | 97 |
98 { | 98 { |
99 small_map<hash_map<int, int>> m(src); | 99 small_map<std::unordered_map<int, int>> m(src); |
100 EXPECT_TRUE(m.empty()); | 100 EXPECT_TRUE(m.empty()); |
101 } | 101 } |
102 | 102 |
103 src[0] = 5; | 103 src[0] = 5; |
104 | 104 |
105 { | 105 { |
106 small_map<hash_map<int, int>> m(src); | 106 small_map<std::unordered_map<int, int>> m(src); |
107 EXPECT_FALSE(m.empty()); | 107 EXPECT_FALSE(m.empty()); |
108 EXPECT_EQ(m.size(), 1u); | 108 EXPECT_EQ(m.size(), 1u); |
109 } | 109 } |
110 | 110 |
111 src[9] = 2; | 111 src[9] = 2; |
112 | 112 |
113 { | 113 { |
114 small_map<hash_map<int, int>> m(src); | 114 small_map<std::unordered_map<int, int>> m(src); |
115 EXPECT_FALSE(m.empty()); | 115 EXPECT_FALSE(m.empty()); |
116 EXPECT_EQ(m.size(), 2u); | 116 EXPECT_EQ(m.size(), 2u); |
117 | 117 |
118 EXPECT_EQ(m[9], 2); | 118 EXPECT_EQ(m[9], 2); |
119 EXPECT_EQ(m[0], 5); | 119 EXPECT_EQ(m[0], 5); |
120 EXPECT_FALSE(m.UsingFullMap()); | 120 EXPECT_FALSE(m.UsingFullMap()); |
121 } | 121 } |
122 | 122 |
123 src[8] = 23; | 123 src[8] = 23; |
124 src[1234] = 90; | 124 src[1234] = 90; |
125 src[-5] = 6; | 125 src[-5] = 6; |
126 | 126 |
127 { | 127 { |
128 small_map<hash_map<int, int>> m(src); | 128 small_map<std::unordered_map<int, int>> m(src); |
129 EXPECT_EQ(m[ 9], 2); | 129 EXPECT_EQ(m[ 9], 2); |
130 EXPECT_EQ(m[ 0], 5); | 130 EXPECT_EQ(m[ 0], 5); |
131 EXPECT_EQ(m[1234], 90); | 131 EXPECT_EQ(m[1234], 90); |
132 EXPECT_EQ(m[ 8], 23); | 132 EXPECT_EQ(m[ 8], 23); |
133 EXPECT_EQ(m[ -5], 6); | 133 EXPECT_EQ(m[ -5], 6); |
134 EXPECT_EQ(m.size(), 5u); | 134 EXPECT_EQ(m.size(), 5u); |
135 EXPECT_FALSE(m.empty()); | 135 EXPECT_FALSE(m.empty()); |
136 EXPECT_TRUE(m.UsingFullMap()); | 136 EXPECT_TRUE(m.UsingFullMap()); |
137 } | 137 } |
138 } | 138 } |
(...skipping 10 matching lines...) Expand all Loading... |
149 return true; | 149 return true; |
150 } | 150 } |
151 | 151 |
152 template <class inner> | 152 template <class inner> |
153 static bool SmallMapEqual(small_map<inner> const& a, | 153 static bool SmallMapEqual(small_map<inner> const& a, |
154 small_map<inner> const& b) { | 154 small_map<inner> const& b) { |
155 return SmallMapIsSubset(a, b) && SmallMapIsSubset(b, a); | 155 return SmallMapIsSubset(a, b) && SmallMapIsSubset(b, a); |
156 } | 156 } |
157 | 157 |
158 TEST(SmallMap, AssignmentOperator) { | 158 TEST(SmallMap, AssignmentOperator) { |
159 small_map<hash_map<int, int>> src_small; | 159 small_map<std::unordered_map<int, int>> src_small; |
160 small_map<hash_map<int, int>> src_large; | 160 small_map<std::unordered_map<int, int>> src_large; |
161 | 161 |
162 src_small[1] = 20; | 162 src_small[1] = 20; |
163 src_small[2] = 21; | 163 src_small[2] = 21; |
164 src_small[3] = 22; | 164 src_small[3] = 22; |
165 EXPECT_FALSE(src_small.UsingFullMap()); | 165 EXPECT_FALSE(src_small.UsingFullMap()); |
166 | 166 |
167 src_large[1] = 20; | 167 src_large[1] = 20; |
168 src_large[2] = 21; | 168 src_large[2] = 21; |
169 src_large[3] = 22; | 169 src_large[3] = 22; |
170 src_large[5] = 23; | 170 src_large[5] = 23; |
171 src_large[6] = 24; | 171 src_large[6] = 24; |
172 src_large[7] = 25; | 172 src_large[7] = 25; |
173 EXPECT_TRUE(src_large.UsingFullMap()); | 173 EXPECT_TRUE(src_large.UsingFullMap()); |
174 | 174 |
175 // Assignments to empty. | 175 // Assignments to empty. |
176 small_map<hash_map<int, int>> dest_small; | 176 small_map<std::unordered_map<int, int>> dest_small; |
177 dest_small = src_small; | 177 dest_small = src_small; |
178 EXPECT_TRUE(SmallMapEqual(dest_small, src_small)); | 178 EXPECT_TRUE(SmallMapEqual(dest_small, src_small)); |
179 EXPECT_EQ(dest_small.UsingFullMap(), | 179 EXPECT_EQ(dest_small.UsingFullMap(), |
180 src_small.UsingFullMap()); | 180 src_small.UsingFullMap()); |
181 | 181 |
182 small_map<hash_map<int, int>> dest_large; | 182 small_map<std::unordered_map<int, int>> dest_large; |
183 dest_large = src_large; | 183 dest_large = src_large; |
184 EXPECT_TRUE(SmallMapEqual(dest_large, src_large)); | 184 EXPECT_TRUE(SmallMapEqual(dest_large, src_large)); |
185 EXPECT_EQ(dest_large.UsingFullMap(), | 185 EXPECT_EQ(dest_large.UsingFullMap(), |
186 src_large.UsingFullMap()); | 186 src_large.UsingFullMap()); |
187 | 187 |
188 // Assignments which assign from full to small, and vice versa. | 188 // Assignments which assign from full to small, and vice versa. |
189 dest_small = src_large; | 189 dest_small = src_large; |
190 EXPECT_TRUE(SmallMapEqual(dest_small, src_large)); | 190 EXPECT_TRUE(SmallMapEqual(dest_small, src_large)); |
191 EXPECT_EQ(dest_small.UsingFullMap(), | 191 EXPECT_EQ(dest_small.UsingFullMap(), |
192 src_large.UsingFullMap()); | 192 src_large.UsingFullMap()); |
193 | 193 |
194 dest_large = src_small; | 194 dest_large = src_small; |
195 EXPECT_TRUE(SmallMapEqual(dest_large, src_small)); | 195 EXPECT_TRUE(SmallMapEqual(dest_large, src_small)); |
196 EXPECT_EQ(dest_large.UsingFullMap(), | 196 EXPECT_EQ(dest_large.UsingFullMap(), |
197 src_small.UsingFullMap()); | 197 src_small.UsingFullMap()); |
198 | 198 |
199 // Double check that SmallMapEqual works: | 199 // Double check that SmallMapEqual works: |
200 dest_large[42] = 666; | 200 dest_large[42] = 666; |
201 EXPECT_FALSE(SmallMapEqual(dest_large, src_small)); | 201 EXPECT_FALSE(SmallMapEqual(dest_large, src_small)); |
202 } | 202 } |
203 | 203 |
204 TEST(SmallMap, Insert) { | 204 TEST(SmallMap, Insert) { |
205 small_map<hash_map<int, int>> sm; | 205 small_map<std::unordered_map<int, int>> sm; |
206 | 206 |
207 // loop through the transition from small map to map. | 207 // loop through the transition from small map to map. |
208 for (int i = 1; i <= 10; ++i) { | 208 for (int i = 1; i <= 10; ++i) { |
209 VLOG(1) << "Iteration " << i; | 209 VLOG(1) << "Iteration " << i; |
210 // insert an element | 210 // insert an element |
211 std::pair<small_map<hash_map<int, int>>::iterator, bool> ret; | 211 std::pair<small_map<std::unordered_map<int, int>>::iterator, bool> ret; |
212 ret = sm.insert(std::make_pair(i, 100*i)); | 212 ret = sm.insert(std::make_pair(i, 100*i)); |
213 EXPECT_TRUE(ret.second); | 213 EXPECT_TRUE(ret.second); |
214 EXPECT_TRUE(ret.first == sm.find(i)); | 214 EXPECT_TRUE(ret.first == sm.find(i)); |
215 EXPECT_EQ(ret.first->first, i); | 215 EXPECT_EQ(ret.first->first, i); |
216 EXPECT_EQ(ret.first->second, 100*i); | 216 EXPECT_EQ(ret.first->second, 100*i); |
217 | 217 |
218 // try to insert it again with different value, fails, but we still get an | 218 // try to insert it again with different value, fails, but we still get an |
219 // iterator back with the original value. | 219 // iterator back with the original value. |
220 ret = sm.insert(std::make_pair(i, -i)); | 220 ret = sm.insert(std::make_pair(i, -i)); |
221 EXPECT_FALSE(ret.second); | 221 EXPECT_FALSE(ret.second); |
222 EXPECT_TRUE(ret.first == sm.find(i)); | 222 EXPECT_TRUE(ret.first == sm.find(i)); |
223 EXPECT_EQ(ret.first->first, i); | 223 EXPECT_EQ(ret.first->first, i); |
224 EXPECT_EQ(ret.first->second, 100*i); | 224 EXPECT_EQ(ret.first->second, 100*i); |
225 | 225 |
226 // check the state of the map. | 226 // check the state of the map. |
227 for (int j = 1; j <= i; ++j) { | 227 for (int j = 1; j <= i; ++j) { |
228 small_map<hash_map<int, int>>::iterator it = sm.find(j); | 228 small_map<std::unordered_map<int, int>>::iterator it = sm.find(j); |
229 EXPECT_TRUE(it != sm.end()); | 229 EXPECT_TRUE(it != sm.end()); |
230 EXPECT_EQ(it->first, j); | 230 EXPECT_EQ(it->first, j); |
231 EXPECT_EQ(it->second, j * 100); | 231 EXPECT_EQ(it->second, j * 100); |
232 } | 232 } |
233 EXPECT_EQ(sm.size(), static_cast<size_t>(i)); | 233 EXPECT_EQ(sm.size(), static_cast<size_t>(i)); |
234 EXPECT_FALSE(sm.empty()); | 234 EXPECT_FALSE(sm.empty()); |
235 } | 235 } |
236 } | 236 } |
237 | 237 |
238 TEST(SmallMap, InsertRange) { | 238 TEST(SmallMap, InsertRange) { |
239 // loop through the transition from small map to map. | 239 // loop through the transition from small map to map. |
240 for (int elements = 0; elements <= 10; ++elements) { | 240 for (int elements = 0; elements <= 10; ++elements) { |
241 VLOG(1) << "Elements " << elements; | 241 VLOG(1) << "Elements " << elements; |
242 hash_map<int, int> normal_map; | 242 std::unordered_map<int, int> normal_map; |
243 for (int i = 1; i <= elements; ++i) { | 243 for (int i = 1; i <= elements; ++i) { |
244 normal_map.insert(std::make_pair(i, 100*i)); | 244 normal_map.insert(std::make_pair(i, 100*i)); |
245 } | 245 } |
246 | 246 |
247 small_map<hash_map<int, int>> sm; | 247 small_map<std::unordered_map<int, int>> sm; |
248 sm.insert(normal_map.begin(), normal_map.end()); | 248 sm.insert(normal_map.begin(), normal_map.end()); |
249 EXPECT_EQ(normal_map.size(), sm.size()); | 249 EXPECT_EQ(normal_map.size(), sm.size()); |
250 for (int i = 1; i <= elements; ++i) { | 250 for (int i = 1; i <= elements; ++i) { |
251 VLOG(1) << "Iteration " << i; | 251 VLOG(1) << "Iteration " << i; |
252 EXPECT_TRUE(sm.find(i) != sm.end()); | 252 EXPECT_TRUE(sm.find(i) != sm.end()); |
253 EXPECT_EQ(sm.find(i)->first, i); | 253 EXPECT_EQ(sm.find(i)->first, i); |
254 EXPECT_EQ(sm.find(i)->second, 100*i); | 254 EXPECT_EQ(sm.find(i)->second, 100*i); |
255 } | 255 } |
256 } | 256 } |
257 } | 257 } |
258 | 258 |
259 TEST(SmallMap, Erase) { | 259 TEST(SmallMap, Erase) { |
260 small_map<hash_map<std::string, int>> m; | 260 small_map<std::unordered_map<std::string, int>> m; |
261 small_map<hash_map<std::string, int>>::iterator iter; | 261 small_map<std::unordered_map<std::string, int>>::iterator iter; |
262 | 262 |
263 m["monday"] = 1; | 263 m["monday"] = 1; |
264 m["tuesday"] = 2; | 264 m["tuesday"] = 2; |
265 m["wednesday"] = 3; | 265 m["wednesday"] = 3; |
266 | 266 |
267 EXPECT_EQ(m["monday" ], 1); | 267 EXPECT_EQ(m["monday" ], 1); |
268 EXPECT_EQ(m["tuesday" ], 2); | 268 EXPECT_EQ(m["tuesday" ], 2); |
269 EXPECT_EQ(m["wednesday"], 3); | 269 EXPECT_EQ(m["wednesday"], 3); |
270 EXPECT_EQ(m.count("tuesday"), 1u); | 270 EXPECT_EQ(m.count("tuesday"), 1u); |
271 EXPECT_FALSE(m.UsingFullMap()); | 271 EXPECT_FALSE(m.UsingFullMap()); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 EXPECT_EQ(m.size(), 3u); | 324 EXPECT_EQ(m.size(), 3u); |
325 EXPECT_FALSE(m.empty()); | 325 EXPECT_FALSE(m.empty()); |
326 | 326 |
327 m.clear(); | 327 m.clear(); |
328 EXPECT_FALSE(m.UsingFullMap()); | 328 EXPECT_FALSE(m.UsingFullMap()); |
329 EXPECT_EQ(m.size(), 0u); | 329 EXPECT_EQ(m.size(), 0u); |
330 EXPECT_TRUE(m.empty()); | 330 EXPECT_TRUE(m.empty()); |
331 } | 331 } |
332 | 332 |
333 TEST(SmallMap, EraseReturnsIteratorFollowingRemovedElement) { | 333 TEST(SmallMap, EraseReturnsIteratorFollowingRemovedElement) { |
334 small_map<hash_map<std::string, int>> m; | 334 small_map<std::unordered_map<std::string, int>> m; |
335 small_map<hash_map<std::string, int>>::iterator iter; | 335 small_map<std::unordered_map<std::string, int>>::iterator iter; |
336 | 336 |
337 m["a"] = 0; | 337 m["a"] = 0; |
338 m["b"] = 1; | 338 m["b"] = 1; |
339 m["c"] = 2; | 339 m["c"] = 2; |
340 | 340 |
341 // Erase first item. | 341 // Erase first item. |
342 auto following_iter = m.erase(m.begin()); | 342 auto following_iter = m.erase(m.begin()); |
343 EXPECT_EQ(m.begin(), following_iter); | 343 EXPECT_EQ(m.begin(), following_iter); |
344 EXPECT_EQ(2u, m.size()); | 344 EXPECT_EQ(2u, m.size()); |
345 EXPECT_EQ(m.count("a"), 0u); | 345 EXPECT_EQ(m.count("a"), 0u); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 EXPECT_TRUE(iter == m.end()); | 426 EXPECT_TRUE(iter == m.end()); |
427 --iter; | 427 --iter; |
428 ASSERT_TRUE(iter != m.end()); | 428 ASSERT_TRUE(iter != m.end()); |
429 EXPECT_EQ(iter->first, 1234); | 429 EXPECT_EQ(iter->first, 1234); |
430 EXPECT_EQ(iter->second, 90); | 430 EXPECT_EQ(iter->second, 90); |
431 } | 431 } |
432 | 432 |
433 TEST(SmallMap, DefaultEqualKeyWorks) { | 433 TEST(SmallMap, DefaultEqualKeyWorks) { |
434 // If these tests compile, they pass. The EXPECT calls are only there to avoid | 434 // If these tests compile, they pass. The EXPECT calls are only there to avoid |
435 // unused variable warnings. | 435 // unused variable warnings. |
436 small_map<hash_map<int, int>> hm; | 436 small_map<std::unordered_map<int, int>> hm; |
437 EXPECT_EQ(0u, hm.size()); | 437 EXPECT_EQ(0u, hm.size()); |
438 small_map<std::map<int, int>> m; | 438 small_map<std::map<int, int>> m; |
439 EXPECT_EQ(0u, m.size()); | 439 EXPECT_EQ(0u, m.size()); |
440 } | 440 } |
441 | 441 |
442 namespace { | 442 namespace { |
443 | 443 |
444 class hash_map_add_item : public hash_map<int, int> { | 444 class unordered_map_add_item : public std::unordered_map<int, int> { |
445 public: | 445 public: |
446 hash_map_add_item() {} | 446 unordered_map_add_item() {} |
447 explicit hash_map_add_item(const std::pair<int, int>& item) { | 447 explicit unordered_map_add_item(const std::pair<int, int>& item) { |
448 insert(item); | 448 insert(item); |
449 } | 449 } |
450 }; | 450 }; |
451 | 451 |
452 void InitMap(ManualConstructor<hash_map_add_item>* map_ctor) { | 452 void InitMap(ManualConstructor<unordered_map_add_item>* map_ctor) { |
453 map_ctor->Init(std::make_pair(0, 0)); | 453 map_ctor->Init(std::make_pair(0, 0)); |
454 } | 454 } |
455 | 455 |
456 class hash_map_add_item_initializer { | 456 class unordered_map_add_item_initializer { |
457 public: | 457 public: |
458 explicit hash_map_add_item_initializer(int item_to_add) | 458 explicit unordered_map_add_item_initializer(int item_to_add) |
459 : item_(item_to_add) {} | 459 : item_(item_to_add) {} |
460 hash_map_add_item_initializer() | 460 unordered_map_add_item_initializer() : item_(0) {} |
461 : item_(0) {} | 461 void operator()(ManualConstructor<unordered_map_add_item>* map_ctor) const { |
462 void operator()(ManualConstructor<hash_map_add_item>* map_ctor) const { | |
463 map_ctor->Init(std::make_pair(item_, item_)); | 462 map_ctor->Init(std::make_pair(item_, item_)); |
464 } | 463 } |
465 | 464 |
466 int item_; | 465 int item_; |
467 }; | 466 }; |
468 | 467 |
469 } // anonymous namespace | 468 } // anonymous namespace |
470 | 469 |
471 TEST(SmallMap, SubclassInitializationWithFunctionPointer) { | 470 TEST(SmallMap, SubclassInitializationWithFunctionPointer) { |
472 small_map<hash_map_add_item, 4, std::equal_to<int>, | 471 small_map<unordered_map_add_item, 4, std::equal_to<int>, |
473 void (&)(ManualConstructor<hash_map_add_item>*)> | 472 void (&)(ManualConstructor<unordered_map_add_item>*)> |
474 m(InitMap); | 473 m(InitMap); |
475 | 474 |
476 EXPECT_TRUE(m.empty()); | 475 EXPECT_TRUE(m.empty()); |
477 | 476 |
478 m[1] = 1; | 477 m[1] = 1; |
479 m[2] = 2; | 478 m[2] = 2; |
480 m[3] = 3; | 479 m[3] = 3; |
481 m[4] = 4; | 480 m[4] = 4; |
482 | 481 |
483 EXPECT_EQ(4u, m.size()); | 482 EXPECT_EQ(4u, m.size()); |
484 EXPECT_EQ(0u, m.count(0)); | 483 EXPECT_EQ(0u, m.count(0)); |
485 | 484 |
486 m[5] = 5; | 485 m[5] = 5; |
487 EXPECT_EQ(6u, m.size()); | 486 EXPECT_EQ(6u, m.size()); |
488 // Our function adds an extra item when we convert to a map. | 487 // Our function adds an extra item when we convert to a map. |
489 EXPECT_EQ(1u, m.count(0)); | 488 EXPECT_EQ(1u, m.count(0)); |
490 } | 489 } |
491 | 490 |
492 TEST(SmallMap, SubclassInitializationWithFunctionObject) { | 491 TEST(SmallMap, SubclassInitializationWithFunctionObject) { |
493 small_map<hash_map_add_item, 4, std::equal_to<int>, | 492 small_map<unordered_map_add_item, 4, std::equal_to<int>, |
494 hash_map_add_item_initializer> | 493 unordered_map_add_item_initializer> |
495 m(hash_map_add_item_initializer(-1)); | 494 m(unordered_map_add_item_initializer(-1)); |
496 | 495 |
497 EXPECT_TRUE(m.empty()); | 496 EXPECT_TRUE(m.empty()); |
498 | 497 |
499 m[1] = 1; | 498 m[1] = 1; |
500 m[2] = 2; | 499 m[2] = 2; |
501 m[3] = 3; | 500 m[3] = 3; |
502 m[4] = 4; | 501 m[4] = 4; |
503 | 502 |
504 EXPECT_EQ(4u, m.size()); | 503 EXPECT_EQ(4u, m.size()); |
505 EXPECT_EQ(0u, m.count(-1)); | 504 EXPECT_EQ(0u, m.count(-1)); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 | 557 |
559 m.erase(m.begin()); | 558 m.erase(m.begin()); |
560 | 559 |
561 // small_map should also let internal std::map erase with a move-only type. | 560 // small_map should also let internal std::map erase with a move-only type. |
562 EXPECT_EQ(m.size(), 2u); | 561 EXPECT_EQ(m.size(), 2u); |
563 EXPECT_EQ(m[1].value(), 2); | 562 EXPECT_EQ(m[1].value(), 2); |
564 EXPECT_EQ(m[2].value(), 3); | 563 EXPECT_EQ(m[2].value(), 3); |
565 } | 564 } |
566 | 565 |
567 } // namespace base | 566 } // namespace base |
OLD | NEW |