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

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

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

Powered by Google App Engine
This is Rietveld 408576698