forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaskWorkspaceTest.h
146 lines (122 loc) · 5.44 KB
/
MaskWorkspaceTest.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
// 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 <cxxtest/TestSuite.h>
#include "MantidDataObjects/MaskWorkspace.h"
#include "MantidFrameworkTestHelpers/ComponentCreationHelper.h"
#include "MantidGeometry/Instrument.h"
#include "PropertyManagerHelper.h"
using Mantid::DataObjects::MaskWorkspace_const_sptr;
using Mantid::DataObjects::MaskWorkspace_sptr;
class MaskWorkspaceTest : public CxxTest::TestSuite {
public:
void test_default_constructor() { TS_ASSERT_THROWS_NOTHING(Mantid::DataObjects::MaskWorkspace()); }
void test_constructor_using_length() {
int nDetectors = 10;
Mantid::DataObjects::MaskWorkspace maskWS(nDetectors);
TS_ASSERT_EQUALS(maskWS.getNumberHistograms(), nDetectors);
TS_ASSERT_EQUALS(maskWS.blocksize(), 1);
TS_ASSERT_THROWS(maskWS.getNumberMasked(), const std::runtime_error &);
TS_ASSERT_THROWS(maskWS.isMasked(0), const std::runtime_error &);
TS_ASSERT_EQUALS(maskWS.isMaskedIndex(0), false);
maskWS.setMaskedIndex(0);
TS_ASSERT_EQUALS(maskWS.isMaskedIndex(0), true);
}
void test_constructure_using_instrument() {
int pixels = 10;
Mantid::Geometry::Instrument_sptr inst = ComponentCreationHelper::createTestInstrumentRectangular2(1, pixels);
inst->setName("MaskWorkspaceTest_Instrument");
Mantid::DataObjects::MaskWorkspace maskWS(inst, false);
for (int i = 0; i < pixels; i++)
maskWS.setValue(i, 1); // mask the pixel
TS_ASSERT_EQUALS(maskWS.getNumberHistograms(), pixels * pixels);
TS_ASSERT_EQUALS(maskWS.getNumberMasked(), pixels);
TS_ASSERT(maskWS.isMasked(0));
}
void testClone() {
// As test_mask_accessors(), set on ws, get on clone.
int pixels = 10;
int maskpixels = 25;
Mantid::Geometry::Instrument_sptr inst = ComponentCreationHelper::createTestInstrumentRectangular2(1, pixels);
inst->setName("MaskWorkspaceTest_Instrument");
Mantid::DataObjects::MaskWorkspace maskWS(inst, false);
for (int i = 0; i < maskpixels; i++)
maskWS.setMasked(i); // mask the pixel
auto cloned = maskWS.clone();
TS_ASSERT_EQUALS(cloned->getNumberHistograms(), pixels * pixels);
TS_ASSERT_EQUALS(cloned->getNumberMasked(), maskpixels);
TS_ASSERT(cloned->isMasked(0));
TS_ASSERT_EQUALS(cloned->isMasked(maskpixels),
false); // one past the masked ones
// unmask a pixel and check it
maskWS.setMasked(0, false);
cloned = maskWS.clone();
TS_ASSERT_EQUALS(cloned->isMasked(0), false);
// check of a group of pixels
std::set<Mantid::detid_t> detIds;
detIds.insert(0); // isn't masked
TS_ASSERT_EQUALS(cloned->isMasked(detIds), false);
detIds.insert(1); // is masked
TS_ASSERT_EQUALS(cloned->isMasked(detIds), false);
detIds.erase(0);
detIds.insert(2);
TS_ASSERT_EQUALS(cloned->isMasked(detIds), true);
}
void test_mask_accessors() {
int pixels = 10;
int maskpixels = 25;
Mantid::Geometry::Instrument_sptr inst = ComponentCreationHelper::createTestInstrumentRectangular2(1, pixels);
inst->setName("MaskWorkspaceTest_Instrument");
Mantid::DataObjects::MaskWorkspace maskWS(inst, false);
for (int i = 0; i < maskpixels; i++)
maskWS.setMasked(i); // mask the pixel
TS_ASSERT_EQUALS(maskWS.getNumberHistograms(), pixels * pixels);
TS_ASSERT_EQUALS(maskWS.getNumberMasked(), maskpixels);
TS_ASSERT(maskWS.isMasked(0));
TS_ASSERT_EQUALS(maskWS.isMasked(maskpixels),
false); // one past the masked ones
// unmask a pixel and check it
maskWS.setMasked(0, false);
TS_ASSERT_EQUALS(maskWS.isMasked(0), false);
// check of a group of pixels
std::set<Mantid::detid_t> detIds;
detIds.insert(0); // isn't masked
TS_ASSERT_EQUALS(maskWS.isMasked(detIds), false);
detIds.insert(1); // is masked
TS_ASSERT_EQUALS(maskWS.isMasked(detIds), false);
detIds.erase(0);
detIds.insert(2);
TS_ASSERT_EQUALS(maskWS.isMasked(detIds), true);
}
/**
* Test declaring an input MaskWorkspace and retrieving it as const_sptr or
* sptr
*/
void testGetProperty_const_sptr() {
const std::string wsName = "InputWorkspace";
MaskWorkspace_sptr wsInput(new Mantid::DataObjects::MaskWorkspace());
PropertyManagerHelper manager;
manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input);
// Check property can be obtained as const_sptr or sptr
MaskWorkspace_const_sptr wsConst;
MaskWorkspace_sptr wsNonConst;
TS_ASSERT_THROWS_NOTHING(wsConst = manager.getValue<MaskWorkspace_const_sptr>(wsName));
TS_ASSERT(wsConst != nullptr);
TS_ASSERT_THROWS_NOTHING(wsNonConst = manager.getValue<MaskWorkspace_sptr>(wsName));
TS_ASSERT(wsNonConst != nullptr);
TS_ASSERT_EQUALS(wsConst, wsNonConst);
// Check TypedValue can be cast to const_sptr or to sptr
PropertyManagerHelper::TypedValue val(manager, wsName);
MaskWorkspace_const_sptr wsCastConst;
MaskWorkspace_sptr wsCastNonConst;
TS_ASSERT_THROWS_NOTHING(wsCastConst = (MaskWorkspace_const_sptr)val);
TS_ASSERT(wsCastConst != nullptr);
TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (MaskWorkspace_sptr)val);
TS_ASSERT(wsCastNonConst != nullptr);
TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst);
}
};