Newer
Older
#ifndef MANTID_GEOMETRY_DETECTORINFOITERATORTEST_H_
#define MANTID_GEOMETRY_DETECTORINFOITERATORTEST_H_
#include <algorithm>
#include <boost/make_shared.hpp>
#include <cxxtest/TestSuite.h>
#include "MantidBeamline/DetectorInfo.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidGeometry/Instrument/DetectorInfoItem.h"
#include "MantidGeometry/Instrument/DetectorInfoIterator.h"
#include "MantidGeometry/Instrument/InstrumentVisitor.h"
#include "MantidGeometry/Instrument/ParameterMap.h"
#include "MantidKernel/EigenConversionHelpers.h"
#include "MantidKernel/V3D.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
using namespace Mantid::Geometry;
using Mantid::Kernel::V3D;
using namespace ComponentCreationHelper;
using Mantid::detid_t;
class DetectorInfoIteratorTest : 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 DetectorInfoIteratorTest *createSuite() {
return new DetectorInfoIteratorTest();
}
static void destroySuite(DetectorInfoIteratorTest *suite) { delete suite; }
Mantid::Geometry::DetectorInfo create_detector_info_object() {
// Create a very basic instrument to visit
auto visitee = createMinimalInstrument(V3D(0, 0, 0), // Source position
V3D(10, 0, 0), // Sample position
V3D(11, 0, 0)); // Detector position
// Add 10 more detectors to the instrument
for (int i = 2; i < 12; i++) {
Detector *det =
new Detector("point-detector", i /*detector id*/, nullptr);
det->setPos(V3D(10 + i, 0, 0));
std::string num = std::to_string(i);
det->setShape(createSphere(0.01 /*1cm*/, V3D(0, 0, 0), num));
visitee->add(det);
visitee->markAsDetector(det);
}
// Create the instrument visitor
InstrumentVisitor visitor(visitee);
// Visit everything
visitor.walkInstrument();
auto detInfo = visitor.detectorInfo();
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
// Get details from DetectorInfo
auto detIds = visitor.detectorIds();
auto detMap = visitor.detectorIdToIndexMap();
// Create the Geometry DetectorInfo
Mantid::Geometry::DetectorInfo detectorInfo{visitor.detectorInfo(), visitee,
detIds, detMap};
return detectorInfo;
}
void test_iterator_begin() {
// Get the DetectorInfo object
auto detectorInfo = create_detector_info_object();
auto iter = detectorInfo.begin();
// Check we start at the correct place
TS_ASSERT(iter != detectorInfo.end());
}
void test_iterator_end() {
// Get the DetectorInfo object
auto detectorInfo = create_detector_info_object();
auto iter = detectorInfo.end();
// Check we start at the correct place
TS_ASSERT(iter != detectorInfo.begin());
}
void test_iterator_increment() {
// Get the DetectorInfo object
auto detectorInfo = create_detector_info_object();
auto iter = detectorInfo.begin();
// Check that we start at the beginning
TS_ASSERT(iter == detectorInfo.begin());
// Increment and check index
for (int i = 0; i < 11; ++i) {
TS_ASSERT_EQUALS(iter->getIndex(), i);
++iter;
}
// Check we've reached the end
TS_ASSERT(iter == detectorInfo.end());
void test_iterator_decrement() {
// Get the DetectorInfo object
auto detectorInfo = create_detector_info_object();
auto iter = detectorInfo.end();
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
// Check that we start at the end
TS_ASSERT(iter == detectorInfo.end());
// Decrement and check index
for (int i = 11; i > 0; --i) {
TS_ASSERT_EQUALS(iter->getIndex(), i);
--iter;
}
// Check we've reached the beginning
TS_ASSERT(iter == detectorInfo.begin());
}
void test_iterator_advance() {
// Get the DetectorInfo object
auto detectorInfo = create_detector_info_object();
auto iter = detectorInfo.begin();
// Advance 6 places
std::advance(iter, 6);
TS_ASSERT_EQUALS(iter->getIndex(), 6);
// Go past end of valid range
std::advance(iter, 8);
TS_ASSERT(iter == detectorInfo.end());
// Go backwards
std::advance(iter, -2);
TS_ASSERT_EQUALS(iter->getIndex(), 9);
// Go past the start
std::advance(iter, -100);
TS_ASSERT(iter == detectorInfo.begin());
}
void test_iterator_position() {
// Get the DetectorInfo object
auto detectorInfo = create_detector_info_object();
auto iter = detectorInfo.begin();
for (int i = 0; i < 11; i++) {
TS_ASSERT_EQUALS(iter->position(), i);
++iter;
}
}
void test_copy_iterator() {
// Get the DetectorInfo object
auto detectorInfo = create_detector_info_object();
auto iter = detectorInfo.begin();
auto iterCopy = DetectorInfoIterator(iter);
TS_ASSERT_EQUALS(iter->getIndex(), 0);
TS_ASSERT_EQUALS(iterCopy->getIndex(), 0);
TS_ASSERT_EQUALS(iterCopy->position(), 0);
++iterCopy;
TS_ASSERT_EQUALS(iter->getIndex(), 1);
TS_ASSERT_EQUALS(iterCopy->getIndex(), 1);
#endif /* MANTID_HISTOGRAMDATA_HISTOGRAMITERATORTEST_H_ */