blob: 3fb483aa82a26b34c8fa82a260cd929d6234bb87 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2019 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.
4
5#include "net/third_party/quiche/src/quic/core/quic_interval_set.h"
6
7#include <stdarg.h>
vasilvv872e7a32019-03-12 16:42:44 -07008
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include <algorithm>
10#include <iostream>
11#include <iterator>
12#include <limits>
vasilvv872e7a32019-03-12 16:42:44 -070013#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#include <vector>
15
QUICHE teama6ef0a62019-03-07 20:34:33 -050016#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
17
18namespace quic {
19namespace test {
20namespace {
21
22using ::testing::ElementsAreArray;
23
24class QuicIntervalSetTest : public QuicTest {
25 protected:
26 virtual void SetUp() {
27 // Initialize two QuicIntervalSets for union, intersection, and difference
28 // tests
29 is.Add(100, 200);
30 is.Add(300, 400);
31 is.Add(500, 600);
32 is.Add(700, 800);
33 is.Add(900, 1000);
34 is.Add(1100, 1200);
35 is.Add(1300, 1400);
36 is.Add(1500, 1600);
37 is.Add(1700, 1800);
38 is.Add(1900, 2000);
39 is.Add(2100, 2200);
40
41 // Lots of different cases:
42 other.Add(50, 70); // disjoint, at the beginning
43 other.Add(2250, 2270); // disjoint, at the end
44 other.Add(650, 670); // disjoint, in the middle
45 other.Add(350, 360); // included
46 other.Add(370, 380); // also included (two at once)
47 other.Add(470, 530); // overlaps low end
48 other.Add(770, 830); // overlaps high end
49 other.Add(870, 900); // meets at low end
50 other.Add(1200, 1230); // meets at high end
51 other.Add(1270, 1830); // overlaps multiple ranges
52 }
53
54 virtual void TearDown() {
55 is.Clear();
56 EXPECT_TRUE(is.Empty());
57 other.Clear();
58 EXPECT_TRUE(other.Empty());
59 }
60 QuicIntervalSet<int> is;
61 QuicIntervalSet<int> other;
62};
63
64TEST_F(QuicIntervalSetTest, IsDisjoint) {
65 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(0, 99)));
66 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(0, 100)));
67 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(200, 200)));
68 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(200, 299)));
69 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(400, 407)));
70 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(405, 499)));
71 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(2300, 2300)));
72 EXPECT_TRUE(
73 is.IsDisjoint(QuicInterval<int>(2300, std::numeric_limits<int>::max())));
74 EXPECT_FALSE(is.IsDisjoint(QuicInterval<int>(100, 105)));
75 EXPECT_FALSE(is.IsDisjoint(QuicInterval<int>(199, 300)));
76 EXPECT_FALSE(is.IsDisjoint(QuicInterval<int>(250, 450)));
77 EXPECT_FALSE(is.IsDisjoint(QuicInterval<int>(299, 400)));
78 EXPECT_FALSE(is.IsDisjoint(QuicInterval<int>(250, 2000)));
79 EXPECT_FALSE(
80 is.IsDisjoint(QuicInterval<int>(2199, std::numeric_limits<int>::max())));
81 // Empty intervals.
82 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(90, 90)));
83 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(100, 100)));
84 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(100, 90)));
85 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(150, 150)));
86 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(200, 200)));
87 EXPECT_TRUE(is.IsDisjoint(QuicInterval<int>(400, 300)));
88}
89
90// Base helper method for verifying the contents of an interval set.
91// Returns true iff <is> contains <count> intervals whose successive
92// endpoints match the sequence of args in <ap>:
93static bool VA_Check(const QuicIntervalSet<int>& is, int count, va_list ap) {
94 std::vector<QuicInterval<int>> intervals(is.begin(), is.end());
95 if (count != static_cast<int>(intervals.size())) {
QUICHE team2d5d2342019-03-21 13:01:37 -070096 QUIC_LOG(ERROR) << "Expected " << count << " intervals, got "
97 << intervals.size() << ": " << is;
QUICHE teama6ef0a62019-03-07 20:34:33 -050098 return false;
99 }
100 if (count != static_cast<int>(is.Size())) {
QUICHE team2d5d2342019-03-21 13:01:37 -0700101 QUIC_LOG(ERROR) << "Expected " << count << " intervals, got Size "
102 << is.Size() << ": " << is;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500103 return false;
104 }
105 bool result = true;
106 for (int i = 0; i < count; i++) {
107 int min = va_arg(ap, int);
108 int max = va_arg(ap, int);
109 if (min != intervals[i].min() || max != intervals[i].max()) {
QUICHE team2d5d2342019-03-21 13:01:37 -0700110 QUIC_LOG(ERROR) << "Expected: [" << min << ", " << max << ") got "
111 << intervals[i] << " in " << is;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112 result = false;
113 }
114 }
115 return result;
116}
117
118static bool Check(const QuicIntervalSet<int>& is, int count, ...) {
119 va_list ap;
120 va_start(ap, count);
121 const bool result = VA_Check(is, count, ap);
122 va_end(ap);
123 return result;
124}
125
126// Some helper functions for testing Contains and Find, which are logically the
127// same.
128static void TestContainsAndFind(const QuicIntervalSet<int>& is, int value) {
129 EXPECT_TRUE(is.Contains(value)) << "Set does not contain " << value;
130 auto it = is.Find(value);
131 EXPECT_NE(it, is.end()) << "No iterator to interval containing " << value;
132 EXPECT_TRUE(it->Contains(value)) << "Iterator does not contain " << value;
133}
134
135static void TestContainsAndFind(const QuicIntervalSet<int>& is,
136 int min,
137 int max) {
138 EXPECT_TRUE(is.Contains(min, max))
139 << "Set does not contain interval with min " << min << "and max " << max;
140 auto it = is.Find(min, max);
141 EXPECT_NE(it, is.end()) << "No iterator to interval with min " << min
142 << "and max " << max;
143 EXPECT_TRUE(it->Contains(QuicInterval<int>(min, max)))
144 << "Iterator does not contain interval with min " << min << "and max "
145 << max;
146}
147
148static void TestNotContainsAndFind(const QuicIntervalSet<int>& is, int value) {
149 EXPECT_FALSE(is.Contains(value)) << "Set contains " << value;
150 auto it = is.Find(value);
151 EXPECT_EQ(it, is.end()) << "There is iterator to interval containing "
152 << value;
153}
154
155static void TestNotContainsAndFind(const QuicIntervalSet<int>& is,
156 int min,
157 int max) {
158 EXPECT_FALSE(is.Contains(min, max))
159 << "Set contains interval with min " << min << "and max " << max;
160 auto it = is.Find(min, max);
161 EXPECT_EQ(it, is.end()) << "There is iterator to interval with min " << min
162 << "and max " << max;
163}
164
165TEST_F(QuicIntervalSetTest, AddOptimizedForAppend) {
166 QuicIntervalSet<int> empty_one, empty_two;
167 empty_one.AddOptimizedForAppend(QuicInterval<int>(0, 99));
168 EXPECT_TRUE(Check(empty_one, 1, 0, 99));
169
170 empty_two.AddOptimizedForAppend(1, 50);
171 EXPECT_TRUE(Check(empty_two, 1, 1, 50));
172
173 QuicIntervalSet<int> iset;
174 iset.AddOptimizedForAppend(100, 150);
175 iset.AddOptimizedForAppend(200, 250);
176 EXPECT_TRUE(Check(iset, 2, 100, 150, 200, 250));
177
178 iset.AddOptimizedForAppend(199, 200);
179 EXPECT_TRUE(Check(iset, 2, 100, 150, 199, 250));
180
181 iset.AddOptimizedForAppend(251, 260);
182 EXPECT_TRUE(Check(iset, 3, 100, 150, 199, 250, 251, 260));
183
184 iset.AddOptimizedForAppend(252, 260);
185 EXPECT_TRUE(Check(iset, 3, 100, 150, 199, 250, 251, 260));
186
187 iset.AddOptimizedForAppend(252, 300);
188 EXPECT_TRUE(Check(iset, 3, 100, 150, 199, 250, 251, 300));
189
190 iset.AddOptimizedForAppend(300, 350);
191 EXPECT_TRUE(Check(iset, 3, 100, 150, 199, 250, 251, 350));
192}
193
194TEST_F(QuicIntervalSetTest, QuicIntervalSetBasic) {
195 // Test Add, Get, Contains and Find
196 QuicIntervalSet<int> iset;
197 EXPECT_TRUE(iset.Empty());
198 EXPECT_EQ(0u, iset.Size());
199 iset.Add(100, 200);
200 EXPECT_FALSE(iset.Empty());
201 EXPECT_EQ(1u, iset.Size());
202 iset.Add(100, 150);
203 iset.Add(150, 200);
204 iset.Add(130, 170);
205 iset.Add(90, 150);
206 iset.Add(170, 220);
207 iset.Add(300, 400);
208 iset.Add(250, 450);
209 EXPECT_FALSE(iset.Empty());
210 EXPECT_EQ(2u, iset.Size());
211 EXPECT_TRUE(Check(iset, 2, 90, 220, 250, 450));
212
213 // Test two intervals with a.max == b.min, that will just join up.
214 iset.Clear();
215 iset.Add(100, 200);
216 iset.Add(200, 300);
217 EXPECT_FALSE(iset.Empty());
218 EXPECT_EQ(1u, iset.Size());
219 EXPECT_TRUE(Check(iset, 1, 100, 300));
220
221 // Test adding two sets together.
222 iset.Clear();
223 QuicIntervalSet<int> iset_add;
224 iset.Add(100, 200);
225 iset.Add(100, 150);
226 iset.Add(150, 200);
227 iset.Add(130, 170);
228 iset_add.Add(90, 150);
229 iset_add.Add(170, 220);
230 iset_add.Add(300, 400);
231 iset_add.Add(250, 450);
232
233 iset.Union(iset_add);
234 EXPECT_FALSE(iset.Empty());
235 EXPECT_EQ(2u, iset.Size());
236 EXPECT_TRUE(Check(iset, 2, 90, 220, 250, 450));
237
238 // Test begin()/end(), and rbegin()/rend()
239 // to iterate over intervals.
240 {
241 std::vector<QuicInterval<int>> expected(iset.begin(), iset.end());
242
243 std::vector<QuicInterval<int>> actual1;
244 std::copy(iset.begin(), iset.end(), back_inserter(actual1));
245 ASSERT_EQ(expected.size(), actual1.size());
246
247 std::vector<QuicInterval<int>> actual2;
248 std::copy(iset.begin(), iset.end(), back_inserter(actual2));
249 ASSERT_EQ(expected.size(), actual2.size());
250
251 for (size_t i = 0; i < expected.size(); i++) {
252 EXPECT_EQ(expected[i].min(), actual1[i].min());
253 EXPECT_EQ(expected[i].max(), actual1[i].max());
254
255 EXPECT_EQ(expected[i].min(), actual2[i].min());
256 EXPECT_EQ(expected[i].max(), actual2[i].max());
257 }
258
259 // Ensure that the rbegin()/rend() iterators correctly yield the intervals
260 // in reverse order.
261 EXPECT_THAT(std::vector<QuicInterval<int>>(iset.rbegin(), iset.rend()),
262 ElementsAreArray(expected.rbegin(), expected.rend()));
263 }
264
265 TestNotContainsAndFind(iset, 89);
266 TestContainsAndFind(iset, 90);
267 TestContainsAndFind(iset, 120);
268 TestContainsAndFind(iset, 219);
269 TestNotContainsAndFind(iset, 220);
270 TestNotContainsAndFind(iset, 235);
271 TestNotContainsAndFind(iset, 249);
272 TestContainsAndFind(iset, 250);
273 TestContainsAndFind(iset, 300);
274 TestContainsAndFind(iset, 449);
275 TestNotContainsAndFind(iset, 450);
276 TestNotContainsAndFind(iset, 451);
277
278 TestNotContainsAndFind(iset, 50, 60);
279 TestNotContainsAndFind(iset, 50, 90);
280 TestNotContainsAndFind(iset, 50, 200);
281 TestNotContainsAndFind(iset, 90, 90);
282 TestContainsAndFind(iset, 90, 200);
283 TestContainsAndFind(iset, 100, 200);
284 TestContainsAndFind(iset, 100, 220);
285 TestNotContainsAndFind(iset, 100, 221);
286 TestNotContainsAndFind(iset, 220, 220);
287 TestNotContainsAndFind(iset, 240, 300);
288 TestContainsAndFind(iset, 250, 300);
289 TestContainsAndFind(iset, 260, 300);
290 TestContainsAndFind(iset, 300, 450);
291 TestNotContainsAndFind(iset, 300, 451);
292
293 QuicIntervalSet<int> iset_contains;
294 iset_contains.Add(50, 90);
295 EXPECT_FALSE(iset.Contains(iset_contains));
296 iset_contains.Clear();
297
298 iset_contains.Add(90, 200);
299 EXPECT_TRUE(iset.Contains(iset_contains));
300 iset_contains.Add(100, 200);
301 EXPECT_TRUE(iset.Contains(iset_contains));
302 iset_contains.Add(100, 220);
303 EXPECT_TRUE(iset.Contains(iset_contains));
304 iset_contains.Add(250, 300);
305 EXPECT_TRUE(iset.Contains(iset_contains));
306 iset_contains.Add(300, 450);
307 EXPECT_TRUE(iset.Contains(iset_contains));
308 iset_contains.Add(300, 451);
309 EXPECT_FALSE(iset.Contains(iset_contains));
310 EXPECT_FALSE(iset.Contains(QuicInterval<int>()));
311 EXPECT_FALSE(iset.Contains(QuicIntervalSet<int>()));
312}
313
314TEST_F(QuicIntervalSetTest, QuicIntervalSetContainsEmpty) {
315 const QuicIntervalSet<int> empty;
316 const QuicIntervalSet<int> other_empty;
317 const QuicIntervalSet<int> non_empty({{10, 20}, {40, 50}});
318 EXPECT_FALSE(empty.Contains(empty));
319 EXPECT_FALSE(empty.Contains(other_empty));
320 EXPECT_FALSE(empty.Contains(non_empty));
321 EXPECT_FALSE(non_empty.Contains(empty));
322}
323
324TEST_F(QuicIntervalSetTest, Equality) {
325 QuicIntervalSet<int> is_copy = is;
326 EXPECT_EQ(is, is);
327 EXPECT_EQ(is, is_copy);
328 EXPECT_NE(is, other);
329 EXPECT_NE(is, QuicIntervalSet<int>());
330 EXPECT_EQ(QuicIntervalSet<int>(), QuicIntervalSet<int>());
331}
332
333TEST_F(QuicIntervalSetTest, LowerAndUpperBound) {
334 QuicIntervalSet<int> intervals;
335 intervals.Add(10, 20);
336 intervals.Add(30, 40);
337
338 // [10, 20) [30, 40) end
339 // ^ LowerBound(5)
340 // ^ LowerBound(10)
341 // ^ LowerBound(15)
342 // ^ LowerBound(20)
343 // ^ LowerBound(25)
344 // ^ LowerBound(30)
345 // ^ LowerBound(35)
346 // ^ LowerBound(40)
347 // ^ LowerBound(50)
348 EXPECT_EQ(intervals.LowerBound(5)->min(), 10);
349 EXPECT_EQ(intervals.LowerBound(10)->min(), 10);
350 EXPECT_EQ(intervals.LowerBound(15)->min(), 10);
351 EXPECT_EQ(intervals.LowerBound(20)->min(), 30);
352 EXPECT_EQ(intervals.LowerBound(25)->min(), 30);
353 EXPECT_EQ(intervals.LowerBound(30)->min(), 30);
354 EXPECT_EQ(intervals.LowerBound(35)->min(), 30);
355 EXPECT_EQ(intervals.LowerBound(40), intervals.end());
356 EXPECT_EQ(intervals.LowerBound(50), intervals.end());
357
358 // [10, 20) [30, 40) end
359 // ^ UpperBound(5)
360 // ^ UpperBound(10)
361 // ^ UpperBound(15)
362 // ^ UpperBound(20)
363 // ^ UpperBound(25)
364 // ^ UpperBound(30)
365 // ^ UpperBound(35)
366 // ^ UpperBound(40)
367 // ^ UpperBound(50)
368 EXPECT_EQ(intervals.UpperBound(5)->min(), 10);
369 EXPECT_EQ(intervals.UpperBound(10)->min(), 30);
370 EXPECT_EQ(intervals.UpperBound(15)->min(), 30);
371 EXPECT_EQ(intervals.UpperBound(20)->min(), 30);
372 EXPECT_EQ(intervals.UpperBound(25)->min(), 30);
373 EXPECT_EQ(intervals.UpperBound(30), intervals.end());
374 EXPECT_EQ(intervals.UpperBound(35), intervals.end());
375 EXPECT_EQ(intervals.UpperBound(40), intervals.end());
376 EXPECT_EQ(intervals.UpperBound(50), intervals.end());
377}
378
379TEST_F(QuicIntervalSetTest, SpanningInterval) {
380 // Spanning interval of an empty set is empty:
381 {
382 QuicIntervalSet<int> iset;
383 const QuicInterval<int>& ival = iset.SpanningInterval();
384 EXPECT_TRUE(ival.Empty());
385 }
386
387 // Spanning interval of a set with one interval is that interval:
388 {
389 QuicIntervalSet<int> iset;
390 iset.Add(100, 200);
391 const QuicInterval<int>& ival = iset.SpanningInterval();
392 EXPECT_EQ(100, ival.min());
393 EXPECT_EQ(200, ival.max());
394 }
395
396 // Spanning interval of a set with multiple elements is determined
397 // by the endpoints of the first and last element:
398 {
399 const QuicInterval<int>& ival = is.SpanningInterval();
400 EXPECT_EQ(100, ival.min());
401 EXPECT_EQ(2200, ival.max());
402 }
403 {
404 const QuicInterval<int>& ival = other.SpanningInterval();
405 EXPECT_EQ(50, ival.min());
406 EXPECT_EQ(2270, ival.max());
407 }
408}
409
410TEST_F(QuicIntervalSetTest, QuicIntervalSetUnion) {
411 is.Union(other);
412 EXPECT_TRUE(Check(is, 12, 50, 70, 100, 200, 300, 400, 470, 600, 650, 670, 700,
413 830, 870, 1000, 1100, 1230, 1270, 1830, 1900, 2000, 2100,
414 2200, 2250, 2270));
415}
416
417TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersection) {
418 EXPECT_TRUE(is.Intersects(other));
419 EXPECT_TRUE(other.Intersects(is));
420 is.Intersection(other);
421 EXPECT_TRUE(Check(is, 7, 350, 360, 370, 380, 500, 530, 770, 800, 1300, 1400,
422 1500, 1600, 1700, 1800));
423 EXPECT_TRUE(is.Intersects(other));
424 EXPECT_TRUE(other.Intersects(is));
425}
426
427TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionBothEmpty) {
vasilvvc48c8712019-03-11 13:38:16 -0700428 QuicIntervalSet<std::string> mine, theirs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500429 EXPECT_FALSE(mine.Intersects(theirs));
430 EXPECT_FALSE(theirs.Intersects(mine));
431 mine.Intersection(theirs);
432 EXPECT_TRUE(mine.Empty());
433 EXPECT_FALSE(mine.Intersects(theirs));
434 EXPECT_FALSE(theirs.Intersects(mine));
435}
436
437TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionEmptyMine) {
vasilvvc48c8712019-03-11 13:38:16 -0700438 QuicIntervalSet<std::string> mine;
439 QuicIntervalSet<std::string> theirs("a", "b");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500440 EXPECT_FALSE(mine.Intersects(theirs));
441 EXPECT_FALSE(theirs.Intersects(mine));
442 mine.Intersection(theirs);
443 EXPECT_TRUE(mine.Empty());
444 EXPECT_FALSE(mine.Intersects(theirs));
445 EXPECT_FALSE(theirs.Intersects(mine));
446}
447
448TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionEmptyTheirs) {
vasilvvc48c8712019-03-11 13:38:16 -0700449 QuicIntervalSet<std::string> mine("a", "b");
450 QuicIntervalSet<std::string> theirs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500451 EXPECT_FALSE(mine.Intersects(theirs));
452 EXPECT_FALSE(theirs.Intersects(mine));
453 mine.Intersection(theirs);
454 EXPECT_TRUE(mine.Empty());
455 EXPECT_FALSE(mine.Intersects(theirs));
456 EXPECT_FALSE(theirs.Intersects(mine));
457}
458
459TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionTheirsBeforeMine) {
vasilvvc48c8712019-03-11 13:38:16 -0700460 QuicIntervalSet<std::string> mine("y", "z");
461 QuicIntervalSet<std::string> theirs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500462 theirs.Add("a", "b");
463 theirs.Add("c", "d");
464 EXPECT_FALSE(mine.Intersects(theirs));
465 EXPECT_FALSE(theirs.Intersects(mine));
466 mine.Intersection(theirs);
467 EXPECT_TRUE(mine.Empty());
468 EXPECT_FALSE(mine.Intersects(theirs));
469 EXPECT_FALSE(theirs.Intersects(mine));
470}
471
472TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionMineBeforeTheirs) {
vasilvvc48c8712019-03-11 13:38:16 -0700473 QuicIntervalSet<std::string> mine;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500474 mine.Add("a", "b");
475 mine.Add("c", "d");
vasilvvc48c8712019-03-11 13:38:16 -0700476 QuicIntervalSet<std::string> theirs("y", "z");
QUICHE teama6ef0a62019-03-07 20:34:33 -0500477 EXPECT_FALSE(mine.Intersects(theirs));
478 EXPECT_FALSE(theirs.Intersects(mine));
479 mine.Intersection(theirs);
480 EXPECT_TRUE(mine.Empty());
481 EXPECT_FALSE(mine.Intersects(theirs));
482 EXPECT_FALSE(theirs.Intersects(mine));
483}
484
485TEST_F(QuicIntervalSetTest,
486 QuicIntervalSetIntersectionTheirsBeforeMineInt64Singletons) {
487 QuicIntervalSet<int64_t> mine({{10, 15}});
488 QuicIntervalSet<int64_t> theirs({{-20, -5}});
489 EXPECT_FALSE(mine.Intersects(theirs));
490 EXPECT_FALSE(theirs.Intersects(mine));
491 mine.Intersection(theirs);
492 EXPECT_TRUE(mine.Empty());
493 EXPECT_FALSE(mine.Intersects(theirs));
494 EXPECT_FALSE(theirs.Intersects(mine));
495}
496
497TEST_F(QuicIntervalSetTest,
498 QuicIntervalSetIntersectionMineBeforeTheirsIntSingletons) {
499 QuicIntervalSet<int> mine({{10, 15}});
500 QuicIntervalSet<int> theirs({{90, 95}});
501 EXPECT_FALSE(mine.Intersects(theirs));
502 EXPECT_FALSE(theirs.Intersects(mine));
503 mine.Intersection(theirs);
504 EXPECT_TRUE(mine.Empty());
505 EXPECT_FALSE(mine.Intersects(theirs));
506 EXPECT_FALSE(theirs.Intersects(mine));
507}
508
509TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionTheirsBetweenMine) {
510 QuicIntervalSet<int64_t> mine({{0, 5}, {40, 50}});
511 QuicIntervalSet<int64_t> theirs({{10, 15}});
512 EXPECT_FALSE(mine.Intersects(theirs));
513 EXPECT_FALSE(theirs.Intersects(mine));
514 mine.Intersection(theirs);
515 EXPECT_TRUE(mine.Empty());
516 EXPECT_FALSE(mine.Intersects(theirs));
517 EXPECT_FALSE(theirs.Intersects(mine));
518}
519
520TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionMineBetweenTheirs) {
521 QuicIntervalSet<int> mine({{20, 25}});
522 QuicIntervalSet<int> theirs({{10, 15}, {30, 32}});
523 EXPECT_FALSE(mine.Intersects(theirs));
524 EXPECT_FALSE(theirs.Intersects(mine));
525 mine.Intersection(theirs);
526 EXPECT_TRUE(mine.Empty());
527 EXPECT_FALSE(mine.Intersects(theirs));
528 EXPECT_FALSE(theirs.Intersects(mine));
529}
530
531TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionAlternatingIntervals) {
532 QuicIntervalSet<int> mine, theirs;
533 mine.Add(10, 20);
534 mine.Add(40, 50);
535 mine.Add(60, 70);
536 theirs.Add(25, 39);
537 theirs.Add(55, 59);
538 theirs.Add(75, 79);
539 EXPECT_FALSE(mine.Intersects(theirs));
540 EXPECT_FALSE(theirs.Intersects(mine));
541 mine.Intersection(theirs);
542 EXPECT_TRUE(mine.Empty());
543 EXPECT_FALSE(mine.Intersects(theirs));
544 EXPECT_FALSE(theirs.Intersects(mine));
545}
546
547TEST_F(QuicIntervalSetTest,
548 QuicIntervalSetIntersectionAdjacentAlternatingNonIntersectingIntervals) {
549 // Make sure that intersection with adjacent interval set is empty.
550 const QuicIntervalSet<int> x1({{0, 10}});
551 const QuicIntervalSet<int> y1({{-50, 0}, {10, 95}});
552
553 QuicIntervalSet<int> result1 = x1;
554 result1.Intersection(y1);
555 EXPECT_TRUE(result1.Empty()) << result1;
556
557 const QuicIntervalSet<int16_t> x2({{0, 10}, {20, 30}, {40, 90}});
558 const QuicIntervalSet<int16_t> y2(
559 {{-50, -40}, {-2, 0}, {10, 20}, {32, 40}, {90, 95}});
560
561 QuicIntervalSet<int16_t> result2 = x2;
562 result2.Intersection(y2);
563 EXPECT_TRUE(result2.Empty()) << result2;
564
565 const QuicIntervalSet<int64_t> x3({{-1, 5}, {5, 10}});
566 const QuicIntervalSet<int64_t> y3({{-10, -1}, {10, 95}});
567
568 QuicIntervalSet<int64_t> result3 = x3;
569 result3.Intersection(y3);
570 EXPECT_TRUE(result3.Empty()) << result3;
571}
572
573TEST_F(QuicIntervalSetTest,
574 QuicIntervalSetIntersectionAlternatingIntersectingIntervals) {
575 const QuicIntervalSet<int> x1({{0, 10}});
576 const QuicIntervalSet<int> y1({{-50, 1}, {9, 95}});
577 const QuicIntervalSet<int> expected_result1({{0, 1}, {9, 10}});
578
579 QuicIntervalSet<int> result1 = x1;
580 result1.Intersection(y1);
581 EXPECT_EQ(result1, expected_result1);
582
583 const QuicIntervalSet<int16_t> x2({{0, 10}, {20, 30}, {40, 90}});
584 const QuicIntervalSet<int16_t> y2(
585 {{-50, -40}, {-2, 2}, {9, 21}, {32, 41}, {85, 95}});
586 const QuicIntervalSet<int16_t> expected_result2(
587 {{0, 2}, {9, 10}, {20, 21}, {40, 41}, {85, 90}});
588
589 QuicIntervalSet<int16_t> result2 = x2;
590 result2.Intersection(y2);
591 EXPECT_EQ(result2, expected_result2);
592
593 const QuicIntervalSet<int64_t> x3({{-1, 5}, {5, 10}});
594 const QuicIntervalSet<int64_t> y3({{-10, 3}, {4, 95}});
595 const QuicIntervalSet<int64_t> expected_result3({{-1, 3}, {4, 10}});
596
597 QuicIntervalSet<int64_t> result3 = x3;
598 result3.Intersection(y3);
599 EXPECT_EQ(result3, expected_result3);
600}
601
602TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionIdentical) {
603 QuicIntervalSet<int> copy(is);
604 EXPECT_TRUE(copy.Intersects(is));
605 EXPECT_TRUE(is.Intersects(copy));
606 is.Intersection(copy);
607 EXPECT_EQ(copy, is);
608}
609
610TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionSuperset) {
611 QuicIntervalSet<int> mine(-1, 10000);
612 EXPECT_TRUE(mine.Intersects(is));
613 EXPECT_TRUE(is.Intersects(mine));
614 mine.Intersection(is);
615 EXPECT_EQ(is, mine);
616}
617
618TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionSubset) {
619 QuicIntervalSet<int> copy(is);
620 QuicIntervalSet<int> theirs(-1, 10000);
621 EXPECT_TRUE(copy.Intersects(theirs));
622 EXPECT_TRUE(theirs.Intersects(copy));
623 is.Intersection(theirs);
624 EXPECT_EQ(copy, is);
625}
626
627TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionLargeSet) {
628 QuicIntervalSet<int> mine, theirs;
629 // mine: [0, 9), [10, 19), ..., [990, 999)
630 for (int i = 0; i < 1000; i += 10) {
631 mine.Add(i, i + 9);
632 }
633
634 theirs.Add(500, 520);
635 theirs.Add(535, 545);
636 theirs.Add(801, 809);
637 EXPECT_TRUE(mine.Intersects(theirs));
638 EXPECT_TRUE(theirs.Intersects(mine));
639 mine.Intersection(theirs);
640 EXPECT_TRUE(Check(mine, 5, 500, 509, 510, 519, 535, 539, 540, 545, 801, 809));
641 EXPECT_TRUE(mine.Intersects(theirs));
642 EXPECT_TRUE(theirs.Intersects(mine));
643}
644
645TEST_F(QuicIntervalSetTest, QuicIntervalSetDifference) {
646 is.Difference(other);
647 EXPECT_TRUE(Check(is, 10, 100, 200, 300, 350, 360, 370, 380, 400, 530, 600,
648 700, 770, 900, 1000, 1100, 1200, 1900, 2000, 2100, 2200));
649 QuicIntervalSet<int> copy = is;
650 is.Difference(copy);
651 EXPECT_TRUE(is.Empty());
652}
653
654TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceSingleBounds) {
655 std::vector<QuicInterval<int>> ivals(other.begin(), other.end());
656 for (const QuicInterval<int>& ival : ivals) {
657 is.Difference(ival.min(), ival.max());
658 }
659 EXPECT_TRUE(Check(is, 10, 100, 200, 300, 350, 360, 370, 380, 400, 530, 600,
660 700, 770, 900, 1000, 1100, 1200, 1900, 2000, 2100, 2200));
661}
662
663TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceSingleInterval) {
664 std::vector<QuicInterval<int>> ivals(other.begin(), other.end());
665 for (const QuicInterval<int>& ival : ivals) {
666 is.Difference(ival);
667 }
668 EXPECT_TRUE(Check(is, 10, 100, 200, 300, 350, 360, 370, 380, 400, 530, 600,
669 700, 770, 900, 1000, 1100, 1200, 1900, 2000, 2100, 2200));
670}
671
672TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceAlternatingIntervals) {
673 QuicIntervalSet<int> mine, theirs;
674 mine.Add(10, 20);
675 mine.Add(40, 50);
676 mine.Add(60, 70);
677 theirs.Add(25, 39);
678 theirs.Add(55, 59);
679 theirs.Add(75, 79);
680
681 mine.Difference(theirs);
682 EXPECT_TRUE(Check(mine, 3, 10, 20, 40, 50, 60, 70));
683}
684
685TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceEmptyMine) {
vasilvvc48c8712019-03-11 13:38:16 -0700686 QuicIntervalSet<std::string> mine, theirs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500687 theirs.Add("a", "b");
688
689 mine.Difference(theirs);
690 EXPECT_TRUE(mine.Empty());
691}
692
693TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceEmptyTheirs) {
vasilvvc48c8712019-03-11 13:38:16 -0700694 QuicIntervalSet<std::string> mine, theirs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500695 mine.Add("a", "b");
696
697 mine.Difference(theirs);
698 EXPECT_EQ(1u, mine.Size());
699 EXPECT_EQ("a", mine.begin()->min());
700 EXPECT_EQ("b", mine.begin()->max());
701}
702
703TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceTheirsBeforeMine) {
vasilvvc48c8712019-03-11 13:38:16 -0700704 QuicIntervalSet<std::string> mine, theirs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500705 mine.Add("y", "z");
706 theirs.Add("a", "b");
707
708 mine.Difference(theirs);
709 EXPECT_EQ(1u, mine.Size());
710 EXPECT_EQ("y", mine.begin()->min());
711 EXPECT_EQ("z", mine.begin()->max());
712}
713
714TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceMineBeforeTheirs) {
vasilvvc48c8712019-03-11 13:38:16 -0700715 QuicIntervalSet<std::string> mine, theirs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500716 mine.Add("a", "b");
717 theirs.Add("y", "z");
718
719 mine.Difference(theirs);
720 EXPECT_EQ(1u, mine.Size());
721 EXPECT_EQ("a", mine.begin()->min());
722 EXPECT_EQ("b", mine.begin()->max());
723}
724
725TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceIdentical) {
vasilvvc48c8712019-03-11 13:38:16 -0700726 QuicIntervalSet<std::string> mine;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500727 mine.Add("a", "b");
728 mine.Add("c", "d");
vasilvvc48c8712019-03-11 13:38:16 -0700729 QuicIntervalSet<std::string> theirs(mine);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500730
731 mine.Difference(theirs);
732 EXPECT_TRUE(mine.Empty());
733}
734
735TEST_F(QuicIntervalSetTest, EmptyComplement) {
736 // The complement of an empty set is the input interval:
737 QuicIntervalSet<int> iset;
738 iset.Complement(100, 200);
739 EXPECT_TRUE(Check(iset, 1, 100, 200));
740}
741
742TEST(QuicIntervalSetMultipleCompactionTest, OuterCovering) {
743 QuicIntervalSet<int> iset;
744 // First add a bunch of disjoint ranges
745 iset.Add(100, 150);
746 iset.Add(200, 250);
747 iset.Add(300, 350);
748 iset.Add(400, 450);
749 EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450));
750 // Now add a big range that covers all of these ranges
751 iset.Add(0, 500);
752 EXPECT_TRUE(Check(iset, 1, 0, 500));
753}
754
755TEST(QuicIntervalSetMultipleCompactionTest, InnerCovering) {
756 QuicIntervalSet<int> iset;
757 // First add a bunch of disjoint ranges
758 iset.Add(100, 150);
759 iset.Add(200, 250);
760 iset.Add(300, 350);
761 iset.Add(400, 450);
762 EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450));
763 // Now add a big range that partially covers the left and right most ranges.
764 iset.Add(125, 425);
765 EXPECT_TRUE(Check(iset, 1, 100, 450));
766}
767
768TEST(QuicIntervalSetMultipleCompactionTest, LeftCovering) {
769 QuicIntervalSet<int> iset;
770 // First add a bunch of disjoint ranges
771 iset.Add(100, 150);
772 iset.Add(200, 250);
773 iset.Add(300, 350);
774 iset.Add(400, 450);
775 EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450));
776 // Now add a big range that partially covers the left most range.
777 iset.Add(125, 500);
778 EXPECT_TRUE(Check(iset, 1, 100, 500));
779}
780
781TEST(QuicIntervalSetMultipleCompactionTest, RightCovering) {
782 QuicIntervalSet<int> iset;
783 // First add a bunch of disjoint ranges
784 iset.Add(100, 150);
785 iset.Add(200, 250);
786 iset.Add(300, 350);
787 iset.Add(400, 450);
788 EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450));
789 // Now add a big range that partially covers the right most range.
790 iset.Add(0, 425);
791 EXPECT_TRUE(Check(iset, 1, 0, 450));
792}
793
794// Helper method for testing and verifying the results of a one-interval
795// completement case.
796static bool CheckOneComplement(int add_min,
797 int add_max,
798 int comp_min,
799 int comp_max,
800 int count,
801 ...) {
802 QuicIntervalSet<int> iset;
803 iset.Add(add_min, add_max);
804 iset.Complement(comp_min, comp_max);
805 bool result = true;
806 va_list ap;
807 va_start(ap, count);
808 if (!VA_Check(iset, count, ap)) {
809 result = false;
810 }
811 va_end(ap);
812 return result;
813}
814
815TEST_F(QuicIntervalSetTest, SingleIntervalComplement) {
816 // Verify the complement of a set with one interval (i):
817 // |----- i -----|
818 // |----- args -----|
819 EXPECT_TRUE(CheckOneComplement(0, 10, 50, 150, 1, 50, 150));
820
821 // |----- i -----|
822 // |----- args -----|
823 EXPECT_TRUE(CheckOneComplement(50, 150, 0, 100, 1, 0, 50));
824
825 // |----- i -----|
826 // |----- args -----|
827 EXPECT_TRUE(CheckOneComplement(50, 150, 50, 150, 0));
828
829 // |---------- i ----------|
830 // |----- args -----|
831 EXPECT_TRUE(CheckOneComplement(50, 500, 100, 300, 0));
832
833 // |----- i -----|
834 // |---------- args ----------|
835 EXPECT_TRUE(CheckOneComplement(50, 500, 0, 800, 2, 0, 50, 500, 800));
836
837 // |----- i -----|
838 // |----- args -----|
839 EXPECT_TRUE(CheckOneComplement(50, 150, 100, 300, 1, 150, 300));
840
841 // |----- i -----|
842 // |----- args -----|
843 EXPECT_TRUE(CheckOneComplement(50, 150, 200, 300, 1, 200, 300));
844}
845
846// Helper method that copies <iset> and takes its complement,
847// returning false if Check succeeds.
848static bool CheckComplement(const QuicIntervalSet<int>& iset,
849 int comp_min,
850 int comp_max,
851 int count,
852 ...) {
853 QuicIntervalSet<int> iset_copy = iset;
854 iset_copy.Complement(comp_min, comp_max);
855 bool result = true;
856 va_list ap;
857 va_start(ap, count);
858 if (!VA_Check(iset_copy, count, ap)) {
859 result = false;
860 }
861 va_end(ap);
862 return result;
863}
864
865TEST_F(QuicIntervalSetTest, MultiIntervalComplement) {
866 // Initialize a small test set:
867 QuicIntervalSet<int> iset;
868 iset.Add(100, 200);
869 iset.Add(300, 400);
870 iset.Add(500, 600);
871
872 // |----- i -----|
873 // |----- comp -----|
874 EXPECT_TRUE(CheckComplement(iset, 0, 50, 1, 0, 50));
875
876 // |----- i -----|
877 // |----- comp -----|
878 EXPECT_TRUE(CheckComplement(iset, 0, 200, 1, 0, 100));
879 EXPECT_TRUE(CheckComplement(iset, 0, 220, 2, 0, 100, 200, 220));
880
881 // |----- i -----|
882 // |----- comp -----|
883 EXPECT_TRUE(CheckComplement(iset, 100, 600, 2, 200, 300, 400, 500));
884
885 // |---------- i ----------|
886 // |----- comp -----|
887 EXPECT_TRUE(CheckComplement(iset, 300, 400, 0));
888 EXPECT_TRUE(CheckComplement(iset, 250, 400, 1, 250, 300));
889 EXPECT_TRUE(CheckComplement(iset, 300, 450, 1, 400, 450));
890 EXPECT_TRUE(CheckComplement(iset, 250, 450, 2, 250, 300, 400, 450));
891
892 // |----- i -----|
893 // |---------- comp ----------|
894 EXPECT_TRUE(
895 CheckComplement(iset, 0, 700, 4, 0, 100, 200, 300, 400, 500, 600, 700));
896
897 // |----- i -----|
898 // |----- comp -----|
899 EXPECT_TRUE(CheckComplement(iset, 400, 700, 2, 400, 500, 600, 700));
900 EXPECT_TRUE(CheckComplement(iset, 350, 700, 2, 400, 500, 600, 700));
901
902 // |----- i -----|
903 // |----- comp -----|
904 EXPECT_TRUE(CheckComplement(iset, 700, 800, 1, 700, 800));
905}
906
907// Verifies ToString, operator<< don't assert.
908TEST_F(QuicIntervalSetTest, ToString) {
909 QuicIntervalSet<int> iset;
910 iset.Add(300, 400);
911 iset.Add(100, 200);
912 iset.Add(500, 600);
913 EXPECT_TRUE(!iset.ToString().empty());
914 QUIC_VLOG(2) << iset;
915 // Order and format of ToString() output is guaranteed.
916 EXPECT_EQ("{ [100, 200) [300, 400) [500, 600) }", iset.ToString());
917 EXPECT_EQ("{ [1, 2) }", QuicIntervalSet<int>(1, 2).ToString());
918 EXPECT_EQ("{ }", QuicIntervalSet<int>().ToString());
919}
920
921TEST_F(QuicIntervalSetTest, ConstructionDiscardsEmptyInterval) {
922 EXPECT_TRUE(QuicIntervalSet<int>(QuicInterval<int>(2, 2)).Empty());
923 EXPECT_TRUE(QuicIntervalSet<int>(2, 2).Empty());
924 EXPECT_FALSE(QuicIntervalSet<int>(QuicInterval<int>(2, 3)).Empty());
925 EXPECT_FALSE(QuicIntervalSet<int>(2, 3).Empty());
926}
927
928TEST_F(QuicIntervalSetTest, Swap) {
929 QuicIntervalSet<int> a, b;
930 a.Add(300, 400);
931 b.Add(100, 200);
932 b.Add(500, 600);
933 a.Swap(&b);
934 EXPECT_TRUE(Check(a, 2, 100, 200, 500, 600));
935 EXPECT_TRUE(Check(b, 1, 300, 400));
936 swap(a, b);
937 EXPECT_TRUE(Check(a, 1, 300, 400));
938 EXPECT_TRUE(Check(b, 2, 100, 200, 500, 600));
939}
940
941TEST_F(QuicIntervalSetTest, OutputReturnsOstreamRef) {
942 std::stringstream ss;
943 const QuicIntervalSet<int> v(QuicInterval<int>(1, 2));
944 auto return_type_is_a_ref = [](std::ostream&) {};
945 return_type_is_a_ref(ss << v);
946}
947
948struct NotOstreamable {
QUICHE team2d5d2342019-03-21 13:01:37 -0700949 bool operator<(const NotOstreamable&) const { return false; }
950 bool operator>(const NotOstreamable&) const { return false; }
951 bool operator!=(const NotOstreamable&) const { return false; }
952 bool operator>=(const NotOstreamable&) const { return true; }
953 bool operator<=(const NotOstreamable&) const { return true; }
954 bool operator==(const NotOstreamable&) const { return true; }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500955};
956
957TEST_F(QuicIntervalSetTest, IntervalOfTypeWithNoOstreamSupport) {
958 const NotOstreamable v;
959 const QuicIntervalSet<NotOstreamable> d(QuicInterval<NotOstreamable>(v, v));
960 // EXPECT_EQ builds a string representation of d. If d::operator<<()
961 // would be defined then this test would not compile because NotOstreamable
962 // objects lack the operator<<() support.
963 EXPECT_EQ(d, d);
964}
965
966class QuicIntervalSetInitTest : public QuicTest {
967 protected:
968 const std::vector<QuicInterval<int>> intervals_{{0, 1}, {2, 4}};
969};
970
971TEST_F(QuicIntervalSetInitTest, DirectInit) {
972 std::initializer_list<QuicInterval<int>> il = {{0, 1}, {2, 3}, {3, 4}};
973 QuicIntervalSet<int> s(il);
974 EXPECT_THAT(s, ElementsAreArray(intervals_));
975}
976
977TEST_F(QuicIntervalSetInitTest, CopyInit) {
978 std::initializer_list<QuicInterval<int>> il = {{0, 1}, {2, 3}, {3, 4}};
979 QuicIntervalSet<int> s = il;
980 EXPECT_THAT(s, ElementsAreArray(intervals_));
981}
982
983TEST_F(QuicIntervalSetInitTest, AssignIterPair) {
984 QuicIntervalSet<int> s(0, 1000); // Make sure assign clears.
985 s.assign(intervals_.begin(), intervals_.end());
986 EXPECT_THAT(s, ElementsAreArray(intervals_));
987}
988
989TEST_F(QuicIntervalSetInitTest, AssignInitList) {
990 QuicIntervalSet<int> s(0, 1000); // Make sure assign clears.
991 s.assign({{0, 1}, {2, 3}, {3, 4}});
992 EXPECT_THAT(s, ElementsAreArray(intervals_));
993}
994
995TEST_F(QuicIntervalSetInitTest, AssignmentInitList) {
996 std::initializer_list<QuicInterval<int>> il = {{0, 1}, {2, 3}, {3, 4}};
997 QuicIntervalSet<int> s;
998 s = il;
999 EXPECT_THAT(s, ElementsAreArray(intervals_));
1000}
1001
1002TEST_F(QuicIntervalSetInitTest, BracedInitThenBracedAssign) {
1003 QuicIntervalSet<int> s{{0, 1}, {2, 3}, {3, 4}};
1004 s = {{0, 1}, {2, 4}};
1005 EXPECT_THAT(s, ElementsAreArray(intervals_));
1006}
1007
1008} // namespace
1009} // namespace test
1010} // namespace quic