OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // ---------------------------------------------------------------------- | 5 // ---------------------------------------------------------------------- |
6 // | 6 // |
7 // Unittest for the Interval class. | 7 // Unittest for the Interval class. |
8 // | 8 // |
9 // Author: Will Neveitt (wneveitt@google.com) | 9 // Author: Will Neveitt (wneveitt@google.com) |
10 // ---------------------------------------------------------------------- | 10 // ---------------------------------------------------------------------- |
11 | 11 |
12 #include "net/quic/interval.h" | 12 #include "net/quic/interval.h" |
13 | 13 |
14 #include <string> | 14 #include <string> |
15 #include <utility> | 15 #include <utility> |
16 | 16 |
17 #include "base/basictypes.h" | |
18 #include "base/logging.h" | 17 #include "base/logging.h" |
19 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
20 #include "net/test/gtest_util.h" | 19 #include "net/test/gtest_util.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
22 | 21 |
23 using std::pair; | 22 using std::pair; |
24 using std::string; | 23 using std::string; |
25 using std::vector; | 24 using std::vector; |
26 | 25 |
27 namespace net { | 26 namespace net { |
28 namespace test { | 27 namespace test { |
29 namespace { | 28 namespace { |
30 | 29 |
31 class IntervalTest : public ::testing::Test { | 30 class IntervalTest : public ::testing::Test { |
32 protected: | 31 protected: |
33 // Test intersection between the two intervals i1 and i2. Tries | 32 // Test intersection between the two intervals i1 and i2. Tries |
34 // i1.IntersectWith(i2) and vice versa. The intersection should change i1 iff | 33 // i1.IntersectWith(i2) and vice versa. The intersection should change i1 iff |
35 // changes_i1 is true, and the same for changes_i2. The resulting | 34 // changes_i1 is true, and the same for changes_i2. The resulting |
36 // intersection should be result. | 35 // intersection should be result. |
37 void TestIntersect(const Interval<int64>& i1, | 36 void TestIntersect(const Interval<int64_t>& i1, |
38 const Interval<int64>& i2, | 37 const Interval<int64_t>& i2, |
39 bool changes_i1, | 38 bool changes_i1, |
40 bool changes_i2, | 39 bool changes_i2, |
41 const Interval<int64>& result) { | 40 const Interval<int64_t>& result) { |
42 Interval<int64> i; | 41 Interval<int64_t> i; |
43 i.CopyFrom(i1); | 42 i.CopyFrom(i1); |
44 EXPECT_TRUE(i.IntersectWith(i2) == changes_i1 && i.Equals(result)); | 43 EXPECT_TRUE(i.IntersectWith(i2) == changes_i1 && i.Equals(result)); |
45 i.CopyFrom(i2); | 44 i.CopyFrom(i2); |
46 EXPECT_TRUE(i.IntersectWith(i1) == changes_i2 && i.Equals(result)); | 45 EXPECT_TRUE(i.IntersectWith(i1) == changes_i2 && i.Equals(result)); |
47 } | 46 } |
48 }; | 47 }; |
49 | 48 |
50 TEST_F(IntervalTest, ConstructorsCopyAndClear) { | 49 TEST_F(IntervalTest, ConstructorsCopyAndClear) { |
51 Interval<int32> empty; | 50 Interval<int32_t> empty; |
52 EXPECT_TRUE(empty.Empty()); | 51 EXPECT_TRUE(empty.Empty()); |
53 | 52 |
54 Interval<int32> d2(0, 100); | 53 Interval<int32_t> d2(0, 100); |
55 EXPECT_EQ(0, d2.min()); | 54 EXPECT_EQ(0, d2.min()); |
56 EXPECT_EQ(100, d2.max()); | 55 EXPECT_EQ(100, d2.max()); |
57 EXPECT_EQ(Interval<int32>(0, 100), d2); | 56 EXPECT_EQ(Interval<int32_t>(0, 100), d2); |
58 EXPECT_NE(Interval<int32>(0, 99), d2); | 57 EXPECT_NE(Interval<int32_t>(0, 99), d2); |
59 | 58 |
60 empty.CopyFrom(d2); | 59 empty.CopyFrom(d2); |
61 EXPECT_EQ(0, d2.min()); | 60 EXPECT_EQ(0, d2.min()); |
62 EXPECT_EQ(100, d2.max()); | 61 EXPECT_EQ(100, d2.max()); |
63 EXPECT_TRUE(empty.Equals(d2)); | 62 EXPECT_TRUE(empty.Equals(d2)); |
64 EXPECT_EQ(empty, d2); | 63 EXPECT_EQ(empty, d2); |
65 EXPECT_TRUE(d2.Equals(empty)); | 64 EXPECT_TRUE(d2.Equals(empty)); |
66 EXPECT_EQ(d2, empty); | 65 EXPECT_EQ(d2, empty); |
67 | 66 |
68 Interval<int32> max_less_than_min(40, 20); | 67 Interval<int32_t> max_less_than_min(40, 20); |
69 EXPECT_TRUE(max_less_than_min.Empty()); | 68 EXPECT_TRUE(max_less_than_min.Empty()); |
70 EXPECT_EQ(40, max_less_than_min.min()); | 69 EXPECT_EQ(40, max_less_than_min.min()); |
71 EXPECT_EQ(20, max_less_than_min.max()); | 70 EXPECT_EQ(20, max_less_than_min.max()); |
72 | 71 |
73 Interval<int> d3(10, 20); | 72 Interval<int> d3(10, 20); |
74 d3.Clear(); | 73 d3.Clear(); |
75 EXPECT_TRUE(d3.Empty()); | 74 EXPECT_TRUE(d3.Empty()); |
76 } | 75 } |
77 | 76 |
78 TEST_F(IntervalTest, GettersSetters) { | 77 TEST_F(IntervalTest, GettersSetters) { |
79 Interval<int32> d1(100, 200); | 78 Interval<int32_t> d1(100, 200); |
80 | 79 |
81 // SetMin: | 80 // SetMin: |
82 d1.SetMin(30); | 81 d1.SetMin(30); |
83 EXPECT_EQ(30, d1.min()); | 82 EXPECT_EQ(30, d1.min()); |
84 EXPECT_EQ(200, d1.max()); | 83 EXPECT_EQ(200, d1.max()); |
85 | 84 |
86 // SetMax: | 85 // SetMax: |
87 d1.SetMax(220); | 86 d1.SetMax(220); |
88 EXPECT_EQ(30, d1.min()); | 87 EXPECT_EQ(30, d1.min()); |
89 EXPECT_EQ(220, d1.max()); | 88 EXPECT_EQ(220, d1.max()); |
90 | 89 |
91 // Set: | 90 // Set: |
92 d1.Clear(); | 91 d1.Clear(); |
93 d1.Set(30, 220); | 92 d1.Set(30, 220); |
94 EXPECT_EQ(30, d1.min()); | 93 EXPECT_EQ(30, d1.min()); |
95 EXPECT_EQ(220, d1.max()); | 94 EXPECT_EQ(220, d1.max()); |
96 | 95 |
97 // SpanningUnion: | 96 // SpanningUnion: |
98 Interval<int32> d2; | 97 Interval<int32_t> d2; |
99 EXPECT_TRUE(!d1.SpanningUnion(d2)); | 98 EXPECT_TRUE(!d1.SpanningUnion(d2)); |
100 EXPECT_EQ(30, d1.min()); | 99 EXPECT_EQ(30, d1.min()); |
101 EXPECT_EQ(220, d1.max()); | 100 EXPECT_EQ(220, d1.max()); |
102 | 101 |
103 EXPECT_TRUE(d2.SpanningUnion(d1)); | 102 EXPECT_TRUE(d2.SpanningUnion(d1)); |
104 EXPECT_EQ(30, d2.min()); | 103 EXPECT_EQ(30, d2.min()); |
105 EXPECT_EQ(220, d2.max()); | 104 EXPECT_EQ(220, d2.max()); |
106 | 105 |
107 d2.SetMin(40); | 106 d2.SetMin(40); |
108 d2.SetMax(100); | 107 d2.SetMax(100); |
(...skipping 23 matching lines...) Expand all Loading... |
132 d2.SetMax(0); | 131 d2.SetMax(0); |
133 EXPECT_TRUE(!d1.SpanningUnion(d2)); | 132 EXPECT_TRUE(!d1.SpanningUnion(d2)); |
134 EXPECT_EQ(0, d1.min()); | 133 EXPECT_EQ(0, d1.min()); |
135 EXPECT_EQ(500, d1.max()); | 134 EXPECT_EQ(500, d1.max()); |
136 EXPECT_TRUE(d2.SpanningUnion(d1)); | 135 EXPECT_TRUE(d2.SpanningUnion(d1)); |
137 EXPECT_EQ(0, d2.min()); | 136 EXPECT_EQ(0, d2.min()); |
138 EXPECT_EQ(500, d2.max()); | 137 EXPECT_EQ(500, d2.max()); |
139 } | 138 } |
140 | 139 |
141 TEST_F(IntervalTest, CoveringOps) { | 140 TEST_F(IntervalTest, CoveringOps) { |
142 const Interval<int64> empty; | 141 const Interval<int64_t> empty; |
143 const Interval<int64> d(100, 200); | 142 const Interval<int64_t> d(100, 200); |
144 const Interval<int64> d1(0, 50); | 143 const Interval<int64_t> d1(0, 50); |
145 const Interval<int64> d2(50, 110); | 144 const Interval<int64_t> d2(50, 110); |
146 const Interval<int64> d3(110, 180); | 145 const Interval<int64_t> d3(110, 180); |
147 const Interval<int64> d4(180, 220); | 146 const Interval<int64_t> d4(180, 220); |
148 const Interval<int64> d5(220, 300); | 147 const Interval<int64_t> d5(220, 300); |
149 const Interval<int64> d6(100, 150); | 148 const Interval<int64_t> d6(100, 150); |
150 const Interval<int64> d7(150, 200); | 149 const Interval<int64_t> d7(150, 200); |
151 const Interval<int64> d8(0, 300); | 150 const Interval<int64_t> d8(0, 300); |
152 | 151 |
153 // Intersection: | 152 // Intersection: |
154 EXPECT_TRUE(d.Intersects(d)); | 153 EXPECT_TRUE(d.Intersects(d)); |
155 EXPECT_TRUE(!empty.Intersects(d) && !d.Intersects(empty)); | 154 EXPECT_TRUE(!empty.Intersects(d) && !d.Intersects(empty)); |
156 EXPECT_TRUE(!d.Intersects(d1) && !d1.Intersects(d)); | 155 EXPECT_TRUE(!d.Intersects(d1) && !d1.Intersects(d)); |
157 EXPECT_TRUE(d.Intersects(d2) && d2.Intersects(d)); | 156 EXPECT_TRUE(d.Intersects(d2) && d2.Intersects(d)); |
158 EXPECT_TRUE(d.Intersects(d3) && d3.Intersects(d)); | 157 EXPECT_TRUE(d.Intersects(d3) && d3.Intersects(d)); |
159 EXPECT_TRUE(d.Intersects(d4) && d4.Intersects(d)); | 158 EXPECT_TRUE(d.Intersects(d4) && d4.Intersects(d)); |
160 EXPECT_TRUE(!d.Intersects(d5) && !d5.Intersects(d)); | 159 EXPECT_TRUE(!d.Intersects(d5) && !d5.Intersects(d)); |
161 EXPECT_TRUE(d.Intersects(d6) && d6.Intersects(d)); | 160 EXPECT_TRUE(d.Intersects(d6) && d6.Intersects(d)); |
162 EXPECT_TRUE(d.Intersects(d7) && d7.Intersects(d)); | 161 EXPECT_TRUE(d.Intersects(d7) && d7.Intersects(d)); |
163 EXPECT_TRUE(d.Intersects(d8) && d8.Intersects(d)); | 162 EXPECT_TRUE(d.Intersects(d8) && d8.Intersects(d)); |
164 | 163 |
165 Interval<int64> i; | 164 Interval<int64_t> i; |
166 EXPECT_TRUE(d.Intersects(d, &i) && d.Equals(i)); | 165 EXPECT_TRUE(d.Intersects(d, &i) && d.Equals(i)); |
167 EXPECT_TRUE(!empty.Intersects(d, NULL) && !d.Intersects(empty, NULL)); | 166 EXPECT_TRUE(!empty.Intersects(d, NULL) && !d.Intersects(empty, NULL)); |
168 EXPECT_TRUE(!d.Intersects(d1, NULL) && !d1.Intersects(d, NULL)); | 167 EXPECT_TRUE(!d.Intersects(d1, NULL) && !d1.Intersects(d, NULL)); |
169 EXPECT_TRUE(d.Intersects(d2, &i) && i.Equals(Interval<int64>(100, 110))); | 168 EXPECT_TRUE(d.Intersects(d2, &i) && i.Equals(Interval<int64_t>(100, 110))); |
170 EXPECT_TRUE(d2.Intersects(d, &i) && i.Equals(Interval<int64>(100, 110))); | 169 EXPECT_TRUE(d2.Intersects(d, &i) && i.Equals(Interval<int64_t>(100, 110))); |
171 EXPECT_TRUE(d.Intersects(d3, &i) && i.Equals(d3)); | 170 EXPECT_TRUE(d.Intersects(d3, &i) && i.Equals(d3)); |
172 EXPECT_TRUE(d3.Intersects(d, &i) && i.Equals(d3)); | 171 EXPECT_TRUE(d3.Intersects(d, &i) && i.Equals(d3)); |
173 EXPECT_TRUE(d.Intersects(d4, &i) && i.Equals(Interval<int64>(180, 200))); | 172 EXPECT_TRUE(d.Intersects(d4, &i) && i.Equals(Interval<int64_t>(180, 200))); |
174 EXPECT_TRUE(d4.Intersects(d, &i) && i.Equals(Interval<int64>(180, 200))); | 173 EXPECT_TRUE(d4.Intersects(d, &i) && i.Equals(Interval<int64_t>(180, 200))); |
175 EXPECT_TRUE(!d.Intersects(d5, NULL) && !d5.Intersects(d, NULL)); | 174 EXPECT_TRUE(!d.Intersects(d5, NULL) && !d5.Intersects(d, NULL)); |
176 EXPECT_TRUE(d.Intersects(d6, &i) && i.Equals(d6)); | 175 EXPECT_TRUE(d.Intersects(d6, &i) && i.Equals(d6)); |
177 EXPECT_TRUE(d6.Intersects(d, &i) && i.Equals(d6)); | 176 EXPECT_TRUE(d6.Intersects(d, &i) && i.Equals(d6)); |
178 EXPECT_TRUE(d.Intersects(d7, &i) && i.Equals(d7)); | 177 EXPECT_TRUE(d.Intersects(d7, &i) && i.Equals(d7)); |
179 EXPECT_TRUE(d7.Intersects(d, &i) && i.Equals(d7)); | 178 EXPECT_TRUE(d7.Intersects(d, &i) && i.Equals(d7)); |
180 EXPECT_TRUE(d.Intersects(d8, &i) && i.Equals(d)); | 179 EXPECT_TRUE(d.Intersects(d8, &i) && i.Equals(d)); |
181 EXPECT_TRUE(d8.Intersects(d, &i) && i.Equals(d)); | 180 EXPECT_TRUE(d8.Intersects(d, &i) && i.Equals(d)); |
182 | 181 |
183 // Test IntersectsWith(). | 182 // Test IntersectsWith(). |
184 // Arguments are TestIntersect(i1, i2, changes_i1, changes_i2, result). | 183 // Arguments are TestIntersect(i1, i2, changes_i1, changes_i2, result). |
185 TestIntersect(empty, d, false, true, empty); | 184 TestIntersect(empty, d, false, true, empty); |
186 TestIntersect(d, d1, true, true, empty); | 185 TestIntersect(d, d1, true, true, empty); |
187 TestIntersect(d1, d2, true, true, empty); | 186 TestIntersect(d1, d2, true, true, empty); |
188 TestIntersect(d, d2, true, true, Interval<int64>(100, 110)); | 187 TestIntersect(d, d2, true, true, Interval<int64_t>(100, 110)); |
189 TestIntersect(d8, d, true, false, d); | 188 TestIntersect(d8, d, true, false, d); |
190 TestIntersect(d8, d1, true, false, d1); | 189 TestIntersect(d8, d1, true, false, d1); |
191 TestIntersect(d8, d5, true, false, d5); | 190 TestIntersect(d8, d5, true, false, d5); |
192 | 191 |
193 // Contains: | 192 // Contains: |
194 EXPECT_TRUE(!empty.Contains(d) && !d.Contains(empty)); | 193 EXPECT_TRUE(!empty.Contains(d) && !d.Contains(empty)); |
195 EXPECT_TRUE(d.Contains(d)); | 194 EXPECT_TRUE(d.Contains(d)); |
196 EXPECT_TRUE(!d.Contains(d1) && !d1.Contains(d)); | 195 EXPECT_TRUE(!d.Contains(d1) && !d1.Contains(d)); |
197 EXPECT_TRUE(!d.Contains(d2) && !d2.Contains(d)); | 196 EXPECT_TRUE(!d.Contains(d2) && !d2.Contains(d)); |
198 EXPECT_TRUE(d.Contains(d3) && !d3.Contains(d)); | 197 EXPECT_TRUE(d.Contains(d3) && !d3.Contains(d)); |
199 EXPECT_TRUE(!d.Contains(d4) && !d4.Contains(d)); | 198 EXPECT_TRUE(!d.Contains(d4) && !d4.Contains(d)); |
200 EXPECT_TRUE(!d.Contains(d5) && !d5.Contains(d)); | 199 EXPECT_TRUE(!d.Contains(d5) && !d5.Contains(d)); |
201 EXPECT_TRUE(d.Contains(d6) && !d6.Contains(d)); | 200 EXPECT_TRUE(d.Contains(d6) && !d6.Contains(d)); |
202 EXPECT_TRUE(d.Contains(d7) && !d7.Contains(d)); | 201 EXPECT_TRUE(d.Contains(d7) && !d7.Contains(d)); |
203 EXPECT_TRUE(!d.Contains(d8) && d8.Contains(d)); | 202 EXPECT_TRUE(!d.Contains(d8) && d8.Contains(d)); |
204 | 203 |
205 EXPECT_TRUE(d.Contains(100)); | 204 EXPECT_TRUE(d.Contains(100)); |
206 EXPECT_TRUE(!d.Contains(200)); | 205 EXPECT_TRUE(!d.Contains(200)); |
207 EXPECT_TRUE(d.Contains(150)); | 206 EXPECT_TRUE(d.Contains(150)); |
208 EXPECT_TRUE(!d.Contains(99)); | 207 EXPECT_TRUE(!d.Contains(99)); |
209 EXPECT_TRUE(!d.Contains(201)); | 208 EXPECT_TRUE(!d.Contains(201)); |
210 | 209 |
211 // Difference: | 210 // Difference: |
212 vector<Interval<int64>*> diff; | 211 vector<Interval<int64_t>*> diff; |
213 | 212 |
214 EXPECT_TRUE(!d.Difference(empty, &diff)); | 213 EXPECT_TRUE(!d.Difference(empty, &diff)); |
215 EXPECT_EQ(1u, diff.size()); | 214 EXPECT_EQ(1u, diff.size()); |
216 EXPECT_EQ(100u, diff[0]->min()); | 215 EXPECT_EQ(100u, diff[0]->min()); |
217 EXPECT_EQ(200u, diff[0]->max()); | 216 EXPECT_EQ(200u, diff[0]->max()); |
218 STLDeleteElements(&diff); | 217 STLDeleteElements(&diff); |
219 EXPECT_TRUE(!empty.Difference(d, &diff) && diff.empty()); | 218 EXPECT_TRUE(!empty.Difference(d, &diff) && diff.empty()); |
220 | 219 |
221 EXPECT_TRUE(d.Difference(d, &diff) && diff.empty()); | 220 EXPECT_TRUE(d.Difference(d, &diff) && diff.empty()); |
222 EXPECT_TRUE(!d.Difference(d1, &diff)); | 221 EXPECT_TRUE(!d.Difference(d1, &diff)); |
223 EXPECT_EQ(1u, diff.size()); | 222 EXPECT_EQ(1u, diff.size()); |
224 EXPECT_EQ(100u, diff[0]->min()); | 223 EXPECT_EQ(100u, diff[0]->min()); |
225 EXPECT_EQ(200u, diff[0]->max()); | 224 EXPECT_EQ(200u, diff[0]->max()); |
226 STLDeleteElements(&diff); | 225 STLDeleteElements(&diff); |
227 | 226 |
228 Interval<int64> lo; | 227 Interval<int64_t> lo; |
229 Interval<int64> hi; | 228 Interval<int64_t> hi; |
230 | 229 |
231 EXPECT_TRUE(d.Difference(d2, &lo, &hi)); | 230 EXPECT_TRUE(d.Difference(d2, &lo, &hi)); |
232 EXPECT_TRUE(lo.Empty()); | 231 EXPECT_TRUE(lo.Empty()); |
233 EXPECT_EQ(110u, hi.min()); | 232 EXPECT_EQ(110u, hi.min()); |
234 EXPECT_EQ(200u, hi.max()); | 233 EXPECT_EQ(200u, hi.max()); |
235 EXPECT_TRUE(d.Difference(d2, &diff)); | 234 EXPECT_TRUE(d.Difference(d2, &diff)); |
236 EXPECT_EQ(1u, diff.size()); | 235 EXPECT_EQ(1u, diff.size()); |
237 EXPECT_EQ(110u, diff[0]->min()); | 236 EXPECT_EQ(110u, diff[0]->min()); |
238 EXPECT_EQ(200u, diff[0]->max()); | 237 EXPECT_EQ(200u, diff[0]->max()); |
239 STLDeleteElements(&diff); | 238 STLDeleteElements(&diff); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 const Interval<pair<int, int>> d2({1, 2}, {4, 3}); | 327 const Interval<pair<int, int>> d2({1, 2}, {4, 3}); |
329 EXPECT_EQ("a", d1.min()); | 328 EXPECT_EQ("a", d1.min()); |
330 EXPECT_EQ("b", d1.max()); | 329 EXPECT_EQ("b", d1.max()); |
331 EXPECT_EQ(std::make_pair(1, 2), d2.min()); | 330 EXPECT_EQ(std::make_pair(1, 2), d2.min()); |
332 EXPECT_EQ(std::make_pair(4, 3), d2.max()); | 331 EXPECT_EQ(std::make_pair(4, 3), d2.max()); |
333 } | 332 } |
334 | 333 |
335 } // unnamed namespace | 334 } // unnamed namespace |
336 } // namespace test | 335 } // namespace test |
337 } // namespace net | 336 } // namespace net |
OLD | NEW |