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> |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 void SmallMapToMap(SmallMap<inner> const& src, inner* dest) { | 141 static bool SmallMapIsSubset(SmallMap<inner> const& a, |
| 142 SmallMap<inner> const& b) { |
142 typename SmallMap<inner>::const_iterator it; | 143 typename SmallMap<inner>::const_iterator it; |
143 for (it = src.begin(); it != src.end(); ++it) { | 144 for (it = a.begin(); it != a.end(); ++it) { |
144 dest->insert(std::make_pair(it->first, it->second)); | 145 typename SmallMap<inner>::const_iterator it_in_b = b.find(it->first); |
| 146 if (it_in_b == b.end() || it_in_b->second != it->second) |
| 147 return false; |
145 } | 148 } |
| 149 return true; |
146 } | 150 } |
147 | 151 |
148 template<class inner> | 152 template<class inner> |
149 static bool SmallMapEqual(SmallMap<inner> const& a, | 153 static bool SmallMapEqual(SmallMap<inner> const& a, |
150 SmallMap<inner> const& b) { | 154 SmallMap<inner> const& b) { |
151 inner ia, ib; | 155 return SmallMapIsSubset(a, b) && SmallMapIsSubset(b, a); |
152 SmallMapToMap(a, &ia); | |
153 SmallMapToMap(b, &ib); | |
154 | |
155 // On most systems we can use operator== here, but under some lesser STL | |
156 // implementations it doesn't seem to work. So we manually compare. | |
157 if (ia.size() != ib.size()) | |
158 return false; | |
159 for (typename inner::iterator ia_it = ia.begin(), ib_it = ib.begin(); | |
160 ia_it != ia.end(); ++ia_it, ++ib_it) { | |
161 if (*ia_it != *ib_it) | |
162 return false; | |
163 } | |
164 return true; | |
165 } | 156 } |
166 | 157 |
167 TEST(SmallMap, AssignmentOperator) { | 158 TEST(SmallMap, AssignmentOperator) { |
168 SmallMap<hash_map<int, int> > src_small; | 159 SmallMap<hash_map<int, int> > src_small; |
169 SmallMap<hash_map<int, int> > src_large; | 160 SmallMap<hash_map<int, int> > src_large; |
170 | 161 |
171 src_small[1] = 20; | 162 src_small[1] = 20; |
172 src_small[2] = 21; | 163 src_small[2] = 21; |
173 src_small[3] = 22; | 164 src_small[3] = 22; |
174 EXPECT_FALSE(src_small.UsingFullMap()); | 165 EXPECT_FALSE(src_small.UsingFullMap()); |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 EXPECT_EQ(4u, m.size()); | 474 EXPECT_EQ(4u, m.size()); |
484 EXPECT_EQ(0u, m.count(-1)); | 475 EXPECT_EQ(0u, m.count(-1)); |
485 | 476 |
486 m[5] = 5; | 477 m[5] = 5; |
487 EXPECT_EQ(6u, m.size()); | 478 EXPECT_EQ(6u, m.size()); |
488 // Our functor adds an extra item when we convert to a map. | 479 // Our functor adds an extra item when we convert to a map. |
489 EXPECT_EQ(1u, m.count(-1)); | 480 EXPECT_EQ(1u, m.count(-1)); |
490 } | 481 } |
491 | 482 |
492 } // namespace base | 483 } // namespace base |
OLD | NEW |