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 | 12 |
13 #include "base/containers/hash_tables.h" | 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 SmallMap<hash_map<int, int> > m; | 20 small_map<hash_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 SmallMap<hash_map<int, int> >::iterator iter(m.begin()); | 38 small_map<hash_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 SmallMap<hash_map<int, int> >& ref = m; | 69 const small_map<hash_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 SmallMap<hash_map<int, int> > m; | 75 small_map<hash_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 SmallMap<hash_map<int, int> >::iterator iter(m.begin()); | 80 small_map<hash_map<int, int>>::iterator iter(m.begin()); |
81 SmallMap<hash_map<int, int> >::iterator last(iter++); | 81 small_map<hash_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 SmallMap<hash_map<int, int> >::const_iterator iter(m.begin()); | 87 small_map<hash_map<int, int>>::const_iterator iter(m.begin()); |
88 SmallMap<hash_map<int, int> >::const_iterator last(iter++); | 88 small_map<hash_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 SmallMap<hash_map<int, int> > src; | 96 small_map<hash_map<int, int>> src; |
97 | 97 |
98 { | 98 { |
99 SmallMap<hash_map<int, int> > m(src); | 99 small_map<hash_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 SmallMap<hash_map<int, int> > m(src); | 106 small_map<hash_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 SmallMap<hash_map<int, int> > m(src); | 114 small_map<hash_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 SmallMap<hash_map<int, int> > m(src); | 128 small_map<hash_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 } |
139 | 139 |
140 template<class inner> | 140 template <class inner> |
141 static bool SmallMapIsSubset(SmallMap<inner> const& a, | 141 static bool SmallMapIsSubset(small_map<inner> const& a, |
142 SmallMap<inner> const& b) { | 142 small_map<inner> const& b) { |
143 typename SmallMap<inner>::const_iterator it; | 143 typename small_map<inner>::const_iterator it; |
144 for (it = a.begin(); it != a.end(); ++it) { | 144 for (it = a.begin(); it != a.end(); ++it) { |
145 typename SmallMap<inner>::const_iterator it_in_b = b.find(it->first); | 145 typename small_map<inner>::const_iterator it_in_b = b.find(it->first); |
146 if (it_in_b == b.end() || it_in_b->second != it->second) | 146 if (it_in_b == b.end() || it_in_b->second != it->second) |
147 return false; | 147 return false; |
148 } | 148 } |
149 return true; | 149 return true; |
150 } | 150 } |
151 | 151 |
152 template<class inner> | 152 template <class inner> |
153 static bool SmallMapEqual(SmallMap<inner> const& a, | 153 static bool SmallMapEqual(small_map<inner> const& a, |
154 SmallMap<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 SmallMap<hash_map<int, int> > src_small; | 159 small_map<hash_map<int, int>> src_small; |
160 SmallMap<hash_map<int, int> > src_large; | 160 small_map<hash_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 SmallMap<hash_map<int, int> > dest_small; | 176 small_map<hash_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 SmallMap<hash_map<int, int> > dest_large; | 182 small_map<hash_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 SmallMap<hash_map<int, int> > sm; | 205 small_map<hash_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<SmallMap<hash_map<int, int> >::iterator, | 211 std::pair<small_map<hash_map<int, int>>::iterator, bool> ret; |
212 bool> ret; | |
213 ret = sm.insert(std::make_pair(i, 100*i)); | 212 ret = sm.insert(std::make_pair(i, 100*i)); |
214 EXPECT_TRUE(ret.second); | 213 EXPECT_TRUE(ret.second); |
215 EXPECT_TRUE(ret.first == sm.find(i)); | 214 EXPECT_TRUE(ret.first == sm.find(i)); |
216 EXPECT_EQ(ret.first->first, i); | 215 EXPECT_EQ(ret.first->first, i); |
217 EXPECT_EQ(ret.first->second, 100*i); | 216 EXPECT_EQ(ret.first->second, 100*i); |
218 | 217 |
219 // 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 |
220 // iterator back with the original value. | 219 // iterator back with the original value. |
221 ret = sm.insert(std::make_pair(i, -i)); | 220 ret = sm.insert(std::make_pair(i, -i)); |
222 EXPECT_FALSE(ret.second); | 221 EXPECT_FALSE(ret.second); |
223 EXPECT_TRUE(ret.first == sm.find(i)); | 222 EXPECT_TRUE(ret.first == sm.find(i)); |
224 EXPECT_EQ(ret.first->first, i); | 223 EXPECT_EQ(ret.first->first, i); |
225 EXPECT_EQ(ret.first->second, 100*i); | 224 EXPECT_EQ(ret.first->second, 100*i); |
226 | 225 |
227 // check the state of the map. | 226 // check the state of the map. |
228 for (int j = 1; j <= i; ++j) { | 227 for (int j = 1; j <= i; ++j) { |
229 SmallMap<hash_map<int, int> >::iterator it = sm.find(j); | 228 small_map<hash_map<int, int>>::iterator it = sm.find(j); |
230 EXPECT_TRUE(it != sm.end()); | 229 EXPECT_TRUE(it != sm.end()); |
231 EXPECT_EQ(it->first, j); | 230 EXPECT_EQ(it->first, j); |
232 EXPECT_EQ(it->second, j * 100); | 231 EXPECT_EQ(it->second, j * 100); |
233 } | 232 } |
234 EXPECT_EQ(sm.size(), static_cast<size_t>(i)); | 233 EXPECT_EQ(sm.size(), static_cast<size_t>(i)); |
235 EXPECT_FALSE(sm.empty()); | 234 EXPECT_FALSE(sm.empty()); |
236 } | 235 } |
237 } | 236 } |
238 | 237 |
239 TEST(SmallMap, InsertRange) { | 238 TEST(SmallMap, InsertRange) { |
240 // loop through the transition from small map to map. | 239 // loop through the transition from small map to map. |
241 for (int elements = 0; elements <= 10; ++elements) { | 240 for (int elements = 0; elements <= 10; ++elements) { |
242 VLOG(1) << "Elements " << elements; | 241 VLOG(1) << "Elements " << elements; |
243 hash_map<int, int> normal_map; | 242 hash_map<int, int> normal_map; |
244 for (int i = 1; i <= elements; ++i) { | 243 for (int i = 1; i <= elements; ++i) { |
245 normal_map.insert(std::make_pair(i, 100*i)); | 244 normal_map.insert(std::make_pair(i, 100*i)); |
246 } | 245 } |
247 | 246 |
248 SmallMap<hash_map<int, int> > sm; | 247 small_map<hash_map<int, int>> sm; |
249 sm.insert(normal_map.begin(), normal_map.end()); | 248 sm.insert(normal_map.begin(), normal_map.end()); |
250 EXPECT_EQ(normal_map.size(), sm.size()); | 249 EXPECT_EQ(normal_map.size(), sm.size()); |
251 for (int i = 1; i <= elements; ++i) { | 250 for (int i = 1; i <= elements; ++i) { |
252 VLOG(1) << "Iteration " << i; | 251 VLOG(1) << "Iteration " << i; |
253 EXPECT_TRUE(sm.find(i) != sm.end()); | 252 EXPECT_TRUE(sm.find(i) != sm.end()); |
254 EXPECT_EQ(sm.find(i)->first, i); | 253 EXPECT_EQ(sm.find(i)->first, i); |
255 EXPECT_EQ(sm.find(i)->second, 100*i); | 254 EXPECT_EQ(sm.find(i)->second, 100*i); |
256 } | 255 } |
257 } | 256 } |
258 } | 257 } |
259 | 258 |
260 TEST(SmallMap, Erase) { | 259 TEST(SmallMap, Erase) { |
261 SmallMap<hash_map<std::string, int> > m; | 260 small_map<hash_map<std::string, int>> m; |
262 SmallMap<hash_map<std::string, int> >::iterator iter; | 261 small_map<hash_map<std::string, int>>::iterator iter; |
263 | 262 |
264 m["monday"] = 1; | 263 m["monday"] = 1; |
265 m["tuesday"] = 2; | 264 m["tuesday"] = 2; |
266 m["wednesday"] = 3; | 265 m["wednesday"] = 3; |
267 | 266 |
268 EXPECT_EQ(m["monday" ], 1); | 267 EXPECT_EQ(m["monday" ], 1); |
269 EXPECT_EQ(m["tuesday" ], 2); | 268 EXPECT_EQ(m["tuesday" ], 2); |
270 EXPECT_EQ(m["wednesday"], 3); | 269 EXPECT_EQ(m["wednesday"], 3); |
271 EXPECT_EQ(m.count("tuesday"), 1u); | 270 EXPECT_EQ(m.count("tuesday"), 1u); |
272 EXPECT_FALSE(m.UsingFullMap()); | 271 EXPECT_FALSE(m.UsingFullMap()); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 EXPECT_EQ(m.size(), 3u); | 324 EXPECT_EQ(m.size(), 3u); |
326 EXPECT_FALSE(m.empty()); | 325 EXPECT_FALSE(m.empty()); |
327 | 326 |
328 m.clear(); | 327 m.clear(); |
329 EXPECT_FALSE(m.UsingFullMap()); | 328 EXPECT_FALSE(m.UsingFullMap()); |
330 EXPECT_EQ(m.size(), 0u); | 329 EXPECT_EQ(m.size(), 0u); |
331 EXPECT_TRUE(m.empty()); | 330 EXPECT_TRUE(m.empty()); |
332 } | 331 } |
333 | 332 |
334 TEST(SmallMap, EraseReturnsIteratorFollowingRemovedElement) { | 333 TEST(SmallMap, EraseReturnsIteratorFollowingRemovedElement) { |
335 SmallMap<hash_map<std::string, int> > m; | 334 small_map<hash_map<std::string, int>> m; |
336 SmallMap<hash_map<std::string, int> >::iterator iter; | 335 small_map<hash_map<std::string, int>>::iterator iter; |
337 | 336 |
338 m["a"] = 0; | 337 m["a"] = 0; |
339 m["b"] = 1; | 338 m["b"] = 1; |
340 m["c"] = 2; | 339 m["c"] = 2; |
341 | 340 |
342 // Erase first item. | 341 // Erase first item. |
343 auto following_iter = m.erase(m.begin()); | 342 auto following_iter = m.erase(m.begin()); |
344 EXPECT_EQ(m.begin(), following_iter); | 343 EXPECT_EQ(m.begin(), following_iter); |
345 EXPECT_EQ(2u, m.size()); | 344 EXPECT_EQ(2u, m.size()); |
346 EXPECT_EQ(m.count("a"), 0u); | 345 EXPECT_EQ(m.count("a"), 0u); |
347 EXPECT_EQ(m.count("b"), 1u); | 346 EXPECT_EQ(m.count("b"), 1u); |
348 EXPECT_EQ(m.count("c"), 1u); | 347 EXPECT_EQ(m.count("c"), 1u); |
349 | 348 |
350 // Iterate to last item and erase it. | 349 // Iterate to last item and erase it. |
351 ++following_iter; | 350 ++following_iter; |
352 following_iter = m.erase(following_iter); | 351 following_iter = m.erase(following_iter); |
353 ASSERT_EQ(1u, m.size()); | 352 ASSERT_EQ(1u, m.size()); |
354 EXPECT_EQ(m.end(), following_iter); | 353 EXPECT_EQ(m.end(), following_iter); |
355 EXPECT_EQ(m.count("b"), 0u); | 354 EXPECT_EQ(m.count("b"), 0u); |
356 EXPECT_EQ(m.count("c"), 1u); | 355 EXPECT_EQ(m.count("c"), 1u); |
357 | 356 |
358 // Erase remaining item. | 357 // Erase remaining item. |
359 following_iter = m.erase(m.begin()); | 358 following_iter = m.erase(m.begin()); |
360 EXPECT_TRUE(m.empty()); | 359 EXPECT_TRUE(m.empty()); |
361 EXPECT_EQ(m.end(), following_iter); | 360 EXPECT_EQ(m.end(), following_iter); |
362 } | 361 } |
363 | 362 |
364 TEST(SmallMap, NonHashMap) { | 363 TEST(SmallMap, NonHashMap) { |
365 SmallMap<std::map<int, int>, 4, std::equal_to<int> > m; | 364 small_map<std::map<int, int>, 4, std::equal_to<int>> m; |
366 EXPECT_TRUE(m.empty()); | 365 EXPECT_TRUE(m.empty()); |
367 | 366 |
368 m[9] = 2; | 367 m[9] = 2; |
369 m[0] = 5; | 368 m[0] = 5; |
370 | 369 |
371 EXPECT_EQ(m[9], 2); | 370 EXPECT_EQ(m[9], 2); |
372 EXPECT_EQ(m[0], 5); | 371 EXPECT_EQ(m[0], 5); |
373 EXPECT_EQ(m.size(), 2u); | 372 EXPECT_EQ(m.size(), 2u); |
374 EXPECT_FALSE(m.empty()); | 373 EXPECT_FALSE(m.empty()); |
375 EXPECT_FALSE(m.UsingFullMap()); | 374 EXPECT_FALSE(m.UsingFullMap()); |
376 | 375 |
377 SmallMap<std::map<int, int>, 4, std::equal_to<int> >::iterator iter( | 376 small_map<std::map<int, int>, 4, std::equal_to<int>>::iterator iter( |
378 m.begin()); | 377 m.begin()); |
379 ASSERT_TRUE(iter != m.end()); | 378 ASSERT_TRUE(iter != m.end()); |
380 EXPECT_EQ(iter->first, 9); | 379 EXPECT_EQ(iter->first, 9); |
381 EXPECT_EQ(iter->second, 2); | 380 EXPECT_EQ(iter->second, 2); |
382 ++iter; | 381 ++iter; |
383 ASSERT_TRUE(iter != m.end()); | 382 ASSERT_TRUE(iter != m.end()); |
384 EXPECT_EQ(iter->first, 0); | 383 EXPECT_EQ(iter->first, 0); |
385 EXPECT_EQ(iter->second, 5); | 384 EXPECT_EQ(iter->second, 5); |
386 ++iter; | 385 ++iter; |
387 EXPECT_TRUE(iter == m.end()); | 386 EXPECT_TRUE(iter == m.end()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 EXPECT_TRUE(iter == m.end()); | 426 EXPECT_TRUE(iter == m.end()); |
428 --iter; | 427 --iter; |
429 ASSERT_TRUE(iter != m.end()); | 428 ASSERT_TRUE(iter != m.end()); |
430 EXPECT_EQ(iter->first, 1234); | 429 EXPECT_EQ(iter->first, 1234); |
431 EXPECT_EQ(iter->second, 90); | 430 EXPECT_EQ(iter->second, 90); |
432 } | 431 } |
433 | 432 |
434 TEST(SmallMap, DefaultEqualKeyWorks) { | 433 TEST(SmallMap, DefaultEqualKeyWorks) { |
435 // 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 |
436 // unused variable warnings. | 435 // unused variable warnings. |
437 SmallMap<hash_map<int, int> > hm; | 436 small_map<hash_map<int, int>> hm; |
438 EXPECT_EQ(0u, hm.size()); | 437 EXPECT_EQ(0u, hm.size()); |
439 SmallMap<std::map<int, int> > m; | 438 small_map<std::map<int, int>> m; |
440 EXPECT_EQ(0u, m.size()); | 439 EXPECT_EQ(0u, m.size()); |
441 } | 440 } |
442 | 441 |
443 namespace { | 442 namespace { |
444 | 443 |
445 class hash_map_add_item : public hash_map<int, int> { | 444 class hash_map_add_item : public hash_map<int, int> { |
446 public: | 445 public: |
447 hash_map_add_item() {} | 446 hash_map_add_item() {} |
448 explicit hash_map_add_item(const std::pair<int, int>& item) { | 447 explicit hash_map_add_item(const std::pair<int, int>& item) { |
449 insert(item); | 448 insert(item); |
(...skipping 13 matching lines...) Expand all Loading... |
463 void operator()(ManualConstructor<hash_map_add_item>* map_ctor) const { | 462 void operator()(ManualConstructor<hash_map_add_item>* map_ctor) const { |
464 map_ctor->Init(std::make_pair(item_, item_)); | 463 map_ctor->Init(std::make_pair(item_, item_)); |
465 } | 464 } |
466 | 465 |
467 int item_; | 466 int item_; |
468 }; | 467 }; |
469 | 468 |
470 } // anonymous namespace | 469 } // anonymous namespace |
471 | 470 |
472 TEST(SmallMap, SubclassInitializationWithFunctionPointer) { | 471 TEST(SmallMap, SubclassInitializationWithFunctionPointer) { |
473 SmallMap<hash_map_add_item, 4, std::equal_to<int>, | 472 small_map<hash_map_add_item, 4, std::equal_to<int>, |
474 void (&)(ManualConstructor<hash_map_add_item>*)> m(InitMap); | 473 void (&)(ManualConstructor<hash_map_add_item>*)> |
| 474 m(InitMap); |
475 | 475 |
476 EXPECT_TRUE(m.empty()); | 476 EXPECT_TRUE(m.empty()); |
477 | 477 |
478 m[1] = 1; | 478 m[1] = 1; |
479 m[2] = 2; | 479 m[2] = 2; |
480 m[3] = 3; | 480 m[3] = 3; |
481 m[4] = 4; | 481 m[4] = 4; |
482 | 482 |
483 EXPECT_EQ(4u, m.size()); | 483 EXPECT_EQ(4u, m.size()); |
484 EXPECT_EQ(0u, m.count(0)); | 484 EXPECT_EQ(0u, m.count(0)); |
485 | 485 |
486 m[5] = 5; | 486 m[5] = 5; |
487 EXPECT_EQ(6u, m.size()); | 487 EXPECT_EQ(6u, m.size()); |
488 // Our function adds an extra item when we convert to a map. | 488 // Our function adds an extra item when we convert to a map. |
489 EXPECT_EQ(1u, m.count(0)); | 489 EXPECT_EQ(1u, m.count(0)); |
490 } | 490 } |
491 | 491 |
492 TEST(SmallMap, SubclassInitializationWithFunctionObject) { | 492 TEST(SmallMap, SubclassInitializationWithFunctionObject) { |
493 SmallMap<hash_map_add_item, 4, std::equal_to<int>, | 493 small_map<hash_map_add_item, 4, std::equal_to<int>, |
494 hash_map_add_item_initializer> m(hash_map_add_item_initializer(-1)); | 494 hash_map_add_item_initializer> |
| 495 m(hash_map_add_item_initializer(-1)); |
495 | 496 |
496 EXPECT_TRUE(m.empty()); | 497 EXPECT_TRUE(m.empty()); |
497 | 498 |
498 m[1] = 1; | 499 m[1] = 1; |
499 m[2] = 2; | 500 m[2] = 2; |
500 m[3] = 3; | 501 m[3] = 3; |
501 m[4] = 4; | 502 m[4] = 4; |
502 | 503 |
503 EXPECT_EQ(4u, m.size()); | 504 EXPECT_EQ(4u, m.size()); |
504 EXPECT_EQ(0u, m.count(-1)); | 505 EXPECT_EQ(0u, m.count(-1)); |
(...skipping 24 matching lines...) Expand all Loading... |
529 MoveOnlyType(const MoveOnlyType&) = delete; | 530 MoveOnlyType(const MoveOnlyType&) = delete; |
530 MoveOnlyType& operator=(const MoveOnlyType&) = delete; | 531 MoveOnlyType& operator=(const MoveOnlyType&) = delete; |
531 | 532 |
532 int value() const { return value_; } | 533 int value() const { return value_; } |
533 | 534 |
534 private: | 535 private: |
535 int value_; | 536 int value_; |
536 }; | 537 }; |
537 | 538 |
538 TEST(SmallMap, MoveOnlyValueType) { | 539 TEST(SmallMap, MoveOnlyValueType) { |
539 SmallMap<std::map<int, MoveOnlyType>, 2> m; | 540 small_map<std::map<int, MoveOnlyType>, 2> m; |
540 | 541 |
541 m[0] = MoveOnlyType(1); | 542 m[0] = MoveOnlyType(1); |
542 m[1] = MoveOnlyType(2); | 543 m[1] = MoveOnlyType(2); |
543 m.erase(m.begin()); | 544 m.erase(m.begin()); |
544 | 545 |
545 // SmallMap will move m[1] to an earlier index in the internal array. | 546 // small_map will move m[1] to an earlier index in the internal array. |
546 EXPECT_EQ(m.size(), 1u); | 547 EXPECT_EQ(m.size(), 1u); |
547 EXPECT_EQ(m[1].value(), 2); | 548 EXPECT_EQ(m[1].value(), 2); |
548 | 549 |
549 m[0] = MoveOnlyType(1); | 550 m[0] = MoveOnlyType(1); |
550 // SmallMap must move the values from the array into the internal std::map. | 551 // small_map must move the values from the array into the internal std::map. |
551 m[2] = MoveOnlyType(3); | 552 m[2] = MoveOnlyType(3); |
552 | 553 |
553 EXPECT_EQ(m.size(), 3u); | 554 EXPECT_EQ(m.size(), 3u); |
554 EXPECT_EQ(m[0].value(), 1); | 555 EXPECT_EQ(m[0].value(), 1); |
555 EXPECT_EQ(m[1].value(), 2); | 556 EXPECT_EQ(m[1].value(), 2); |
556 EXPECT_EQ(m[2].value(), 3); | 557 EXPECT_EQ(m[2].value(), 3); |
557 | 558 |
558 m.erase(m.begin()); | 559 m.erase(m.begin()); |
559 | 560 |
560 // SmallMap should also let internal std::map erase with a move-only type. | 561 // small_map should also let internal std::map erase with a move-only type. |
561 EXPECT_EQ(m.size(), 2u); | 562 EXPECT_EQ(m.size(), 2u); |
562 EXPECT_EQ(m[1].value(), 2); | 563 EXPECT_EQ(m[1].value(), 2); |
563 EXPECT_EQ(m[2].value(), 3); | 564 EXPECT_EQ(m[2].value(), 3); |
564 } | 565 } |
565 | 566 |
566 } // namespace base | 567 } // namespace base |
OLD | NEW |