• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // Borrowed from Chromium's src/base/stl_util_unittest.cc
12 #include "webrtc/system_wrappers/include/stl_util.h"
13 
14 #include <set>
15 
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace {
19 
20 // Used as test case to ensure the various base::STLXxx functions don't require
21 // more than operators "<" and "==" on values stored in containers.
22 class ComparableValue {
23  public:
ComparableValue(int value)24   explicit ComparableValue(int value) : value_(value) {}
25 
operator ==(const ComparableValue & rhs) const26   bool operator==(const ComparableValue& rhs) const {
27     return value_ == rhs.value_;
28   }
29 
operator <(const ComparableValue & rhs) const30   bool operator<(const ComparableValue& rhs) const {
31     return value_ < rhs.value_;
32   }
33 
34  private:
35   int value_;
36 };
37 
38 }  // namespace
39 
40 namespace webrtc {
41 namespace {
42 
TEST(STLUtilTest,STLIsSorted)43 TEST(STLUtilTest, STLIsSorted) {
44   {
45     std::set<int> set;
46     set.insert(24);
47     set.insert(1);
48     set.insert(12);
49     EXPECT_TRUE(STLIsSorted(set));
50   }
51 
52   {
53     std::set<ComparableValue> set;
54     set.insert(ComparableValue(24));
55     set.insert(ComparableValue(1));
56     set.insert(ComparableValue(12));
57     EXPECT_TRUE(STLIsSorted(set));
58   }
59 
60   {
61     std::vector<int> vector;
62     vector.push_back(1);
63     vector.push_back(1);
64     vector.push_back(4);
65     vector.push_back(64);
66     vector.push_back(12432);
67     EXPECT_TRUE(STLIsSorted(vector));
68     vector.back() = 1;
69     EXPECT_FALSE(STLIsSorted(vector));
70   }
71 }
72 
TEST(STLUtilTest,STLSetDifference)73 TEST(STLUtilTest, STLSetDifference) {
74   std::set<int> a1;
75   a1.insert(1);
76   a1.insert(2);
77   a1.insert(3);
78   a1.insert(4);
79 
80   std::set<int> a2;
81   a2.insert(3);
82   a2.insert(4);
83   a2.insert(5);
84   a2.insert(6);
85   a2.insert(7);
86 
87   {
88     std::set<int> difference;
89     difference.insert(1);
90     difference.insert(2);
91     EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2));
92   }
93 
94   {
95     std::set<int> difference;
96     difference.insert(5);
97     difference.insert(6);
98     difference.insert(7);
99     EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a2, a1));
100   }
101 
102   {
103     std::vector<int> difference;
104     difference.push_back(1);
105     difference.push_back(2);
106     EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a1, a2));
107   }
108 
109   {
110     std::vector<int> difference;
111     difference.push_back(5);
112     difference.push_back(6);
113     difference.push_back(7);
114     EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a2, a1));
115   }
116 }
117 
TEST(STLUtilTest,STLSetUnion)118 TEST(STLUtilTest, STLSetUnion) {
119   std::set<int> a1;
120   a1.insert(1);
121   a1.insert(2);
122   a1.insert(3);
123   a1.insert(4);
124 
125   std::set<int> a2;
126   a2.insert(3);
127   a2.insert(4);
128   a2.insert(5);
129   a2.insert(6);
130   a2.insert(7);
131 
132   {
133     std::set<int> result;
134     result.insert(1);
135     result.insert(2);
136     result.insert(3);
137     result.insert(4);
138     result.insert(5);
139     result.insert(6);
140     result.insert(7);
141     EXPECT_EQ(result, STLSetUnion<std::set<int> >(a1, a2));
142   }
143 
144   {
145     std::set<int> result;
146     result.insert(1);
147     result.insert(2);
148     result.insert(3);
149     result.insert(4);
150     result.insert(5);
151     result.insert(6);
152     result.insert(7);
153     EXPECT_EQ(result, STLSetUnion<std::set<int> >(a2, a1));
154   }
155 
156   {
157     std::vector<int> result;
158     result.push_back(1);
159     result.push_back(2);
160     result.push_back(3);
161     result.push_back(4);
162     result.push_back(5);
163     result.push_back(6);
164     result.push_back(7);
165     EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a1, a2));
166   }
167 
168   {
169     std::vector<int> result;
170     result.push_back(1);
171     result.push_back(2);
172     result.push_back(3);
173     result.push_back(4);
174     result.push_back(5);
175     result.push_back(6);
176     result.push_back(7);
177     EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a2, a1));
178   }
179 }
180 
TEST(STLUtilTest,STLSetIntersection)181 TEST(STLUtilTest, STLSetIntersection) {
182   std::set<int> a1;
183   a1.insert(1);
184   a1.insert(2);
185   a1.insert(3);
186   a1.insert(4);
187 
188   std::set<int> a2;
189   a2.insert(3);
190   a2.insert(4);
191   a2.insert(5);
192   a2.insert(6);
193   a2.insert(7);
194 
195   {
196     std::set<int> result;
197     result.insert(3);
198     result.insert(4);
199     EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2));
200   }
201 
202   {
203     std::set<int> result;
204     result.insert(3);
205     result.insert(4);
206     EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1));
207   }
208 
209   {
210     std::vector<int> result;
211     result.push_back(3);
212     result.push_back(4);
213     EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2));
214   }
215 
216   {
217     std::vector<int> result;
218     result.push_back(3);
219     result.push_back(4);
220     EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1));
221   }
222 }
223 
TEST(STLUtilTest,STLIncludes)224 TEST(STLUtilTest, STLIncludes) {
225   std::set<int> a1;
226   a1.insert(1);
227   a1.insert(2);
228   a1.insert(3);
229   a1.insert(4);
230 
231   std::set<int> a2;
232   a2.insert(3);
233   a2.insert(4);
234 
235   std::set<int> a3;
236   a3.insert(3);
237   a3.insert(4);
238   a3.insert(5);
239 
240   EXPECT_TRUE(STLIncludes<std::set<int> >(a1, a2));
241   EXPECT_FALSE(STLIncludes<std::set<int> >(a1, a3));
242   EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a1));
243   EXPECT_FALSE(STLIncludes<std::set<int> >(a2, a3));
244   EXPECT_FALSE(STLIncludes<std::set<int> >(a3, a1));
245   EXPECT_TRUE(STLIncludes<std::set<int> >(a3, a2));
246 }
247 
248 }  // namespace
249 }  // namespace webrtc
250 
251