OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/stl_util.h" | 5 #include "base/stl_util.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 int value_; | 28 int value_; |
29 }; | 29 }; |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 namespace base { | 33 namespace base { |
34 namespace { | 34 namespace { |
35 | 35 |
36 TEST(STLUtilTest, STLIsSorted) { | 36 TEST(STLUtilTest, STLIsSorted) { |
37 { | 37 { |
38 std::set<int> set; | 38 std::set<int> set = {24, 1, 12}; |
39 set.insert(24); | |
40 set.insert(1); | |
41 set.insert(12); | |
42 EXPECT_TRUE(STLIsSorted(set)); | 39 EXPECT_TRUE(STLIsSorted(set)); |
43 } | 40 } |
44 | 41 |
45 { | 42 { |
46 std::set<ComparableValue> set; | 43 std::set<ComparableValue> set = {ComparableValue(24), ComparableValue(1), |
47 set.insert(ComparableValue(24)); | 44 ComparableValue(12)}; |
48 set.insert(ComparableValue(1)); | |
49 set.insert(ComparableValue(12)); | |
50 EXPECT_TRUE(STLIsSorted(set)); | 45 EXPECT_TRUE(STLIsSorted(set)); |
51 } | 46 } |
52 | 47 |
53 { | 48 { |
54 std::vector<int> vector; | 49 std::vector<int> vector = {1, 1, 4, 64, 12432}; |
55 vector.push_back(1); | |
56 vector.push_back(1); | |
57 vector.push_back(4); | |
58 vector.push_back(64); | |
59 vector.push_back(12432); | |
60 EXPECT_TRUE(STLIsSorted(vector)); | 50 EXPECT_TRUE(STLIsSorted(vector)); |
61 vector.back() = 1; | 51 vector.back() = 1; |
62 EXPECT_FALSE(STLIsSorted(vector)); | 52 EXPECT_FALSE(STLIsSorted(vector)); |
63 } | 53 } |
64 } | 54 } |
65 | 55 |
66 TEST(STLUtilTest, STLSetDifference) { | 56 TEST(STLUtilTest, STLSetDifference) { |
67 std::set<int> a1; | 57 std::set<int> a1 = {1, 2, 3, 4}; |
68 a1.insert(1); | 58 std::set<int> a2 = {3, 4, 5, 6, 7}; |
69 a1.insert(2); | |
70 a1.insert(3); | |
71 a1.insert(4); | |
72 | |
73 std::set<int> a2; | |
74 a2.insert(3); | |
75 a2.insert(4); | |
76 a2.insert(5); | |
77 a2.insert(6); | |
78 a2.insert(7); | |
79 | |
80 { | 59 { |
81 std::set<int> difference; | 60 std::set<int> difference = {1, 2}; |
82 difference.insert(1); | |
83 difference.insert(2); | |
84 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2)); | 61 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2)); |
85 } | 62 } |
86 | 63 |
87 { | 64 { |
88 std::set<int> difference; | 65 std::set<int> difference = {5, 6, 7}; |
89 difference.insert(5); | |
90 difference.insert(6); | |
91 difference.insert(7); | |
92 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a2, a1)); | 66 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a2, a1)); |
93 } | 67 } |
94 | 68 |
95 { | 69 { |
96 std::vector<int> difference; | 70 std::vector<int> difference = {1, 2}; |
97 difference.push_back(1); | |
98 difference.push_back(2); | |
99 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a1, a2)); | 71 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a1, a2)); |
100 } | 72 } |
101 | 73 |
102 { | 74 { |
103 std::vector<int> difference; | 75 std::vector<int> difference = {5, 6, 7}; |
104 difference.push_back(5); | |
105 difference.push_back(6); | |
106 difference.push_back(7); | |
107 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a2, a1)); | 76 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a2, a1)); |
108 } | 77 } |
109 } | 78 } |
110 | 79 |
111 TEST(STLUtilTest, STLSetUnion) { | 80 TEST(STLUtilTest, STLSetUnion) { |
112 std::set<int> a1; | 81 std::set<int> a1 = {1, 2, 3, 4}; |
113 a1.insert(1); | 82 std::set<int> a2 = {3, 4, 5, 6, 7}; |
114 a1.insert(2); | |
115 a1.insert(3); | |
116 a1.insert(4); | |
117 | |
118 std::set<int> a2; | |
119 a2.insert(3); | |
120 a2.insert(4); | |
121 a2.insert(5); | |
122 a2.insert(6); | |
123 a2.insert(7); | |
124 | |
125 { | 83 { |
126 std::set<int> result; | 84 std::set<int> result = {1, 2, 3, 4, 5, 6, 7}; |
127 result.insert(1); | |
128 result.insert(2); | |
129 result.insert(3); | |
130 result.insert(4); | |
131 result.insert(5); | |
132 result.insert(6); | |
133 result.insert(7); | |
134 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a1, a2)); | 85 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a1, a2)); |
135 } | 86 } |
136 | 87 |
137 { | 88 { |
138 std::set<int> result; | 89 std::set<int> result = {1, 2, 3, 4, 5, 6, 7}; |
139 result.insert(1); | |
140 result.insert(2); | |
141 result.insert(3); | |
142 result.insert(4); | |
143 result.insert(5); | |
144 result.insert(6); | |
145 result.insert(7); | |
146 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a2, a1)); | 90 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a2, a1)); |
147 } | 91 } |
148 | 92 |
149 { | 93 { |
150 std::vector<int> result; | 94 std::vector<int> result = {1, 2, 3, 4, 5, 6, 7}; |
151 result.push_back(1); | |
152 result.push_back(2); | |
153 result.push_back(3); | |
154 result.push_back(4); | |
155 result.push_back(5); | |
156 result.push_back(6); | |
157 result.push_back(7); | |
158 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a1, a2)); | 95 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a1, a2)); |
159 } | 96 } |
160 | 97 |
161 { | 98 { |
162 std::vector<int> result; | 99 std::vector<int> result = {1, 2, 3, 4, 5, 6, 7}; |
163 result.push_back(1); | |
164 result.push_back(2); | |
165 result.push_back(3); | |
166 result.push_back(4); | |
167 result.push_back(5); | |
168 result.push_back(6); | |
169 result.push_back(7); | |
170 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a2, a1)); | 100 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a2, a1)); |
171 } | 101 } |
172 } | 102 } |
173 | 103 |
174 TEST(STLUtilTest, STLSetIntersection) { | 104 TEST(STLUtilTest, STLSetIntersection) { |
175 std::set<int> a1; | 105 std::set<int> a1 = {1, 2, 3, 4}; |
176 a1.insert(1); | 106 std::set<int> a2 = {3, 4, 5, 6, 7}; |
177 a1.insert(2); | |
178 a1.insert(3); | |
179 a1.insert(4); | |
180 | |
181 std::set<int> a2; | |
182 a2.insert(3); | |
183 a2.insert(4); | |
184 a2.insert(5); | |
185 a2.insert(6); | |
186 a2.insert(7); | |
187 | |
188 { | 107 { |
189 std::set<int> result; | 108 std::set<int> result = {3, 4}; |
190 result.insert(3); | |
191 result.insert(4); | |
192 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2)); | 109 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2)); |
193 } | 110 } |
194 | 111 |
195 { | 112 { |
196 std::set<int> result; | 113 std::set<int> result = {3, 4}; |
197 result.insert(3); | |
198 result.insert(4); | |
199 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1)); | 114 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1)); |
200 } | 115 } |
201 | 116 |
202 { | 117 { |
203 std::vector<int> result; | 118 std::vector<int> result = {3, 4}; |
204 result.push_back(3); | |
205 result.push_back(4); | |
206 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2)); | 119 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2)); |
207 } | 120 } |
208 | 121 |
209 { | 122 { |
210 std::vector<int> result; | 123 std::vector<int> result = {3, 4}; |
211 result.push_back(3); | |
212 result.push_back(4); | |
213 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1)); | 124 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1)); |
214 } | 125 } |
215 } | 126 } |
216 | 127 |
217 TEST(STLUtilTest, STLIncludes) { | 128 TEST(STLUtilTest, STLIncludes) { |
218 std::set<int> a1; | 129 std::set<int> a1 = {1, 2, 3, 4}; |
219 a1.insert(1); | 130 std::set<int> a2 = {3, 4}; |
220 a1.insert(2); | 131 std::set<int> a3 = {3, 4, 5}; |
221 a1.insert(3); | |
222 a1.insert(4); | |
223 | |
224 std::set<int> a2; | |
225 a2.insert(3); | |
226 a2.insert(4); | |
227 | |
228 std::set<int> a3; | |
229 a3.insert(3); | |
230 a3.insert(4); | |
231 a3.insert(5); | |
232 | 132 |
233 EXPECT_TRUE(STLIncludes<std::set<int> >(a1, a2)); | 133 EXPECT_TRUE(STLIncludes<std::set<int> >(a1, a2)); |
234 EXPECT_FALSE(STLIncludes<std::set<int> >(a1, a3)); | 134 EXPECT_FALSE(STLIncludes<std::set<int> >(a1, a3)); |
235 EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a1)); | 135 EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a1)); |
236 EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a3)); | 136 EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a3)); |
237 EXPECT_FALSE(STLIncludes<std::set<int> >(a3, a1)); | 137 EXPECT_FALSE(STLIncludes<std::set<int> >(a3, a1)); |
238 EXPECT_TRUE(STLIncludes<std::set<int> >(a3, a2)); | 138 EXPECT_TRUE(STLIncludes<std::set<int> >(a3, a2)); |
239 } | 139 } |
240 | 140 |
241 TEST(StringAsArrayTest, Empty) { | 141 TEST(StringAsArrayTest, Empty) { |
242 std::string empty; | 142 std::string empty; |
243 EXPECT_EQ(nullptr, string_as_array(&empty)); | 143 EXPECT_EQ(nullptr, string_as_array(&empty)); |
244 } | 144 } |
245 | 145 |
246 TEST(StringAsArrayTest, NullTerminated) { | 146 TEST(StringAsArrayTest, NullTerminated) { |
247 // If any std::string implementation is not null-terminated, this should | 147 // If any std::string implementation is not null-terminated, this should |
248 // fail. All compilers we use return a null-terminated buffer, but please do | 148 // fail. All compilers we use return a null-terminated buffer, but please do |
249 // not rely on this fact in your code. | 149 // not rely on this fact in your code. |
250 std::string str("abcde"); | 150 std::string str = {"abcde"}; |
251 str.resize(3); | 151 str.resize(3); |
252 EXPECT_STREQ("abc", string_as_array(&str)); | 152 EXPECT_STREQ("abc", string_as_array(&str)); |
253 } | 153 } |
254 | 154 |
255 TEST(StringAsArrayTest, WriteCopy) { | 155 TEST(StringAsArrayTest, WriteCopy) { |
256 // With a COW implementation, this test will fail if | 156 // With a COW implementation, this test will fail if |
257 // string_as_array(&str) is implemented as | 157 // string_as_array(&str) is implemented as |
258 // const_cast<char*>(str->data()). | 158 // const_cast<char*>(str->data()). |
259 std::string s1("abc"); | 159 std::string s1 = {"abc"}; |
260 const std::string s2(s1); | 160 const std::string s2 = {s1}; |
261 string_as_array(&s1)[1] = 'x'; | 161 string_as_array(&s1)[1] = 'x'; |
262 EXPECT_EQ("axc", s1); | 162 EXPECT_EQ("axc", s1); |
263 EXPECT_EQ("abc", s2); | 163 EXPECT_EQ("abc", s2); |
264 } | 164 } |
265 | 165 |
266 } // namespace | 166 } // namespace |
267 } // namespace base | 167 } // namespace base |
OLD | NEW |