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
3 // found in the LICENSE file.
5 #include "base/stl_util.h"
9 #include <forward_list>
11 #include <initializer_list>
19 #include <type_traits>
20 #include <unordered_map>
21 #include <unordered_set>
24 #include "base/containers/queue.h"
25 #include "base/strings/string16.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
32 // Used as test case to ensure the various base::STLXxx functions don't require
33 // more than operators "<" and "==" on values stored in containers.
34 class ComparableValue {
36 explicit ComparableValue(int value) : value_(value) {}
38 bool operator==(const ComparableValue& rhs) const {
39 return value_ == rhs.value_;
42 bool operator<(const ComparableValue& rhs) const {
43 return value_ < rhs.value_;
50 template <typename Container>
52 const std::pair<Container, Container> test_data[] = {
53 {Container(), Container()}, {{1, 2, 3}, {1, 3}}, {{1, 2, 3, 2}, {1, 3}}};
55 for (auto test_case : test_data) {
56 base::Erase(test_case.first, 2);
57 EXPECT_EQ(test_case.second, test_case.first);
61 // This test is written for containers of std::pair<int, int> to support maps.
62 template <typename Container>
63 void RunEraseIfTest() {
69 {Container(), Container(), Container()},
70 {{{1, 1}, {2, 2}, {3, 3}}, {{1, 1}, {3, 3}}, {{2, 2}}},
71 {{{1, 1}, {2, 2}, {3, 3}, {4, 4}}, {{1, 1}, {3, 3}}, {{2, 2}, {4, 4}}},
74 for (auto test_case : test_data) {
75 base::EraseIf(test_case.input, [](const std::pair<int, int>& elem) {
76 return !(elem.first & 1);
78 EXPECT_EQ(test_case.erase_even, test_case.input);
81 for (auto test_case : test_data) {
82 base::EraseIf(test_case.input, [](const std::pair<int, int>& elem) {
83 return elem.first & 1;
85 EXPECT_EQ(test_case.erase_odd, test_case.input);
89 struct CustomIntHash {
90 size_t operator()(int elem) const { return std::hash<int>()(elem) + 1; }
94 size_t operator()(const std::pair<int, int>& elem) const {
95 return std::hash<int>()(elem.first);
104 TEST(STLUtilTest, Size) {
106 std::vector<int> vector = {1, 2, 3, 4, 5};
108 std::is_same<decltype(base::size(vector)),
109 decltype(vector.size())>::value,
110 "base::size(vector) should have the same type as vector.size()");
111 EXPECT_EQ(vector.size(), base::size(vector));
115 std::string empty_str;
117 std::is_same<decltype(base::size(empty_str)),
118 decltype(empty_str.size())>::value,
119 "base::size(empty_str) should have the same type as empty_str.size()");
120 EXPECT_EQ(0u, base::size(empty_str));
124 std::array<int, 4> array = {{1, 2, 3, 4}};
126 std::is_same<decltype(base::size(array)),
127 decltype(array.size())>::value,
128 "base::size(array) should have the same type as array.size()");
129 static_assert(base::size(array) == array.size(),
130 "base::size(array) should be equal to array.size()");
134 int array[] = {1, 2, 3};
135 static_assert(std::is_same<size_t, decltype(base::size(array))>::value,
136 "base::size(array) should be of type size_t");
137 static_assert(3u == base::size(array), "base::size(array) should be 3");
141 TEST(STLUtilTest, Empty) {
143 std::vector<int> vector;
145 std::is_same<decltype(base::empty(vector)),
146 decltype(vector.empty())>::value,
147 "base::empty(vector) should have the same type as vector.empty()");
148 EXPECT_EQ(vector.empty(), base::empty(vector));
152 std::array<int, 4> array = {{1, 2, 3, 4}};
154 std::is_same<decltype(base::empty(array)),
155 decltype(array.empty())>::value,
156 "base::empty(array) should have the same type as array.empty()");
157 static_assert(base::empty(array) == array.empty(),
158 "base::empty(array) should be equal to array.empty()");
162 int array[] = {1, 2, 3};
163 static_assert(std::is_same<bool, decltype(base::empty(array))>::value,
164 "base::empty(array) should be of type bool");
165 static_assert(!base::empty(array), "base::empty(array) should be false");
169 constexpr std::initializer_list<int> il;
170 static_assert(std::is_same<bool, decltype(base::empty(il))>::value,
171 "base::empty(il) should be of type bool");
172 static_assert(base::empty(il), "base::empty(il) should be true");
176 TEST(STLUtilTest, Data) {
178 std::vector<int> vector = {1, 2, 3, 4, 5};
180 std::is_same<decltype(base::data(vector)),
181 decltype(vector.data())>::value,
182 "base::data(vector) should have the same type as vector.data()");
183 EXPECT_EQ(vector.data(), base::data(vector));
187 const std::string cstr = "const string";
189 std::is_same<decltype(base::data(cstr)), decltype(cstr.data())>::value,
190 "base::data(cstr) should have the same type as cstr.data()");
192 EXPECT_EQ(cstr.data(), base::data(cstr));
196 std::string str = "mutable string";
197 static_assert(std::is_same<decltype(base::data(str)), char*>::value,
198 "base::data(str) should be of type char*");
199 EXPECT_EQ(str.data(), base::data(str));
203 std::string empty_str;
204 static_assert(std::is_same<decltype(base::data(empty_str)), char*>::value,
205 "base::data(empty_str) should be of type char*");
206 EXPECT_EQ(empty_str.data(), base::data(empty_str));
210 std::array<int, 4> array = {{1, 2, 3, 4}};
212 std::is_same<decltype(base::data(array)),
213 decltype(array.data())>::value,
214 "base::data(array) should have the same type as array.data()");
215 // std::array::data() is not constexpr prior to C++17, hence the runtime
217 EXPECT_EQ(array.data(), base::data(array));
221 constexpr int array[] = {1, 2, 3};
222 static_assert(std::is_same<const int*, decltype(base::data(array))>::value,
223 "base::data(array) should be of type const int*");
224 static_assert(array == base::data(array),
225 "base::data(array) should be array");
229 constexpr std::initializer_list<int> il;
231 std::is_same<decltype(il.begin()), decltype(base::data(il))>::value,
232 "base::data(il) should have the same type as il.begin()");
233 static_assert(il.begin() == base::data(il),
234 "base::data(il) should be equal to il.begin()");
238 TEST(STLUtilTest, GetUnderlyingContainer) {
240 std::queue<int> queue({1, 2, 3, 4, 5});
241 static_assert(std::is_same<decltype(GetUnderlyingContainer(queue)),
242 const std::deque<int>&>::value,
243 "GetUnderlyingContainer(queue) should be of type deque");
244 EXPECT_THAT(GetUnderlyingContainer(queue),
245 testing::ElementsAre(1, 2, 3, 4, 5));
249 std::queue<int> queue;
250 EXPECT_THAT(GetUnderlyingContainer(queue), testing::ElementsAre());
254 base::queue<int> queue({1, 2, 3, 4, 5});
256 std::is_same<decltype(GetUnderlyingContainer(queue)),
257 const base::circular_deque<int>&>::value,
258 "GetUnderlyingContainer(queue) should be of type circular_deque");
259 EXPECT_THAT(GetUnderlyingContainer(queue),
260 testing::ElementsAre(1, 2, 3, 4, 5));
264 std::vector<int> values = {1, 2, 3, 4, 5};
265 std::priority_queue<int> queue(values.begin(), values.end());
266 static_assert(std::is_same<decltype(GetUnderlyingContainer(queue)),
267 const std::vector<int>&>::value,
268 "GetUnderlyingContainer(queue) should be of type vector");
269 EXPECT_THAT(GetUnderlyingContainer(queue),
270 testing::UnorderedElementsAre(1, 2, 3, 4, 5));
274 std::stack<int> stack({1, 2, 3, 4, 5});
275 static_assert(std::is_same<decltype(GetUnderlyingContainer(stack)),
276 const std::deque<int>&>::value,
277 "GetUnderlyingContainer(stack) should be of type deque");
278 EXPECT_THAT(GetUnderlyingContainer(stack),
279 testing::ElementsAre(1, 2, 3, 4, 5));
283 TEST(STLUtilTest, STLIsSorted) {
289 EXPECT_TRUE(STLIsSorted(set));
293 std::set<ComparableValue> set;
294 set.insert(ComparableValue(24));
295 set.insert(ComparableValue(1));
296 set.insert(ComparableValue(12));
297 EXPECT_TRUE(STLIsSorted(set));
301 std::vector<int> vector;
305 vector.push_back(64);
306 vector.push_back(12432);
307 EXPECT_TRUE(STLIsSorted(vector));
309 EXPECT_FALSE(STLIsSorted(vector));
313 TEST(STLUtilTest, STLSetDifference) {
328 std::set<int> difference;
329 difference.insert(1);
330 difference.insert(2);
331 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2));
335 std::set<int> difference;
336 difference.insert(5);
337 difference.insert(6);
338 difference.insert(7);
339 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a2, a1));
343 std::vector<int> difference;
344 difference.push_back(1);
345 difference.push_back(2);
346 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a1, a2));
350 std::vector<int> difference;
351 difference.push_back(5);
352 difference.push_back(6);
353 difference.push_back(7);
354 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a2, a1));
358 TEST(STLUtilTest, STLSetUnion) {
373 std::set<int> result;
381 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a1, a2));
385 std::set<int> result;
393 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a2, a1));
397 std::vector<int> result;
405 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a1, a2));
409 std::vector<int> result;
417 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a2, a1));
421 TEST(STLUtilTest, STLSetIntersection) {
436 std::set<int> result;
439 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2));
443 std::set<int> result;
446 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1));
450 std::vector<int> result;
453 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2));
457 std::vector<int> result;
460 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1));
464 TEST(STLUtilTest, STLIncludes) {
480 EXPECT_TRUE(STLIncludes<std::set<int> >(a1, a2));
481 EXPECT_FALSE(STLIncludes<std::set<int> >(a1, a3));
482 EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a1));
483 EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a3));
484 EXPECT_FALSE(STLIncludes<std::set<int> >(a3, a1));
485 EXPECT_TRUE(STLIncludes<std::set<int> >(a3, a2));
488 TEST(Erase, String) {
489 const std::pair<std::string, std::string> test_data[] = {
490 {"", ""}, {"abc", "bc"}, {"abca", "bc"},
493 for (auto test_case : test_data) {
494 Erase(test_case.first, 'a');
495 EXPECT_EQ(test_case.second, test_case.first);
498 for (auto test_case : test_data) {
499 EraseIf(test_case.first, [](char elem) { return elem < 'b'; });
500 EXPECT_EQ(test_case.second, test_case.first);
504 TEST(Erase, String16) {
505 std::pair<base::string16, base::string16> test_data[] = {
506 {base::string16(), base::string16()},
507 {UTF8ToUTF16("abc"), UTF8ToUTF16("bc")},
508 {UTF8ToUTF16("abca"), UTF8ToUTF16("bc")},
511 const base::string16 letters = UTF8ToUTF16("ab");
512 for (auto test_case : test_data) {
513 Erase(test_case.first, letters[0]);
514 EXPECT_EQ(test_case.second, test_case.first);
517 for (auto test_case : test_data) {
518 EraseIf(test_case.first, [&](short elem) { return elem < letters[1]; });
519 EXPECT_EQ(test_case.second, test_case.first);
524 RunEraseTest<std::deque<int>>();
525 RunEraseIfTest<std::deque<std::pair<int, int>>>();
528 TEST(Erase, Vector) {
529 RunEraseTest<std::vector<int>>();
530 RunEraseIfTest<std::vector<std::pair<int, int>>>();
533 TEST(Erase, ForwardList) {
534 RunEraseTest<std::forward_list<int>>();
535 RunEraseIfTest<std::forward_list<std::pair<int, int>>>();
539 RunEraseTest<std::list<int>>();
540 RunEraseIfTest<std::list<std::pair<int, int>>>();
544 RunEraseIfTest<std::map<int, int>>();
545 RunEraseIfTest<std::map<int, int, std::greater<int>>>();
548 TEST(Erase, Multimap) {
549 RunEraseIfTest<std::multimap<int, int>>();
550 RunEraseIfTest<std::multimap<int, int, std::greater<int>>>();
554 RunEraseIfTest<std::set<std::pair<int, int>>>();
556 std::set<std::pair<int, int>, std::greater<std::pair<int, int>>>>();
559 TEST(Erase, Multiset) {
560 RunEraseIfTest<std::multiset<std::pair<int, int>>>();
562 std::multiset<std::pair<int, int>, std::greater<std::pair<int, int>>>>();
565 TEST(Erase, UnorderedMap) {
566 RunEraseIfTest<std::unordered_map<int, int>>();
567 RunEraseIfTest<std::unordered_map<int, int, CustomIntHash>>();
570 TEST(Erase, UnorderedMultimap) {
571 RunEraseIfTest<std::unordered_multimap<int, int>>();
572 RunEraseIfTest<std::unordered_multimap<int, int, CustomIntHash>>();
575 TEST(Erase, UnorderedSet) {
576 RunEraseIfTest<std::unordered_set<std::pair<int, int>, HashByFirst>>();
579 TEST(Erase, UnorderedMultiset) {
580 RunEraseIfTest<std::unordered_multiset<std::pair<int, int>, HashByFirst>>();
583 TEST(Erase, IsNotIn) {
584 // Should keep both '2' but only one '4', like std::set_intersection.
585 std::vector<int> lhs = {0, 2, 2, 4, 4, 4, 6, 8, 10};
586 std::vector<int> rhs = {1, 2, 2, 4, 5, 6, 7};
587 std::vector<int> expected = {2, 2, 4, 6};
588 EraseIf(lhs, IsNotIn<std::vector<int>>(rhs));
589 EXPECT_EQ(expected, lhs);
592 TEST(ContainsValue, OrdinaryArrays) {
593 const char allowed_chars[] = {'a', 'b', 'c', 'd'};
594 EXPECT_TRUE(ContainsValue(allowed_chars, 'a'));
595 EXPECT_FALSE(ContainsValue(allowed_chars, 'z'));
596 EXPECT_FALSE(ContainsValue(allowed_chars, 0));
598 const char allowed_chars_including_nul[] = "abcd";
599 EXPECT_TRUE(ContainsValue(allowed_chars_including_nul, 0));
602 TEST(STLUtilTest, OptionalOrNullptr) {
603 Optional<float> optional;
604 EXPECT_EQ(nullptr, base::OptionalOrNullptr(optional));
607 EXPECT_EQ(&optional.value(), base::OptionalOrNullptr(optional));
608 EXPECT_NE(nullptr, base::OptionalOrNullptr(optional));