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_tree.h" | 5 #include "base/containers/flat_tree.h" |
6 | 6 |
7 // Following tests are ported and extended tests from libcpp for std::set. | 7 // Following tests are ported and extended tests from libcpp for std::set. |
8 // They can be found here: | 8 // They can be found here: |
9 // https://github.com/llvm-mirror/libcxx/tree/master/test/std/containers/associa tive/set | 9 // https://github.com/llvm-mirror/libcxx/tree/master/test/std/containers/associa tive/set |
10 // | 10 // |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 | 163 |
164 using TreeWithStrangeCompare = flat_tree<int, | 164 using TreeWithStrangeCompare = flat_tree<int, |
165 int, | 165 int, |
166 GetKeyFromValueIdentity<int>, | 166 GetKeyFromValueIdentity<int>, |
167 NonDefaultConstructibleCompare>; | 167 NonDefaultConstructibleCompare>; |
168 | 168 |
169 using ::testing::ElementsAre; | 169 using ::testing::ElementsAre; |
170 | 170 |
171 } // namespace | 171 } // namespace |
172 | 172 |
173 TEST(FlatTree, LastUnique) { | |
dyaroshev
2017/04/08 16:48:03
@brettew - I haven't seen these tests.
I like my t
| |
174 using Pair = std::pair<int, int>; | |
175 using Vect = std::vector<Pair>; | |
176 | |
177 auto cmp = [](const Pair& lhs, const Pair& rhs) { | |
178 return lhs.first == rhs.first; | |
179 }; | |
180 | |
181 // Empty case. | |
182 Vect empty; | |
183 EXPECT_EQ(empty.end(), LastUnique(empty.begin(), empty.end(), cmp)); | |
184 | |
185 // Single element. | |
186 Vect one; | |
187 one.push_back(Pair(1, 1)); | |
188 EXPECT_EQ(one.end(), LastUnique(one.begin(), one.end(), cmp)); | |
189 ASSERT_EQ(1u, one.size()); | |
190 EXPECT_THAT(one, ElementsAre(Pair(1, 1))); | |
191 | |
192 // Two elements, already unique. | |
193 Vect two_u; | |
194 two_u.push_back(Pair(1, 1)); | |
195 two_u.push_back(Pair(2, 2)); | |
196 EXPECT_EQ(two_u.end(), LastUnique(two_u.begin(), two_u.end(), cmp)); | |
197 EXPECT_THAT(two_u, ElementsAre(Pair(1, 1), Pair(2, 2))); | |
198 | |
199 // Two elements, dupes. | |
200 Vect two_d; | |
201 two_d.push_back(Pair(1, 1)); | |
202 two_d.push_back(Pair(1, 2)); | |
203 auto last = LastUnique(two_d.begin(), two_d.end(), cmp); | |
204 EXPECT_EQ(two_d.begin() + 1, last); | |
205 two_d.erase(last, two_d.end()); | |
206 EXPECT_THAT(two_d, ElementsAre(Pair(1, 2))); | |
207 | |
208 // Non-dupes, dupes, non-dupes. | |
209 Vect ndn; | |
210 ndn.push_back(Pair(1, 1)); | |
211 ndn.push_back(Pair(2, 1)); | |
212 ndn.push_back(Pair(2, 2)); | |
213 ndn.push_back(Pair(2, 3)); | |
214 ndn.push_back(Pair(3, 1)); | |
215 last = LastUnique(ndn.begin(), ndn.end(), cmp); | |
216 EXPECT_EQ(ndn.begin() + 3, last); | |
217 ndn.erase(last, ndn.end()); | |
218 EXPECT_THAT(ndn, ElementsAre(Pair(1, 1), Pair(2, 3), Pair(3, 1))); | |
219 | |
220 // Dupes, non-dupes, dupes. | |
221 Vect dnd; | |
222 dnd.push_back(Pair(1, 1)); | |
223 dnd.push_back(Pair(1, 2)); | |
224 dnd.push_back(Pair(1, 3)); | |
225 dnd.push_back(Pair(2, 1)); | |
226 dnd.push_back(Pair(3, 1)); | |
227 dnd.push_back(Pair(3, 2)); | |
228 dnd.push_back(Pair(3, 3)); | |
229 last = LastUnique(dnd.begin(), dnd.end(), cmp); | |
230 EXPECT_EQ(dnd.begin() + 3, last); | |
231 dnd.erase(last, dnd.end()); | |
232 EXPECT_THAT(dnd, ElementsAre(Pair(1, 3), Pair(2, 1), Pair(3, 3))); | |
233 } | |
234 | |
235 // ---------------------------------------------------------------------------- | 173 // ---------------------------------------------------------------------------- |
236 // Class. | 174 // Class. |
237 | 175 |
238 // Check that flat_tree and its iterators can be instantiated with an | 176 // Check that flat_tree and its iterators can be instantiated with an |
239 // incomplete type. | 177 // incomplete type. |
240 | 178 |
241 TEST(FlatTree, IncompleteType) { | 179 TEST(FlatTree, IncompleteType) { |
242 struct A { | 180 struct A { |
243 using Tree = flat_tree<A, A, GetKeyFromValueIdentity<A>, std::less<A>>; | 181 using Tree = flat_tree<A, A, GetKeyFromValueIdentity<A>, std::less<A>>; |
244 int data; | 182 int data; |
(...skipping 1131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1376 EraseIf(x, [](int elem) { return !(elem & 1); }); | 1314 EraseIf(x, [](int elem) { return !(elem & 1); }); |
1377 EXPECT_THAT(x, ElementsAre(1, 3)); | 1315 EXPECT_THAT(x, ElementsAre(1, 3)); |
1378 | 1316 |
1379 x = {1, 2, 3, 4}; | 1317 x = {1, 2, 3, 4}; |
1380 EraseIf(x, [](int elem) { return elem & 1; }); | 1318 EraseIf(x, [](int elem) { return elem & 1; }); |
1381 EXPECT_THAT(x, ElementsAre(2, 4)); | 1319 EXPECT_THAT(x, ElementsAre(2, 4)); |
1382 } | 1320 } |
1383 | 1321 |
1384 } // namespace internal | 1322 } // namespace internal |
1385 } // namespace base | 1323 } // namespace base |
OLD | NEW |