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

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

Issue 2715433007: Add a flat_map container (Closed)
Patch Set: New 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 {
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, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2),
68 std::make_pair(3, 3)));
69 }
70
71 TEST(FlatMap, MoveConstructor) {
72 std::pair<int, int> input_range[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
73
74 flat_map<MoveOnly, MoveOnly> original(std::begin(input_range),
75 std::end(input_range));
76 flat_map<MoveOnly, MoveOnly> moved(std::move(original));
77
78 EXPECT_EQ(1U, moved.count(MoveOnly(1)));
79 EXPECT_EQ(1U, moved.count(MoveOnly(2)));
80 EXPECT_EQ(1U, moved.count(MoveOnly(3)));
81 EXPECT_EQ(1U, moved.count(MoveOnly(4)));
82 }
83
84 TEST(FlatMap, InitializerListConstructor) {
85 flat_map<int, int> cont{{1, 1}, {2, 2}, {3, 3}, {4, 4},
86 {5, 5}, {6, 6}, {10, 10}, {8, 8}};
87 EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2),
88 std::make_pair(3, 3), std::make_pair(4, 4),
89 std::make_pair(5, 5), std::make_pair(6, 6),
90 std::make_pair(8, 8), std::make_pair(10, 10)));
91 }
92
93 TEST(FlatMap, InsertFindSize) {
94 base::flat_map<int, int> s;
95 s.insert(std::make_pair(1, 1));
96 s.insert(std::make_pair(1, 1));
97 s.insert(std::make_pair(2, 2));
98
99 EXPECT_EQ(2u, s.size());
100 EXPECT_EQ(std::make_pair(1, 1), *s.find(1));
101 EXPECT_EQ(std::make_pair(2, 2), *s.find(2));
102 EXPECT_EQ(s.end(), s.find(7));
103 }
104
105 TEST(FlatMap, CopySwap) {
106 base::flat_map<int, int> original;
107 original.insert({1, 1});
108 original.insert({2, 2});
109 EXPECT_THAT(original,
110 ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
111
112 base::flat_map<int, int> copy(original);
113 EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
114
115 copy.erase(copy.begin());
116 copy.insert({10, 10});
117 EXPECT_THAT(copy, ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));
118
119 original.swap(copy);
120 EXPECT_THAT(original,
121 ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10)));
122 EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2)));
123 }
124
125 // operator[](const Key&)
126 TEST(FlatMap, SubscriptConstKey) {
127 base::flat_map<std::string, int> m;
128
129 // Default construct elements that don't exist yet.
130 int& s = m["a"];
131 EXPECT_EQ(0, s);
132 EXPECT_EQ(1u, m.size());
133
134 // The returned mapped reference should refer into the map.
135 s = 22;
136 EXPECT_EQ(22, m["a"]);
137
138 // Overwrite existing elements.
139 m["a"] = 44;
140 EXPECT_EQ(44, m["a"]);
141 }
142
143 // operator[](Key&&)
144 TEST(FlatMap, SubscriptMoveOnlyKey) {
145 base::flat_map<MoveOnly, int> m;
146
147 // Default construct elements that don't exist yet.
148 int& s = m[MoveOnly(1)];
149 EXPECT_EQ(0, s);
150 EXPECT_EQ(1u, m.size());
151
152 // The returned mapped reference should refer into the map.
153 s = 22;
154 EXPECT_EQ(22, m[MoveOnly(1)]);
155
156 // Overwrite existing elements.
157 m[MoveOnly(1)] = 44;
158 EXPECT_EQ(44, m[MoveOnly(1)]);
159 }
160
161 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698