forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MDBoxFlatTreeTest.h
103 lines (81 loc) · 3.85 KB
/
MDBoxFlatTreeTest.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
// 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/MDBoxFlatTree.h"
#include "MantidDataObjects/MDLeanEvent.h"
#include "MantidFrameworkTestHelpers/MDEventsTestHelper.h"
#include <Poco/File.h>
#include <cxxtest/TestSuite.h>
#include <memory>
using Mantid::DataObjects::MDBoxFlatTree;
class MDBoxFlatTreeTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static MDBoxFlatTreeTest *createSuite() { return new MDBoxFlatTreeTest(); }
static void destroySuite(MDBoxFlatTreeTest *suite) { delete suite; }
MDBoxFlatTreeTest() {
using Mantid::DataObjects::MDEventsTestHelper::makeFakeMDEventWorkspace;
// make non-file backet mdEv workspace with 10000 events
spEw3 = makeFakeMDEventWorkspace("TestLeanEvWS", 10000);
}
void testFlatTreeOperations() {
MDBoxFlatTree BoxTree;
TS_ASSERT_EQUALS(0, BoxTree.getNBoxes());
TS_ASSERT_THROWS_NOTHING((BoxTree.initFlatStructure(spEw3, "aFile")));
TSM_ASSERT_EQUALS("Workspace creatrion helper should generate ws split into 1001 boxes", 1001, BoxTree.getNBoxes());
TS_ASSERT_THROWS_NOTHING(BoxTree.saveBoxStructure("someFile.nxs"));
Poco::File testFile("someFile.nxs");
TSM_ASSERT("BoxTree was not able to create test file", testFile.exists());
MDBoxFlatTree BoxStoredTree;
int nDims = 3;
TSM_ASSERT_THROWS("Should throw as the box data were written for lean event and now we "
"try to retrieve full events",
BoxStoredTree.loadBoxStructure("someFile.nxs", nDims, "MDEvent"), const std::runtime_error &);
nDims = 0;
TSM_ASSERT_THROWS_NOTHING("Should path now and return nDims",
BoxStoredTree.loadBoxStructure("someFile.nxs", nDims, "MDLeanEvent"));
TSM_ASSERT_EQUALS("Should be nDims = 3", 3, nDims);
TS_ASSERT_THROWS_NOTHING(BoxStoredTree.loadBoxStructure("someFile.nxs", nDims, "MDLeanEvent"));
size_t nDim = size_t(BoxStoredTree.getNDims());
auto new_bc = std::make_shared<Mantid::API::BoxController>(nDim);
new_bc->fromXMLString(BoxStoredTree.getBCXMLdescr());
TSM_ASSERT("Should restore the box controller equal to the one before saving ",
*(spEw3->getBoxController()) == *(new_bc));
std::vector<Mantid::API::IMDNode *> Boxes;
TS_ASSERT_THROWS_NOTHING(BoxStoredTree.restoreBoxTree(Boxes, new_bc, false, false));
std::vector<Mantid::API::IMDNode *> OldBoxes;
TS_ASSERT_THROWS_NOTHING(spEw3->getBoxes(OldBoxes, 1000, false));
// just in case, should be already sorted
Mantid::API::IMDNode::sortObjByID(OldBoxes);
for (size_t i = 0; i < OldBoxes.size(); i++) {
TS_ASSERT(OldBoxes[i]->getID() == Boxes[i]->getID());
size_t numChildren = Boxes[i]->getNumChildren();
TS_ASSERT(OldBoxes[i]->getNumChildren() == numChildren);
if (numChildren > 0) {
TS_ASSERT(OldBoxes[i]->getChild(0)->getID() == Boxes[i]->getChild(0)->getID());
}
}
// Clean up MDGridBoxes as they will take care of their children
// If we delete directly in the first loop over then the pointers later in
// the list
// become invalid
std::vector<size_t> gridIndices;
for (size_t i = 0; i < Boxes.size(); ++i) {
if (!Boxes[i]->isBox())
gridIndices.emplace_back(i);
}
for (auto gridIndex : gridIndices) {
delete Boxes[gridIndex];
}
// Clean up file
if (testFile.exists())
testFile.remove();
}
private:
Mantid::API::IMDEventWorkspace_sptr spEw3;
};