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

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

Issue 2715433007: Add a flat_map container (Closed)
Patch Set: Fix Created 3 years, 10 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/containers/flat_map.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/macros.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 // A flat_map is basically a interface to flat_tree. So several basic
15 // operations are tested to make sure things are set up properly, but the bulk
16 // of the tests are in flat_tree_unittests.cc.
17
18 using ::testing::ElementsAre;
19
20 namespace base {
21
22 namespace {
23
24 class MoveOnly {
dyaroshev 2017/02/24 22:55:49 Can we maybe move it to some common location? Or,
25 public:
26 explicit MoveOnly(int data = 1) : data_(data) {}
27 MoveOnly(MoveOnly&& other) : data_(other.data_) { other.data_ = 0; }
28 MoveOnly& operator=(MoveOnly&& other) {
29 data_ = other.data_;
30 other.data_ = 0;
31 return *this;
32 }
33
34 friend bool operator<(const MoveOnly& lhs, const MoveOnly& rhs) {
35 return lhs.data_ < rhs.data_;
36 }
37
38 int data() const { return data_; }
39
40 private:
41 int data_;
42
43 DISALLOW_COPY_AND_ASSIGN(MoveOnly);
44 };
45
46 } // namespace
47
48 TEST(FlatMap, IncompleteType) {
49 struct A {
50 using Map = flat_map<A, A>;
51 int data;
52 Map set_with_incomplete_type;
53 Map::iterator it;
54 Map::const_iterator cit;
55
56 // We do not declare operator< because clang complains that it's unused.
57 };
58
59 A a;
60 }
61
62 TEST(FlatMap, RangeConstructor) {
63 flat_map<int, int>::value_type input_vals[] = {
64 {1, 1}, {1, 1}, {1, 1}, {2, 2}, {2, 2}, {2, 2}, {3, 3}, {3, 3}, {3, 3}};
65
66 flat_map<int, int> cont(std::begin(input_vals), std::end(input_vals));
67 EXPECT_THAT(cont,
68 ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2),
69 std::make_pair(3, 3)));
70 }
71
72 TEST(FlatMap, MoveConstructor) {
73 std::pair<int, int> input_range[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
74
75 flat_map<MoveOnly, MoveOnly> original(std::begin(input_range),
76 std::end(input_range));
77 flat_map<MoveOnly, MoveOnly> moved(std::move(original));
78
79 EXPECT_EQ(1U, moved.count(MoveOnly(1)));
80 EXPECT_EQ(1U, moved.count(MoveOnly(2)));
81 EXPECT_EQ(1U, moved.count(MoveOnly(3)));
82 EXPECT_EQ(1U, moved.count(MoveOnly(4)));
83 }
84
85 TEST(FlatMap, InitializerListConstructor) {
86 flat_map<int, int> cont{{1, 1}, {2, 2}, {3, 3}, {4, 4},
87 {5, 5}, {6, 6}, {10, 10}, {8, 8}};
88 EXPECT_THAT(cont,
89 ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2),
90 std::make_pair(3, 3), std::make_pair(4, 4),
91 std::make_pair(5, 5), std::make_pair(6, 6),
92 std::make_pair(8, 8), std::make_pair(10, 10)));
93 }
94
95 TEST(FlatMap, InsertFindSize) {
96 base::flat_map<int, int> s;
97 s.insert(std::make_pair(1, 1));
98 s.insert(std::make_pair(1, 1));
99 s.insert(std::make_pair(2, 2));
100
101 EXPECT_EQ(2u, s.size());
102 EXPECT_EQ(std::make_pair(1, 1), *s.find(1));
103 EXPECT_EQ(std::make_pair(2, 2), *s.find(2));
104 EXPECT_EQ(s.end(), s.find(7));
105 }
106
107 TEST(FlatMap, CopySwap) {
108 base::flat_map<int, int> original;
109 original.insert({1, 1});
110 original.insert({2, 2});
111 EXPECT_THAT(original,
112 ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
113
114 base::flat_map<int, int> copy(original);
115 EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
116
117 copy.erase(copy.begin());
118 copy.insert({10, 10});
119 EXPECT_THAT(copy, ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));
120
121 original.swap(copy);
122 EXPECT_THAT(original,
123 ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));
124 EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
125 }
126
127 // operator[](const Key&)
128 TEST(FlatMap, SubscriptConstKey) {
129 base::flat_map<std::string, int> m;
130
131 // Default construct elements that don't exist yet.
132 int& s = m["a"];
133 EXPECT_EQ(0, s);
134 EXPECT_EQ(1u, m.size());
135
136 // The returned mapped reference should refer into the map.
137 s = 22;
138 EXPECT_EQ(22, m["a"]);
139
140 // Overwrite existing elements.
141 m["a"] = 44;
142 EXPECT_EQ(44, m["a"]);
143 }
144
145 // operator[](Key&&)
146 TEST(FlatMap, SubscriptMoveOnlyKey) {
147 base::flat_map<MoveOnly, int> m;
148
149 // Default construct elements that don't exist yet.
150 int& s = m[MoveOnly(1)];
151 EXPECT_EQ(0, s);
152 EXPECT_EQ(1u, m.size());
153
154 // The returned mapped reference should refer into the map.
155 s = 22;
156 EXPECT_EQ(22, m[MoveOnly(1)]);
157
158 // Overwrite existing elements.
159 m[MoveOnly(1)] = 44;
160 EXPECT_EQ(44, m[MoveOnly(1)]);
161 }
162
163 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698