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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 class NonDefaultConstructibleCompare { | 123 class NonDefaultConstructibleCompare { |
124 public: | 124 public: |
125 explicit NonDefaultConstructibleCompare(int) {} | 125 explicit NonDefaultConstructibleCompare(int) {} |
126 | 126 |
127 template <typename T> | 127 template <typename T> |
128 bool operator()(const T& lhs, const T& rhs) const { | 128 bool operator()(const T& lhs, const T& rhs) const { |
129 return std::less<T>()(lhs, rhs); | 129 return std::less<T>()(lhs, rhs); |
130 } | 130 } |
131 }; | 131 }; |
132 | 132 |
| 133 template <class PairType> |
| 134 struct LessByFirst { |
| 135 bool operator()(const PairType& lhs, const PairType& rhs) const { |
| 136 return lhs.first < rhs.first; |
| 137 } |
| 138 }; |
| 139 |
133 // Common test trees. | 140 // Common test trees. |
134 | 141 |
135 // TODO(dyaroshev): replace less<int> with less<>, once we have it | 142 // TODO(dyaroshev): replace less<int> with less<>, once we have it |
136 // crbug.com/682254. This will make it different than IntTree. | 143 // crbug.com/682254. This will make it different than IntTree. |
137 using IntTreeWithLess = | 144 using IntTreeWithLess = |
138 flat_tree<int, int, GetKeyFromValueIdentity<int>, std::less<int>>; | 145 flat_tree<int, int, GetKeyFromValueIdentity<int>, std::less<int>>; |
139 using IntTree = | 146 using IntTree = |
140 flat_tree<int, int, GetKeyFromValueIdentity<int>, std::less<int>>; | 147 flat_tree<int, int, GetKeyFromValueIdentity<int>, std::less<int>>; |
| 148 using IntPair = std::pair<int, int>; |
| 149 using IntPairTree = flat_tree<IntPair, |
| 150 IntPair, |
| 151 GetKeyFromValueIdentity<IntPair>, |
| 152 LessByFirst<IntPair>>; |
141 using MoveOnlyTree = flat_tree<MoveOnlyInt, | 153 using MoveOnlyTree = flat_tree<MoveOnlyInt, |
142 MoveOnlyInt, | 154 MoveOnlyInt, |
143 GetKeyFromValueIdentity<MoveOnlyInt>, | 155 GetKeyFromValueIdentity<MoveOnlyInt>, |
144 std::less<MoveOnlyInt>>; | 156 std::less<MoveOnlyInt>>; |
145 using EmplaceableTree = flat_tree<Emplaceable, | 157 using EmplaceableTree = flat_tree<Emplaceable, |
146 Emplaceable, | 158 Emplaceable, |
147 GetKeyFromValueIdentity<Emplaceable>, | 159 GetKeyFromValueIdentity<Emplaceable>, |
148 std::less<Emplaceable>>; | 160 std::less<Emplaceable>>; |
149 using ReversedTree = | 161 using ReversedTree = |
150 flat_tree<int, int, GetKeyFromValueIdentity<int>, std::greater<int>>; | 162 flat_tree<int, int, GetKeyFromValueIdentity<int>, std::greater<int>>; |
151 | 163 |
152 using TreeWithStrangeCompare = flat_tree<int, | 164 using TreeWithStrangeCompare = flat_tree<int, |
153 int, | 165 int, |
154 GetKeyFromValueIdentity<int>, | 166 GetKeyFromValueIdentity<int>, |
155 NonDefaultConstructibleCompare>; | 167 NonDefaultConstructibleCompare>; |
156 | 168 |
157 using ::testing::ElementsAre; | 169 using ::testing::ElementsAre; |
158 | 170 |
159 } // namespace | 171 } // namespace |
160 | 172 |
| 173 TEST(FlatTree, LastUnique) { |
| 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 |
161 // ---------------------------------------------------------------------------- | 235 // ---------------------------------------------------------------------------- |
162 // Class. | 236 // Class. |
163 | 237 |
164 // Check that flat_tree and its iterators can be instantiated with an | 238 // Check that flat_tree and its iterators can be instantiated with an |
165 // incomplete type. | 239 // incomplete type. |
166 | 240 |
167 TEST(FlatTree, IncompleteType) { | 241 TEST(FlatTree, IncompleteType) { |
168 struct A { | 242 struct A { |
169 using Tree = flat_tree<A, A, GetKeyFromValueIdentity<A>, std::less<A>>; | 243 using Tree = flat_tree<A, A, GetKeyFromValueIdentity<A>, std::less<A>>; |
170 int data; | 244 int data; |
171 Tree set_with_incomplete_type; | 245 Tree set_with_incomplete_type; |
172 Tree::iterator it; | 246 Tree::iterator it; |
173 Tree::const_iterator cit; | 247 Tree::const_iterator cit; |
174 | 248 |
175 // We do not declare operator< because clang complains that it's unused. | 249 // We do not declare operator< because clang complains that it's unused. |
176 }; | 250 }; |
177 | 251 |
178 A a; | 252 A a; |
179 } | 253 } |
180 | 254 |
181 TEST(FlatTree, Stability) { | 255 TEST(FlatTree, Stability) { |
182 using Pair = std::pair<int, int>; | 256 using Pair = std::pair<int, int>; |
183 | 257 |
184 struct LessByFirst { | 258 using Tree = |
185 bool operator()(const Pair& lhs, const Pair& rhs) const { | 259 flat_tree<Pair, Pair, GetKeyFromValueIdentity<Pair>, LessByFirst<Pair>>; |
186 return lhs.first < rhs.first; | 260 |
187 } | 261 // Constructors are stable. |
| 262 Tree cont({{0, 0}, {1, 0}, {0, 1}, {2, 0}, {0, 2}, {1, 1}}, |
| 263 KEEP_FIRST_OF_DUPES); |
| 264 |
| 265 auto AllOfSecondsAreZero = [&cont] { |
| 266 return std::all_of(cont.begin(), cont.end(), |
| 267 [](const Pair& elem) { return elem.second == 0; }); |
188 }; | 268 }; |
189 | 269 |
190 using Tree = | 270 EXPECT_TRUE(AllOfSecondsAreZero()) << "constructor should be stable"; |
191 flat_tree<Pair, Pair, GetKeyFromValueIdentity<Pair>, LessByFirst>; | |
192 | |
193 // Constructors are not stable. | |
194 Tree cont{{0, 0}, {1, 0}, {0, 1}, {2, 0}, {0, 2}, {1, 1}}; | |
195 | |
196 auto NoneOfSecondsAreTwo = [&cont] { | |
197 return std::none_of(cont.begin(), cont.end(), | |
198 [](const Pair& elem) { return elem.second == 2; }); | |
199 }; | |
200 | 271 |
201 // Should not replace existing. | 272 // Should not replace existing. |
202 cont.insert(Pair(0, 2)); | 273 cont.insert(Pair(0, 2)); |
203 cont.insert(Pair(1, 2)); | 274 cont.insert(Pair(1, 2)); |
204 cont.insert(Pair(2, 2)); | 275 cont.insert(Pair(2, 2)); |
205 | 276 |
206 EXPECT_TRUE(NoneOfSecondsAreTwo()) | 277 EXPECT_TRUE(AllOfSecondsAreZero()) << "insert should be stable"; |
207 << "insert should be stable with respect to constructor"; | |
208 | 278 |
209 cont.insert(Pair(3, 0)); | 279 cont.insert(Pair(3, 0)); |
210 cont.insert(Pair(3, 2)); | 280 cont.insert(Pair(3, 2)); |
211 | 281 |
212 EXPECT_TRUE(NoneOfSecondsAreTwo()) | 282 EXPECT_TRUE(AllOfSecondsAreZero()) << "insert should be stable"; |
213 << "insert should be stable with respect to previous insert"; | |
214 } | 283 } |
215 | 284 |
216 // ---------------------------------------------------------------------------- | 285 // ---------------------------------------------------------------------------- |
217 // Types. | 286 // Types. |
218 | 287 |
219 // key_type | 288 // key_type |
220 // key_compare | 289 // key_compare |
221 // value_type | 290 // value_type |
222 // value_compare | 291 // value_compare |
223 // pointer | 292 // pointer |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 EXPECT_THAT(cont, ElementsAre()); | 325 EXPECT_THAT(cont, ElementsAre()); |
257 } | 326 } |
258 | 327 |
259 { | 328 { |
260 TreeWithStrangeCompare cont(NonDefaultConstructibleCompare(0)); | 329 TreeWithStrangeCompare cont(NonDefaultConstructibleCompare(0)); |
261 EXPECT_THAT(cont, ElementsAre()); | 330 EXPECT_THAT(cont, ElementsAre()); |
262 } | 331 } |
263 } | 332 } |
264 | 333 |
265 // flat_tree(InputIterator first, | 334 // flat_tree(InputIterator first, |
266 // InputIterator last, | 335 // InputIterator last, |
267 // const Compare& comp = Compare()) | 336 // FlatContainerDupes dupe_handling, |
| 337 // const Compare& comp = Compare()) |
268 | 338 |
269 TEST(FlatTree, RangeConstructor) { | 339 TEST(FlatTree, RangeConstructor) { |
270 { | 340 { |
271 IntTree::value_type input_vals[] = {1, 1, 1, 2, 2, 2, 3, 3, 3}; | 341 IntPair input_vals[] = {{1, 1}, {1, 2}, {2, 1}, {2, 2}, {1, 3}, |
| 342 {2, 3}, {3, 1}, {3, 2}, {3, 3}}; |
272 | 343 |
273 IntTree cont(MakeInputIterator(std::begin(input_vals)), | 344 IntPairTree first_of(MakeInputIterator(std::begin(input_vals)), |
274 MakeInputIterator(std::end(input_vals))); | 345 MakeInputIterator(std::end(input_vals)), |
275 EXPECT_THAT(cont, ElementsAre(1, 2, 3)); | 346 KEEP_FIRST_OF_DUPES); |
| 347 EXPECT_THAT(first_of, |
| 348 ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1))); |
| 349 |
| 350 IntPairTree last_of(MakeInputIterator(std::begin(input_vals)), |
| 351 MakeInputIterator(std::end(input_vals)), |
| 352 KEEP_LAST_OF_DUPES); |
| 353 EXPECT_THAT(last_of, |
| 354 ElementsAre(IntPair(1, 3), IntPair(2, 3), IntPair(3, 3))); |
276 } | 355 } |
277 { | 356 { |
278 TreeWithStrangeCompare::value_type input_vals[] = {1, 1, 1, 2, 2, | 357 TreeWithStrangeCompare::value_type input_vals[] = {1, 1, 1, 2, 2, |
279 2, 3, 3, 3}; | 358 2, 3, 3, 3}; |
280 | 359 |
281 TreeWithStrangeCompare cont(MakeInputIterator(std::begin(input_vals)), | 360 TreeWithStrangeCompare cont(MakeInputIterator(std::begin(input_vals)), |
282 MakeInputIterator(std::end(input_vals)), | 361 MakeInputIterator(std::end(input_vals)), |
| 362 KEEP_FIRST_OF_DUPES, |
283 NonDefaultConstructibleCompare(0)); | 363 NonDefaultConstructibleCompare(0)); |
284 EXPECT_THAT(cont, ElementsAre(1, 2, 3)); | 364 EXPECT_THAT(cont, ElementsAre(1, 2, 3)); |
285 } | 365 } |
286 } | 366 } |
287 | 367 |
288 // flat_tree(const flat_tree& x) | 368 // flat_tree(const flat_tree& x) |
289 | 369 |
290 TEST(FlatTree, CopyConstructor) { | 370 TEST(FlatTree, CopyConstructor) { |
291 IntTree original{1, 2, 3, 4}; | 371 IntTree original({1, 2, 3, 4}, KEEP_FIRST_OF_DUPES); |
292 IntTree copied(original); | 372 IntTree copied(original); |
293 | 373 |
294 EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4)); | 374 EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4)); |
295 | 375 |
296 EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4)); | 376 EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4)); |
297 EXPECT_THAT(original, ElementsAre(1, 2, 3, 4)); | 377 EXPECT_THAT(original, ElementsAre(1, 2, 3, 4)); |
298 EXPECT_EQ(original, copied); | 378 EXPECT_EQ(original, copied); |
299 } | 379 } |
300 | 380 |
301 // flat_tree(flat_tree&& x) | 381 // flat_tree(flat_tree&& x) |
302 | 382 |
303 TEST(FlatTree, MoveConstructor) { | 383 TEST(FlatTree, MoveConstructor) { |
304 int input_range[] = {1, 2, 3, 4}; | 384 int input_range[] = {1, 2, 3, 4}; |
305 | 385 |
306 MoveOnlyTree original(std::begin(input_range), std::end(input_range)); | 386 MoveOnlyTree original(std::begin(input_range), std::end(input_range), |
| 387 KEEP_FIRST_OF_DUPES); |
307 MoveOnlyTree moved(std::move(original)); | 388 MoveOnlyTree moved(std::move(original)); |
308 | 389 |
309 EXPECT_EQ(1U, moved.count(MoveOnlyInt(1))); | 390 EXPECT_EQ(1U, moved.count(MoveOnlyInt(1))); |
310 EXPECT_EQ(1U, moved.count(MoveOnlyInt(2))); | 391 EXPECT_EQ(1U, moved.count(MoveOnlyInt(2))); |
311 EXPECT_EQ(1U, moved.count(MoveOnlyInt(3))); | 392 EXPECT_EQ(1U, moved.count(MoveOnlyInt(3))); |
312 EXPECT_EQ(1U, moved.count(MoveOnlyInt(4))); | 393 EXPECT_EQ(1U, moved.count(MoveOnlyInt(4))); |
313 } | 394 } |
314 | 395 |
315 // flat_tree(std::initializer_list<value_type> ilist, | 396 // flat_tree(std::vector<value_type>, FlatContainerDupes dupe_handling) |
| 397 |
| 398 TEST(FlatTree, VectorConstructor) { |
| 399 using Pair = std::pair<int, MoveOnlyInt>; |
| 400 |
| 401 // Construct an unsorted vector with a duplicate item in it. Sorted by the |
| 402 // first item, the second allows us to test for stability. Using a move |
| 403 // only type to ensure the vector is not copied. |
| 404 std::vector<Pair> storage; |
| 405 storage.push_back(Pair(2, MoveOnlyInt(0))); |
| 406 storage.push_back(Pair(1, MoveOnlyInt(0))); |
| 407 storage.push_back(Pair(2, MoveOnlyInt(1))); |
| 408 |
| 409 using Tree = |
| 410 flat_tree<Pair, Pair, GetKeyFromValueIdentity<Pair>, LessByFirst<Pair>>; |
| 411 Tree tree(std::move(storage), KEEP_FIRST_OF_DUPES); |
| 412 |
| 413 // The list should be two items long, with only the first "2" saved. |
| 414 ASSERT_EQ(2u, tree.size()); |
| 415 const Pair& zeroth = *tree.begin(); |
| 416 ASSERT_EQ(1, zeroth.first); |
| 417 ASSERT_EQ(0, zeroth.second.data()); |
| 418 |
| 419 const Pair& first = *(tree.begin() + 1); |
| 420 ASSERT_EQ(2, first.first); |
| 421 ASSERT_EQ(0, first.second.data()); |
| 422 |
| 423 // Test KEEP_LAST_OF_DUPES with a simple vector constructor. |
| 424 std::vector<IntPair> int_storage{{1, 1}, {1, 2}, {2, 1}}; |
| 425 IntPairTree int_tree(std::move(int_storage), KEEP_LAST_OF_DUPES); |
| 426 EXPECT_THAT(int_tree, ElementsAre(IntPair(1, 2), IntPair(2, 1))); |
| 427 } |
| 428 |
| 429 // flat_tree(std::initializer_list<value_type> ilist, |
| 430 // FlatContainerDupes dupe_handling, |
316 // const Compare& comp = Compare()) | 431 // const Compare& comp = Compare()) |
317 | 432 |
318 TEST(FlatTree, InitializerListConstructor) { | 433 TEST(FlatTree, InitializerListConstructor) { |
319 { | 434 { |
320 IntTree cont{1, 2, 3, 4, 5, 6, 10, 8}; | 435 IntTree cont({1, 2, 3, 4, 5, 6, 10, 8}, KEEP_FIRST_OF_DUPES); |
321 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10)); | 436 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10)); |
322 } | 437 } |
323 { | 438 { |
324 TreeWithStrangeCompare cont({1, 2, 3, 4, 5, 6, 10, 8}, | 439 IntTree cont({1, 2, 3, 4, 5, 6, 10, 8}, KEEP_FIRST_OF_DUPES); |
| 440 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10)); |
| 441 } |
| 442 { |
| 443 TreeWithStrangeCompare cont({1, 2, 3, 4, 5, 6, 10, 8}, KEEP_FIRST_OF_DUPES, |
325 NonDefaultConstructibleCompare(0)); | 444 NonDefaultConstructibleCompare(0)); |
326 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10)); | 445 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10)); |
327 } | 446 } |
| 447 { |
| 448 IntPairTree first_of({{1, 1}, {2, 1}, {1, 2}}, KEEP_FIRST_OF_DUPES); |
| 449 EXPECT_THAT(first_of, ElementsAre(IntPair(1, 1), IntPair(2, 1))); |
| 450 } |
| 451 { |
| 452 IntPairTree last_of({{1, 1}, {2, 1}, {1, 2}}, KEEP_LAST_OF_DUPES); |
| 453 EXPECT_THAT(last_of, ElementsAre(IntPair(1, 2), IntPair(2, 1))); |
| 454 } |
328 } | 455 } |
329 | 456 |
330 // ---------------------------------------------------------------------------- | 457 // ---------------------------------------------------------------------------- |
331 // Assignments. | 458 // Assignments. |
332 | 459 |
333 // flat_tree& operator=(const flat_tree&) | 460 // flat_tree& operator=(const flat_tree&) |
334 | 461 |
335 TEST(FlatTree, CopyAssignable) { | 462 TEST(FlatTree, CopyAssignable) { |
336 IntTree original{1, 2, 3, 4}; | 463 IntTree original({1, 2, 3, 4}, KEEP_FIRST_OF_DUPES); |
337 IntTree copied; | 464 IntTree copied; |
338 copied = original; | 465 copied = original; |
339 | 466 |
340 EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4)); | 467 EXPECT_THAT(copied, ElementsAre(1, 2, 3, 4)); |
341 EXPECT_THAT(original, ElementsAre(1, 2, 3, 4)); | 468 EXPECT_THAT(original, ElementsAre(1, 2, 3, 4)); |
342 EXPECT_EQ(original, copied); | 469 EXPECT_EQ(original, copied); |
343 } | 470 } |
344 | 471 |
345 // flat_tree& operator=(flat_tree&&) | 472 // flat_tree& operator=(flat_tree&&) |
346 | 473 |
347 TEST(FlatTree, MoveAssignable) { | 474 TEST(FlatTree, MoveAssignable) { |
348 int input_range[] = {1, 2, 3, 4}; | 475 int input_range[] = {1, 2, 3, 4}; |
349 | 476 |
350 MoveOnlyTree original(std::begin(input_range), std::end(input_range)); | 477 MoveOnlyTree original(std::begin(input_range), std::end(input_range), |
| 478 KEEP_FIRST_OF_DUPES); |
351 MoveOnlyTree moved; | 479 MoveOnlyTree moved; |
352 moved = std::move(original); | 480 moved = std::move(original); |
353 | 481 |
354 EXPECT_EQ(1U, moved.count(MoveOnlyInt(1))); | 482 EXPECT_EQ(1U, moved.count(MoveOnlyInt(1))); |
355 EXPECT_EQ(1U, moved.count(MoveOnlyInt(2))); | 483 EXPECT_EQ(1U, moved.count(MoveOnlyInt(2))); |
356 EXPECT_EQ(1U, moved.count(MoveOnlyInt(3))); | 484 EXPECT_EQ(1U, moved.count(MoveOnlyInt(3))); |
357 EXPECT_EQ(1U, moved.count(MoveOnlyInt(4))); | 485 EXPECT_EQ(1U, moved.count(MoveOnlyInt(4))); |
358 } | 486 } |
359 | 487 |
360 // flat_tree& operator=(std::initializer_list<value_type> ilist) | 488 // flat_tree& operator=(std::initializer_list<value_type> ilist) |
361 | 489 |
362 TEST(FlatTree, InitializerListAssignable) { | 490 TEST(FlatTree, InitializerListAssignable) { |
363 IntTree cont{0}; | 491 IntTree cont({0}, KEEP_FIRST_OF_DUPES); |
364 cont = {1, 2, 3, 4, 5, 6, 10, 8}; | 492 cont = {1, 2, 3, 4, 5, 6, 10, 8}; |
365 | 493 |
366 EXPECT_EQ(0U, cont.count(0)); | 494 EXPECT_EQ(0U, cont.count(0)); |
367 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10)); | 495 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 8, 10)); |
368 } | 496 } |
369 | 497 |
370 // -------------------------------------------------------------------------- | 498 // -------------------------------------------------------------------------- |
371 // Memory management. | 499 // Memory management. |
372 | 500 |
373 // void reserve(size_type new_capacity) | 501 // void reserve(size_type new_capacity) |
374 | 502 |
375 TEST(FlatTree, Reserve) { | 503 TEST(FlatTree, Reserve) { |
376 IntTree cont{1, 2, 3}; | 504 IntTree cont({1, 2, 3}, KEEP_FIRST_OF_DUPES); |
377 | 505 |
378 cont.reserve(5); | 506 cont.reserve(5); |
379 EXPECT_LE(5U, cont.capacity()); | 507 EXPECT_LE(5U, cont.capacity()); |
380 } | 508 } |
381 | 509 |
382 // size_type capacity() const | 510 // size_type capacity() const |
383 | 511 |
384 TEST(FlatTree, Capacity) { | 512 TEST(FlatTree, Capacity) { |
385 IntTree cont{1, 2, 3}; | 513 IntTree cont({1, 2, 3}, KEEP_FIRST_OF_DUPES); |
386 | 514 |
387 EXPECT_LE(cont.size(), cont.capacity()); | 515 EXPECT_LE(cont.size(), cont.capacity()); |
388 cont.reserve(5); | 516 cont.reserve(5); |
389 EXPECT_LE(cont.size(), cont.capacity()); | 517 EXPECT_LE(cont.size(), cont.capacity()); |
390 } | 518 } |
391 | 519 |
392 // void shrink_to_fit() | 520 // void shrink_to_fit() |
393 | 521 |
394 TEST(FlatTree, ShrinkToFit) { | 522 TEST(FlatTree, ShrinkToFit) { |
395 IntTree cont{1, 2, 3}; | 523 IntTree cont({1, 2, 3}, KEEP_FIRST_OF_DUPES); |
396 | 524 |
397 IntTree::size_type capacity_before = cont.capacity(); | 525 IntTree::size_type capacity_before = cont.capacity(); |
398 cont.shrink_to_fit(); | 526 cont.shrink_to_fit(); |
399 EXPECT_GE(capacity_before, cont.capacity()); | 527 EXPECT_GE(capacity_before, cont.capacity()); |
400 } | 528 } |
401 | 529 |
402 // ---------------------------------------------------------------------------- | 530 // ---------------------------------------------------------------------------- |
403 // Size management. | 531 // Size management. |
404 | 532 |
405 // void clear() | 533 // void clear() |
406 | 534 |
407 TEST(FlatTree, Clear) { | 535 TEST(FlatTree, Clear) { |
408 IntTree cont{1, 2, 3, 4, 5, 6, 7, 8}; | 536 IntTree cont({1, 2, 3, 4, 5, 6, 7, 8}, KEEP_FIRST_OF_DUPES); |
409 cont.clear(); | 537 cont.clear(); |
410 EXPECT_THAT(cont, ElementsAre()); | 538 EXPECT_THAT(cont, ElementsAre()); |
411 } | 539 } |
412 | 540 |
413 // size_type size() const | 541 // size_type size() const |
414 | 542 |
415 TEST(FlatTree, Size) { | 543 TEST(FlatTree, Size) { |
416 IntTree cont; | 544 IntTree cont; |
417 | 545 |
418 EXPECT_EQ(0U, cont.size()); | 546 EXPECT_EQ(0U, cont.size()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 // const_reverse_iterator rbegin() const | 582 // const_reverse_iterator rbegin() const |
455 // reverse_iterator rend() | 583 // reverse_iterator rend() |
456 // const_reverse_iterator rend() const | 584 // const_reverse_iterator rend() const |
457 // | 585 // |
458 // const_iterator cbegin() const | 586 // const_iterator cbegin() const |
459 // const_iterator cend() const | 587 // const_iterator cend() const |
460 // const_reverse_iterator crbegin() const | 588 // const_reverse_iterator crbegin() const |
461 // const_reverse_iterator crend() const | 589 // const_reverse_iterator crend() const |
462 | 590 |
463 TEST(FlatTree, Iterators) { | 591 TEST(FlatTree, Iterators) { |
464 IntTree cont{1, 2, 3, 4, 5, 6, 7, 8}; | 592 IntTree cont({1, 2, 3, 4, 5, 6, 7, 8}, KEEP_FIRST_OF_DUPES); |
465 | 593 |
466 auto size = static_cast<IntTree::difference_type>(cont.size()); | 594 auto size = static_cast<IntTree::difference_type>(cont.size()); |
467 | 595 |
468 EXPECT_EQ(size, std::distance(cont.begin(), cont.end())); | 596 EXPECT_EQ(size, std::distance(cont.begin(), cont.end())); |
469 EXPECT_EQ(size, std::distance(cont.cbegin(), cont.cend())); | 597 EXPECT_EQ(size, std::distance(cont.cbegin(), cont.cend())); |
470 EXPECT_EQ(size, std::distance(cont.rbegin(), cont.rend())); | 598 EXPECT_EQ(size, std::distance(cont.rbegin(), cont.rend())); |
471 EXPECT_EQ(size, std::distance(cont.crbegin(), cont.crend())); | 599 EXPECT_EQ(size, std::distance(cont.crbegin(), cont.crend())); |
472 | 600 |
473 { | 601 { |
474 IntTree::iterator it = cont.begin(); | 602 IntTree::iterator it = cont.begin(); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 EXPECT_EQ(2, *result); | 805 EXPECT_EQ(2, *result); |
678 } | 806 } |
679 } | 807 } |
680 | 808 |
681 // ---------------------------------------------------------------------------- | 809 // ---------------------------------------------------------------------------- |
682 // Erase operations. | 810 // Erase operations. |
683 | 811 |
684 // iterator erase(const_iterator position_hint) | 812 // iterator erase(const_iterator position_hint) |
685 | 813 |
686 TEST(FlatTree, ErasePosition) { | 814 TEST(FlatTree, ErasePosition) { |
687 IntTree cont{1, 2, 3, 4, 5, 6, 7, 8}; | 815 IntTree cont({1, 2, 3, 4, 5, 6, 7, 8}, KEEP_FIRST_OF_DUPES); |
688 | 816 |
689 IntTree::iterator it = cont.erase(std::next(cont.cbegin(), 3)); | 817 IntTree::iterator it = cont.erase(std::next(cont.cbegin(), 3)); |
690 EXPECT_EQ(std::next(cont.begin(), 3), it); | 818 EXPECT_EQ(std::next(cont.begin(), 3), it); |
691 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8)); | 819 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8)); |
692 | 820 |
693 it = cont.erase(std::next(cont.cbegin(), 0)); | 821 it = cont.erase(std::next(cont.cbegin(), 0)); |
694 EXPECT_EQ(cont.begin(), it); | 822 EXPECT_EQ(cont.begin(), it); |
695 EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7, 8)); | 823 EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7, 8)); |
696 | 824 |
697 it = cont.erase(std::next(cont.cbegin(), 5)); | 825 it = cont.erase(std::next(cont.cbegin(), 5)); |
(...skipping 17 matching lines...) Expand all Loading... |
715 EXPECT_THAT(cont, ElementsAre(5)); | 843 EXPECT_THAT(cont, ElementsAre(5)); |
716 | 844 |
717 it = cont.erase(cont.cbegin()); | 845 it = cont.erase(cont.cbegin()); |
718 EXPECT_EQ(cont.begin(), it); | 846 EXPECT_EQ(cont.begin(), it); |
719 EXPECT_EQ(cont.end(), it); | 847 EXPECT_EQ(cont.end(), it); |
720 } | 848 } |
721 | 849 |
722 // iterator erase(const_iterator first, const_iterator last) | 850 // iterator erase(const_iterator first, const_iterator last) |
723 | 851 |
724 TEST(FlatTree, EraseRange) { | 852 TEST(FlatTree, EraseRange) { |
725 IntTree cont{1, 2, 3, 4, 5, 6, 7, 8}; | 853 IntTree cont({1, 2, 3, 4, 5, 6, 7, 8}, KEEP_FIRST_OF_DUPES); |
726 | 854 |
727 IntTree::iterator it = | 855 IntTree::iterator it = |
728 cont.erase(std::next(cont.cbegin(), 5), std::next(cont.cbegin(), 5)); | 856 cont.erase(std::next(cont.cbegin(), 5), std::next(cont.cbegin(), 5)); |
729 EXPECT_EQ(std::next(cont.begin(), 5), it); | 857 EXPECT_EQ(std::next(cont.begin(), 5), it); |
730 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8)); | 858 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8)); |
731 | 859 |
732 it = cont.erase(std::next(cont.cbegin(), 3), std::next(cont.cbegin(), 4)); | 860 it = cont.erase(std::next(cont.cbegin(), 3), std::next(cont.cbegin(), 4)); |
733 EXPECT_EQ(std::next(cont.begin(), 3), it); | 861 EXPECT_EQ(std::next(cont.begin(), 3), it); |
734 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8)); | 862 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8)); |
735 | 863 |
736 it = cont.erase(std::next(cont.cbegin(), 2), std::next(cont.cbegin(), 5)); | 864 it = cont.erase(std::next(cont.cbegin(), 2), std::next(cont.cbegin(), 5)); |
737 EXPECT_EQ(std::next(cont.begin(), 2), it); | 865 EXPECT_EQ(std::next(cont.begin(), 2), it); |
738 EXPECT_THAT(cont, ElementsAre(1, 2, 7, 8)); | 866 EXPECT_THAT(cont, ElementsAre(1, 2, 7, 8)); |
739 | 867 |
740 it = cont.erase(std::next(cont.cbegin(), 0), std::next(cont.cbegin(), 2)); | 868 it = cont.erase(std::next(cont.cbegin(), 0), std::next(cont.cbegin(), 2)); |
741 EXPECT_EQ(std::next(cont.begin(), 0), it); | 869 EXPECT_EQ(std::next(cont.begin(), 0), it); |
742 EXPECT_THAT(cont, ElementsAre(7, 8)); | 870 EXPECT_THAT(cont, ElementsAre(7, 8)); |
743 | 871 |
744 it = cont.erase(cont.cbegin(), cont.cend()); | 872 it = cont.erase(cont.cbegin(), cont.cend()); |
745 EXPECT_EQ(cont.begin(), it); | 873 EXPECT_EQ(cont.begin(), it); |
746 EXPECT_EQ(cont.end(), it); | 874 EXPECT_EQ(cont.end(), it); |
747 } | 875 } |
748 | 876 |
749 // size_type erase(const key_type& key) | 877 // size_type erase(const key_type& key) |
750 | 878 |
751 TEST(FlatTree, EraseKey) { | 879 TEST(FlatTree, EraseKey) { |
752 IntTree cont{1, 2, 3, 4, 5, 6, 7, 8}; | 880 IntTree cont({1, 2, 3, 4, 5, 6, 7, 8}, KEEP_FIRST_OF_DUPES); |
753 | 881 |
754 EXPECT_EQ(0U, cont.erase(9)); | 882 EXPECT_EQ(0U, cont.erase(9)); |
755 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8)); | 883 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8)); |
756 | 884 |
757 EXPECT_EQ(1U, cont.erase(4)); | 885 EXPECT_EQ(1U, cont.erase(4)); |
758 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8)); | 886 EXPECT_THAT(cont, ElementsAre(1, 2, 3, 5, 6, 7, 8)); |
759 | 887 |
760 EXPECT_EQ(1U, cont.erase(1)); | 888 EXPECT_EQ(1U, cont.erase(1)); |
761 EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7, 8)); | 889 EXPECT_THAT(cont, ElementsAre(2, 3, 5, 6, 7, 8)); |
762 | 890 |
(...skipping 15 matching lines...) Expand all Loading... |
778 EXPECT_EQ(1U, cont.erase(5)); | 906 EXPECT_EQ(1U, cont.erase(5)); |
779 EXPECT_THAT(cont, ElementsAre()); | 907 EXPECT_THAT(cont, ElementsAre()); |
780 } | 908 } |
781 | 909 |
782 // ---------------------------------------------------------------------------- | 910 // ---------------------------------------------------------------------------- |
783 // Comparators. | 911 // Comparators. |
784 | 912 |
785 // key_compare key_comp() const | 913 // key_compare key_comp() const |
786 | 914 |
787 TEST(FlatTree, KeyComp) { | 915 TEST(FlatTree, KeyComp) { |
788 ReversedTree cont{1, 2, 3, 4, 5}; | 916 ReversedTree cont({1, 2, 3, 4, 5}, KEEP_FIRST_OF_DUPES); |
789 | 917 |
790 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp())); | 918 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp())); |
791 int new_elements[] = {6, 7, 8, 9, 10}; | 919 int new_elements[] = {6, 7, 8, 9, 10}; |
792 std::copy(std::begin(new_elements), std::end(new_elements), | 920 std::copy(std::begin(new_elements), std::end(new_elements), |
793 std::inserter(cont, cont.end())); | 921 std::inserter(cont, cont.end())); |
794 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp())); | 922 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.key_comp())); |
795 } | 923 } |
796 | 924 |
797 // value_compare value_comp() const | 925 // value_compare value_comp() const |
798 | 926 |
799 TEST(FlatTree, ValueComp) { | 927 TEST(FlatTree, ValueComp) { |
800 ReversedTree cont{1, 2, 3, 4, 5}; | 928 ReversedTree cont({1, 2, 3, 4, 5}, KEEP_FIRST_OF_DUPES); |
801 | 929 |
802 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp())); | 930 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp())); |
803 int new_elements[] = {6, 7, 8, 9, 10}; | 931 int new_elements[] = {6, 7, 8, 9, 10}; |
804 std::copy(std::begin(new_elements), std::end(new_elements), | 932 std::copy(std::begin(new_elements), std::end(new_elements), |
805 std::inserter(cont, cont.end())); | 933 std::inserter(cont, cont.end())); |
806 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp())); | 934 EXPECT_TRUE(std::is_sorted(cont.begin(), cont.end(), cont.value_comp())); |
807 } | 935 } |
808 | 936 |
809 // ---------------------------------------------------------------------------- | 937 // ---------------------------------------------------------------------------- |
810 // Search operations. | 938 // Search operations. |
811 | 939 |
812 // size_type count(const key_type& key) const | 940 // size_type count(const key_type& key) const |
813 | 941 |
814 TEST(FlatTree, Count) { | 942 TEST(FlatTree, Count) { |
815 { | 943 { |
816 const IntTree cont{5, 6, 7, 8, 9, 10, 11, 12}; | 944 const IntTree cont({5, 6, 7, 8, 9, 10, 11, 12}, KEEP_FIRST_OF_DUPES); |
817 | 945 |
818 EXPECT_EQ(1U, cont.count(5)); | 946 EXPECT_EQ(1U, cont.count(5)); |
819 EXPECT_EQ(1U, cont.count(6)); | 947 EXPECT_EQ(1U, cont.count(6)); |
820 EXPECT_EQ(1U, cont.count(7)); | 948 EXPECT_EQ(1U, cont.count(7)); |
821 EXPECT_EQ(1U, cont.count(8)); | 949 EXPECT_EQ(1U, cont.count(8)); |
822 EXPECT_EQ(1U, cont.count(9)); | 950 EXPECT_EQ(1U, cont.count(9)); |
823 EXPECT_EQ(1U, cont.count(10)); | 951 EXPECT_EQ(1U, cont.count(10)); |
824 EXPECT_EQ(1U, cont.count(11)); | 952 EXPECT_EQ(1U, cont.count(11)); |
825 EXPECT_EQ(1U, cont.count(12)); | 953 EXPECT_EQ(1U, cont.count(12)); |
826 EXPECT_EQ(0U, cont.count(4)); | 954 EXPECT_EQ(0U, cont.count(4)); |
827 } | 955 } |
828 { | 956 { |
829 const IntTreeWithLess cont{5, 6, 7, 8, 9, 10, 11, 12}; | 957 const IntTreeWithLess cont({5, 6, 7, 8, 9, 10, 11, 12}, |
| 958 KEEP_FIRST_OF_DUPES); |
830 | 959 |
831 EXPECT_EQ(1U, cont.count(5)); | 960 EXPECT_EQ(1U, cont.count(5)); |
832 EXPECT_EQ(1U, cont.count(6)); | 961 EXPECT_EQ(1U, cont.count(6)); |
833 EXPECT_EQ(1U, cont.count(7)); | 962 EXPECT_EQ(1U, cont.count(7)); |
834 EXPECT_EQ(1U, cont.count(8)); | 963 EXPECT_EQ(1U, cont.count(8)); |
835 EXPECT_EQ(1U, cont.count(9)); | 964 EXPECT_EQ(1U, cont.count(9)); |
836 EXPECT_EQ(1U, cont.count(10)); | 965 EXPECT_EQ(1U, cont.count(10)); |
837 EXPECT_EQ(1U, cont.count(11)); | 966 EXPECT_EQ(1U, cont.count(11)); |
838 EXPECT_EQ(1U, cont.count(12)); | 967 EXPECT_EQ(1U, cont.count(12)); |
839 EXPECT_EQ(0U, cont.count(4)); | 968 EXPECT_EQ(0U, cont.count(4)); |
840 } | 969 } |
841 } | 970 } |
842 | 971 |
843 // iterator find(const key_type& key) | 972 // iterator find(const key_type& key) |
844 // const_iterator find(const key_type& key) const | 973 // const_iterator find(const key_type& key) const |
845 | 974 |
846 TEST(FlatTree, Find) { | 975 TEST(FlatTree, Find) { |
847 { | 976 { |
848 IntTree cont{5, 6, 7, 8, 9, 10, 11, 12}; | 977 IntTree cont({5, 6, 7, 8, 9, 10, 11, 12}, KEEP_FIRST_OF_DUPES); |
849 | 978 |
850 EXPECT_EQ(cont.begin(), cont.find(5)); | 979 EXPECT_EQ(cont.begin(), cont.find(5)); |
851 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); | 980 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); |
852 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); | 981 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); |
853 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); | 982 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); |
854 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); | 983 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); |
855 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); | 984 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); |
856 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); | 985 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); |
857 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); | 986 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); |
858 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); | 987 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); |
859 } | 988 } |
860 { | 989 { |
861 const IntTree cont{5, 6, 7, 8, 9, 10, 11, 12}; | 990 const IntTree cont({5, 6, 7, 8, 9, 10, 11, 12}, KEEP_FIRST_OF_DUPES); |
862 | 991 |
863 EXPECT_EQ(cont.begin(), cont.find(5)); | 992 EXPECT_EQ(cont.begin(), cont.find(5)); |
864 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); | 993 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); |
865 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); | 994 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); |
866 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); | 995 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); |
867 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); | 996 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); |
868 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); | 997 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); |
869 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); | 998 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); |
870 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); | 999 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); |
871 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); | 1000 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); |
872 } | 1001 } |
873 { | 1002 { |
874 IntTreeWithLess cont{5, 6, 7, 8, 9, 10, 11, 12}; | 1003 IntTreeWithLess cont({5, 6, 7, 8, 9, 10, 11, 12}, KEEP_FIRST_OF_DUPES); |
875 | 1004 |
876 EXPECT_EQ(cont.begin(), cont.find(5)); | 1005 EXPECT_EQ(cont.begin(), cont.find(5)); |
877 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); | 1006 EXPECT_EQ(std::next(cont.begin()), cont.find(6)); |
878 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); | 1007 EXPECT_EQ(std::next(cont.begin(), 2), cont.find(7)); |
879 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); | 1008 EXPECT_EQ(std::next(cont.begin(), 3), cont.find(8)); |
880 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); | 1009 EXPECT_EQ(std::next(cont.begin(), 4), cont.find(9)); |
881 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); | 1010 EXPECT_EQ(std::next(cont.begin(), 5), cont.find(10)); |
882 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); | 1011 EXPECT_EQ(std::next(cont.begin(), 6), cont.find(11)); |
883 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); | 1012 EXPECT_EQ(std::next(cont.begin(), 7), cont.find(12)); |
884 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); | 1013 EXPECT_EQ(std::next(cont.begin(), 8), cont.find(4)); |
885 } | 1014 } |
886 } | 1015 } |
887 | 1016 |
888 // pair<iterator, iterator> equal_range(const key_type& key) | 1017 // pair<iterator, iterator> equal_range(const key_type& key) |
889 // pair<const_iterator, const_iterator> equal_range(const key_type& key) const | 1018 // pair<const_iterator, const_iterator> equal_range(const key_type& key) const |
890 | 1019 |
891 TEST(FlatTree, EqualRange) { | 1020 TEST(FlatTree, EqualRange) { |
892 { | 1021 { |
893 IntTree cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1022 IntTree cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
894 | 1023 |
895 std::pair<IntTree::iterator, IntTree::iterator> result = | 1024 std::pair<IntTree::iterator, IntTree::iterator> result = |
896 cont.equal_range(5); | 1025 cont.equal_range(5); |
897 EXPECT_EQ(std::next(cont.begin(), 0), result.first); | 1026 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
898 EXPECT_EQ(std::next(cont.begin(), 1), result.second); | 1027 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
899 result = cont.equal_range(7); | 1028 result = cont.equal_range(7); |
900 EXPECT_EQ(std::next(cont.begin(), 1), result.first); | 1029 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
901 EXPECT_EQ(std::next(cont.begin(), 2), result.second); | 1030 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
902 result = cont.equal_range(9); | 1031 result = cont.equal_range(9); |
903 EXPECT_EQ(std::next(cont.begin(), 2), result.first); | 1032 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 EXPECT_EQ(std::next(cont.begin(), 6), result.first); | 1068 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
940 EXPECT_EQ(std::next(cont.begin(), 6), result.second); | 1069 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
941 result = cont.equal_range(18); | 1070 result = cont.equal_range(18); |
942 EXPECT_EQ(std::next(cont.begin(), 7), result.first); | 1071 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
943 EXPECT_EQ(std::next(cont.begin(), 7), result.second); | 1072 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
944 result = cont.equal_range(20); | 1073 result = cont.equal_range(20); |
945 EXPECT_EQ(std::next(cont.begin(), 8), result.first); | 1074 EXPECT_EQ(std::next(cont.begin(), 8), result.first); |
946 EXPECT_EQ(std::next(cont.begin(), 8), result.second); | 1075 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
947 } | 1076 } |
948 { | 1077 { |
949 const IntTree cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1078 const IntTree cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
950 | 1079 |
951 std::pair<IntTree::const_iterator, IntTree::const_iterator> result = | 1080 std::pair<IntTree::const_iterator, IntTree::const_iterator> result = |
952 cont.equal_range(5); | 1081 cont.equal_range(5); |
953 EXPECT_EQ(std::next(cont.begin(), 0), result.first); | 1082 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
954 EXPECT_EQ(std::next(cont.begin(), 1), result.second); | 1083 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
955 result = cont.equal_range(7); | 1084 result = cont.equal_range(7); |
956 EXPECT_EQ(std::next(cont.begin(), 1), result.first); | 1085 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
957 EXPECT_EQ(std::next(cont.begin(), 2), result.second); | 1086 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
958 result = cont.equal_range(9); | 1087 result = cont.equal_range(9); |
959 EXPECT_EQ(std::next(cont.begin(), 2), result.first); | 1088 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
995 EXPECT_EQ(std::next(cont.begin(), 6), result.first); | 1124 EXPECT_EQ(std::next(cont.begin(), 6), result.first); |
996 EXPECT_EQ(std::next(cont.begin(), 6), result.second); | 1125 EXPECT_EQ(std::next(cont.begin(), 6), result.second); |
997 result = cont.equal_range(18); | 1126 result = cont.equal_range(18); |
998 EXPECT_EQ(std::next(cont.begin(), 7), result.first); | 1127 EXPECT_EQ(std::next(cont.begin(), 7), result.first); |
999 EXPECT_EQ(std::next(cont.begin(), 7), result.second); | 1128 EXPECT_EQ(std::next(cont.begin(), 7), result.second); |
1000 result = cont.equal_range(20); | 1129 result = cont.equal_range(20); |
1001 EXPECT_EQ(std::next(cont.begin(), 8), result.first); | 1130 EXPECT_EQ(std::next(cont.begin(), 8), result.first); |
1002 EXPECT_EQ(std::next(cont.begin(), 8), result.second); | 1131 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
1003 } | 1132 } |
1004 { | 1133 { |
1005 IntTreeWithLess cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1134 IntTreeWithLess cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
1006 | 1135 |
1007 std::pair<IntTree::iterator, IntTree::iterator> result = | 1136 std::pair<IntTree::iterator, IntTree::iterator> result = |
1008 cont.equal_range(5); | 1137 cont.equal_range(5); |
1009 EXPECT_EQ(std::next(cont.begin(), 0), result.first); | 1138 EXPECT_EQ(std::next(cont.begin(), 0), result.first); |
1010 EXPECT_EQ(std::next(cont.begin(), 1), result.second); | 1139 EXPECT_EQ(std::next(cont.begin(), 1), result.second); |
1011 result = cont.equal_range(7); | 1140 result = cont.equal_range(7); |
1012 EXPECT_EQ(std::next(cont.begin(), 1), result.first); | 1141 EXPECT_EQ(std::next(cont.begin(), 1), result.first); |
1013 EXPECT_EQ(std::next(cont.begin(), 2), result.second); | 1142 EXPECT_EQ(std::next(cont.begin(), 2), result.second); |
1014 result = cont.equal_range(9); | 1143 result = cont.equal_range(9); |
1015 EXPECT_EQ(std::next(cont.begin(), 2), result.first); | 1144 EXPECT_EQ(std::next(cont.begin(), 2), result.first); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 EXPECT_EQ(std::next(cont.begin(), 8), result.first); | 1186 EXPECT_EQ(std::next(cont.begin(), 8), result.first); |
1058 EXPECT_EQ(std::next(cont.begin(), 8), result.second); | 1187 EXPECT_EQ(std::next(cont.begin(), 8), result.second); |
1059 } | 1188 } |
1060 } | 1189 } |
1061 | 1190 |
1062 // iterator lower_bound(const key_type& key); | 1191 // iterator lower_bound(const key_type& key); |
1063 // const_iterator lower_bound(const key_type& key) const; | 1192 // const_iterator lower_bound(const key_type& key) const; |
1064 | 1193 |
1065 TEST(FlatTree, LowerBound) { | 1194 TEST(FlatTree, LowerBound) { |
1066 { | 1195 { |
1067 IntTree cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1196 IntTree cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
1068 | 1197 |
1069 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); | 1198 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); |
1070 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); | 1199 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); |
1071 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); | 1200 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); |
1072 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); | 1201 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); |
1073 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); | 1202 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); |
1074 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); | 1203 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); |
1075 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); | 1204 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); |
1076 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); | 1205 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); |
1077 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); | 1206 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); |
1078 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); | 1207 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); |
1079 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); | 1208 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); |
1080 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); | 1209 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); |
1081 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); | 1210 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); |
1082 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); | 1211 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); |
1083 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); | 1212 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); |
1084 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); | 1213 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); |
1085 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); | 1214 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); |
1086 } | 1215 } |
1087 { | 1216 { |
1088 const IntTree cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1217 const IntTree cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
1089 | 1218 |
1090 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); | 1219 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); |
1091 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); | 1220 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); |
1092 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); | 1221 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); |
1093 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); | 1222 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); |
1094 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); | 1223 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); |
1095 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); | 1224 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); |
1096 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); | 1225 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); |
1097 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); | 1226 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); |
1098 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); | 1227 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); |
1099 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); | 1228 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); |
1100 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); | 1229 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); |
1101 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); | 1230 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); |
1102 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); | 1231 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); |
1103 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); | 1232 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); |
1104 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); | 1233 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); |
1105 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); | 1234 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); |
1106 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); | 1235 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); |
1107 } | 1236 } |
1108 { | 1237 { |
1109 IntTreeWithLess cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1238 IntTreeWithLess cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
1110 | 1239 |
1111 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); | 1240 EXPECT_EQ(cont.begin(), cont.lower_bound(5)); |
1112 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); | 1241 EXPECT_EQ(std::next(cont.begin()), cont.lower_bound(7)); |
1113 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); | 1242 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(9)); |
1114 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); | 1243 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(11)); |
1115 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); | 1244 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(13)); |
1116 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); | 1245 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(15)); |
1117 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); | 1246 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(17)); |
1118 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); | 1247 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(19)); |
1119 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); | 1248 EXPECT_EQ(std::next(cont.begin(), 0), cont.lower_bound(4)); |
1120 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); | 1249 EXPECT_EQ(std::next(cont.begin(), 1), cont.lower_bound(6)); |
1121 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); | 1250 EXPECT_EQ(std::next(cont.begin(), 2), cont.lower_bound(8)); |
1122 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); | 1251 EXPECT_EQ(std::next(cont.begin(), 3), cont.lower_bound(10)); |
1123 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); | 1252 EXPECT_EQ(std::next(cont.begin(), 4), cont.lower_bound(12)); |
1124 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); | 1253 EXPECT_EQ(std::next(cont.begin(), 5), cont.lower_bound(14)); |
1125 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); | 1254 EXPECT_EQ(std::next(cont.begin(), 6), cont.lower_bound(16)); |
1126 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); | 1255 EXPECT_EQ(std::next(cont.begin(), 7), cont.lower_bound(18)); |
1127 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); | 1256 EXPECT_EQ(std::next(cont.begin(), 8), cont.lower_bound(20)); |
1128 } | 1257 } |
1129 } | 1258 } |
1130 | 1259 |
1131 // iterator upper_bound(const key_type& key) | 1260 // iterator upper_bound(const key_type& key) |
1132 // const_iterator upper_bound(const key_type& key) const | 1261 // const_iterator upper_bound(const key_type& key) const |
1133 | 1262 |
1134 TEST(FlatTree, UpperBound) { | 1263 TEST(FlatTree, UpperBound) { |
1135 { | 1264 { |
1136 IntTree cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1265 IntTree cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
1137 | 1266 |
1138 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); | 1267 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); |
1139 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); | 1268 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); |
1140 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); | 1269 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); |
1141 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); | 1270 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); |
1142 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); | 1271 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); |
1143 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); | 1272 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); |
1144 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); | 1273 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); |
1145 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); | 1274 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); |
1146 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); | 1275 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); |
1147 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); | 1276 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); |
1148 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); | 1277 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); |
1149 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); | 1278 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); |
1150 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); | 1279 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); |
1151 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); | 1280 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); |
1152 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); | 1281 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); |
1153 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); | 1282 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); |
1154 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); | 1283 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); |
1155 } | 1284 } |
1156 { | 1285 { |
1157 const IntTree cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1286 const IntTree cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
1158 | 1287 |
1159 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); | 1288 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); |
1160 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); | 1289 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); |
1161 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); | 1290 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); |
1162 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); | 1291 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); |
1163 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); | 1292 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); |
1164 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); | 1293 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); |
1165 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); | 1294 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); |
1166 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); | 1295 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); |
1167 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); | 1296 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); |
1168 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); | 1297 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); |
1169 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); | 1298 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); |
1170 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); | 1299 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); |
1171 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); | 1300 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); |
1172 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); | 1301 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); |
1173 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); | 1302 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); |
1174 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); | 1303 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); |
1175 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); | 1304 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); |
1176 } | 1305 } |
1177 { | 1306 { |
1178 IntTreeWithLess cont{5, 7, 9, 11, 13, 15, 17, 19}; | 1307 IntTreeWithLess cont({5, 7, 9, 11, 13, 15, 17, 19}, KEEP_FIRST_OF_DUPES); |
1179 | 1308 |
1180 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); | 1309 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(5)); |
1181 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); | 1310 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(7)); |
1182 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); | 1311 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(9)); |
1183 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); | 1312 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(11)); |
1184 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); | 1313 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(13)); |
1185 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); | 1314 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(15)); |
1186 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); | 1315 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(17)); |
1187 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); | 1316 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(19)); |
1188 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); | 1317 EXPECT_EQ(std::next(cont.begin(), 0), cont.upper_bound(4)); |
1189 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); | 1318 EXPECT_EQ(std::next(cont.begin(), 1), cont.upper_bound(6)); |
1190 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); | 1319 EXPECT_EQ(std::next(cont.begin(), 2), cont.upper_bound(8)); |
1191 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); | 1320 EXPECT_EQ(std::next(cont.begin(), 3), cont.upper_bound(10)); |
1192 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); | 1321 EXPECT_EQ(std::next(cont.begin(), 4), cont.upper_bound(12)); |
1193 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); | 1322 EXPECT_EQ(std::next(cont.begin(), 5), cont.upper_bound(14)); |
1194 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); | 1323 EXPECT_EQ(std::next(cont.begin(), 6), cont.upper_bound(16)); |
1195 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); | 1324 EXPECT_EQ(std::next(cont.begin(), 7), cont.upper_bound(18)); |
1196 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); | 1325 EXPECT_EQ(std::next(cont.begin(), 8), cont.upper_bound(20)); |
1197 } | 1326 } |
1198 } | 1327 } |
1199 | 1328 |
1200 // ---------------------------------------------------------------------------- | 1329 // ---------------------------------------------------------------------------- |
1201 // General operations. | 1330 // General operations. |
1202 | 1331 |
1203 // void swap(flat_tree& other) | 1332 // void swap(flat_tree& other) |
1204 // void swap(flat_tree& lhs, flat_tree& rhs) | 1333 // void swap(flat_tree& lhs, flat_tree& rhs) |
1205 | 1334 |
1206 TEST(FlatTreeOurs, Swap) { | 1335 TEST(FlatTreeOurs, Swap) { |
1207 IntTree x{1, 2, 3}; | 1336 IntTree x({1, 2, 3}, KEEP_FIRST_OF_DUPES); |
1208 IntTree y{4}; | 1337 IntTree y({4}, KEEP_FIRST_OF_DUPES); |
1209 swap(x, y); | 1338 swap(x, y); |
1210 EXPECT_THAT(x, ElementsAre(4)); | 1339 EXPECT_THAT(x, ElementsAre(4)); |
1211 EXPECT_THAT(y, ElementsAre(1, 2, 3)); | 1340 EXPECT_THAT(y, ElementsAre(1, 2, 3)); |
1212 | 1341 |
1213 y.swap(x); | 1342 y.swap(x); |
1214 EXPECT_THAT(x, ElementsAre(1, 2, 3)); | 1343 EXPECT_THAT(x, ElementsAre(1, 2, 3)); |
1215 EXPECT_THAT(y, ElementsAre(4)); | 1344 EXPECT_THAT(y, ElementsAre(4)); |
1216 } | 1345 } |
1217 | 1346 |
1218 // bool operator==(const flat_tree& lhs, const flat_tree& rhs) | 1347 // bool operator==(const flat_tree& lhs, const flat_tree& rhs) |
1219 // bool operator!=(const flat_tree& lhs, const flat_tree& rhs) | 1348 // bool operator!=(const flat_tree& lhs, const flat_tree& rhs) |
1220 // bool operator<(const flat_tree& lhs, const flat_tree& rhs) | 1349 // bool operator<(const flat_tree& lhs, const flat_tree& rhs) |
1221 // bool operator>(const flat_tree& lhs, const flat_tree& rhs) | 1350 // bool operator>(const flat_tree& lhs, const flat_tree& rhs) |
1222 // bool operator<=(const flat_tree& lhs, const flat_tree& rhs) | 1351 // bool operator<=(const flat_tree& lhs, const flat_tree& rhs) |
1223 // bool operator>=(const flat_tree& lhs, const flat_tree& rhs) | 1352 // bool operator>=(const flat_tree& lhs, const flat_tree& rhs) |
1224 | 1353 |
1225 TEST(FlatTree, Comparison) { | 1354 TEST(FlatTree, Comparison) { |
1226 // Provided comparator does not participate in comparison. | 1355 // Provided comparator does not participate in comparison. |
1227 ReversedTree biggest{3}; | 1356 ReversedTree biggest({3}, KEEP_FIRST_OF_DUPES); |
1228 ReversedTree smallest{1}; | 1357 ReversedTree smallest({1}, KEEP_FIRST_OF_DUPES); |
1229 ReversedTree middle{1, 2}; | 1358 ReversedTree middle({1, 2}, KEEP_FIRST_OF_DUPES); |
1230 | 1359 |
1231 EXPECT_EQ(biggest, biggest); | 1360 EXPECT_EQ(biggest, biggest); |
1232 EXPECT_NE(biggest, smallest); | 1361 EXPECT_NE(biggest, smallest); |
1233 EXPECT_LT(smallest, middle); | 1362 EXPECT_LT(smallest, middle); |
1234 EXPECT_LE(smallest, middle); | 1363 EXPECT_LE(smallest, middle); |
1235 EXPECT_LE(middle, middle); | 1364 EXPECT_LE(middle, middle); |
1236 EXPECT_GT(biggest, middle); | 1365 EXPECT_GT(biggest, middle); |
1237 EXPECT_GE(biggest, middle); | 1366 EXPECT_GE(biggest, middle); |
1238 EXPECT_GE(biggest, biggest); | 1367 EXPECT_GE(biggest, biggest); |
1239 } | 1368 } |
1240 | 1369 |
1241 TEST(FlatSet, EraseIf) { | 1370 TEST(FlatSet, EraseIf) { |
1242 IntTree x; | 1371 IntTree x; |
1243 EraseIf(x, [](int) { return false; }); | 1372 EraseIf(x, [](int) { return false; }); |
1244 EXPECT_THAT(x, ElementsAre()); | 1373 EXPECT_THAT(x, ElementsAre()); |
1245 | 1374 |
1246 x = {1, 2, 3}; | 1375 x = {1, 2, 3}; |
1247 EraseIf(x, [](int elem) { return !(elem & 1); }); | 1376 EraseIf(x, [](int elem) { return !(elem & 1); }); |
1248 EXPECT_THAT(x, ElementsAre(1, 3)); | 1377 EXPECT_THAT(x, ElementsAre(1, 3)); |
1249 | 1378 |
1250 x = {1, 2, 3, 4}; | 1379 x = {1, 2, 3, 4}; |
1251 EraseIf(x, [](int elem) { return elem & 1; }); | 1380 EraseIf(x, [](int elem) { return elem & 1; }); |
1252 EXPECT_THAT(x, ElementsAre(2, 4)); | 1381 EXPECT_THAT(x, ElementsAre(2, 4)); |
1253 } | 1382 } |
1254 | 1383 |
1255 } // namespace internal | 1384 } // namespace internal |
1256 } // namespace base | 1385 } // namespace base |
OLD | NEW |