RangeSet_t.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (RangeSet_t)
2 #include "boost/test/unit_test.hpp"
5 
6 #include <iostream>
7 #include <string>
8 
9 using namespace std::string_literals;
10 using art::EventRange;
11 using art::RangeSet;
12 using art::RunID;
13 
14 BOOST_AUTO_TEST_SUITE(RangeSet_t)
15 
17 {
18  RangeSet const rs{1};
19  BOOST_TEST(rs.empty());
20  BOOST_TEST(rs.has_disjoint_ranges());
21  BOOST_TEST(rs.is_sorted());
22 }
23 
25 {
26  RangeSet const rs1{1};
27  RangeSet const rs2{1};
28  BOOST_TEST(art::disjoint_ranges(rs1, rs2));
29 }
30 
32 {
33  RangeSet rs{1};
34  rs.emplace_range(1, 2, 2);
35  rs.emplace_range(1, 7, 7);
36  rs.emplace_range(1, 7, 7);
37  BOOST_TEST(rs.empty());
38 }
39 
41 {
42  auto rs = RangeSet::forRun(RunID{72});
43  BOOST_TEST(!rs.empty());
44  BOOST_TEST(rs.has_disjoint_ranges());
45  BOOST_TEST(rs.is_valid());
46  BOOST_TEST(rs.is_sorted());
47  BOOST_TEST(rs.is_collapsed());
48  BOOST_TEST(rs.is_full_run());
49  std::ostringstream oss;
50  oss << rs;
51  BOOST_TEST(oss.str() == " Run: 72 (full run)"s);
52 }
53 
55 {
56  auto const rs1 = RangeSet::forRun(RunID{1});
57  auto const rs2 = RangeSet::forRun(RunID{2});
58 
59  BOOST_TEST(rs1.is_full_run());
60  BOOST_TEST(rs2.is_full_run());
61  BOOST_TEST(rs1.has_disjoint_ranges());
62  BOOST_TEST(rs2.has_disjoint_ranges());
63  BOOST_TEST(art::disjoint_ranges(rs1, rs2));
64 }
65 
67 {
68  RangeSet rs1{1};
69  rs1.emplace_range(1, 1, 2);
70  auto const rs2 = RangeSet::forRun(RunID{1});
71 
72  BOOST_TEST(!rs1.is_full_run());
73  BOOST_TEST(rs2.is_full_run());
74  BOOST_TEST(rs1.has_disjoint_ranges());
75  BOOST_TEST(rs2.has_disjoint_ranges());
76  BOOST_TEST(!art::disjoint_ranges(rs1, rs2));
77 }
78 
80 {
81  // Mimic constructing a full-run range set based on its internal
82  // representation (this is how a full-run range set would be
83  // constructed from reading from a file).
85  auto const rs2 = RangeSet::forRun(RunID{1});
86  BOOST_TEST(rs1 == rs2);
87 }
88 
90 {
91  RangeSet rs{1};
92  rs.emplace_range(1, 2, 7);
93  rs.emplace_range(1, 9, 14);
94  BOOST_TEST(rs.has_disjoint_ranges());
95  BOOST_TEST(rs.is_sorted());
96  BOOST_TEST(!rs.is_full_run());
97 }
98 
100 {
101  RangeSet rs1{1};
102  rs1.emplace_range(1, 1, 8);
103  rs1.emplace_range(1, 9, 14);
104  rs1.emplace_range(3, 1, 8);
105  rs1.emplace_range(4, 5, 8);
106  BOOST_TEST(rs1.has_disjoint_ranges());
107  BOOST_TEST(rs1.is_sorted());
108 
109  RangeSet rs2{1};
110  rs2.emplace_range(1, 8, 9);
111  rs2.emplace_range(1, 14, 101);
112  rs2.emplace_range(2, 14, 101);
113  rs2.emplace_range(4, 1, 5);
114  BOOST_TEST(rs2.has_disjoint_ranges());
115  BOOST_TEST(rs2.is_sorted());
116 
117  BOOST_TEST(art::disjoint_ranges(rs1, rs2));
118 }
119 
121 {
122  RangeSet rs{1};
123  rs.emplace_range(1, 1, 4);
124  rs.emplace_range(1, 4, 6);
125  rs.emplace_range(1, 6, 11);
126  rs.emplace_range(1, 11, 101);
127  BOOST_TEST(rs.ranges().size() == 4u);
128  rs.collapse();
129  BOOST_TEST(rs.ranges().size() == 1u);
130  BOOST_TEST(rs.front().begin() == 1u);
131  BOOST_TEST(rs.front().end() == 101u);
132  BOOST_TEST(rs.has_disjoint_ranges());
133 }
134 
136 {
137  RangeSet rs{1};
138  rs.emplace_range(1, 1, 4);
139  rs.emplace_range(1, 1, 11);
140  rs.emplace_range(1, 4, 11);
141  BOOST_TEST(rs.is_sorted());
142  BOOST_CHECK_EXCEPTION(
143  rs.collapse(), art::Exception, [](art::Exception const& e) {
144  return e.categoryCode() == art::errors::EventRangeOverlap;
145  });
146 }
147 
149 {
150  RangeSet rs{1};
151  rs.emplace_range(1, 2, 7);
152  rs.emplace_range(1, 9, 14);
153  rs.split_range(1, 4);
154  RangeSet ref{1};
155  ref.emplace_range(1, 2, 4);
156  ref.emplace_range(1, 4, 7);
157  ref.emplace_range(1, 9, 14);
158  BOOST_TEST(rs == ref);
159 }
160 
162 {
163  RangeSet rs{1};
164  rs.emplace_range(1, 2, 7);
165  rs.emplace_range(1, 9, 14);
166  auto split_range1 =
167  rs.split_range(1, 7); // SubRun 1, Event 7 not contained in range
168  auto split_range2 =
169  rs.split_range(2, 7); // SubRun 2, Event 7 not contained " "
170  BOOST_TEST(split_range1.first == rs.end_idx());
171  BOOST_TEST(split_range2.first == rs.end_idx());
172  BOOST_TEST(!split_range1.second);
173  BOOST_TEST(!split_range2.second);
174 }
175 
177 {
178  RangeSet rs{1};
179  rs.emplace_range(1, 2, 3);
180  auto split_range1 = rs.split_range(1, 2);
181  auto split_range2 = rs.split_range(1, 3);
182  BOOST_TEST(split_range1.first == rs.end_idx()); // Range too small to split
183  BOOST_TEST(split_range2.first ==
184  rs.end_idx()); // SubRun 1, Event 3 not contained in range
185  BOOST_TEST(!split_range1.second);
186  BOOST_TEST(!split_range2.second);
187 }
188 
190 {
191  RangeSet rs{1};
192  rs.emplace_range(1, 2, 7);
193  rs.emplace_range(1, 9, 14);
194  rs.split_range(1, 4);
195  RangeSet ref{1};
196  ref.assign_ranges(rs, rs.begin_idx(), rs.end_idx());
197  BOOST_TEST(rs == ref);
198 }
199 
201 {
202  // Ranges: [1,3) & [4,8)
203  RangeSet rs1{2};
204  rs1.emplace_range(1, 1, 3);
205  rs1.emplace_range(1, 4, 8);
206  rs1.collapse();
207  BOOST_TEST_REQUIRE(rs1.has_disjoint_ranges());
208 
209  // Ranges: [3,4) & [8,11)
210  RangeSet rs2{2};
211  rs2.emplace_range(1, 3, 4);
212  rs2.emplace_range(1, 8, 11);
213  rs2.collapse();
214  BOOST_TEST_REQUIRE(rs2.has_disjoint_ranges());
215 
216  rs1.merge(rs2); // Ranges: [1,3), [3,4), [4,8) & [8,11)
217  // collapse to: [1,11)
218  BOOST_TEST(rs1.ranges().size() == 1u);
219  BOOST_TEST(rs1.front().begin() == 1u);
220  BOOST_TEST(rs1.front().end() == 11u);
221 }
222 
224 {
225  // Ranges: [1,3) & [4,8)
226  RangeSet rs1{2};
227  rs1.emplace_range(1, 1, 3);
228  rs1.emplace_range(1, 4, 8);
229  rs1.collapse();
230  BOOST_TEST_REQUIRE(rs1.has_disjoint_ranges());
231 
232  // Ranges: [1,7)
233  RangeSet rs2{2};
234  rs2.emplace_range(1, 1, 7);
235  rs2.collapse();
236  BOOST_TEST_REQUIRE(rs2.has_disjoint_ranges());
237 
238  BOOST_TEST(!art::disjoint_ranges(rs1, rs2));
239  BOOST_CHECK_EXCEPTION(
240  rs1.merge(rs2), art::Exception, [](art::Exception const& e) {
241  return e.categoryCode() == art::errors::EventRangeOverlap;
242  });
243 }
244 
246 {
247  // Ranges: [1,3) & [4,8)
248  RangeSet rs1{2};
249  rs1.emplace_range(1, 1, 3);
250  rs1.emplace_range(1, 4, 8);
251  rs1.collapse();
252  BOOST_TEST_REQUIRE(rs1.has_disjoint_ranges());
253 
254  // Ranges: [3,4)
255  RangeSet rs2{2};
256  rs2.emplace_range(1, 3, 4);
257  rs2.collapse();
258  BOOST_TEST_REQUIRE(rs2.has_disjoint_ranges());
259 
261  rs1.merge(rs2);
262 
263  std::vector<EventRange> const ref_ranges{
264  EventRange{1, 1, 8},
265  };
266 
267  RangeSet const ref{2, ref_ranges};
268  BOOST_TEST(rs1 == ref);
269 }
270 
272 {
273  // Range: [1,3)
274  RangeSet rs1{2};
275  rs1.emplace_range(1, 1, 3);
276  rs1.collapse();
277  auto const ref = rs1;
278 
279  // Empty range
280  RangeSet rs2{2};
281  rs2.collapse();
282 
284  rs1.merge(rs2);
285 
286  BOOST_TEST(rs1 == ref);
287 }
288 
290 {
291  // Range for full run
292  auto rs1 = RangeSet::forRun(RunID{2});
293  auto const ref = rs1;
294 
295  // Empty range
296  RangeSet rs2{2};
297  rs2.collapse();
298 
300  rs1.merge(rs2);
301 
302  BOOST_TEST(rs1 == ref);
303 }
304 
306 {
307  // Test merging of empty event ranges
308  RangeSet rs1{1};
309  rs1.emplace_range(1, 2, 2);
310  rs1.emplace_range(1, 2, 2);
311  rs1.emplace_range(1, 2, 2);
312  rs1.collapse(); // Should all collapse to (1, 2, 2).
313 
314  RangeSet const rs2{1, {EventRange{1, 2, 2}}};
315  BOOST_TEST(rs1 == rs2);
316 
317  // Now merge the RangeSets and confirm that it equals rs2.
318  rs1.merge(rs2);
319  BOOST_TEST(rs1 == rs2);
320 }
321 
323 {
324  RangeSet rs1{2};
325  rs1.emplace_range(1, 1, 3);
326  rs1.emplace_range(1, 4, 8);
327  rs1.emplace_range(2, 3, 17);
328  rs1.collapse();
329 
330  RangeSet rs2{2};
331  rs2.emplace_range(1, 4, 8);
332  rs2.emplace_range(2, 7, 9);
333  rs2.collapse();
334 
335  BOOST_TEST(art::overlapping_ranges(rs1, rs2));
336  BOOST_TEST(art::overlapping_ranges(rs2, rs1));
337 }
338 
340 {
341  auto const rs1 = RangeSet::invalid();
342  auto const rs2 = RangeSet::invalid();
343  BOOST_TEST(!art::overlapping_ranges(rs1, rs2));
344  BOOST_TEST(!art::same_ranges(rs1, rs2));
345  BOOST_TEST(!art::disjoint_ranges(rs1, rs2));
346 }
347 
348 BOOST_AUTO_TEST_SUITE_END()
EventRange full_run_event_range()
Definition: RangeSet.cc:81
void assign_ranges(RangeSet const &rs, std::size_t const b, std::size_t const e)
Definition: RangeSet.cc:317
BOOST_TEST_REQUIRE(static_cast< bool >(inFile))
RangeSet & collapse()
Definition: RangeSet.cc:262
BOOST_AUTO_TEST_CASE(empty1)
Definition: RangeSet_t.cc:16
const double e
std::enable_if_t< detail::are_handles< T, U >::value, bool > disjoint_ranges(T const &a, U const &b)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::enable_if_t< detail::are_handles< T, U >::value, bool > overlapping_ranges(T const &a, U const &b)
std::enable_if_t< detail::are_handles< T, U >::value, bool > same_ranges(T const &a, U const &b)
static QCString * s
Definition: config.cpp:1042
void emplace_range(ARGS &&...)