OLD | NEW |
---|---|
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" | |
11 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/test/move_only_int.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 // A flat_map is basically a interface to flat_tree. So several basic | 15 // A flat_map is basically a interface to flat_tree. So several basic |
16 // operations are tested to make sure things are set up properly, but the bulk | 16 // operations are tested to make sure things are set up properly, but the bulk |
17 // of the tests are in flat_tree_unittests.cc. | 17 // of the tests are in flat_tree_unittests.cc. |
18 | 18 |
19 using ::testing::ElementsAre; | 19 using ::testing::ElementsAre; |
20 | 20 |
21 namespace base { | 21 namespace base { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 | 163 |
164 // The returned mapped reference should refer into the map. | 164 // The returned mapped reference should refer into the map. |
165 s = 22; | 165 s = 22; |
166 EXPECT_EQ(22, m[MoveOnlyInt(1)]); | 166 EXPECT_EQ(22, m[MoveOnlyInt(1)]); |
167 | 167 |
168 // Overwrite existing elements. | 168 // Overwrite existing elements. |
169 m[MoveOnlyInt(1)] = 44; | 169 m[MoveOnlyInt(1)] = 44; |
170 EXPECT_EQ(44, m[MoveOnlyInt(1)]); | 170 EXPECT_EQ(44, m[MoveOnlyInt(1)]); |
171 } | 171 } |
172 | 172 |
173 TEST(FlatMap, UsingTransparentCompare) { | |
174 using ExplicitInt = base::MoveOnlyInt; | |
175 base::flat_map<ExplicitInt, int> m; | |
176 const auto& m1 = m; | |
177 int x = 0; | |
178 | |
179 // Check if we can use lookup functions without converting to key_type. | |
180 // Correctness is checked in flat_tree tests. | |
181 m.count(x); | |
182 m1.count(x); | |
183 m.find(x); | |
184 m1.find(x); | |
185 m.equal_range(x); | |
186 m1.equal_range(x); | |
187 m.lower_bound(x); | |
188 m1.lower_bound(x); | |
189 m.upper_bound(x); | |
190 m1.upper_bound(x); | |
191 m.erase(x); | |
192 | |
193 // Check if we broke overload resolution. | |
194 m.emplace(ExplicitInt(0), 0); | |
195 m.emplace(ExplicitInt(1), 0); | |
196 m.erase(m.begin()); | |
197 m.erase(m.cbegin()); | |
198 } | |
199 | |
200 TEST(FlatMap, Examples) { | |
201 base::flat_map<std::string, int> str_to_int({{"a", 1}, {"c", 2}, {"b", 2}}, | |
202 base::KEEP_FIRST_OF_DUPES); | |
203 | |
204 // NOTE: does construct a temporary string. | |
205 str_to_int["c"] = 3; | |
206 | |
207 // Do not construct temporary strings. | |
danakj
2017/06/27 23:48:25
If we're not testing somehow the temp strings are
dyaroshev
2017/06/28 09:13:59
Done
| |
208 str_to_int.lower_bound("abc")->second = 3; | |
209 str_to_int.erase("c"); | |
210 EXPECT_EQ(str_to_int.end(), str_to_int.find("c")); | |
211 } | |
212 | |
173 } // namespace base | 213 } // namespace base |
OLD | NEW |