OLD | NEW |
---|---|
(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, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2), | |
43 std::make_pair(3, 3))); | |
44 } | |
45 | |
46 TEST(FlatMap, MoveConstructor) { | |
47 using pair = std::pair<MoveOnlyInt, MoveOnlyInt>; | |
48 | |
49 flat_map<MoveOnlyInt, MoveOnlyInt> original; | |
50 original.insert(pair(MoveOnlyInt(1), MoveOnlyInt(1))); | |
51 original.insert(pair(MoveOnlyInt(2), MoveOnlyInt(2))); | |
52 original.insert(pair(MoveOnlyInt(3), MoveOnlyInt(3))); | |
53 original.insert(pair(MoveOnlyInt(4), MoveOnlyInt(4))); | |
54 | |
55 flat_map<MoveOnlyInt, MoveOnlyInt> moved(std::move(original)); | |
56 | |
57 EXPECT_EQ(1U, moved.count(MoveOnlyInt(1))); | |
58 EXPECT_EQ(1U, moved.count(MoveOnlyInt(2))); | |
59 EXPECT_EQ(1U, moved.count(MoveOnlyInt(3))); | |
60 EXPECT_EQ(1U, moved.count(MoveOnlyInt(4))); | |
61 } | |
62 | |
63 TEST(FlatMap, InitializerListConstructor) { | |
64 flat_map<int, int> cont{{1, 1}, {2, 2}, {3, 3}, {4, 4}, | |
65 {5, 5}, {6, 6}, {10, 10}, {8, 8}}; | |
66 EXPECT_THAT(cont, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2), | |
67 std::make_pair(3, 3), std::make_pair(4, 4), | |
68 std::make_pair(5, 5), std::make_pair(6, 6), | |
69 std::make_pair(8, 8), std::make_pair(10, 10))); | |
70 } | |
71 | |
72 TEST(FlatMap, InsertFindSize) { | |
73 base::flat_map<int, int> s; | |
74 s.insert(std::make_pair(1, 1)); | |
75 s.insert(std::make_pair(1, 1)); | |
76 s.insert(std::make_pair(2, 2)); | |
77 | |
78 EXPECT_EQ(2u, s.size()); | |
79 EXPECT_EQ(std::make_pair(1, 1), *s.find(1)); | |
80 EXPECT_EQ(std::make_pair(2, 2), *s.find(2)); | |
81 EXPECT_EQ(s.end(), s.find(7)); | |
82 } | |
83 | |
84 TEST(FlatMap, CopySwap) { | |
85 base::flat_map<int, int> original; | |
86 original.insert({1, 1}); | |
87 original.insert({2, 2}); | |
88 EXPECT_THAT(original, | |
89 ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2))); | |
90 | |
91 base::flat_map<int, int> copy(original); | |
92 EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2))); | |
93 | |
94 copy.erase(copy.begin()); | |
95 copy.insert({10, 10}); | |
96 EXPECT_THAT(copy, ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10))); | |
97 | |
98 original.swap(copy); | |
99 EXPECT_THAT(original, | |
100 ElementsAre(std::make_pair(2, 2), std::make_pair(10, 10))); | |
101 EXPECT_THAT(copy, ElementsAre(std::make_pair(1, 1), std::make_pair(2, 2))); | |
102 } | |
103 | |
104 // operator[](const Key&) | |
105 TEST(FlatMap, SubscriptConstKey) { | |
106 base::flat_map<std::string, int> m; | |
107 | |
108 // Default construct elements that don't exist yet. | |
109 int& s = m["a"]; | |
110 EXPECT_EQ(0, s); | |
111 EXPECT_EQ(1u, m.size()); | |
112 | |
113 // The returned mapped reference should refer into the map. | |
114 s = 22; | |
115 EXPECT_EQ(22, m["a"]); | |
116 | |
117 // Overwrite existing elements. | |
118 m["a"] = 44; | |
119 EXPECT_EQ(44, m["a"]); | |
120 } | |
121 | |
122 // operator[](Key&&) | |
123 TEST(FlatMap, SubscriptMoveOnlyKey) { | |
124 base::flat_map<MoveOnlyInt, int> m; | |
125 | |
126 // Default construct elements that don't exist yet. | |
127 int& s = m[MoveOnlyInt(1)]; | |
128 EXPECT_EQ(0, s); | |
129 EXPECT_EQ(1u, m.size()); | |
130 | |
131 // The returned mapped reference should refer into the map. | |
132 s = 22; | |
133 EXPECT_EQ(22, m[MoveOnlyInt(1)]); | |
134 | |
135 // Overwrite existing elements. | |
136 m[MoveOnlyInt(1)] = 44; | |
137 EXPECT_EQ(44, m[MoveOnlyInt(1)]); | |
138 } | |
dyaroshev
2017/03/21 16:03:57
Tests for at?
brettw
2017/03/21 18:32:51
Yeah, thanks. I actually messed up at() in multipl
| |
139 | |
140 } // namespace base | |
OLD | NEW |