Add ability to query and allocate zero copy buffers.
[chromium-blink-merge.git] / base / stl_util_unittest.cc
bloba3f8e16f2ce2c0a5411d1491254245c80d89ab3d
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"
7 #include <set>
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
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 {
16 public:
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_;
27 private:
28 int value_;
33 namespace base {
34 namespace {
36 TEST(STLUtilTest, STLIsSorted) {
38 std::set<int> set;
39 set.insert(24);
40 set.insert(1);
41 set.insert(12);
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;
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));
61 vector.back() = 1;
62 EXPECT_FALSE(STLIsSorted(vector));
66 TEST(STLUtilTest, STLSetDifference) {
67 std::set<int> a1;
68 a1.insert(1);
69 a1.insert(2);
70 a1.insert(3);
71 a1.insert(4);
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);
81 std::set<int> difference;
82 difference.insert(1);
83 difference.insert(2);
84 EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2));
88 std::set<int> difference;
89 difference.insert(5);
90 difference.insert(6);
91 difference.insert(7);
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) {
112 std::set<int> a1;
113 a1.insert(1);
114 a1.insert(2);
115 a1.insert(3);
116 a1.insert(4);
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);
126 std::set<int> result;
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));
138 std::set<int> result;
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));
150 std::vector<int> result;
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));
162 std::vector<int> result;
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));
174 TEST(STLUtilTest, STLSetIntersection) {
175 std::set<int> a1;
176 a1.insert(1);
177 a1.insert(2);
178 a1.insert(3);
179 a1.insert(4);
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);
189 std::set<int> result;
190 result.insert(3);
191 result.insert(4);
192 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2));
196 std::set<int> result;
197 result.insert(3);
198 result.insert(4);
199 EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1));
203 std::vector<int> result;
204 result.push_back(3);
205 result.push_back(4);
206 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2));
210 std::vector<int> result;
211 result.push_back(3);
212 result.push_back(4);
213 EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1));
217 TEST(STLUtilTest, STLIncludes) {
218 std::set<int> a1;
219 a1.insert(1);
220 a1.insert(2);
221 a1.insert(3);
222 a1.insert(4);
224 std::set<int> a2;
225 a2.insert(3);
226 a2.insert(4);
228 std::set<int> a3;
229 a3.insert(3);
230 a3.insert(4);
231 a3.insert(5);
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));
241 } // namespace
242 } // namespace base