forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialWorkspace2DTest.h
237 lines (197 loc) · 9.11 KB
/
SpecialWorkspace2DTest.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
// 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/WorkspaceProperty.h"
#include "MantidDataObjects/SpecialWorkspace2D.h"
#include "MantidFrameworkTestHelpers/ComponentCreationHelper.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include "PropertyManagerHelper.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
using namespace Mantid::API;
using namespace Mantid;
class SpecialWorkspace2DTest : public CxxTest::TestSuite {
public:
void test_default_constructor() {
SpecialWorkspace2D_sptr ws(new SpecialWorkspace2D());
TSM_ASSERT_THROWS_ANYTHING("Can't init with > 1 X or Y entries.", ws->initialize(100, 2, 1));
TSM_ASSERT_THROWS_ANYTHING("Can't init with > 1 X or Y entries.", ws->initialize(100, 1, 2));
TS_ASSERT_THROWS_NOTHING(ws->initialize(100, 1, 1));
TS_ASSERT_EQUALS(ws->getNumberHistograms(), 100);
TS_ASSERT_EQUALS(ws->blocksize(), 1);
}
void testClone() {
// As test_setValue_getValue, set on ws, get on clone.
Instrument_sptr inst = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws(new SpecialWorkspace2D(inst));
auto cloned = ws->clone();
TS_ASSERT_DIFFERS(cloned->getValue(1), 12.3);
TS_ASSERT_THROWS_NOTHING(ws->setValue(1, 12.3));
cloned = ws->clone();
TS_ASSERT_DELTA(cloned->getValue(1), 12.3, 1e-6);
TS_ASSERT_THROWS_ANYTHING(ws->setValue(46, 789));
TS_ASSERT_THROWS_ANYTHING(ws->setValue(-1, 789));
cloned = ws->clone();
TS_ASSERT_THROWS_ANYTHING(cloned->getValue(47));
TS_ASSERT_THROWS_ANYTHING(cloned->getValue(-34));
TS_ASSERT_EQUALS(cloned->getValue(47, 5.0), 5.0);
TS_ASSERT_EQUALS(cloned->getValue(147, -12.0), -12.0);
// Some extra tests: 1. clone ws, 2. set on ws, 3. clone should differ.
cloned = ws->clone();
TS_ASSERT_DELTA(cloned->getValue(1), 12.3, 1e-6);
TS_ASSERT_THROWS_NOTHING(ws->setValue(1, 1.1));
TS_ASSERT_DIFFERS(cloned->getValue(1), 1.1);
}
void test_constructor_from_Instrument() {
// Fake instrument with 5*9 pixels with ID starting at 1
Instrument_sptr inst = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws(new SpecialWorkspace2D(inst));
TS_ASSERT_EQUALS(ws->getNumberHistograms(), 45);
TS_ASSERT_EQUALS(ws->blocksize(), 1);
TS_ASSERT_EQUALS(ws->getInstrument()->getName(),
"basic"); // Name of the test instrument
const auto &dets = ws->getSpectrum(0).getDetectorIDs();
TS_ASSERT_EQUALS(dets.size(), 1);
TS_ASSERT_EQUALS(*(ws->getDetectorIDs(0).begin()), 1);
TS_ASSERT_EQUALS(*(ws->getDetectorIDs(1).begin()), 2);
}
void test_setValue_getValue() {
Instrument_sptr inst = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws(new SpecialWorkspace2D(inst));
TS_ASSERT_DIFFERS(ws->getValue(1), 12.3);
TS_ASSERT_THROWS_NOTHING(ws->setValue(1, 12.3));
TS_ASSERT_DELTA(ws->getValue(1), 12.3, 1e-6);
TS_ASSERT_THROWS_ANYTHING(ws->setValue(46, 789));
TS_ASSERT_THROWS_ANYTHING(ws->setValue(-1, 789));
TS_ASSERT_THROWS_ANYTHING(ws->getValue(47));
TS_ASSERT_THROWS_ANYTHING(ws->getValue(-34));
TS_ASSERT_EQUALS(ws->getValue(47, 5.0), 5.0);
TS_ASSERT_EQUALS(ws->getValue(147, -12.0), -12.0);
}
void test_binaryOperator() {
Instrument_sptr inst1 = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws1(new SpecialWorkspace2D(inst1));
Instrument_sptr inst2 = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws2raw(new SpecialWorkspace2D(inst2));
SpecialWorkspace2D_const_sptr ws2 = std::dynamic_pointer_cast<const SpecialWorkspace2D>(ws2raw);
// 1. AND operation
ws1->setValue(2, 1);
ws2raw->setValue(2, 1);
ws1->binaryOperation(ws2, BinaryOperator::AND);
TS_ASSERT_EQUALS(ws1->getValue(2), 2);
ws1->setValue(2, 0);
ws2raw->setValue(2, 1);
ws1->binaryOperation(ws2, BinaryOperator::AND);
TS_ASSERT_EQUALS(ws1->getValue(2), 0);
ws1->setValue(2, 1);
ws2raw->setValue(2, 0);
ws1->binaryOperation(ws2, BinaryOperator::AND);
TS_ASSERT_EQUALS(ws1->getValue(2), 0);
ws1->setValue(2, 0);
ws2raw->setValue(2, 0);
ws1->binaryOperation(ws2, BinaryOperator::AND);
TS_ASSERT_EQUALS(ws1->getValue(2), 0);
// 2. OR operation
ws1->setValue(2, 1);
ws2raw->setValue(2, 1);
ws1->binaryOperation(ws2, BinaryOperator::OR);
TS_ASSERT_EQUALS(ws1->getValue(2), 1);
ws1->setValue(2, 0);
ws2raw->setValue(2, 1);
ws1->binaryOperation(ws2, BinaryOperator::OR);
TS_ASSERT_EQUALS(ws1->getValue(2), 1);
ws1->setValue(2, 1);
ws2raw->setValue(2, 0);
ws1->binaryOperation(ws2, BinaryOperator::OR);
TS_ASSERT_EQUALS(ws1->getValue(2), 1);
ws1->setValue(2, 0);
ws2raw->setValue(2, 0);
ws1->binaryOperation(ws2, BinaryOperator::OR);
TS_ASSERT_EQUALS(ws1->getValue(2), 0);
// 3. XOR operation
// 2. OR operation
ws1->setValue(2, 1);
ws2raw->setValue(2, 1);
ws1->binaryOperation(ws2, BinaryOperator::XOR);
TS_ASSERT_EQUALS(ws1->getValue(2), 0);
ws1->setValue(2, 0);
ws2raw->setValue(2, 1);
ws1->binaryOperation(ws2, BinaryOperator::XOR);
TS_ASSERT_EQUALS(ws1->getValue(2), 1);
ws1->setValue(2, 1);
ws2raw->setValue(2, 0);
ws1->binaryOperation(ws2, BinaryOperator::XOR);
TS_ASSERT_EQUALS(ws1->getValue(2), 1);
ws1->setValue(2, 0);
ws2raw->setValue(2, 0);
ws1->binaryOperation(ws2, BinaryOperator::XOR);
TS_ASSERT_EQUALS(ws1->getValue(2), 0);
}
void test_checkcompatible() {
Instrument_sptr inst1 = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws1(new SpecialWorkspace2D(inst1));
Instrument_sptr inst2 = ComponentCreationHelper::createTestInstrumentCylindrical(6);
SpecialWorkspace2D_sptr ws2(new SpecialWorkspace2D(inst2));
// 1. AND operation
ws1->setValue(2, 1);
ws2->setValue(2, 1);
SpecialWorkspace2D_const_sptr cws2 = std::dynamic_pointer_cast<const SpecialWorkspace2D>(ws2);
TS_ASSERT_THROWS_ANYTHING(ws1->binaryOperation(cws2, BinaryOperator::AND));
}
void test_binaryNOT() {
Instrument_sptr inst1 = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws1(new SpecialWorkspace2D(inst1));
Instrument_sptr inst2 = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws2(new SpecialWorkspace2D(inst2));
Instrument_sptr inst3 = ComponentCreationHelper::createTestInstrumentCylindrical(5);
SpecialWorkspace2D_sptr ws3(new SpecialWorkspace2D(inst3));
ws2->binaryOperation(BinaryOperator::NOT);
SpecialWorkspace2D_const_sptr cws2 = std::dynamic_pointer_cast<const SpecialWorkspace2D>(ws2);
ws1->binaryOperation(cws2, BinaryOperator::AND);
ws3->binaryOperation(cws2, BinaryOperator::OR);
for (size_t i = 0; i < ws1->getNumberHistograms(); i++) {
detid_t did = *(ws1->getDetectorIDs(i).begin());
TS_ASSERT_EQUALS(ws1->getValue(did), 0);
TS_ASSERT_EQUALS(ws3->getValue(did), 1);
}
}
void test_known_to_property_for_unmangling() {
Mantid::API::WorkspaceProperty<DataObjects::SpecialWorkspace2D> property("DummyProperty", "DummyWorkspace",
Mantid::Kernel::Direction::Input);
TS_ASSERT_EQUALS("SpecialWorkspace2D", Mantid::Kernel::getUnmangledTypeName(*property.type_info()));
}
/**
* Test declaring an input SpecialWorkspace2D and retrieving it as const_sptr
* or sptr
*/
void testGetProperty_const_sptr() {
const std::string wsName = "InputWorkspace";
SpecialWorkspace2D_sptr wsInput(new SpecialWorkspace2D());
PropertyManagerHelper manager;
manager.declareProperty(wsName, wsInput, Mantid::Kernel::Direction::Input);
// Check property can be obtained as const_sptr or sptr
SpecialWorkspace2D_const_sptr wsConst;
SpecialWorkspace2D_sptr wsNonConst;
TS_ASSERT_THROWS_NOTHING(wsConst = manager.getValue<SpecialWorkspace2D_const_sptr>(wsName));
TS_ASSERT(wsConst != nullptr);
TS_ASSERT_THROWS_NOTHING(wsNonConst = manager.getValue<SpecialWorkspace2D_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);
SpecialWorkspace2D_const_sptr wsCastConst;
SpecialWorkspace2D_sptr wsCastNonConst;
TS_ASSERT_THROWS_NOTHING(wsCastConst = (SpecialWorkspace2D_const_sptr)val);
TS_ASSERT(wsCastConst != nullptr);
TS_ASSERT_THROWS_NOTHING(wsCastNonConst = (SpecialWorkspace2D_sptr)val);
TS_ASSERT(wsCastNonConst != nullptr);
TS_ASSERT_EQUALS(wsCastConst, wsCastNonConst);
}
};