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 "testing/gtest/include/gtest/gtest.h"
13 // Used as test case to ensure the various base::STLXxx functions don't require
14 // more than operators "<" and "==" on values stored in containers.
15 class ComparableValue
{
17 explicit ComparableValue(int value
) : value_(value
) {}
19 bool operator==(const ComparableValue
& rhs
) const {
20 return value_
== rhs
.value_
;
23 bool operator<(const ComparableValue
& rhs
) const {
24 return value_
< rhs
.value_
;
36 TEST(STLUtilTest
, STLIsSorted
) {
42 EXPECT_TRUE(STLIsSorted(set
));
46 std::set
<ComparableValue
> set
;
47 set
.insert(ComparableValue(24));
48 set
.insert(ComparableValue(1));
49 set
.insert(ComparableValue(12));
50 EXPECT_TRUE(STLIsSorted(set
));
54 std::vector
<int> vector
;
59 vector
.push_back(12432);
60 EXPECT_TRUE(STLIsSorted(vector
));
62 EXPECT_FALSE(STLIsSorted(vector
));
66 TEST(STLUtilTest
, STLSetDifference
) {
81 std::set
<int> difference
;
84 EXPECT_EQ(difference
, STLSetDifference
<std::set
<int> >(a1
, a2
));
88 std::set
<int> difference
;
92 EXPECT_EQ(difference
, STLSetDifference
<std::set
<int> >(a2
, a1
));
96 std::vector
<int> difference
;
97 difference
.push_back(1);
98 difference
.push_back(2);
99 EXPECT_EQ(difference
, STLSetDifference
<std::vector
<int> >(a1
, a2
));
103 std::vector
<int> difference
;
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
));
111 TEST(STLUtilTest
, STLSetUnion
) {
126 std::set
<int> result
;
134 EXPECT_EQ(result
, STLSetUnion
<std::set
<int> >(a1
, a2
));
138 std::set
<int> result
;
146 EXPECT_EQ(result
, STLSetUnion
<std::set
<int> >(a2
, a1
));
150 std::vector
<int> result
;
158 EXPECT_EQ(result
, STLSetUnion
<std::vector
<int> >(a1
, a2
));
162 std::vector
<int> result
;
170 EXPECT_EQ(result
, STLSetUnion
<std::vector
<int> >(a2
, a1
));
174 TEST(STLUtilTest
, STLSetIntersection
) {
189 std::set
<int> result
;
192 EXPECT_EQ(result
, STLSetIntersection
<std::set
<int> >(a1
, a2
));
196 std::set
<int> result
;
199 EXPECT_EQ(result
, STLSetIntersection
<std::set
<int> >(a2
, a1
));
203 std::vector
<int> result
;
206 EXPECT_EQ(result
, STLSetIntersection
<std::vector
<int> >(a1
, a2
));
210 std::vector
<int> result
;
213 EXPECT_EQ(result
, STLSetIntersection
<std::vector
<int> >(a2
, a1
));
217 TEST(STLUtilTest
, STLIncludes
) {
233 EXPECT_TRUE(STLIncludes
<std::set
<int> >(a1
, a2
));
234 EXPECT_FALSE(STLIncludes
<std::set
<int> >(a1
, a3
));
235 EXPECT_FALSE(STLIncludes
<std::set
<int> >(a2
, a1
));
236 EXPECT_FALSE(STLIncludes
<std::set
<int> >(a2
, a3
));
237 EXPECT_FALSE(STLIncludes
<std::set
<int> >(a3
, a1
));
238 EXPECT_TRUE(STLIncludes
<std::set
<int> >(a3
, a2
));