QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | #include <iostream> |
| 11 | #include <iterator> |
| 12 | #include <limits> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 13 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 17 | |
| 18 | namespace quic { |
| 19 | namespace test { |
| 20 | namespace { |
| 21 | |
| 22 | using ::testing::ElementsAreArray; |
| 23 | |
| 24 | class 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 | |
| 64 | TEST_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>: |
| 93 | static 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 team | 2d5d234 | 2019-03-21 13:01:37 -0700 | [diff] [blame] | 96 | QUIC_LOG(ERROR) << "Expected " << count << " intervals, got " |
| 97 | << intervals.size() << ": " << is; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | if (count != static_cast<int>(is.Size())) { |
QUICHE team | 2d5d234 | 2019-03-21 13:01:37 -0700 | [diff] [blame] | 101 | QUIC_LOG(ERROR) << "Expected " << count << " intervals, got Size " |
| 102 | << is.Size() << ": " << is; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 103 | 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 team | 2d5d234 | 2019-03-21 13:01:37 -0700 | [diff] [blame] | 110 | QUIC_LOG(ERROR) << "Expected: [" << min << ", " << max << ") got " |
| 111 | << intervals[i] << " in " << is; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | result = false; |
| 113 | } |
| 114 | } |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | static 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. |
| 128 | static 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 | |
| 135 | static 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 | |
| 148 | static 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 | |
| 155 | static 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 | |
| 165 | TEST_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 | |
wub | 4f59d71 | 2019-11-05 06:48:55 -0800 | [diff] [blame] | 194 | TEST_F(QuicIntervalSetTest, PopFront) { |
| 195 | QuicIntervalSet<int> iset{{100, 200}, {400, 500}, {700, 800}}; |
| 196 | EXPECT_TRUE(Check(iset, 3, 100, 200, 400, 500, 700, 800)); |
| 197 | |
| 198 | iset.PopFront(); |
| 199 | EXPECT_TRUE(Check(iset, 2, 400, 500, 700, 800)); |
| 200 | |
| 201 | iset.PopFront(); |
| 202 | EXPECT_TRUE(Check(iset, 1, 700, 800)); |
| 203 | |
| 204 | iset.PopFront(); |
| 205 | EXPECT_TRUE(iset.Empty()); |
| 206 | } |
| 207 | |
| 208 | TEST_F(QuicIntervalSetTest, TrimLessThan) { |
| 209 | QuicIntervalSet<int> iset{{100, 200}, {400, 500}, {700, 800}}; |
| 210 | EXPECT_TRUE(Check(iset, 3, 100, 200, 400, 500, 700, 800)); |
| 211 | |
| 212 | EXPECT_FALSE(iset.TrimLessThan(99)); |
| 213 | EXPECT_FALSE(iset.TrimLessThan(100)); |
| 214 | EXPECT_TRUE(Check(iset, 3, 100, 200, 400, 500, 700, 800)); |
| 215 | |
| 216 | EXPECT_TRUE(iset.TrimLessThan(101)); |
| 217 | EXPECT_TRUE(Check(iset, 3, 101, 200, 400, 500, 700, 800)); |
| 218 | |
| 219 | EXPECT_TRUE(iset.TrimLessThan(199)); |
| 220 | EXPECT_TRUE(Check(iset, 3, 199, 200, 400, 500, 700, 800)); |
| 221 | |
| 222 | EXPECT_TRUE(iset.TrimLessThan(450)); |
| 223 | EXPECT_TRUE(Check(iset, 2, 450, 500, 700, 800)); |
| 224 | |
| 225 | EXPECT_TRUE(iset.TrimLessThan(500)); |
| 226 | EXPECT_TRUE(Check(iset, 1, 700, 800)); |
| 227 | |
| 228 | EXPECT_TRUE(iset.TrimLessThan(801)); |
| 229 | EXPECT_TRUE(iset.Empty()); |
| 230 | |
| 231 | EXPECT_FALSE(iset.TrimLessThan(900)); |
| 232 | EXPECT_TRUE(iset.Empty()); |
| 233 | } |
| 234 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 235 | TEST_F(QuicIntervalSetTest, QuicIntervalSetBasic) { |
| 236 | // Test Add, Get, Contains and Find |
| 237 | QuicIntervalSet<int> iset; |
| 238 | EXPECT_TRUE(iset.Empty()); |
| 239 | EXPECT_EQ(0u, iset.Size()); |
| 240 | iset.Add(100, 200); |
| 241 | EXPECT_FALSE(iset.Empty()); |
| 242 | EXPECT_EQ(1u, iset.Size()); |
| 243 | iset.Add(100, 150); |
| 244 | iset.Add(150, 200); |
| 245 | iset.Add(130, 170); |
| 246 | iset.Add(90, 150); |
| 247 | iset.Add(170, 220); |
| 248 | iset.Add(300, 400); |
| 249 | iset.Add(250, 450); |
| 250 | EXPECT_FALSE(iset.Empty()); |
| 251 | EXPECT_EQ(2u, iset.Size()); |
| 252 | EXPECT_TRUE(Check(iset, 2, 90, 220, 250, 450)); |
| 253 | |
| 254 | // Test two intervals with a.max == b.min, that will just join up. |
| 255 | iset.Clear(); |
| 256 | iset.Add(100, 200); |
| 257 | iset.Add(200, 300); |
| 258 | EXPECT_FALSE(iset.Empty()); |
| 259 | EXPECT_EQ(1u, iset.Size()); |
| 260 | EXPECT_TRUE(Check(iset, 1, 100, 300)); |
| 261 | |
| 262 | // Test adding two sets together. |
| 263 | iset.Clear(); |
| 264 | QuicIntervalSet<int> iset_add; |
| 265 | iset.Add(100, 200); |
| 266 | iset.Add(100, 150); |
| 267 | iset.Add(150, 200); |
| 268 | iset.Add(130, 170); |
| 269 | iset_add.Add(90, 150); |
| 270 | iset_add.Add(170, 220); |
| 271 | iset_add.Add(300, 400); |
| 272 | iset_add.Add(250, 450); |
| 273 | |
| 274 | iset.Union(iset_add); |
| 275 | EXPECT_FALSE(iset.Empty()); |
| 276 | EXPECT_EQ(2u, iset.Size()); |
| 277 | EXPECT_TRUE(Check(iset, 2, 90, 220, 250, 450)); |
| 278 | |
| 279 | // Test begin()/end(), and rbegin()/rend() |
| 280 | // to iterate over intervals. |
| 281 | { |
| 282 | std::vector<QuicInterval<int>> expected(iset.begin(), iset.end()); |
| 283 | |
| 284 | std::vector<QuicInterval<int>> actual1; |
| 285 | std::copy(iset.begin(), iset.end(), back_inserter(actual1)); |
| 286 | ASSERT_EQ(expected.size(), actual1.size()); |
| 287 | |
| 288 | std::vector<QuicInterval<int>> actual2; |
| 289 | std::copy(iset.begin(), iset.end(), back_inserter(actual2)); |
| 290 | ASSERT_EQ(expected.size(), actual2.size()); |
| 291 | |
| 292 | for (size_t i = 0; i < expected.size(); i++) { |
| 293 | EXPECT_EQ(expected[i].min(), actual1[i].min()); |
| 294 | EXPECT_EQ(expected[i].max(), actual1[i].max()); |
| 295 | |
| 296 | EXPECT_EQ(expected[i].min(), actual2[i].min()); |
| 297 | EXPECT_EQ(expected[i].max(), actual2[i].max()); |
| 298 | } |
| 299 | |
| 300 | // Ensure that the rbegin()/rend() iterators correctly yield the intervals |
| 301 | // in reverse order. |
| 302 | EXPECT_THAT(std::vector<QuicInterval<int>>(iset.rbegin(), iset.rend()), |
| 303 | ElementsAreArray(expected.rbegin(), expected.rend())); |
| 304 | } |
| 305 | |
| 306 | TestNotContainsAndFind(iset, 89); |
| 307 | TestContainsAndFind(iset, 90); |
| 308 | TestContainsAndFind(iset, 120); |
| 309 | TestContainsAndFind(iset, 219); |
| 310 | TestNotContainsAndFind(iset, 220); |
| 311 | TestNotContainsAndFind(iset, 235); |
| 312 | TestNotContainsAndFind(iset, 249); |
| 313 | TestContainsAndFind(iset, 250); |
| 314 | TestContainsAndFind(iset, 300); |
| 315 | TestContainsAndFind(iset, 449); |
| 316 | TestNotContainsAndFind(iset, 450); |
| 317 | TestNotContainsAndFind(iset, 451); |
| 318 | |
| 319 | TestNotContainsAndFind(iset, 50, 60); |
| 320 | TestNotContainsAndFind(iset, 50, 90); |
| 321 | TestNotContainsAndFind(iset, 50, 200); |
| 322 | TestNotContainsAndFind(iset, 90, 90); |
| 323 | TestContainsAndFind(iset, 90, 200); |
| 324 | TestContainsAndFind(iset, 100, 200); |
| 325 | TestContainsAndFind(iset, 100, 220); |
| 326 | TestNotContainsAndFind(iset, 100, 221); |
| 327 | TestNotContainsAndFind(iset, 220, 220); |
| 328 | TestNotContainsAndFind(iset, 240, 300); |
| 329 | TestContainsAndFind(iset, 250, 300); |
| 330 | TestContainsAndFind(iset, 260, 300); |
| 331 | TestContainsAndFind(iset, 300, 450); |
| 332 | TestNotContainsAndFind(iset, 300, 451); |
| 333 | |
| 334 | QuicIntervalSet<int> iset_contains; |
| 335 | iset_contains.Add(50, 90); |
| 336 | EXPECT_FALSE(iset.Contains(iset_contains)); |
| 337 | iset_contains.Clear(); |
| 338 | |
| 339 | iset_contains.Add(90, 200); |
| 340 | EXPECT_TRUE(iset.Contains(iset_contains)); |
| 341 | iset_contains.Add(100, 200); |
| 342 | EXPECT_TRUE(iset.Contains(iset_contains)); |
| 343 | iset_contains.Add(100, 220); |
| 344 | EXPECT_TRUE(iset.Contains(iset_contains)); |
| 345 | iset_contains.Add(250, 300); |
| 346 | EXPECT_TRUE(iset.Contains(iset_contains)); |
| 347 | iset_contains.Add(300, 450); |
| 348 | EXPECT_TRUE(iset.Contains(iset_contains)); |
| 349 | iset_contains.Add(300, 451); |
| 350 | EXPECT_FALSE(iset.Contains(iset_contains)); |
| 351 | EXPECT_FALSE(iset.Contains(QuicInterval<int>())); |
| 352 | EXPECT_FALSE(iset.Contains(QuicIntervalSet<int>())); |
| 353 | } |
| 354 | |
| 355 | TEST_F(QuicIntervalSetTest, QuicIntervalSetContainsEmpty) { |
| 356 | const QuicIntervalSet<int> empty; |
| 357 | const QuicIntervalSet<int> other_empty; |
| 358 | const QuicIntervalSet<int> non_empty({{10, 20}, {40, 50}}); |
| 359 | EXPECT_FALSE(empty.Contains(empty)); |
| 360 | EXPECT_FALSE(empty.Contains(other_empty)); |
| 361 | EXPECT_FALSE(empty.Contains(non_empty)); |
| 362 | EXPECT_FALSE(non_empty.Contains(empty)); |
| 363 | } |
| 364 | |
| 365 | TEST_F(QuicIntervalSetTest, Equality) { |
| 366 | QuicIntervalSet<int> is_copy = is; |
| 367 | EXPECT_EQ(is, is); |
| 368 | EXPECT_EQ(is, is_copy); |
| 369 | EXPECT_NE(is, other); |
| 370 | EXPECT_NE(is, QuicIntervalSet<int>()); |
| 371 | EXPECT_EQ(QuicIntervalSet<int>(), QuicIntervalSet<int>()); |
| 372 | } |
| 373 | |
| 374 | TEST_F(QuicIntervalSetTest, LowerAndUpperBound) { |
| 375 | QuicIntervalSet<int> intervals; |
| 376 | intervals.Add(10, 20); |
| 377 | intervals.Add(30, 40); |
| 378 | |
| 379 | // [10, 20) [30, 40) end |
| 380 | // ^ LowerBound(5) |
| 381 | // ^ LowerBound(10) |
| 382 | // ^ LowerBound(15) |
| 383 | // ^ LowerBound(20) |
| 384 | // ^ LowerBound(25) |
| 385 | // ^ LowerBound(30) |
| 386 | // ^ LowerBound(35) |
| 387 | // ^ LowerBound(40) |
| 388 | // ^ LowerBound(50) |
| 389 | EXPECT_EQ(intervals.LowerBound(5)->min(), 10); |
| 390 | EXPECT_EQ(intervals.LowerBound(10)->min(), 10); |
| 391 | EXPECT_EQ(intervals.LowerBound(15)->min(), 10); |
| 392 | EXPECT_EQ(intervals.LowerBound(20)->min(), 30); |
| 393 | EXPECT_EQ(intervals.LowerBound(25)->min(), 30); |
| 394 | EXPECT_EQ(intervals.LowerBound(30)->min(), 30); |
| 395 | EXPECT_EQ(intervals.LowerBound(35)->min(), 30); |
| 396 | EXPECT_EQ(intervals.LowerBound(40), intervals.end()); |
| 397 | EXPECT_EQ(intervals.LowerBound(50), intervals.end()); |
| 398 | |
| 399 | // [10, 20) [30, 40) end |
| 400 | // ^ UpperBound(5) |
| 401 | // ^ UpperBound(10) |
| 402 | // ^ UpperBound(15) |
| 403 | // ^ UpperBound(20) |
| 404 | // ^ UpperBound(25) |
| 405 | // ^ UpperBound(30) |
| 406 | // ^ UpperBound(35) |
| 407 | // ^ UpperBound(40) |
| 408 | // ^ UpperBound(50) |
| 409 | EXPECT_EQ(intervals.UpperBound(5)->min(), 10); |
| 410 | EXPECT_EQ(intervals.UpperBound(10)->min(), 30); |
| 411 | EXPECT_EQ(intervals.UpperBound(15)->min(), 30); |
| 412 | EXPECT_EQ(intervals.UpperBound(20)->min(), 30); |
| 413 | EXPECT_EQ(intervals.UpperBound(25)->min(), 30); |
| 414 | EXPECT_EQ(intervals.UpperBound(30), intervals.end()); |
| 415 | EXPECT_EQ(intervals.UpperBound(35), intervals.end()); |
| 416 | EXPECT_EQ(intervals.UpperBound(40), intervals.end()); |
| 417 | EXPECT_EQ(intervals.UpperBound(50), intervals.end()); |
| 418 | } |
| 419 | |
| 420 | TEST_F(QuicIntervalSetTest, SpanningInterval) { |
| 421 | // Spanning interval of an empty set is empty: |
| 422 | { |
| 423 | QuicIntervalSet<int> iset; |
| 424 | const QuicInterval<int>& ival = iset.SpanningInterval(); |
| 425 | EXPECT_TRUE(ival.Empty()); |
| 426 | } |
| 427 | |
| 428 | // Spanning interval of a set with one interval is that interval: |
| 429 | { |
| 430 | QuicIntervalSet<int> iset; |
| 431 | iset.Add(100, 200); |
| 432 | const QuicInterval<int>& ival = iset.SpanningInterval(); |
| 433 | EXPECT_EQ(100, ival.min()); |
| 434 | EXPECT_EQ(200, ival.max()); |
| 435 | } |
| 436 | |
| 437 | // Spanning interval of a set with multiple elements is determined |
| 438 | // by the endpoints of the first and last element: |
| 439 | { |
| 440 | const QuicInterval<int>& ival = is.SpanningInterval(); |
| 441 | EXPECT_EQ(100, ival.min()); |
| 442 | EXPECT_EQ(2200, ival.max()); |
| 443 | } |
| 444 | { |
| 445 | const QuicInterval<int>& ival = other.SpanningInterval(); |
| 446 | EXPECT_EQ(50, ival.min()); |
| 447 | EXPECT_EQ(2270, ival.max()); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | TEST_F(QuicIntervalSetTest, QuicIntervalSetUnion) { |
| 452 | is.Union(other); |
| 453 | EXPECT_TRUE(Check(is, 12, 50, 70, 100, 200, 300, 400, 470, 600, 650, 670, 700, |
| 454 | 830, 870, 1000, 1100, 1230, 1270, 1830, 1900, 2000, 2100, |
| 455 | 2200, 2250, 2270)); |
| 456 | } |
| 457 | |
| 458 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersection) { |
| 459 | EXPECT_TRUE(is.Intersects(other)); |
| 460 | EXPECT_TRUE(other.Intersects(is)); |
| 461 | is.Intersection(other); |
| 462 | EXPECT_TRUE(Check(is, 7, 350, 360, 370, 380, 500, 530, 770, 800, 1300, 1400, |
| 463 | 1500, 1600, 1700, 1800)); |
| 464 | EXPECT_TRUE(is.Intersects(other)); |
| 465 | EXPECT_TRUE(other.Intersects(is)); |
| 466 | } |
| 467 | |
| 468 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionBothEmpty) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 469 | QuicIntervalSet<std::string> mine, theirs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 470 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 471 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 472 | mine.Intersection(theirs); |
| 473 | EXPECT_TRUE(mine.Empty()); |
| 474 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 475 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 476 | } |
| 477 | |
| 478 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionEmptyMine) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 479 | QuicIntervalSet<std::string> mine; |
| 480 | QuicIntervalSet<std::string> theirs("a", "b"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 481 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 482 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 483 | mine.Intersection(theirs); |
| 484 | EXPECT_TRUE(mine.Empty()); |
| 485 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 486 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 487 | } |
| 488 | |
| 489 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionEmptyTheirs) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 490 | QuicIntervalSet<std::string> mine("a", "b"); |
| 491 | QuicIntervalSet<std::string> theirs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 492 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 493 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 494 | mine.Intersection(theirs); |
| 495 | EXPECT_TRUE(mine.Empty()); |
| 496 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 497 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 498 | } |
| 499 | |
| 500 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionTheirsBeforeMine) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 501 | QuicIntervalSet<std::string> mine("y", "z"); |
| 502 | QuicIntervalSet<std::string> theirs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 503 | theirs.Add("a", "b"); |
| 504 | theirs.Add("c", "d"); |
| 505 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 506 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 507 | mine.Intersection(theirs); |
| 508 | EXPECT_TRUE(mine.Empty()); |
| 509 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 510 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 511 | } |
| 512 | |
| 513 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionMineBeforeTheirs) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 514 | QuicIntervalSet<std::string> mine; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 515 | mine.Add("a", "b"); |
| 516 | mine.Add("c", "d"); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 517 | QuicIntervalSet<std::string> theirs("y", "z"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 518 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 519 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 520 | mine.Intersection(theirs); |
| 521 | EXPECT_TRUE(mine.Empty()); |
| 522 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 523 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 524 | } |
| 525 | |
| 526 | TEST_F(QuicIntervalSetTest, |
| 527 | QuicIntervalSetIntersectionTheirsBeforeMineInt64Singletons) { |
| 528 | QuicIntervalSet<int64_t> mine({{10, 15}}); |
| 529 | QuicIntervalSet<int64_t> theirs({{-20, -5}}); |
| 530 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 531 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 532 | mine.Intersection(theirs); |
| 533 | EXPECT_TRUE(mine.Empty()); |
| 534 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 535 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 536 | } |
| 537 | |
| 538 | TEST_F(QuicIntervalSetTest, |
| 539 | QuicIntervalSetIntersectionMineBeforeTheirsIntSingletons) { |
| 540 | QuicIntervalSet<int> mine({{10, 15}}); |
| 541 | QuicIntervalSet<int> theirs({{90, 95}}); |
| 542 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 543 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 544 | mine.Intersection(theirs); |
| 545 | EXPECT_TRUE(mine.Empty()); |
| 546 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 547 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 548 | } |
| 549 | |
| 550 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionTheirsBetweenMine) { |
| 551 | QuicIntervalSet<int64_t> mine({{0, 5}, {40, 50}}); |
| 552 | QuicIntervalSet<int64_t> theirs({{10, 15}}); |
| 553 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 554 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 555 | mine.Intersection(theirs); |
| 556 | EXPECT_TRUE(mine.Empty()); |
| 557 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 558 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 559 | } |
| 560 | |
| 561 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionMineBetweenTheirs) { |
| 562 | QuicIntervalSet<int> mine({{20, 25}}); |
| 563 | QuicIntervalSet<int> theirs({{10, 15}, {30, 32}}); |
| 564 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 565 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 566 | mine.Intersection(theirs); |
| 567 | EXPECT_TRUE(mine.Empty()); |
| 568 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 569 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 570 | } |
| 571 | |
| 572 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionAlternatingIntervals) { |
| 573 | QuicIntervalSet<int> mine, theirs; |
| 574 | mine.Add(10, 20); |
| 575 | mine.Add(40, 50); |
| 576 | mine.Add(60, 70); |
| 577 | theirs.Add(25, 39); |
| 578 | theirs.Add(55, 59); |
| 579 | theirs.Add(75, 79); |
| 580 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 581 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 582 | mine.Intersection(theirs); |
| 583 | EXPECT_TRUE(mine.Empty()); |
| 584 | EXPECT_FALSE(mine.Intersects(theirs)); |
| 585 | EXPECT_FALSE(theirs.Intersects(mine)); |
| 586 | } |
| 587 | |
| 588 | TEST_F(QuicIntervalSetTest, |
| 589 | QuicIntervalSetIntersectionAdjacentAlternatingNonIntersectingIntervals) { |
| 590 | // Make sure that intersection with adjacent interval set is empty. |
| 591 | const QuicIntervalSet<int> x1({{0, 10}}); |
| 592 | const QuicIntervalSet<int> y1({{-50, 0}, {10, 95}}); |
| 593 | |
| 594 | QuicIntervalSet<int> result1 = x1; |
| 595 | result1.Intersection(y1); |
| 596 | EXPECT_TRUE(result1.Empty()) << result1; |
| 597 | |
| 598 | const QuicIntervalSet<int16_t> x2({{0, 10}, {20, 30}, {40, 90}}); |
| 599 | const QuicIntervalSet<int16_t> y2( |
| 600 | {{-50, -40}, {-2, 0}, {10, 20}, {32, 40}, {90, 95}}); |
| 601 | |
| 602 | QuicIntervalSet<int16_t> result2 = x2; |
| 603 | result2.Intersection(y2); |
| 604 | EXPECT_TRUE(result2.Empty()) << result2; |
| 605 | |
| 606 | const QuicIntervalSet<int64_t> x3({{-1, 5}, {5, 10}}); |
| 607 | const QuicIntervalSet<int64_t> y3({{-10, -1}, {10, 95}}); |
| 608 | |
| 609 | QuicIntervalSet<int64_t> result3 = x3; |
| 610 | result3.Intersection(y3); |
| 611 | EXPECT_TRUE(result3.Empty()) << result3; |
| 612 | } |
| 613 | |
| 614 | TEST_F(QuicIntervalSetTest, |
| 615 | QuicIntervalSetIntersectionAlternatingIntersectingIntervals) { |
| 616 | const QuicIntervalSet<int> x1({{0, 10}}); |
| 617 | const QuicIntervalSet<int> y1({{-50, 1}, {9, 95}}); |
| 618 | const QuicIntervalSet<int> expected_result1({{0, 1}, {9, 10}}); |
| 619 | |
| 620 | QuicIntervalSet<int> result1 = x1; |
| 621 | result1.Intersection(y1); |
| 622 | EXPECT_EQ(result1, expected_result1); |
| 623 | |
| 624 | const QuicIntervalSet<int16_t> x2({{0, 10}, {20, 30}, {40, 90}}); |
| 625 | const QuicIntervalSet<int16_t> y2( |
| 626 | {{-50, -40}, {-2, 2}, {9, 21}, {32, 41}, {85, 95}}); |
| 627 | const QuicIntervalSet<int16_t> expected_result2( |
| 628 | {{0, 2}, {9, 10}, {20, 21}, {40, 41}, {85, 90}}); |
| 629 | |
| 630 | QuicIntervalSet<int16_t> result2 = x2; |
| 631 | result2.Intersection(y2); |
| 632 | EXPECT_EQ(result2, expected_result2); |
| 633 | |
| 634 | const QuicIntervalSet<int64_t> x3({{-1, 5}, {5, 10}}); |
| 635 | const QuicIntervalSet<int64_t> y3({{-10, 3}, {4, 95}}); |
| 636 | const QuicIntervalSet<int64_t> expected_result3({{-1, 3}, {4, 10}}); |
| 637 | |
| 638 | QuicIntervalSet<int64_t> result3 = x3; |
| 639 | result3.Intersection(y3); |
| 640 | EXPECT_EQ(result3, expected_result3); |
| 641 | } |
| 642 | |
| 643 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionIdentical) { |
| 644 | QuicIntervalSet<int> copy(is); |
| 645 | EXPECT_TRUE(copy.Intersects(is)); |
| 646 | EXPECT_TRUE(is.Intersects(copy)); |
| 647 | is.Intersection(copy); |
| 648 | EXPECT_EQ(copy, is); |
| 649 | } |
| 650 | |
| 651 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionSuperset) { |
| 652 | QuicIntervalSet<int> mine(-1, 10000); |
| 653 | EXPECT_TRUE(mine.Intersects(is)); |
| 654 | EXPECT_TRUE(is.Intersects(mine)); |
| 655 | mine.Intersection(is); |
| 656 | EXPECT_EQ(is, mine); |
| 657 | } |
| 658 | |
| 659 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionSubset) { |
| 660 | QuicIntervalSet<int> copy(is); |
| 661 | QuicIntervalSet<int> theirs(-1, 10000); |
| 662 | EXPECT_TRUE(copy.Intersects(theirs)); |
| 663 | EXPECT_TRUE(theirs.Intersects(copy)); |
| 664 | is.Intersection(theirs); |
| 665 | EXPECT_EQ(copy, is); |
| 666 | } |
| 667 | |
| 668 | TEST_F(QuicIntervalSetTest, QuicIntervalSetIntersectionLargeSet) { |
| 669 | QuicIntervalSet<int> mine, theirs; |
| 670 | // mine: [0, 9), [10, 19), ..., [990, 999) |
| 671 | for (int i = 0; i < 1000; i += 10) { |
| 672 | mine.Add(i, i + 9); |
| 673 | } |
| 674 | |
| 675 | theirs.Add(500, 520); |
| 676 | theirs.Add(535, 545); |
| 677 | theirs.Add(801, 809); |
| 678 | EXPECT_TRUE(mine.Intersects(theirs)); |
| 679 | EXPECT_TRUE(theirs.Intersects(mine)); |
| 680 | mine.Intersection(theirs); |
| 681 | EXPECT_TRUE(Check(mine, 5, 500, 509, 510, 519, 535, 539, 540, 545, 801, 809)); |
| 682 | EXPECT_TRUE(mine.Intersects(theirs)); |
| 683 | EXPECT_TRUE(theirs.Intersects(mine)); |
| 684 | } |
| 685 | |
| 686 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifference) { |
| 687 | is.Difference(other); |
| 688 | EXPECT_TRUE(Check(is, 10, 100, 200, 300, 350, 360, 370, 380, 400, 530, 600, |
| 689 | 700, 770, 900, 1000, 1100, 1200, 1900, 2000, 2100, 2200)); |
| 690 | QuicIntervalSet<int> copy = is; |
| 691 | is.Difference(copy); |
| 692 | EXPECT_TRUE(is.Empty()); |
| 693 | } |
| 694 | |
| 695 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceSingleBounds) { |
| 696 | std::vector<QuicInterval<int>> ivals(other.begin(), other.end()); |
| 697 | for (const QuicInterval<int>& ival : ivals) { |
| 698 | is.Difference(ival.min(), ival.max()); |
| 699 | } |
| 700 | EXPECT_TRUE(Check(is, 10, 100, 200, 300, 350, 360, 370, 380, 400, 530, 600, |
| 701 | 700, 770, 900, 1000, 1100, 1200, 1900, 2000, 2100, 2200)); |
| 702 | } |
| 703 | |
| 704 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceSingleInterval) { |
| 705 | std::vector<QuicInterval<int>> ivals(other.begin(), other.end()); |
| 706 | for (const QuicInterval<int>& ival : ivals) { |
| 707 | is.Difference(ival); |
| 708 | } |
| 709 | EXPECT_TRUE(Check(is, 10, 100, 200, 300, 350, 360, 370, 380, 400, 530, 600, |
| 710 | 700, 770, 900, 1000, 1100, 1200, 1900, 2000, 2100, 2200)); |
| 711 | } |
| 712 | |
| 713 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceAlternatingIntervals) { |
| 714 | QuicIntervalSet<int> mine, theirs; |
| 715 | mine.Add(10, 20); |
| 716 | mine.Add(40, 50); |
| 717 | mine.Add(60, 70); |
| 718 | theirs.Add(25, 39); |
| 719 | theirs.Add(55, 59); |
| 720 | theirs.Add(75, 79); |
| 721 | |
| 722 | mine.Difference(theirs); |
| 723 | EXPECT_TRUE(Check(mine, 3, 10, 20, 40, 50, 60, 70)); |
| 724 | } |
| 725 | |
| 726 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceEmptyMine) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 727 | QuicIntervalSet<std::string> mine, theirs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 728 | theirs.Add("a", "b"); |
| 729 | |
| 730 | mine.Difference(theirs); |
| 731 | EXPECT_TRUE(mine.Empty()); |
| 732 | } |
| 733 | |
| 734 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceEmptyTheirs) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 735 | QuicIntervalSet<std::string> mine, theirs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 736 | mine.Add("a", "b"); |
| 737 | |
| 738 | mine.Difference(theirs); |
| 739 | EXPECT_EQ(1u, mine.Size()); |
| 740 | EXPECT_EQ("a", mine.begin()->min()); |
| 741 | EXPECT_EQ("b", mine.begin()->max()); |
| 742 | } |
| 743 | |
| 744 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceTheirsBeforeMine) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 745 | QuicIntervalSet<std::string> mine, theirs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 746 | mine.Add("y", "z"); |
| 747 | theirs.Add("a", "b"); |
| 748 | |
| 749 | mine.Difference(theirs); |
| 750 | EXPECT_EQ(1u, mine.Size()); |
| 751 | EXPECT_EQ("y", mine.begin()->min()); |
| 752 | EXPECT_EQ("z", mine.begin()->max()); |
| 753 | } |
| 754 | |
| 755 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceMineBeforeTheirs) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 756 | QuicIntervalSet<std::string> mine, theirs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 757 | mine.Add("a", "b"); |
| 758 | theirs.Add("y", "z"); |
| 759 | |
| 760 | mine.Difference(theirs); |
| 761 | EXPECT_EQ(1u, mine.Size()); |
| 762 | EXPECT_EQ("a", mine.begin()->min()); |
| 763 | EXPECT_EQ("b", mine.begin()->max()); |
| 764 | } |
| 765 | |
| 766 | TEST_F(QuicIntervalSetTest, QuicIntervalSetDifferenceIdentical) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 767 | QuicIntervalSet<std::string> mine; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 768 | mine.Add("a", "b"); |
| 769 | mine.Add("c", "d"); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 770 | QuicIntervalSet<std::string> theirs(mine); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 771 | |
| 772 | mine.Difference(theirs); |
| 773 | EXPECT_TRUE(mine.Empty()); |
| 774 | } |
| 775 | |
| 776 | TEST_F(QuicIntervalSetTest, EmptyComplement) { |
| 777 | // The complement of an empty set is the input interval: |
| 778 | QuicIntervalSet<int> iset; |
| 779 | iset.Complement(100, 200); |
| 780 | EXPECT_TRUE(Check(iset, 1, 100, 200)); |
| 781 | } |
| 782 | |
| 783 | TEST(QuicIntervalSetMultipleCompactionTest, OuterCovering) { |
| 784 | QuicIntervalSet<int> iset; |
| 785 | // First add a bunch of disjoint ranges |
| 786 | iset.Add(100, 150); |
| 787 | iset.Add(200, 250); |
| 788 | iset.Add(300, 350); |
| 789 | iset.Add(400, 450); |
| 790 | EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450)); |
| 791 | // Now add a big range that covers all of these ranges |
| 792 | iset.Add(0, 500); |
| 793 | EXPECT_TRUE(Check(iset, 1, 0, 500)); |
| 794 | } |
| 795 | |
| 796 | TEST(QuicIntervalSetMultipleCompactionTest, InnerCovering) { |
| 797 | QuicIntervalSet<int> iset; |
| 798 | // First add a bunch of disjoint ranges |
| 799 | iset.Add(100, 150); |
| 800 | iset.Add(200, 250); |
| 801 | iset.Add(300, 350); |
| 802 | iset.Add(400, 450); |
| 803 | EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450)); |
| 804 | // Now add a big range that partially covers the left and right most ranges. |
| 805 | iset.Add(125, 425); |
| 806 | EXPECT_TRUE(Check(iset, 1, 100, 450)); |
| 807 | } |
| 808 | |
| 809 | TEST(QuicIntervalSetMultipleCompactionTest, LeftCovering) { |
| 810 | QuicIntervalSet<int> iset; |
| 811 | // First add a bunch of disjoint ranges |
| 812 | iset.Add(100, 150); |
| 813 | iset.Add(200, 250); |
| 814 | iset.Add(300, 350); |
| 815 | iset.Add(400, 450); |
| 816 | EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450)); |
| 817 | // Now add a big range that partially covers the left most range. |
| 818 | iset.Add(125, 500); |
| 819 | EXPECT_TRUE(Check(iset, 1, 100, 500)); |
| 820 | } |
| 821 | |
| 822 | TEST(QuicIntervalSetMultipleCompactionTest, RightCovering) { |
| 823 | QuicIntervalSet<int> iset; |
| 824 | // First add a bunch of disjoint ranges |
| 825 | iset.Add(100, 150); |
| 826 | iset.Add(200, 250); |
| 827 | iset.Add(300, 350); |
| 828 | iset.Add(400, 450); |
| 829 | EXPECT_TRUE(Check(iset, 4, 100, 150, 200, 250, 300, 350, 400, 450)); |
| 830 | // Now add a big range that partially covers the right most range. |
| 831 | iset.Add(0, 425); |
| 832 | EXPECT_TRUE(Check(iset, 1, 0, 450)); |
| 833 | } |
| 834 | |
| 835 | // Helper method for testing and verifying the results of a one-interval |
| 836 | // completement case. |
| 837 | static bool CheckOneComplement(int add_min, |
| 838 | int add_max, |
| 839 | int comp_min, |
| 840 | int comp_max, |
| 841 | int count, |
| 842 | ...) { |
| 843 | QuicIntervalSet<int> iset; |
| 844 | iset.Add(add_min, add_max); |
| 845 | iset.Complement(comp_min, comp_max); |
| 846 | bool result = true; |
| 847 | va_list ap; |
| 848 | va_start(ap, count); |
| 849 | if (!VA_Check(iset, count, ap)) { |
| 850 | result = false; |
| 851 | } |
| 852 | va_end(ap); |
| 853 | return result; |
| 854 | } |
| 855 | |
| 856 | TEST_F(QuicIntervalSetTest, SingleIntervalComplement) { |
| 857 | // Verify the complement of a set with one interval (i): |
| 858 | // |----- i -----| |
| 859 | // |----- args -----| |
| 860 | EXPECT_TRUE(CheckOneComplement(0, 10, 50, 150, 1, 50, 150)); |
| 861 | |
| 862 | // |----- i -----| |
| 863 | // |----- args -----| |
| 864 | EXPECT_TRUE(CheckOneComplement(50, 150, 0, 100, 1, 0, 50)); |
| 865 | |
| 866 | // |----- i -----| |
| 867 | // |----- args -----| |
| 868 | EXPECT_TRUE(CheckOneComplement(50, 150, 50, 150, 0)); |
| 869 | |
| 870 | // |---------- i ----------| |
| 871 | // |----- args -----| |
| 872 | EXPECT_TRUE(CheckOneComplement(50, 500, 100, 300, 0)); |
| 873 | |
| 874 | // |----- i -----| |
| 875 | // |---------- args ----------| |
| 876 | EXPECT_TRUE(CheckOneComplement(50, 500, 0, 800, 2, 0, 50, 500, 800)); |
| 877 | |
| 878 | // |----- i -----| |
| 879 | // |----- args -----| |
| 880 | EXPECT_TRUE(CheckOneComplement(50, 150, 100, 300, 1, 150, 300)); |
| 881 | |
| 882 | // |----- i -----| |
| 883 | // |----- args -----| |
| 884 | EXPECT_TRUE(CheckOneComplement(50, 150, 200, 300, 1, 200, 300)); |
| 885 | } |
| 886 | |
| 887 | // Helper method that copies <iset> and takes its complement, |
| 888 | // returning false if Check succeeds. |
| 889 | static bool CheckComplement(const QuicIntervalSet<int>& iset, |
| 890 | int comp_min, |
| 891 | int comp_max, |
| 892 | int count, |
| 893 | ...) { |
| 894 | QuicIntervalSet<int> iset_copy = iset; |
| 895 | iset_copy.Complement(comp_min, comp_max); |
| 896 | bool result = true; |
| 897 | va_list ap; |
| 898 | va_start(ap, count); |
| 899 | if (!VA_Check(iset_copy, count, ap)) { |
| 900 | result = false; |
| 901 | } |
| 902 | va_end(ap); |
| 903 | return result; |
| 904 | } |
| 905 | |
| 906 | TEST_F(QuicIntervalSetTest, MultiIntervalComplement) { |
| 907 | // Initialize a small test set: |
| 908 | QuicIntervalSet<int> iset; |
| 909 | iset.Add(100, 200); |
| 910 | iset.Add(300, 400); |
| 911 | iset.Add(500, 600); |
| 912 | |
| 913 | // |----- i -----| |
| 914 | // |----- comp -----| |
| 915 | EXPECT_TRUE(CheckComplement(iset, 0, 50, 1, 0, 50)); |
| 916 | |
| 917 | // |----- i -----| |
| 918 | // |----- comp -----| |
| 919 | EXPECT_TRUE(CheckComplement(iset, 0, 200, 1, 0, 100)); |
| 920 | EXPECT_TRUE(CheckComplement(iset, 0, 220, 2, 0, 100, 200, 220)); |
| 921 | |
| 922 | // |----- i -----| |
| 923 | // |----- comp -----| |
| 924 | EXPECT_TRUE(CheckComplement(iset, 100, 600, 2, 200, 300, 400, 500)); |
| 925 | |
| 926 | // |---------- i ----------| |
| 927 | // |----- comp -----| |
| 928 | EXPECT_TRUE(CheckComplement(iset, 300, 400, 0)); |
| 929 | EXPECT_TRUE(CheckComplement(iset, 250, 400, 1, 250, 300)); |
| 930 | EXPECT_TRUE(CheckComplement(iset, 300, 450, 1, 400, 450)); |
| 931 | EXPECT_TRUE(CheckComplement(iset, 250, 450, 2, 250, 300, 400, 450)); |
| 932 | |
| 933 | // |----- i -----| |
| 934 | // |---------- comp ----------| |
| 935 | EXPECT_TRUE( |
| 936 | CheckComplement(iset, 0, 700, 4, 0, 100, 200, 300, 400, 500, 600, 700)); |
| 937 | |
| 938 | // |----- i -----| |
| 939 | // |----- comp -----| |
| 940 | EXPECT_TRUE(CheckComplement(iset, 400, 700, 2, 400, 500, 600, 700)); |
| 941 | EXPECT_TRUE(CheckComplement(iset, 350, 700, 2, 400, 500, 600, 700)); |
| 942 | |
| 943 | // |----- i -----| |
| 944 | // |----- comp -----| |
| 945 | EXPECT_TRUE(CheckComplement(iset, 700, 800, 1, 700, 800)); |
| 946 | } |
| 947 | |
| 948 | // Verifies ToString, operator<< don't assert. |
| 949 | TEST_F(QuicIntervalSetTest, ToString) { |
| 950 | QuicIntervalSet<int> iset; |
| 951 | iset.Add(300, 400); |
| 952 | iset.Add(100, 200); |
| 953 | iset.Add(500, 600); |
| 954 | EXPECT_TRUE(!iset.ToString().empty()); |
| 955 | QUIC_VLOG(2) << iset; |
| 956 | // Order and format of ToString() output is guaranteed. |
| 957 | EXPECT_EQ("{ [100, 200) [300, 400) [500, 600) }", iset.ToString()); |
| 958 | EXPECT_EQ("{ [1, 2) }", QuicIntervalSet<int>(1, 2).ToString()); |
| 959 | EXPECT_EQ("{ }", QuicIntervalSet<int>().ToString()); |
| 960 | } |
| 961 | |
| 962 | TEST_F(QuicIntervalSetTest, ConstructionDiscardsEmptyInterval) { |
| 963 | EXPECT_TRUE(QuicIntervalSet<int>(QuicInterval<int>(2, 2)).Empty()); |
| 964 | EXPECT_TRUE(QuicIntervalSet<int>(2, 2).Empty()); |
| 965 | EXPECT_FALSE(QuicIntervalSet<int>(QuicInterval<int>(2, 3)).Empty()); |
| 966 | EXPECT_FALSE(QuicIntervalSet<int>(2, 3).Empty()); |
| 967 | } |
| 968 | |
| 969 | TEST_F(QuicIntervalSetTest, Swap) { |
| 970 | QuicIntervalSet<int> a, b; |
| 971 | a.Add(300, 400); |
| 972 | b.Add(100, 200); |
| 973 | b.Add(500, 600); |
| 974 | a.Swap(&b); |
| 975 | EXPECT_TRUE(Check(a, 2, 100, 200, 500, 600)); |
| 976 | EXPECT_TRUE(Check(b, 1, 300, 400)); |
| 977 | swap(a, b); |
| 978 | EXPECT_TRUE(Check(a, 1, 300, 400)); |
| 979 | EXPECT_TRUE(Check(b, 2, 100, 200, 500, 600)); |
| 980 | } |
| 981 | |
| 982 | TEST_F(QuicIntervalSetTest, OutputReturnsOstreamRef) { |
| 983 | std::stringstream ss; |
| 984 | const QuicIntervalSet<int> v(QuicInterval<int>(1, 2)); |
| 985 | auto return_type_is_a_ref = [](std::ostream&) {}; |
| 986 | return_type_is_a_ref(ss << v); |
| 987 | } |
| 988 | |
| 989 | struct NotOstreamable { |
QUICHE team | 2d5d234 | 2019-03-21 13:01:37 -0700 | [diff] [blame] | 990 | bool operator<(const NotOstreamable&) const { return false; } |
| 991 | bool operator>(const NotOstreamable&) const { return false; } |
| 992 | bool operator!=(const NotOstreamable&) const { return false; } |
| 993 | bool operator>=(const NotOstreamable&) const { return true; } |
| 994 | bool operator<=(const NotOstreamable&) const { return true; } |
| 995 | bool operator==(const NotOstreamable&) const { return true; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 996 | }; |
| 997 | |
| 998 | TEST_F(QuicIntervalSetTest, IntervalOfTypeWithNoOstreamSupport) { |
| 999 | const NotOstreamable v; |
| 1000 | const QuicIntervalSet<NotOstreamable> d(QuicInterval<NotOstreamable>(v, v)); |
| 1001 | // EXPECT_EQ builds a string representation of d. If d::operator<<() |
| 1002 | // would be defined then this test would not compile because NotOstreamable |
| 1003 | // objects lack the operator<<() support. |
| 1004 | EXPECT_EQ(d, d); |
| 1005 | } |
| 1006 | |
| 1007 | class QuicIntervalSetInitTest : public QuicTest { |
| 1008 | protected: |
| 1009 | const std::vector<QuicInterval<int>> intervals_{{0, 1}, {2, 4}}; |
| 1010 | }; |
| 1011 | |
| 1012 | TEST_F(QuicIntervalSetInitTest, DirectInit) { |
| 1013 | std::initializer_list<QuicInterval<int>> il = {{0, 1}, {2, 3}, {3, 4}}; |
| 1014 | QuicIntervalSet<int> s(il); |
| 1015 | EXPECT_THAT(s, ElementsAreArray(intervals_)); |
| 1016 | } |
| 1017 | |
| 1018 | TEST_F(QuicIntervalSetInitTest, CopyInit) { |
| 1019 | std::initializer_list<QuicInterval<int>> il = {{0, 1}, {2, 3}, {3, 4}}; |
| 1020 | QuicIntervalSet<int> s = il; |
| 1021 | EXPECT_THAT(s, ElementsAreArray(intervals_)); |
| 1022 | } |
| 1023 | |
| 1024 | TEST_F(QuicIntervalSetInitTest, AssignIterPair) { |
| 1025 | QuicIntervalSet<int> s(0, 1000); // Make sure assign clears. |
| 1026 | s.assign(intervals_.begin(), intervals_.end()); |
| 1027 | EXPECT_THAT(s, ElementsAreArray(intervals_)); |
| 1028 | } |
| 1029 | |
| 1030 | TEST_F(QuicIntervalSetInitTest, AssignInitList) { |
| 1031 | QuicIntervalSet<int> s(0, 1000); // Make sure assign clears. |
| 1032 | s.assign({{0, 1}, {2, 3}, {3, 4}}); |
| 1033 | EXPECT_THAT(s, ElementsAreArray(intervals_)); |
| 1034 | } |
| 1035 | |
| 1036 | TEST_F(QuicIntervalSetInitTest, AssignmentInitList) { |
| 1037 | std::initializer_list<QuicInterval<int>> il = {{0, 1}, {2, 3}, {3, 4}}; |
| 1038 | QuicIntervalSet<int> s; |
| 1039 | s = il; |
| 1040 | EXPECT_THAT(s, ElementsAreArray(intervals_)); |
| 1041 | } |
| 1042 | |
| 1043 | TEST_F(QuicIntervalSetInitTest, BracedInitThenBracedAssign) { |
| 1044 | QuicIntervalSet<int> s{{0, 1}, {2, 3}, {3, 4}}; |
| 1045 | s = {{0, 1}, {2, 4}}; |
| 1046 | EXPECT_THAT(s, ElementsAreArray(intervals_)); |
| 1047 | } |
| 1048 | |
| 1049 | } // namespace |
| 1050 | } // namespace test |
| 1051 | } // namespace quic |