Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: base/containers/flat_tree_unittest.cc

Issue 2853333004: Add range insertion for base::flat_tree (Closed)
Patch Set: Rename prev_end to middle and check for sortedness Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/containers/flat_tree.h ('K') | « base/containers/flat_tree.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 13 matching lines...) Expand all
24 // * No tests with DefaultOnly. 24 // * No tests with DefaultOnly.
25 // Standard containers allocate each element in the separate node on the heap 25 // Standard containers allocate each element in the separate node on the heap
26 // and then manipulate these nodes. Flat containers store their elements in 26 // and then manipulate these nodes. Flat containers store their elements in
27 // contiguous memory and move them around, type is required to be movable. 27 // contiguous memory and move them around, type is required to be movable.
28 // * No tests for N3644. 28 // * No tests for N3644.
29 // This proposal suggests that all default constructed iterators compare 29 // This proposal suggests that all default constructed iterators compare
30 // equal. Currently we use std::vector iterators and they don't implement 30 // equal. Currently we use std::vector iterators and they don't implement
31 // this. 31 // this.
32 // * No tests with min_allocator and no tests counting allocations. 32 // * No tests with min_allocator and no tests counting allocations.
33 // Flat sets currently don't support allocators. 33 // Flat sets currently don't support allocators.
34 // * No tests for range insertion. Flat sets currently do not support this
35 // functionality.
36 34
37 #include <string> 35 #include <string>
38 #include <vector> 36 #include <vector>
39 37
40 #include "base/containers/container_test_utils.h" 38 #include "base/containers/container_test_utils.h"
41 #include "base/macros.h" 39 #include "base/macros.h"
42 #include "testing/gmock/include/gmock/gmock.h" 40 #include "testing/gmock/include/gmock/gmock.h"
43 #include "testing/gtest/include/gtest/gtest.h" 41 #include "testing/gtest/include/gtest/gtest.h"
44 42
45 namespace base { 43 namespace base {
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 EXPECT_EQ(std::prev(cont.end()), result); 729 EXPECT_EQ(std::prev(cont.end()), result);
732 EXPECT_EQ(3U, cont.size()); 730 EXPECT_EQ(3U, cont.size());
733 EXPECT_EQ(3, result->data()); 731 EXPECT_EQ(3, result->data());
734 732
735 result = cont.insert(cont.cend(), MoveOnlyInt(3)); 733 result = cont.insert(cont.cend(), MoveOnlyInt(3));
736 EXPECT_EQ(std::prev(cont.end()), result); 734 EXPECT_EQ(std::prev(cont.end()), result);
737 EXPECT_EQ(3U, cont.size()); 735 EXPECT_EQ(3U, cont.size());
738 EXPECT_EQ(3, result->data()); 736 EXPECT_EQ(3, result->data());
739 } 737 }
740 738
739 // template <class InputIterator>
740 // void insert(InputIterator first, InputIterator last);
741
742 TEST(FlatTree, InsertIterIter) {
743 struct GetKeyFromIntIntPair {
744 int operator()(const std::pair<int, int>& p) const { return p.first; }
745 };
746
747 using IntIntMap =
748 flat_tree<int, IntPair, GetKeyFromIntIntPair, std::less<int>>;
749 IntIntMap cont;
750
751 {
752 IntPair int_pairs[] = {{3, 1}, {1, 1}, {4, 1}, {2, 1}};
753 cont.insert(std::begin(int_pairs), std::end(int_pairs));
754 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
755 IntPair(4, 1)));
756 }
757
758 {
759 IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}};
760 cont.insert(std::begin(int_pairs), std::end(int_pairs));
761 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
762 IntPair(4, 1)));
763 }
764
765 {
766 IntPair int_pairs[] = {{3, 2}, {1, 2}, {4, 2}, {2, 2}, {7, 2}, {6, 2},
767 {8, 2}, {5, 2}, {5, 3}, {6, 3}, {7, 3}, {8, 3}};
768 cont.insert(std::begin(int_pairs), std::end(int_pairs));
769 EXPECT_THAT(cont, ElementsAre(IntPair(1, 1), IntPair(2, 1), IntPair(3, 1),
770 IntPair(4, 1), IntPair(5, 2), IntPair(6, 2),
771 IntPair(7, 2), IntPair(8, 2)));
772 }
dyaroshev 2017/05/09 09:32:46 An empty range check is important. Maybe we want
jdoerrie 2017/05/09 17:03:30 Done.
773 }
774
741 // template <class... Args> 775 // template <class... Args>
742 // pair<iterator, bool> emplace(Args&&... args) 776 // pair<iterator, bool> emplace(Args&&... args)
743 777
744 TEST(FlatTree, Emplace) { 778 TEST(FlatTree, Emplace) {
745 { 779 {
746 EmplaceableTree cont; 780 EmplaceableTree cont;
747 781
748 std::pair<EmplaceableTree::iterator, bool> result = cont.emplace(); 782 std::pair<EmplaceableTree::iterator, bool> result = cont.emplace();
749 EXPECT_TRUE(result.second); 783 EXPECT_TRUE(result.second);
750 EXPECT_EQ(cont.begin(), result.first); 784 EXPECT_EQ(cont.begin(), result.first);
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after
1376 EraseIf(x, [](int elem) { return !(elem & 1); }); 1410 EraseIf(x, [](int elem) { return !(elem & 1); });
1377 EXPECT_THAT(x, ElementsAre(1, 3)); 1411 EXPECT_THAT(x, ElementsAre(1, 3));
1378 1412
1379 x = {1, 2, 3, 4}; 1413 x = {1, 2, 3, 4};
1380 EraseIf(x, [](int elem) { return elem & 1; }); 1414 EraseIf(x, [](int elem) { return elem & 1; });
1381 EXPECT_THAT(x, ElementsAre(2, 4)); 1415 EXPECT_THAT(x, ElementsAre(2, 4));
1382 } 1416 }
1383 1417
1384 } // namespace internal 1418 } // namespace internal
1385 } // namespace base 1419 } // namespace base
OLDNEW
« base/containers/flat_tree.h ('K') | « base/containers/flat_tree.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698