1 // Copyright 2012 The Chromium Authors
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>
18 #include <type_traits>
19 #include <unordered_set>
22 #include "base/containers/cxx20_erase_vector.h"
23 #include "base/containers/queue.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/abseil-cpp/absl/types/optional.h"
31 TEST(STLUtilTest, GetUnderlyingContainer) {
33 std::queue<int> queue({1, 2, 3, 4, 5});
34 static_assert(std::is_same_v<decltype(GetUnderlyingContainer(queue)),
35 const std::deque<int>&>,
36 "GetUnderlyingContainer(queue) should be of type deque");
37 EXPECT_THAT(GetUnderlyingContainer(queue),
38 testing::ElementsAre(1, 2, 3, 4, 5));
42 std::queue<int> queue;
43 EXPECT_THAT(GetUnderlyingContainer(queue), testing::ElementsAre());
47 base::queue<int> queue({1, 2, 3, 4, 5});
49 std::is_same_v<decltype(GetUnderlyingContainer(queue)),
50 const base::circular_deque<int>&>,
51 "GetUnderlyingContainer(queue) should be of type circular_deque");
52 EXPECT_THAT(GetUnderlyingContainer(queue),
53 testing::ElementsAre(1, 2, 3, 4, 5));
57 std::vector<int> values = {1, 2, 3, 4, 5};
58 std::priority_queue<int> queue(values.begin(), values.end());
59 static_assert(std::is_same_v<decltype(GetUnderlyingContainer(queue)),
60 const std::vector<int>&>,
61 "GetUnderlyingContainer(queue) should be of type vector");
62 EXPECT_THAT(GetUnderlyingContainer(queue),
63 testing::UnorderedElementsAre(1, 2, 3, 4, 5));
67 std::stack<int> stack({1, 2, 3, 4, 5});
68 static_assert(std::is_same_v<decltype(GetUnderlyingContainer(stack)),
69 const std::deque<int>&>,
70 "GetUnderlyingContainer(stack) should be of type deque");
71 EXPECT_THAT(GetUnderlyingContainer(stack),
72 testing::ElementsAre(1, 2, 3, 4, 5));
76 TEST(STLUtilTest, STLSetDifference) {
91 std::set<int> difference;
94 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2));
98 std::set<int> difference;
100 difference.insert(6);
101 difference.insert(7);
102 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a2, a1));
106 std::vector<int> difference;
107 difference.push_back(1);
108 difference.push_back(2);
109 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a1, a2));
113 std::vector<int> difference;
114 difference.push_back(5);
115 difference.push_back(6);
116 difference.push_back(7);
117 EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a2, a1));
121 TEST(STLUtilTest, STLSetUnion) {
136 std::set<int> result;
144 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a1, a2));
148 std::set<int> result;
156 EXPECT_EQ(result, STLSetUnion<std::set<int> >(a2, a1));
160 std::vector<int> result;
168 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a1, a2));
172 std::vector<int> result;
180 EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a2, a1));
184 TEST(STLUtilTest, STLSetIntersection) {
199 std::set<int> result;
202 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2));
206 std::set<int> result;
209 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1));
213 std::vector<int> result;
216 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2));
220 std::vector<int> result;
223 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1));
227 TEST(Erase, IsNotIn) {
228 // Should keep both '2' but only one '4', like std::set_intersection.
229 std::vector<int> lhs = {0, 2, 2, 4, 4, 4, 6, 8, 10};
230 std::vector<int> rhs = {1, 2, 2, 4, 5, 6, 7};
231 std::vector<int> expected = {2, 2, 4, 6};
232 EXPECT_EQ(5u, EraseIf(lhs, IsNotIn<std::vector<int>>(rhs)));
233 EXPECT_EQ(expected, lhs);