forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MDBoxIteratorTest.h
775 lines (678 loc) · 27.1 KB
/
MDBoxIteratorTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidDataObjects/MDBox.h"
#include "MantidDataObjects/MDBoxIterator.h"
#include "MantidDataObjects/MDEventFactory.h"
#include "MantidDataObjects/MDGridBox.h"
#include "MantidGeometry/MDGeometry/MDBoxImplicitFunction.h"
#include "MantidGeometry/MDGeometry/MDImplicitFunction.h"
#include "MantidGeometry/MDGeometry/MDPlane.h"
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include "MantidKernel/WarningSuppressions.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"
#include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>
using namespace Mantid::DataObjects;
using namespace Mantid::API;
using namespace Mantid;
using namespace Mantid::Kernel;
using Mantid::Geometry::MDBoxImplicitFunction;
using Mantid::Geometry::MDImplicitFunction;
using Mantid::Geometry::MDPlane;
class MDBoxIteratorTest : public CxxTest::TestSuite {
public:
using gbox_t = MDGridBox<MDLeanEvent<1>, 1>;
using ibox_t = MDBoxBase<MDLeanEvent<1>, 1>;
//--------------------------------------------------------------------------------------
/** Make a gridded box with this structure:
Names Width of each box
A 64
|
B0 -- B1 -------- B2 ------------ B3 16
| |
C00-3 C20 C21 C22 C23 4
|
D210 D211 D212 D213 1
*/
gbox_t *A;
ibox_t *B1, *B3;
gbox_t *B0, *B2;
ibox_t *C00, *C01, *C02, *C03;
ibox_t *C20, *C22, *C23;
gbox_t *C21;
ibox_t *D210, *D211, *D212, *D213;
void setUp() override {
// Top level grid box
A = MDEventsTestHelper::makeMDGridBox<1>(4, 1, 0.0, 64.0);
A->splitContents(0); // Split B0 into C00 to C03
A->splitContents(2); // Split B2 into C20 to C23
B0 = dynamic_cast<gbox_t *>(A->getChild(0));
B2 = dynamic_cast<gbox_t *>(A->getChild(2));
B2->splitContents(1); // Split C21 into D210 to D212
B1 = dynamic_cast<ibox_t *>(A->getChild(1));
B3 = dynamic_cast<ibox_t *>(A->getChild(3));
TS_ASSERT_EQUALS(B1, dynamic_cast<ibox_t *>(A->getChild(1)));
C00 = dynamic_cast<ibox_t *>(B0->getChild(0));
C01 = dynamic_cast<ibox_t *>(B0->getChild(1));
C02 = dynamic_cast<ibox_t *>(B0->getChild(2));
C03 = dynamic_cast<ibox_t *>(B0->getChild(3));
C20 = dynamic_cast<ibox_t *>(B2->getChild(0));
C21 = dynamic_cast<gbox_t *>(B2->getChild(1));
C22 = dynamic_cast<ibox_t *>(B2->getChild(2));
C23 = dynamic_cast<ibox_t *>(B2->getChild(3));
D210 = dynamic_cast<ibox_t *>(C21->getChild(0));
D211 = dynamic_cast<ibox_t *>(C21->getChild(1));
D212 = dynamic_cast<ibox_t *>(C21->getChild(2));
D213 = dynamic_cast<ibox_t *>(C21->getChild(3));
}
void tearDown() override {
delete A->getBoxController();
delete A;
}
//--------------------------------------------------------------------------------------
void test_ctor_with_null_box_fails() {
using boxit_t = MDBoxIterator<MDLeanEvent<1>, 1>;
TS_ASSERT_THROWS_ANYTHING(new boxit_t(nullptr, 10, false););
}
//--------------------------------------------------------------------------------------
/** Increment the iterator and return true if the next box is the expected
* one*/
bool nextIs(MDBoxIterator<MDLeanEvent<1>, 1> *it, ibox_t *expected) {
// std::cout << it->getBox()->getExtentsStr() << '\n';
if (!it->next())
return false;
if (it->getBox() != expected)
return false;
return true;
}
void test_iterator_basic() {
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, false);
// Start with the top one
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(nextIs(it.get(), B0));
TS_ASSERT(nextIs(it.get(), C00));
TS_ASSERT(nextIs(it.get(), C01));
TS_ASSERT(nextIs(it.get(), C02));
TS_ASSERT(nextIs(it.get(), C03));
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), B2));
TS_ASSERT(nextIs(it.get(), C20));
TS_ASSERT(nextIs(it.get(), C21));
TS_ASSERT(nextIs(it.get(), D210));
TS_ASSERT(nextIs(it.get(), D211));
TS_ASSERT(nextIs(it.get(), D212));
TS_ASSERT(nextIs(it.get(), D213));
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(nextIs(it.get(), B3));
// No more!
TS_ASSERT(!it->next());
// Calling next again does not cause problems.
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_depth_limit_1() {
// Limit depth to 1 (the B level)
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 1, false);
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(nextIs(it.get(), B0));
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), B2));
TS_ASSERT(nextIs(it.get(), B3));
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_depth_limit_0() {
// Limit depth to 0 (the A level)
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 0, false);
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_starting_deeper() {
// Start at a depth of 1 (on B0)
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(B0, 20, false);
TS_ASSERT_EQUALS(it->getBox(), B0);
TS_ASSERT(nextIs(it.get(), C00));
TS_ASSERT(nextIs(it.get(), C01));
TS_ASSERT(nextIs(it.get(), C02));
TS_ASSERT(nextIs(it.get(), C03));
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_leaf_only() {
// Leaf-only iterator = skips anything with children
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true);
// This is the first leaf node
TS_ASSERT_EQUALS(it->getBox(), C00);
TS_ASSERT(nextIs(it.get(), C01));
TS_ASSERT(nextIs(it.get(), C02));
TS_ASSERT(nextIs(it.get(), C03));
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), C20));
TS_ASSERT(nextIs(it.get(), D210));
TS_ASSERT(nextIs(it.get(), D211));
TS_ASSERT(nextIs(it.get(), D212));
TS_ASSERT(nextIs(it.get(), D213));
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(nextIs(it.get(), B3));
// No more!
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_leaf_only_depth_2() {
// A node is considered a 'leaf' if it is at maxDepth
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 2, true);
// This is the first leaf node
TS_ASSERT_EQUALS(it->getBox(), C00);
TS_ASSERT(nextIs(it.get(), C01));
TS_ASSERT(nextIs(it.get(), C02));
TS_ASSERT(nextIs(it.get(), C03));
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), C20));
TS_ASSERT(
nextIs(it.get(), C21)); // This is now a 'leaf' due to the maxDepth
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(nextIs(it.get(), B3));
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_leaf_only_depth_1() {
// A node is considered a 'leaf' if it is at maxDepth
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 1, true);
// This is the first leaf node
TS_ASSERT_EQUALS(it->getBox(), B0);
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), B2));
TS_ASSERT(nextIs(it.get(), B3));
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_leaf_only_depth_0() {
// A node is considered a 'leaf' if it is at maxDepth
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 0, true);
// This is the ONLY leaf node
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_leaf_only_starting_deeper() {
// Now we start at B2 and look at only leaves
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(B2, 10, true);
TS_ASSERT_EQUALS(it->getBox(), C20);
TS_ASSERT(nextIs(it.get(), D210));
TS_ASSERT(nextIs(it.get(), D211));
TS_ASSERT(nextIs(it.get(), D212));
TS_ASSERT(nextIs(it.get(), D213));
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_leaf_only_starting_deeper_depth_limited() {
// Now we start at B2 and look at only leaves up to depth 2
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(B2, 2, true);
TS_ASSERT_EQUALS(it->getBox(), C20);
TS_ASSERT(nextIs(it.get(), C21));
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
//--------------------------------------------------------------------------------------
/** If you have just a MD Box, there is only one return from the iterator,
* and it doesn't crash or anything ;)
*/
void test_iterator_just_one_box() {
// Top level grid box
ibox_t *A = MDEventsTestHelper::makeMDBox1();
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, false);
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
BoxController *const bc = A->getBoxController();
delete bc;
delete A;
}
//--------------------------------------------------------------------------------------
/** Get the inner data from an MDBox
*/
void test_iterator_getInnerData() {
// Make a MDBox with 10 events
ibox_t *A = MDEventsTestHelper::makeMDBox1();
MDEventsTestHelper::feedMDBox<1>(A, 1, 10, 0.5, 1.0);
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, false);
TS_ASSERT_EQUALS(it->getBox(), A);
// It has some events!
TS_ASSERT_EQUALS(it->getNumEvents(), 10);
// Check inner data
for (size_t i = 0; i < 10; i++) {
TS_ASSERT_DELTA(it->getInnerSignal(i), 1.0, 1e-6);
TS_ASSERT_DELTA(it->getInnerError(i), 1.0, 1e-6);
TS_ASSERT_DELTA(it->getInnerRunIndex(i), 0, 0);
TS_ASSERT_DELTA(it->getInnerDetectorID(i), 0, 0);
TS_ASSERT_DELTA(it->getInnerPosition(i, 0), 0.5 + double(i), 1e-6);
}
// Other functions that go back to the box
TS_ASSERT_DELTA(it->getCenter()[0], 5.0, 1e-5);
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
BoxController *const bc = A->getBoxController();
delete bc;
delete A;
}
//--------------------------------------------------------------------------------------
void test_iterator_withImplicitFunction_above11() {
// Implicit function = only boxes with x > 5.0
auto func = std::make_unique<MDImplicitFunction>();
coord_t normal[1] = {+1.0};
coord_t origin[1] = {11.0};
func->addPlane(MDPlane(1, normal, origin));
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, false,
func.get());
// Start with the top one
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(nextIs(it.get(), B0));
// C00-C01 are outside the range
TS_ASSERT(nextIs(it.get(), C02));
TS_ASSERT(nextIs(it.get(), C03));
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), B2));
TS_ASSERT(nextIs(it.get(), C20));
TS_ASSERT(nextIs(it.get(), C21));
TS_ASSERT(nextIs(it.get(), D210));
TS_ASSERT(nextIs(it.get(), D211));
TS_ASSERT(nextIs(it.get(), D212));
TS_ASSERT(nextIs(it.get(), D213));
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(nextIs(it.get(), B3));
// No more!
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
//--------------------------------------------------------------------------------------
void test_iterator_withImplicitFunction_above11_leafOnly() {
// Implicit function = only boxes with x > 5.0
auto func = std::make_unique<MDImplicitFunction>();
coord_t normal[1] = {+1.0};
coord_t origin[1] = {11.0};
func->addPlane(MDPlane(1, normal, origin));
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true,
func.get());
// C00-C01 are outside the range, so the first one is C02
TS_ASSERT_EQUALS(it->getBox(), C02);
TS_ASSERT(nextIs(it.get(), C03));
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), C20));
TS_ASSERT(nextIs(it.get(), D210));
TS_ASSERT(nextIs(it.get(), D211));
TS_ASSERT(nextIs(it.get(), D212));
TS_ASSERT(nextIs(it.get(), D213));
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(nextIs(it.get(), B3));
// No more!
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
//--------------------------------------------------------------------------------------
void test_iterator_withImplicitFunction_above17() {
// Implicit function = only boxes with x > 17.0
auto func = std::make_unique<MDImplicitFunction>();
coord_t normal[1] = {+1.0};
coord_t origin[1] = {17.0};
func->addPlane(MDPlane(1, normal, origin));
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, false,
func.get());
// Start with the top one
TS_ASSERT_EQUALS(it->getBox(), A);
// B0 (and all children) are outside the range
TS_ASSERT(nextIs(it.get(), B1));
TS_ASSERT(nextIs(it.get(), B2));
TS_ASSERT(nextIs(it.get(), C20));
TS_ASSERT(nextIs(it.get(), C21));
TS_ASSERT(nextIs(it.get(), D210));
TS_ASSERT(nextIs(it.get(), D211));
TS_ASSERT(nextIs(it.get(), D212));
TS_ASSERT(nextIs(it.get(), D213));
TS_ASSERT(nextIs(it.get(), C22));
TS_ASSERT(nextIs(it.get(), C23));
TS_ASSERT(nextIs(it.get(), B3));
// No more!
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
//--------------------------------------------------------------------------------------
void test_iterator_withImplicitFunction_between_37_and_39() {
auto func = std::make_unique<MDImplicitFunction>();
coord_t normal[1] = {+1.0};
coord_t origin[1] = {37.1f};
func->addPlane(MDPlane(1, normal, origin));
coord_t normal2[1] = {-1.0};
coord_t origin2[1] = {38.9f};
func->addPlane(MDPlane(1, normal2, origin2));
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, false,
func.get());
// Go down to the only two leaf boxes that are in range
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(nextIs(it.get(), B2));
TS_ASSERT(nextIs(it.get(), C21));
TS_ASSERT(nextIs(it.get(), D211));
TS_ASSERT(nextIs(it.get(), D212));
// No more!
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
//--------------------------------------------------------------------------------------
void test_iterator_withImplicitFunction_between_37_and_39_leafOnly() {
auto func = std::make_unique<MDImplicitFunction>();
coord_t normal[1] = {+1.0};
coord_t origin[1] = {37.1f};
func->addPlane(MDPlane(1, normal, origin));
coord_t normal2[1] = {-1.0};
coord_t origin2[1] = {38.9f};
func->addPlane(MDPlane(1, normal2, origin2));
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true,
func.get());
// Only two leaf boxes are in range
TS_ASSERT_EQUALS(it->getBox(), D211);
TS_ASSERT(nextIs(it.get(), D212));
// No more!
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
//--------------------------------------------------------------------------------------
void test_iterator_withImplicitFunction_noBoxInRange() {
auto func = std::make_unique<MDImplicitFunction>();
coord_t normal[1] = {+1.0};
coord_t origin[1] = {234};
func->addPlane(MDPlane(1, normal, origin));
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, false,
func.get());
// Returns the first box but that's it
TS_ASSERT_EQUALS(it->getBox(), A);
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
//--------------------------------------------------------------------------------------
void test_iterator_withImplicitFunction_noBoxInRange_leafOnly() {
auto func = std::make_unique<MDImplicitFunction>();
coord_t normal[1] = {+1.0};
coord_t origin[1] = {234};
func->addPlane(MDPlane(1, normal, origin));
// Create an iterator
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true,
func.get());
// Nothing in the iterator!
TS_ASSERT_EQUALS(it->getDataSize(), 0);
TS_ASSERT(!it->next());
TS_ASSERT(!it->next());
}
void test_getIsMasked() {
// Mock MDBox. Only one method of interest to the mocking.
class MockMDBox : public MDBox<MDLeanEvent<2>, 2> {
API::BoxController *const pBC;
public:
MockMDBox()
: MDBox<MDLeanEvent<2>, 2>(new API::BoxController(2)),
pBC(MDBox<MDLeanEvent<2>, 2>::getBoxController())
{}
GNU_DIAG_OFF_SUGGEST_OVERRIDE
MOCK_CONST_METHOD0(getIsMasked, bool());
GNU_DIAG_ON_SUGGEST_OVERRIDE
~MockMDBox() override { delete pBC; }
};
MockMDBox mockBox;
MDBoxIterator<MDLeanEvent<2>, 2> it(&mockBox, 1, true);
// All that we want to test is that iterator::getIsMasked calls
// MDBoxBase::getIsMasked
EXPECT_CALL(mockBox, getIsMasked()).Times(1);
it.getIsMasked();
TSM_ASSERT("Iterator does not use boxes as expected",
testing::Mock::VerifyAndClearExpectations(&mockBox));
}
void test_skip_masked_detectors() {
auto setupIterator =
std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true);
// mask box 0, unmask 1 and Mask box 2. From box 3 onwards, boxes will be
// unmasked.
setupIterator->getBox()->mask();
setupIterator->next(1);
setupIterator->getBox()->unmask();
setupIterator->next(1);
setupIterator->getBox()->mask();
setupIterator->next(1);
auto evaluationIterator =
std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true);
TS_ASSERT_THROWS_NOTHING(evaluationIterator->next());
TSM_ASSERT_EQUALS("Should have skipped to the first non-masked box", 1,
evaluationIterator->getPosition());
TS_ASSERT_THROWS_NOTHING(evaluationIterator->next());
TSM_ASSERT_EQUALS("Should have skipped to the second non-masked box", 3,
evaluationIterator->getPosition());
TSM_ASSERT("The last box should be masked",
!evaluationIterator->getIsMasked());
}
void test_no_skipping_policy() {
auto setupIterator =
std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true);
// mask box 0, unmask 1 and Mask box 2. From box 3 onwards, boxes will be
// unmasked.
setupIterator->getBox()->mask();
setupIterator->next(1);
setupIterator->getBox()->unmask();
setupIterator->next(1);
setupIterator->getBox()->mask();
setupIterator->next(1);
auto evaluationIterator =
std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(
A, 20, true, new SkipNothing); // Using skip nothing policy.
TS_ASSERT_THROWS_NOTHING(evaluationIterator->next());
TSM_ASSERT_EQUALS("Should NOT have skipped to the first box", 1,
evaluationIterator->getPosition());
TS_ASSERT_THROWS_NOTHING(evaluationIterator->next());
TSM_ASSERT_EQUALS("Should NOT have skipped to the second box", 2,
evaluationIterator->getPosition());
TS_ASSERT_THROWS_NOTHING(evaluationIterator->next());
TSM_ASSERT_EQUALS("Should NOT have skipped to the third box", 3,
evaluationIterator->getPosition());
}
void test_custom_skipping_policy() {
/// Mock Skipping Policy Type to inject.
class MockSkippingPolicy : public SkippingPolicy {
public:
GNU_DIAG_OFF_SUGGEST_OVERRIDE
MOCK_CONST_METHOD0(keepGoing, bool());
MOCK_METHOD0(Die, void());
GNU_DIAG_ON_SUGGEST_OVERRIDE
~MockSkippingPolicy() override { Die(); }
};
MockSkippingPolicy *mockPolicy = new MockSkippingPolicy;
auto evaluationIterator =
std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(
A, 20, true,
mockPolicy); // Using custom policy
EXPECT_CALL(*mockPolicy, Die())
.Times(1); // Should call destructor automatically within MDBoxIterator
EXPECT_CALL(*mockPolicy, keepGoing())
.Times(static_cast<int>(
evaluationIterator->getDataSize())); // Should apply test
while (evaluationIterator->next()) // Keep calling next while true. Will
// iterate through all boxes.
{
}
// verifying that mockPoly is deleted requires calling evaluationIterator's
// destructor before the end of the function.
evaluationIterator.reset();
TSM_ASSERT("Has not used SkippingPolicy as expected.",
testing::Mock::VerifyAndClearExpectations(mockPolicy));
}
void test_getNormalizedSignal_with_mask() {
// Make a MDBox with 10 events
ibox_t *A = MDEventsTestHelper::makeMDBox1();
MDEventsTestHelper::feedMDBox<1>(A, 1, 10, 0.5, 1.0);
auto it = std::make_unique<MDBoxIterator<MDLeanEvent<1>, 1>>(A, 20, true);
// Initially the box is unmasked
TS_ASSERT_DELTA(it->getNormalizedSignal(), 1.0, 1e-5);
it->next();
// Now mask the box
it->getBox()->mask();
// For masked boxes, getNormalizedSignal() should return NaN.
TS_ASSERT(std::isnan(it->getNormalizedSignal()));
delete A->getBoxController();
delete A;
}
};
class MDBoxIteratorTestPerformance : public CxxTest::TestSuite {
MDGridBox<MDLeanEvent<3>, 3> *top;
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static MDBoxIteratorTestPerformance *createSuite() {
return new MDBoxIteratorTestPerformance();
}
static void destroySuite(MDBoxIteratorTestPerformance *suite) {
delete suite;
}
MDBoxIteratorTestPerformance() {
// 1968876 boxes in this. Top box is 5*5*5
top = MDEventsTestHelper::makeRecursiveMDGridBox<3>(5, 2);
}
public:
// ---------------------------------------------------------------
/** Make a simple iterator that will go through all the boxes */
void do_test_iterator(bool leafOnly, bool ImplicitFunction, size_t expected) {
// Count the top level box.
size_t counter = 1;
MDBoxBase<MDLeanEvent<3>, 3> *box = nullptr;
MDBoxImplicitFunction *function = nullptr;
if (ImplicitFunction) {
std::vector<coord_t> min(3, 2.001f);
std::vector<coord_t> max(3, 2.999f);
function = new MDBoxImplicitFunction(min, max);
}
MDBoxIterator<MDLeanEvent<3>, 3> it(top, 20, leafOnly, new SkipNothing,
function);
// Count all of them
while (it.next()) {
box = it.getBox();
counter++;
}
TS_ASSERT(box);
TS_ASSERT_EQUALS(counter, expected);
}
void test_iterator() {
do_test_iterator(false, false, 125 * 125 * 125 + 125 * 125 + 125 + 1);
}
void test_iterator_leafOnly() {
do_test_iterator(true, false, 125 * 125 * 125);
}
void test_iterator_withImplicitFunction() {
do_test_iterator(false, true, 1 + 125 * 125 + 125 + 1);
}
void test_iterator_withImplicitFunction_leafOnly() {
do_test_iterator(true, true, 125 * 125);
}
// ---------------------------------------------------------------
/** This iterator will also take the boxes and fill
* a vector with them; this is a full speed comparison to getBoxes()
* which directly returns that vector, if that happens to be what you need.*/
void do_test_iterator_that_fills_a_vector(bool leafOnly) {
MDBoxBase<MDLeanEvent<3>, 3> *box;
MDBoxIterator<MDLeanEvent<3>, 3> it(top, 20, leafOnly, new SkipNothing);
std::vector<MDBoxBase<MDLeanEvent<3>, 3> *> boxes;
// Iterate and fill the vector as you go.
boxes.emplace_back(it.getBox());
while (it.next()) {
box = it.getBox();
boxes.emplace_back(box);
}
TS_ASSERT(box);
size_t expected = 125 * 125 * 125 + 125 * 125 + 125 + 1;
if (leafOnly)
expected = 125 * 125 * 125;
TS_ASSERT_EQUALS(boxes.size(), expected);
}
void test_iterator_that_fills_a_vector() {
do_test_iterator_that_fills_a_vector(false);
}
void test_iterator_that_fills_a_vector_leafOnly() {
do_test_iterator_that_fills_a_vector(true);
}
// ---------------------------------------------------------------
/** For comparison, let's use getBoxes() that fills a vector directly.
* After that, we iterate through them to compare how long the whole operation
* takes.
*/
void do_test_getBoxes(bool leafOnly, int ImplicitFunction, size_t expected) {
std::vector<API::IMDNode *> boxes;
MDImplicitFunction *function = nullptr;
if (ImplicitFunction == 1) {
// Box in 3D where 2 < (x,y,z) < 3
std::vector<coord_t> min(3, 2.001f);
std::vector<coord_t> max(3, 2.999f);
function = new MDBoxImplicitFunction(min, max);
top->getBoxes(boxes, 20, leafOnly, function);
} else if (ImplicitFunction == 2) {
// Plane defining 2.2 < x < 2.4
function = new MDImplicitFunction();
coord_t normal1[3] = {+1.000f, 0, 0};
coord_t origin1[3] = {+2.201f, 0, 0};
function->addPlane(MDPlane(3, normal1, origin1));
coord_t normal2[3] = {-1.000f, 0, 0};
coord_t origin2[3] = {+2.399f, 0, 0};
function->addPlane(MDPlane(3, normal2, origin2));
top->getBoxes(boxes, 20, leafOnly, function);
} else if (ImplicitFunction == 3) {
// Box in 3D where -5 < (x,y,z) < +10
std::vector<coord_t> min(3, -4.999f);
std::vector<coord_t> max(3, +9.999f);
function = new MDBoxImplicitFunction(min, max);
top->getBoxes(boxes, 20, leafOnly, function);
} else {
top->getBoxes(boxes, 20, leafOnly);
}
TS_ASSERT_EQUALS(boxes.size(), expected);
// Now we still need to iterate through the vector to do anything, so this
// is a more fair comparison
size_t counter = 0;
for (const auto &box : boxes) {
(void)box;
++counter;
}
TS_ASSERT_EQUALS(counter, expected);
}
void test_getBoxes() {
do_test_getBoxes(false, 0, 125 * 125 * 125 + 125 * 125 + 125 + 1);
}
void test_getBoxes_leafOnly() { do_test_getBoxes(true, 0, 125 * 125 * 125); }
void test_getBoxes_withImplicitFunction() {
do_test_getBoxes(false, 1, 1 + 125 * 125 + 125 + 1);
}
void test_getBoxes_withImplicitFunction_leafOnly() {
do_test_getBoxes(true, 1, 125 * 125);
}
void test_getBoxes_withPlaneImplicitFunction() {
do_test_getBoxes(true, 2, 125 * 125 * 125 / 25);
}
void test_getBoxes_withHugeImplicitFunction() {
do_test_getBoxes(true, 3, 125 * 125 * 125);
}
};
#undef RUN_CXX_PERFORMANCE_TEST_EMBEDDED