-
Notifications
You must be signed in to change notification settings - Fork 122
/
FileBackedExperimentInfoTest.h
236 lines (180 loc) · 7.64 KB
/
FileBackedExperimentInfoTest.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
#ifndef MANTID_API_FILEBACKEDEXPERIMENTINFOTEST_H_
#define MANTID_API_FILEBACKEDEXPERIMENTINFOTEST_H_
#include <cxxtest/TestSuite.h>
#include "ExperimentInfoTest.h"
#include "MantidAPI/FileBackedExperimentInfo.h"
#include "MantidAPI/FileFinder.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/Sample.h"
#include <nexus/NeXusFile.hpp>
using Mantid::API::FileBackedExperimentInfo;
class FileBackedExperimentInfoTest : 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 FileBackedExperimentInfoTest *createSuite() {
return new FileBackedExperimentInfoTest();
}
static void destroySuite(FileBackedExperimentInfoTest *suite) {
delete suite;
}
FileBackedExperimentInfoTest() {
using Mantid::API::ExperimentInfo;
using Mantid::API::FileFinder;
// Cache in-memory experiment info for comparison
m_filename = FileFinder::Instance().getFullPath("HRP38692a.nxs");
if (m_filename.empty()) {
throw std::runtime_error("Cannot find test file HRP38692a.nxs");
}
m_inMemoryExptInfo = boost::make_shared<ExperimentInfo>();
::NeXus::File nxFile(m_filename, NXACC_READ);
nxFile.openGroup("mantid_workspace_1", "NXentry");
std::string paramString;
m_inMemoryExptInfo->loadExperimentInfoNexus(&nxFile, paramString);
m_inMemoryExptInfo->readParameterMap(paramString);
}
void test_toString_populates_object() {
auto fileBacked = createTestObject();
TS_ASSERT_EQUALS(fileBacked->toString(), m_inMemoryExptInfo->toString());
}
void test_cloneExperimentInfo_returns_new_file_backed_object_and_does_not_touch_file() {
auto fileBacked = createTestObject();
auto *clonedFileBacked = fileBacked->cloneExperimentInfo();
TS_ASSERT(dynamic_cast<FileBackedExperimentInfo*>(clonedFileBacked));
delete clonedFileBacked;
}
void test_getInstrument_populates_object() {
auto fileBacked = createTestObject();
auto fileBackedInstrument = fileBacked->getInstrument();
auto inMemoryInstrument = m_inMemoryExptInfo->getInstrument();
TS_ASSERT_EQUALS(fileBacked->constInstrumentParameters(),
m_inMemoryExptInfo->constInstrumentParameters());
}
void test_instrumentParameters_const_ref_method_populate_object() {
auto fileBacked = createTestObject();
const auto &pmap = fileBacked->instrumentParameters(); // const
TS_ASSERT(pmap.size() > 0);
}
void test_nonconst_ref_instrumentParameters_method_populate_object() {
auto fileBacked = createTestObject();
auto &pmap = fileBacked->instrumentParameters(); // non-const
TS_ASSERT(pmap.size() > 0);
}
void test_constInstrumentParameters_method_populate_object() {
auto fileBacked = createTestObject();
const auto &pmap = fileBacked->constInstrumentParameters();
TS_ASSERT(pmap.size() > 0);
}
void test_populateInstrumentParameters_method_populate_object() {
auto fileBacked = createTestObject();
fileBacked->populateInstrumentParameters();
const auto &pmap = fileBacked->constInstrumentParameters();
TS_ASSERT(pmap.size() > 0);
}
void test_replaceInstrumentParameters_method_populate_object() {
using Mantid::Geometry::ParameterMap;
auto fileBacked = createTestObject();
ParameterMap emptyMap;
fileBacked->replaceInstrumentParameters(emptyMap);
const auto &pmap = fileBacked->constInstrumentParameters();
TS_ASSERT_EQUALS(0, pmap.size());
}
void test_swapInstrumentParameters_method_populate_object() {
using Mantid::Geometry::ParameterMap;
auto fileBacked = createTestObject();
ParameterMap emptyMap;
fileBacked->swapInstrumentParameters(emptyMap);
const auto &pmap = fileBacked->constInstrumentParameters();
TS_ASSERT_EQUALS(0, pmap.size());
}
void test_cacheDetectorGroupings() {
auto fileBacked = createTestObject();
std::vector<Mantid::detid_t> group(2, 1);
group[1] = 2;
Mantid::det2group_map mapping;
mapping.insert(std::make_pair(1, group));
fileBacked->cacheDetectorGroupings(mapping);
}
void test_getGroupMembers() {
auto fileBacked = createTestObject();
std::vector<Mantid::detid_t> group(2, 1);
group[1] = 2;
Mantid::det2group_map mapping;
mapping.insert(std::make_pair(1, group));
fileBacked->cacheDetectorGroupings(mapping);
TS_ASSERT_EQUALS(group, fileBacked->getGroupMembers(1));
}
void test_getDetectorByID() {
auto fileBacked = createTestObject();
TS_ASSERT(fileBacked->getDetectorByID(10100));
}
void test_ModeratorModelMethods() {
auto fileBacked = createTestObject();
ModeratorModel *source = new FakeSource;
TS_ASSERT_THROWS_NOTHING(fileBacked->setModeratorModel(source));
const ModeratorModel &fetched = fileBacked->moderatorModel();
const ModeratorModel &constInput =
const_cast<const Mantid::API::ModeratorModel &>(*source);
TS_ASSERT_EQUALS(&fetched, &constInput);
}
void test_chopperModelMethods() {
auto fileBacked = createTestObject();
TS_ASSERT_THROWS_NOTHING(fileBacked->setChopperModel(new FakeChopper));
TS_ASSERT_THROWS_NOTHING(fileBacked->chopperModel(0));
}
void test_sample() {
auto fileBacked = createTestObject();
TS_ASSERT_EQUALS(m_inMemoryExptInfo->sample().getGeometryFlag(),
fileBacked->sample().getGeometryFlag());
TS_ASSERT_EQUALS(m_inMemoryExptInfo->sample().getGeometryFlag(),
fileBacked->mutableSample().getGeometryFlag());
}
void test_run() {
auto fileBacked = createTestObject();
TS_ASSERT_EQUALS(m_inMemoryExptInfo->run().getProtonCharge(),
fileBacked->run().getProtonCharge())
}
void test_getLog() {
auto fileBacked = createTestObject();
TS_ASSERT_EQUALS(m_inMemoryExptInfo->getLogAsSingleValue("gd_prtn_chrg"),
fileBacked->getLogAsSingleValue("gd_prtn_chrg"));
auto *inMemoryProp = m_inMemoryExptInfo->getLog("gd_prtn_chrg");
auto *fileBackedProp = fileBacked->getLog("gd_prtn_chrg");
TS_ASSERT_EQUALS(inMemoryProp->value(), fileBackedProp->value());
}
void test_getRunNumber() {
auto fileBacked = createTestObject();
TS_ASSERT_EQUALS(m_inMemoryExptInfo->getRunNumber(),
fileBacked->getRunNumber());
}
void test_getEMode() {
auto fileBacked = createTestObject();
TS_ASSERT_EQUALS(m_inMemoryExptInfo->getEMode(), fileBacked->getEMode());
}
void test_getEFixed() {
auto fileBacked = createTestObject();
TS_ASSERT_THROWS(fileBacked->getEFixed(10100), std::runtime_error);
}
void test_setEFixed() {
auto fileBacked = createTestObject();
TS_ASSERT_THROWS_NOTHING(fileBacked->setEFixed(10100, 12.5));
}
//------------------------------------------------------------------------------------------------
// Failure tests
//------------------------------------------------------------------------------------------------
void test_runtime_error_generated_when_unable_to_load_from_file() {
// Create the file backed experiment info, shouldn't be loaded yet
FileBackedExperimentInfo fileBacked(m_filename, "/not/right/path");
TS_ASSERT_THROWS(fileBacked.toString(), std::runtime_error);
}
private:
Mantid::API::ExperimentInfo_sptr createTestObject() {
// Create the file backed experiment info, shouldn't be loaded yet.
// Manipulate it through the interface
return boost::make_shared<FileBackedExperimentInfo>(m_filename,
"/mantid_workspace_1");
}
Mantid::API::ExperimentInfo_sptr m_inMemoryExptInfo;
std::string m_filename;
};
#endif /* MANTID_API_FILEBACKEDEXPERIMENTINFOTEST_H_ */