xref: /aosp_15_r20/external/perfetto/src/trace_processor/containers/interval_tree_unittest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_processor/containers/interval_tree.h"
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <numeric>
22 #include <random>
23 #include <tuple>
24 #include <utility>
25 #include <vector>
26 
27 #include "perfetto/base/compiler.h"
28 #include "test/gtest_and_gmock.h"
29 
30 namespace perfetto::trace_processor {
31 
operator ==(const Interval & a,const Interval & b)32 inline bool operator==(const Interval& a, const Interval& b) {
33   return std::tie(a.start, a.end, a.id) == std::tie(b.start, b.end, b.id);
34 }
35 
36 namespace {
37 
38 using Interval = Interval;
39 using testing::IsEmpty;
40 using testing::UnorderedElementsAre;
41 
CreateIntervals(std::vector<std::pair<uint32_t,uint32_t>> periods)42 std::vector<Interval> CreateIntervals(
43     std::vector<std::pair<uint32_t, uint32_t>> periods) {
44   std::vector<Interval> res;
45   uint32_t id = 0;
46   for (auto period : periods) {
47     res.push_back({period.first, period.second, id++});
48   }
49   return res;
50 }
51 
TEST(IntervalTree,Trivial)52 TEST(IntervalTree, Trivial) {
53   std::vector<Interval> interval({{10, 20, 5}});
54   IntervalTree tree(interval);
55   std::vector<uint32_t> overlaps;
56   tree.FindOverlaps(5, 30, overlaps);
57 
58   ASSERT_THAT(overlaps, UnorderedElementsAre(5));
59 }
60 
TEST(IntervalTree,Simple)61 TEST(IntervalTree, Simple) {
62   auto intervals = CreateIntervals({{0, 10}, {5, 20}, {30, 40}});
63   IntervalTree tree(intervals);
64   std::vector<uint32_t> overlaps;
65   tree.FindOverlaps(4, 30, overlaps);
66 
67   ASSERT_THAT(overlaps, UnorderedElementsAre(0, 1));
68 }
69 
TEST(IntervalTree,SinglePointOverlap)70 TEST(IntervalTree, SinglePointOverlap) {
71   auto intervals = CreateIntervals({{10, 20}});
72   IntervalTree tree(intervals);
73   std::vector<uint32_t> overlaps;
74 
75   // Overlaps at the start point only
76   tree.FindOverlaps(10, 10, overlaps);
77   ASSERT_THAT(overlaps, IsEmpty());
78 
79   overlaps.clear();
80 
81   // Overlaps at the end point only
82   tree.FindOverlaps(20, 20, overlaps);
83   ASSERT_THAT(overlaps, IsEmpty());
84 }
85 
TEST(IntervalTree,NoOverlaps)86 TEST(IntervalTree, NoOverlaps) {
87   auto intervals = CreateIntervals({{10, 20}, {30, 40}});
88   IntervalTree tree(intervals);
89   std::vector<uint32_t> overlaps;
90 
91   // Before all intervals
92   tree.FindOverlaps(5, 9, overlaps);
93   ASSERT_THAT(overlaps, IsEmpty());
94   overlaps.clear();
95 
96   // Between intervals
97   tree.FindOverlaps(21, 29, overlaps);
98   ASSERT_THAT(overlaps, IsEmpty());
99   overlaps.clear();
100 
101   // After all intervals
102   tree.FindOverlaps(41, 50, overlaps);
103   ASSERT_THAT(overlaps, IsEmpty());
104 }
105 
TEST(IntervalTree,IdenticalIntervals)106 TEST(IntervalTree, IdenticalIntervals) {
107   auto intervals = CreateIntervals({{10, 20}, {10, 20}});
108   IntervalTree tree(intervals);
109   std::vector<uint32_t> overlaps;
110   tree.FindOverlaps(10, 20, overlaps);
111   ASSERT_THAT(overlaps, UnorderedElementsAre(0, 1));
112 }
113 
TEST(IntervalTree,MultipleOverlapsVariousPositions)114 TEST(IntervalTree, MultipleOverlapsVariousPositions) {
115   auto intervals = CreateIntervals({{5, 15}, {10, 20}, {12, 22}, {25, 35}});
116   IntervalTree tree(intervals);
117 
118   std::vector<uint32_t> overlaps;
119   /// Starts before, ends within
120   tree.FindOverlaps(9, 11, overlaps);
121   ASSERT_THAT(overlaps, UnorderedElementsAre(0, 1));
122 
123   overlaps.clear();
124   // Starts within, ends within
125   tree.FindOverlaps(13, 21, overlaps);
126   ASSERT_THAT(overlaps, UnorderedElementsAre(0, 1, 2));
127 
128   overlaps.clear();
129   // Starts within, ends after
130   tree.FindOverlaps(18, 26, overlaps);
131   ASSERT_THAT(overlaps, UnorderedElementsAre(1, 2, 3));
132 }
133 
TEST(IntervalTree,OverlappingEndpoints)134 TEST(IntervalTree, OverlappingEndpoints) {
135   auto intervals = CreateIntervals({{10, 20}, {20, 30}});
136   IntervalTree tree(intervals);
137   std::vector<uint32_t> overlaps;
138 
139   tree.FindOverlaps(19, 21, overlaps);
140   ASSERT_THAT(overlaps, UnorderedElementsAre(0, 1));
141 }
142 
TEST(IntervalTree,Stress)143 TEST(IntervalTree, Stress) {
144   static constexpr size_t kCount = 9249;
145   std::minstd_rand0 rng(42);
146 
147   std::vector<std::pair<uint32_t, uint32_t>> periods;
148   uint32_t prev_max = 0;
149   for (uint32_t i = 0; i < kCount; ++i) {
150     prev_max += static_cast<uint32_t>(rng()) % 100;
151     periods.push_back(
152         {prev_max, prev_max + (static_cast<uint32_t>(rng()) % 100)});
153   }
154   auto intervals = CreateIntervals(periods);
155   IntervalTree tree(intervals);
156   std::vector<uint32_t> overlaps;
157   tree.FindOverlaps(periods.front().first, periods.back().first + 1, overlaps);
158 
159   EXPECT_EQ(overlaps.size(), kCount);
160 }
161 
162 }  // namespace
163 }  // namespace perfetto::trace_processor
164