forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkspaceValidatorsTest.h
245 lines (205 loc) · 9.81 KB
/
WorkspaceValidatorsTest.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
// 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 "MantidAPI/Axis.h"
#include "MantidAPI/CommonBinsValidator.h"
#include "MantidAPI/HistogramValidator.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/OrientedLatticeValidator.h"
#include "MantidAPI/RawCountValidator.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/SampleValidator.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidFrameworkTestHelpers/ComponentCreationHelper.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidKernel/Material.h"
#include "MantidKernel/NeutronAtom.h"
#include "MantidKernel/UnitFactory.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid::Kernel;
using namespace Mantid::API;
class WorkspaceValidatorsTest : public CxxTest::TestSuite {
private:
WorkspaceUnitValidator *wavUnitVal;
WorkspaceUnitValidator *anyUnitVal;
HistogramValidator *histVal;
RawCountValidator *rawVal;
RawCountValidator *nonRawVal;
CommonBinsValidator *binVal;
MatrixWorkspace_sptr ws1;
MatrixWorkspace_sptr ws2;
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static WorkspaceValidatorsTest *createSuite() { return new WorkspaceValidatorsTest(); }
static void destroySuite(WorkspaceValidatorsTest *suite) { delete suite; }
WorkspaceValidatorsTest() {
wavUnitVal = new WorkspaceUnitValidator("Wavelength");
anyUnitVal = new WorkspaceUnitValidator("");
histVal = new HistogramValidator();
rawVal = new RawCountValidator();
nonRawVal = new RawCountValidator(false);
binVal = new CommonBinsValidator();
ws1 = MatrixWorkspace_sptr(new Mantid::DataObjects::Workspace2D);
ws1->initialize(2, 10, 9);
ws2 = MatrixWorkspace_sptr(new Mantid::DataObjects::Workspace2D);
ws2->initialize(2, 10, 10);
ws2->getAxis(0)->unit() = UnitFactory::Instance().create("Wavelength");
ws2->setDistribution(true);
}
~WorkspaceValidatorsTest() override {
delete wavUnitVal;
delete anyUnitVal;
delete histVal;
delete rawVal;
delete nonRawVal;
delete binVal;
}
void testCast() {
TS_ASSERT(dynamic_cast<IValidator *>(wavUnitVal));
TS_ASSERT(dynamic_cast<IValidator *>(anyUnitVal));
TS_ASSERT(dynamic_cast<IValidator *>(histVal));
TS_ASSERT(dynamic_cast<IValidator *>(rawVal));
TS_ASSERT(dynamic_cast<IValidator *>(nonRawVal));
TS_ASSERT(dynamic_cast<IValidator *>(binVal));
}
void testWorkspaceUnitValidator() { TS_ASSERT_THROWS_NOTHING(WorkspaceUnitValidator()); }
void testWorkspaceUnitValidator_getType() {
TS_ASSERT_EQUALS(wavUnitVal->getType(), "workspaceunit");
TS_ASSERT_EQUALS(anyUnitVal->getType(), "workspaceunit");
}
void testWorkspaceUnitValidator_isValid() {
TS_ASSERT_EQUALS(wavUnitVal->isValid(ws1), "The workspace must have units of Wavelength");
TS_ASSERT_EQUALS(wavUnitVal->isValid(ws2), "");
TS_ASSERT_EQUALS(anyUnitVal->isValid(ws1), "The workspace must have units");
TS_ASSERT_EQUALS(anyUnitVal->isValid(ws2), "");
}
void testHistogramValidator() { TS_ASSERT_THROWS_NOTHING(HistogramValidator(false)); }
void testHistogramValidator_getType() { TS_ASSERT_EQUALS(histVal->getType(), "histogram"); }
void testHistogramValidator_isValid() {
TS_ASSERT_EQUALS(histVal->isValid(ws1), "");
TS_ASSERT_EQUALS(histVal->isValid(ws2), "The workspace must contain histogram data");
HistogramValidator reverse(false);
TS_ASSERT_EQUALS(reverse.isValid(ws1), "The workspace must not contain histogram data");
TS_ASSERT_EQUALS(reverse.isValid(ws2), "");
}
void testRawCountValidator_getType() { TS_ASSERT_EQUALS(nonRawVal->getType(), "rawcount"); }
void testRawCountValidator_isValid() {
TS_ASSERT_EQUALS(rawVal->isValid(ws1), "");
TS_ASSERT_EQUALS(rawVal->isValid(ws2), "A workspace containing numbers of counts is required here");
TS_ASSERT_EQUALS(nonRawVal->isValid(ws1), "A workspace of numbers of counts is not allowed here");
TS_ASSERT_EQUALS(nonRawVal->isValid(ws2), "");
}
void testCommonBinsValidator_getType() { TS_ASSERT_EQUALS(binVal->getType(), "commonbins"); }
void testCommonBinsValidator_isValid() {
TS_ASSERT_EQUALS(binVal->isValid(ws1), "");
TS_ASSERT_EQUALS(binVal->isValid(ws2), "");
ws1->dataX(0)[5] = 1.1;
TS_ASSERT_EQUALS(binVal->isValid(ws1), "The workspace must have common bin boundaries for all histograms");
}
void testWSPropertyandValidator() {
auto wavUnitValidator = std::make_shared<WorkspaceUnitValidator>("Wavelength");
WorkspaceProperty<MatrixWorkspace> wsp1("workspace1", "ws1", Direction::Input, wavUnitValidator);
// test property validation
TS_ASSERT_EQUALS(wsp1.isValid(), "Workspace \"ws1\" was not found in the Analysis Data Service");
;
TS_ASSERT_EQUALS(wsp1.setValue(""), "Enter a name for the Input/InOut workspace");
// fine and correct unit
wsp1 = ws2;
TS_ASSERT_EQUALS(wsp1.isValid(), "");
;
// fine and no unit
TS_ASSERT_THROWS(wsp1 = ws1, const std::invalid_argument &);
TS_ASSERT_EQUALS(wsp1.setValue(""), "Enter a name for the Input/InOut workspace");
TS_ASSERT_EQUALS(wsp1.isValid(), "Enter a name for the Input/InOut workspace");
}
void testInstrumentValidator() {
{ // default validator
auto ws = std::make_shared<Mantid::DataObjects::Workspace2D>();
auto inst = std::make_shared<Mantid::Geometry::Instrument>();
auto *sample = new Mantid::Geometry::Component("Sample");
inst->add(sample);
inst->markAsSamplePos(sample);
auto instVal = std::make_shared<InstrumentValidator>();
TS_ASSERT_EQUALS(instVal->isValid(ws), "The instrument is missing the following components: sample holder");
ws->setInstrument(inst);
TS_ASSERT_EQUALS(instVal->isValid(ws), "");
}
{ // requires just a source
auto ws = std::make_shared<Mantid::DataObjects::Workspace2D>();
auto inst = std::make_shared<Mantid::Geometry::Instrument>();
auto instVal = std::make_shared<InstrumentValidator>(InstrumentValidator::SourcePosition);
TS_ASSERT_EQUALS(instVal->isValid(ws), "The instrument is missing the following components: source");
auto *src = new Mantid::Geometry::ObjComponent("Source");
inst->add(src);
inst->markAsSource(src);
ws->setInstrument(inst);
TS_ASSERT_EQUALS(instVal->isValid(ws), "");
}
{ // requires source & sample position
auto ws = std::make_shared<Mantid::DataObjects::Workspace2D>();
auto inst = std::make_shared<Mantid::Geometry::Instrument>();
auto instVal = std::make_shared<InstrumentValidator>(InstrumentValidator::SourcePosition |
InstrumentValidator::SamplePosition);
TS_ASSERT_EQUALS(instVal->isValid(ws), "The instrument is missing the "
"following components: "
"source,sample holder");
auto *sample = new Mantid::Geometry::Component("Sample");
inst->add(sample);
inst->markAsSamplePos(sample);
auto *src = new Mantid::Geometry::ObjComponent("Source");
inst->add(src);
inst->markAsSource(src);
ws->setInstrument(inst);
TS_ASSERT_EQUALS(instVal->isValid(ws), "");
}
}
void testOrientedLatticeValidator() {
using Mantid::API::OrientedLatticeValidator;
using Mantid::DataObjects::Workspace2D;
using Mantid::Geometry::OrientedLattice;
OrientedLatticeValidator validator;
auto ws = std::make_shared<Workspace2D>();
TS_ASSERT_EQUALS(validator.isValid(ws), "Workspace must have a sample with an orientation matrix defined.");
ws->mutableSample().setOrientedLattice(std::make_unique<OrientedLattice>());
TS_ASSERT_EQUALS(validator.isValid(ws), "");
}
void testSampleValidator() {
using Mantid::Geometry::CSGObject;
using Mantid::Kernel::Material;
using Mantid::PhysicalConstants::NeutronAtom;
// These should be separate tests when they are refactored out
{ // requires just shape
auto ws = std::make_shared<Mantid::DataObjects::Workspace2D>();
auto sampleVal = std::make_shared<SampleValidator>(SampleValidator::Shape);
TS_ASSERT_EQUALS(sampleVal->isValid(ws), "The sample is missing the following properties: shape");
auto shape = ComponentCreationHelper::createSphere(0.01);
ws->mutableSample().setShape(shape);
TS_ASSERT_EQUALS(sampleVal->isValid(ws), "");
}
{ // requires just material
auto ws = std::make_shared<Mantid::DataObjects::Workspace2D>();
auto sampleVal = std::make_shared<SampleValidator>(SampleValidator::Material);
TS_ASSERT_EQUALS(sampleVal->isValid(ws), "The sample is missing the following properties: material");
auto noShape = std::make_shared<CSGObject>();
noShape->setMaterial(Material("V", NeutronAtom(), 0.072));
ws->mutableSample().setShape(noShape);
TS_ASSERT_EQUALS(sampleVal->isValid(ws), "");
}
{ // requires both
auto ws = std::make_shared<Mantid::DataObjects::Workspace2D>();
auto sampleVal = std::make_shared<SampleValidator>(SampleValidator::Shape | SampleValidator::Material);
TS_ASSERT_EQUALS(sampleVal->isValid(ws), "The sample is missing the following properties: shape,material");
auto shape = ComponentCreationHelper::createSphere(0.01);
shape->setMaterial(Material("V", NeutronAtom(), 0.072));
ws->mutableSample().setShape(shape);
TS_ASSERT_EQUALS(sampleVal->isValid(ws), "");
}
}
};