• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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.
4 //
5 // ----------------------------------------------------------------------
6 //
7 // Unittest for the Interval class.
8 //
9 // Author: Will Neveitt (wneveitt@google.com)
10 // ----------------------------------------------------------------------
11 
12 #include "net/base/interval.h"
13 
14 #include "net/test/gtest_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 using std::string;
18 
19 namespace net::test {
20 namespace {
21 
22 class IntervalTest : public ::testing::Test {
23  protected:
24   // Test intersection between the two intervals i1 and i2.  Tries
25   // i1.IntersectWith(i2) and vice versa. The intersection should change i1 iff
26   // changes_i1 is true, and the same for changes_i2.  The resulting
27   // intersection should be result.
TestIntersect(const Interval<int64_t> & i1,const Interval<int64_t> & i2,bool changes_i1,bool changes_i2,const Interval<int64_t> & result)28   void TestIntersect(const Interval<int64_t>& i1,
29                      const Interval<int64_t>& i2,
30                      bool changes_i1,
31                      bool changes_i2,
32                      const Interval<int64_t>& result) {
33     Interval<int64_t> i;
34     i.CopyFrom(i1);
35     EXPECT_TRUE(i.IntersectWith(i2) == changes_i1 && i.Equals(result));
36     i.CopyFrom(i2);
37     EXPECT_TRUE(i.IntersectWith(i1) == changes_i2 && i.Equals(result));
38   }
39 };
40 
TEST_F(IntervalTest,ConstructorsCopyAndClear)41 TEST_F(IntervalTest, ConstructorsCopyAndClear) {
42   Interval<int32_t> empty;
43   EXPECT_TRUE(empty.Empty());
44 
45   Interval<int32_t> d2(0, 100);
46   EXPECT_EQ(0, d2.min());
47   EXPECT_EQ(100, d2.max());
48   EXPECT_EQ(Interval<int32_t>(0, 100), d2);
49   EXPECT_NE(Interval<int32_t>(0, 99), d2);
50 
51   empty.CopyFrom(d2);
52   EXPECT_EQ(0, d2.min());
53   EXPECT_EQ(100, d2.max());
54   EXPECT_TRUE(empty.Equals(d2));
55   EXPECT_EQ(empty, d2);
56   EXPECT_TRUE(d2.Equals(empty));
57   EXPECT_EQ(d2, empty);
58 
59   Interval<int32_t> max_less_than_min(40, 20);
60   EXPECT_TRUE(max_less_than_min.Empty());
61   EXPECT_EQ(40, max_less_than_min.min());
62   EXPECT_EQ(20, max_less_than_min.max());
63 
64   Interval<int> d3(10, 20);
65   d3.Clear();
66   EXPECT_TRUE(d3.Empty());
67 }
68 
TEST_F(IntervalTest,GettersSetters)69 TEST_F(IntervalTest, GettersSetters) {
70   Interval<int32_t> d1(100, 200);
71 
72   // SetMin:
73   d1.SetMin(30);
74   EXPECT_EQ(30, d1.min());
75   EXPECT_EQ(200, d1.max());
76 
77   // SetMax:
78   d1.SetMax(220);
79   EXPECT_EQ(30, d1.min());
80   EXPECT_EQ(220, d1.max());
81 
82   // Set:
83   d1.Clear();
84   d1.Set(30, 220);
85   EXPECT_EQ(30, d1.min());
86   EXPECT_EQ(220, d1.max());
87 
88   // SpanningUnion:
89   Interval<int32_t> d2;
90   EXPECT_TRUE(!d1.SpanningUnion(d2));
91   EXPECT_EQ(30, d1.min());
92   EXPECT_EQ(220, d1.max());
93 
94   EXPECT_TRUE(d2.SpanningUnion(d1));
95   EXPECT_EQ(30, d2.min());
96   EXPECT_EQ(220, d2.max());
97 
98   d2.SetMin(40);
99   d2.SetMax(100);
100   EXPECT_TRUE(!d1.SpanningUnion(d2));
101   EXPECT_EQ(30, d1.min());
102   EXPECT_EQ(220, d1.max());
103 
104   d2.SetMin(20);
105   d2.SetMax(100);
106   EXPECT_TRUE(d1.SpanningUnion(d2));
107   EXPECT_EQ(20, d1.min());
108   EXPECT_EQ(220, d1.max());
109 
110   d2.SetMin(50);
111   d2.SetMax(300);
112   EXPECT_TRUE(d1.SpanningUnion(d2));
113   EXPECT_EQ(20, d1.min());
114   EXPECT_EQ(300, d1.max());
115 
116   d2.SetMin(0);
117   d2.SetMax(500);
118   EXPECT_TRUE(d1.SpanningUnion(d2));
119   EXPECT_EQ(0, d1.min());
120   EXPECT_EQ(500, d1.max());
121 
122   d2.SetMin(100);
123   d2.SetMax(0);
124   EXPECT_TRUE(!d1.SpanningUnion(d2));
125   EXPECT_EQ(0, d1.min());
126   EXPECT_EQ(500, d1.max());
127   EXPECT_TRUE(d2.SpanningUnion(d1));
128   EXPECT_EQ(0, d2.min());
129   EXPECT_EQ(500, d2.max());
130 }
131 
TEST_F(IntervalTest,CoveringOps)132 TEST_F(IntervalTest, CoveringOps) {
133   const Interval<int64_t> empty;
134   const Interval<int64_t> d(100, 200);
135   const Interval<int64_t> d1(0, 50);
136   const Interval<int64_t> d2(50, 110);
137   const Interval<int64_t> d3(110, 180);
138   const Interval<int64_t> d4(180, 220);
139   const Interval<int64_t> d5(220, 300);
140   const Interval<int64_t> d6(100, 150);
141   const Interval<int64_t> d7(150, 200);
142   const Interval<int64_t> d8(0, 300);
143 
144   // Intersection:
145   EXPECT_TRUE(d.Intersects(d));
146   EXPECT_TRUE(!empty.Intersects(d) && !d.Intersects(empty));
147   EXPECT_TRUE(!d.Intersects(d1) && !d1.Intersects(d));
148   EXPECT_TRUE(d.Intersects(d2) && d2.Intersects(d));
149   EXPECT_TRUE(d.Intersects(d3) && d3.Intersects(d));
150   EXPECT_TRUE(d.Intersects(d4) && d4.Intersects(d));
151   EXPECT_TRUE(!d.Intersects(d5) && !d5.Intersects(d));
152   EXPECT_TRUE(d.Intersects(d6) && d6.Intersects(d));
153   EXPECT_TRUE(d.Intersects(d7) && d7.Intersects(d));
154   EXPECT_TRUE(d.Intersects(d8) && d8.Intersects(d));
155 
156   Interval<int64_t> i;
157   EXPECT_TRUE(d.Intersects(d, &i) && d.Equals(i));
158   EXPECT_TRUE(!empty.Intersects(d, nullptr) && !d.Intersects(empty, nullptr));
159   EXPECT_TRUE(!d.Intersects(d1, nullptr) && !d1.Intersects(d, nullptr));
160   EXPECT_TRUE(d.Intersects(d2, &i) && i.Equals(Interval<int64_t>(100, 110)));
161   EXPECT_TRUE(d2.Intersects(d, &i) && i.Equals(Interval<int64_t>(100, 110)));
162   EXPECT_TRUE(d.Intersects(d3, &i) && i.Equals(d3));
163   EXPECT_TRUE(d3.Intersects(d, &i) && i.Equals(d3));
164   EXPECT_TRUE(d.Intersects(d4, &i) && i.Equals(Interval<int64_t>(180, 200)));
165   EXPECT_TRUE(d4.Intersects(d, &i) && i.Equals(Interval<int64_t>(180, 200)));
166   EXPECT_TRUE(!d.Intersects(d5, nullptr) && !d5.Intersects(d, nullptr));
167   EXPECT_TRUE(d.Intersects(d6, &i) && i.Equals(d6));
168   EXPECT_TRUE(d6.Intersects(d, &i) && i.Equals(d6));
169   EXPECT_TRUE(d.Intersects(d7, &i) && i.Equals(d7));
170   EXPECT_TRUE(d7.Intersects(d, &i) && i.Equals(d7));
171   EXPECT_TRUE(d.Intersects(d8, &i) && i.Equals(d));
172   EXPECT_TRUE(d8.Intersects(d, &i) && i.Equals(d));
173 
174   // Test IntersectsWith().
175   // Arguments are TestIntersect(i1, i2, changes_i1, changes_i2, result).
176   TestIntersect(empty, d, false, true, empty);
177   TestIntersect(d, d1, true, true, empty);
178   TestIntersect(d1, d2, true, true, empty);
179   TestIntersect(d, d2, true, true, Interval<int64_t>(100, 110));
180   TestIntersect(d8, d, true, false, d);
181   TestIntersect(d8, d1, true, false, d1);
182   TestIntersect(d8, d5, true, false, d5);
183 
184   // Contains:
185   EXPECT_TRUE(!empty.Contains(d) && !d.Contains(empty));
186   EXPECT_TRUE(d.Contains(d));
187   EXPECT_TRUE(!d.Contains(d1) && !d1.Contains(d));
188   EXPECT_TRUE(!d.Contains(d2) && !d2.Contains(d));
189   EXPECT_TRUE(d.Contains(d3) && !d3.Contains(d));
190   EXPECT_TRUE(!d.Contains(d4) && !d4.Contains(d));
191   EXPECT_TRUE(!d.Contains(d5) && !d5.Contains(d));
192   EXPECT_TRUE(d.Contains(d6) && !d6.Contains(d));
193   EXPECT_TRUE(d.Contains(d7) && !d7.Contains(d));
194   EXPECT_TRUE(!d.Contains(d8) && d8.Contains(d));
195 
196   EXPECT_TRUE(d.Contains(100));
197   EXPECT_TRUE(!d.Contains(200));
198   EXPECT_TRUE(d.Contains(150));
199   EXPECT_TRUE(!d.Contains(99));
200   EXPECT_TRUE(!d.Contains(201));
201 
202   // Difference:
203   Interval<int64_t> lo;
204   Interval<int64_t> hi;
205 
206   EXPECT_TRUE(d.Difference(d2, &lo, &hi));
207   EXPECT_TRUE(lo.Empty());
208   EXPECT_EQ(110u, hi.min());
209   EXPECT_EQ(200u, hi.max());
210 
211   EXPECT_TRUE(d.Difference(d3, &lo, &hi));
212   EXPECT_EQ(100u, lo.min());
213   EXPECT_EQ(110u, lo.max());
214   EXPECT_EQ(180u, hi.min());
215   EXPECT_EQ(200u, hi.max());
216 
217   EXPECT_TRUE(d.Difference(d4, &lo, &hi));
218   EXPECT_EQ(100u, lo.min());
219   EXPECT_EQ(180u, lo.max());
220   EXPECT_TRUE(hi.Empty());
221 
222   EXPECT_FALSE(d.Difference(d5, &lo, &hi));
223   EXPECT_EQ(100u, lo.min());
224   EXPECT_EQ(200u, lo.max());
225   EXPECT_TRUE(hi.Empty());
226 
227   EXPECT_TRUE(d.Difference(d6, &lo, &hi));
228   EXPECT_TRUE(lo.Empty());
229   EXPECT_EQ(150u, hi.min());
230   EXPECT_EQ(200u, hi.max());
231 
232   EXPECT_TRUE(d.Difference(d7, &lo, &hi));
233   EXPECT_EQ(100u, lo.min());
234   EXPECT_EQ(150u, lo.max());
235   EXPECT_TRUE(hi.Empty());
236 
237   EXPECT_TRUE(d.Difference(d8, &lo, &hi));
238   EXPECT_TRUE(lo.Empty());
239   EXPECT_TRUE(hi.Empty());
240 }
241 
TEST_F(IntervalTest,Length)242 TEST_F(IntervalTest, Length) {
243   const Interval<int> empty1;
244   const Interval<int> empty2(1, 1);
245   const Interval<int> empty3(1, 0);
246   const Interval<base::TimeDelta> empty4(base::TimeDelta() + base::Seconds(1),
247                                          base::TimeDelta());
248   const Interval<int> d1(1, 2);
249   const Interval<int> d2(0, 50);
250   const Interval<base::TimeDelta> d3(base::TimeDelta(),
251                                      base::TimeDelta() + base::Seconds(1));
252   const Interval<base::TimeDelta> d4(base::TimeDelta() + base::Hours(1),
253                                      base::TimeDelta() + base::Minutes(90));
254 
255   EXPECT_EQ(0, empty1.Length());
256   EXPECT_EQ(0, empty2.Length());
257   EXPECT_EQ(0, empty3.Length());
258   EXPECT_EQ(base::TimeDelta(), empty4.Length());
259   EXPECT_EQ(1, d1.Length());
260   EXPECT_EQ(50, d2.Length());
261   EXPECT_EQ(base::Seconds(1), d3.Length());
262   EXPECT_EQ(base::Minutes(30), d4.Length());
263 }
264 
TEST_F(IntervalTest,IntervalOfTypeWithNoOperatorMinus)265 TEST_F(IntervalTest, IntervalOfTypeWithNoOperatorMinus) {
266   // Interval<T> should work even if T does not support operator-().  We just
267   // can't call Interval<T>::Length() for such types.
268   const Interval<string> d1("a", "b");
269   const Interval<std::pair<int, int>> d2({1, 2}, {4, 3});
270   EXPECT_EQ("a", d1.min());
271   EXPECT_EQ("b", d1.max());
272   EXPECT_EQ(std::make_pair(1, 2), d2.min());
273   EXPECT_EQ(std::make_pair(4, 3), d2.max());
274 }
275 
276 }  // unnamed namespace
277 }  // namespace net::test
278