Verify that IntervalTree::FindIntersectingInterval yields the same results as the naive brute-force O(n) algorithm.
| 217 | // Verify that IntervalTree::FindIntersectingInterval yields the same results as the naive |
| 218 | // brute-force O(n) algorithm. |
| 219 | static void VerifyFindIntersectingInterval(const vector<IntInterval>& all_intervals, |
| 220 | const IntervalTree<IntTraits>& tree, |
| 221 | const IntInterval& query_interval) { |
| 222 | const auto& Process = [&] (const optional<int>& lower, |
| 223 | const optional<int>& upper) { |
| 224 | vector<IntInterval> results; |
| 225 | tree.FindIntersectingInterval(lower, upper, &results); |
| 226 | std::sort(results.begin(), results.end(), CompareIntervals); |
| 227 | |
| 228 | vector<IntInterval> brute_force; |
| 229 | FindIntersectingBruteForce(all_intervals, lower, upper, &brute_force); |
| 230 | std::sort(brute_force.begin(), brute_force.end(), CompareIntervals); |
| 231 | EXPECT_EQ(Stringify(brute_force), Stringify(results)); |
| 232 | }; |
| 233 | |
| 234 | { |
| 235 | // [lower, upper) |
| 236 | optional<int> lower = query_interval.left; |
| 237 | optional<int> upper = query_interval.right; |
| 238 | SCOPED_TRACE(Stringify(all_intervals) + StringPrintf(" {q=[%d, %d)}", *lower, *upper)); |
| 239 | Process(lower, upper); |
| 240 | } |
| 241 | |
| 242 | { |
| 243 | // [-OO, upper) |
| 244 | optional<int> lower = nullopt; |
| 245 | optional<int> upper = query_interval.right; |
| 246 | SCOPED_TRACE(Stringify(all_intervals) + StringPrintf(" {q=[-OO, %d)}", *upper)); |
| 247 | Process(lower, upper); |
| 248 | } |
| 249 | |
| 250 | { |
| 251 | // [lower, +OO) |
| 252 | optional<int> lower = query_interval.left; |
| 253 | optional<int> upper = nullopt; |
| 254 | SCOPED_TRACE(Stringify(all_intervals) + StringPrintf(" {q=[%d, +OO)}", *lower)); |
| 255 | Process(lower, upper); |
| 256 | } |
| 257 | |
| 258 | { |
| 259 | // [-OO, +OO) |
| 260 | optional<int> lower = query_interval.left; |
| 261 | optional<int> upper = nullopt; |
| 262 | SCOPED_TRACE(Stringify(all_intervals) + StringPrintf(" {q=[-OO, +OO)}")); |
| 263 | Process(lower, upper); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static vector<IntInterval> CreateRandomIntervals(int n = 100) { |
| 268 | vector<IntInterval> intervals; |
no test coverage detected