Newer
Older
#ifndef WORKSPACETEST_H_
#define WORKSPACETEST_H_
#include "MantidAPI/ISpectrum.h"
Roman Tolchenov
committed
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/SpectrumDetectorMapping.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidGeometry/Instrument/ComponentHelper.h"
Russell Taylor
committed
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidKernel/make_cow.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/VMD.h"
#include "MantidTestHelpers/FakeGmockObjects.h"
#include "MantidTestHelpers/FakeObjects.h"
#include "MantidTestHelpers/InstrumentCreationHelper.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/NexusTestHelper.h"
#include <cxxtest/TestSuite.h>
#include <boost/make_shared.hpp>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;
Janik Zikovsky
committed
// Declare into the factory.
Janik Zikovsky
committed
/** Create a workspace with numSpectra, with
* each spectrum having one detector, at id = workspace index.
* @param numSpectra
* @return
*/
boost::shared_ptr<MatrixWorkspace> makeWorkspaceWithDetectors(size_t numSpectra,
size_t numBins) {
boost::shared_ptr<MatrixWorkspace> ws2 =
boost::make_shared<WorkspaceTester>();
ws2->initialize(numSpectra, numBins, numBins);
Gigg, Martyn Anthony
committed
auto inst = boost::make_shared<Instrument>("TestInstrument");
Russell Taylor
committed
ws2->setInstrument(inst);
// We get a 1:1 map by default so the detector ID should match the spectrum
// number
for (size_t i = 0; i < ws2->getNumberHistograms(); ++i) {
Janik Zikovsky
committed
// Create a detector for each spectra
Detector *det = new Detector("pixel", static_cast<detid_t>(i), inst.get());
Janik Zikovsky
committed
inst->add(det);
inst->markAsDetector(det);
ws2->getSpectrum(i).addDetectorID(static_cast<detid_t>(i));
Gigg, Martyn Anthony
committed
}
Janik Zikovsky
committed
return ws2;
Gigg, Martyn Anthony
committed
}
Janik Zikovsky
committed
class MatrixWorkspaceTest : public CxxTest::TestSuite {
Russell Taylor
committed
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static MatrixWorkspaceTest *createSuite() {
return new MatrixWorkspaceTest();
Russell Taylor
committed
}
static void destroySuite(MatrixWorkspaceTest *suite) { delete suite; }
MatrixWorkspaceTest() : ws(boost::make_shared<WorkspaceTester>()) {
ws->initialize(1, 1, 1);
}
void test_toString_Produces_Expected_Contents() {
auto testWS = boost::make_shared<WorkspaceTester>();
testWS->setTitle("A test run");
testWS->getAxis(0)->setUnit("TOF");
testWS->setYUnitLabel("Counts");
std::string expected = "WorkspaceTester\n"
"Title: A test run\n"
"Histograms: 1\n"
"Bins: 1\n"
"Histogram\n"
"X axis: Time-of-flight / microsecond\n"
"Y axis: Counts\n"
"Distribution: False\n"
"Instrument: None\n"
"Run start: not available\n"
"Run end: not available\n";
TS_ASSERT_EQUALS(expected, testWS->toString());
}
void testGetSetTitle() {
TS_ASSERT_EQUALS(ws->getTitle(), "");
Russell Taylor
committed
ws->setTitle("something");
TS_ASSERT_EQUALS(ws->getTitle(), "something");
Russell Taylor
committed
ws->setTitle("");
void testGetSetComment() {
TS_ASSERT_EQUALS(ws->getComment(), "");
Russell Taylor
committed
ws->setComment("commenting");
TS_ASSERT_EQUALS(ws->getComment(), "commenting");
Russell Taylor
committed
ws->setComment("");
void test_getIndicesFromDetectorIDs() {
WorkspaceTester ws;
ws.initialize(10, 1, 1);
for (size_t i = 0; i < 10; i++)
ws.getSpectrum(i).setDetectorID(detid_t(i * 10));
std::vector<detid_t> dets;
dets.push_back(60);
dets.push_back(20);
dets.push_back(90);
std::vector<size_t> indices = ws.getIndicesFromDetectorIDs(dets);
TS_ASSERT_EQUALS(indices.size(), 3);
TS_ASSERT_EQUALS(indices[0], 6);
TS_ASSERT_EQUALS(indices[1], 2);
TS_ASSERT_EQUALS(indices[2], 9);
}
void
test_That_A_Workspace_Gets_SpectraMap_When_Initialized_With_NVector_Elements() {
WorkspaceTester testWS;
Gigg, Martyn Anthony
committed
const size_t nhist(10);
testWS.initialize(nhist, 1, 1);
for (size_t i = 0; i < testWS.getNumberHistograms(); i++) {
TS_ASSERT_EQUALS(testWS.getSpectrum(i).getSpectrumNo(), specnum_t(i + 1));
TS_ASSERT(testWS.getSpectrum(i).hasDetectorID(detid_t(i)));
Gigg, Martyn Anthony
committed
}
void test_updateSpectraUsing() {
WorkspaceTester testWS;
specnum_t specs[] = {1, 2, 2, 3};
detid_t detids[] = {10, 99, 20, 30};
TS_ASSERT_THROWS_NOTHING(
testWS.updateSpectraUsing(SpectrumDetectorMapping(specs, detids, 4)));
TS_ASSERT(testWS.getSpectrum(0).hasDetectorID(10));
TS_ASSERT(testWS.getSpectrum(1).hasDetectorID(20));
TS_ASSERT(testWS.getSpectrum(1).hasDetectorID(99));
TS_ASSERT(testWS.getSpectrum(2).hasDetectorID(30));
}
void testDetectorMappingCopiedWhenAWorkspaceIsCopied() {
boost::shared_ptr<MatrixWorkspace> parent =
boost::make_shared<WorkspaceTester>();
parent->getSpectrum(0).setSpectrumNo(99);
parent->getSpectrum(0).setDetectorID(999);
MatrixWorkspace_sptr copied = WorkspaceFactory::Instance().create(parent);
// Has it been copied?
TS_ASSERT_EQUALS(copied->getSpectrum(0).getSpectrumNo(), 99);
TS_ASSERT(copied->getSpectrum(0).hasDetectorID(999));
Russell Taylor
committed
}
Russell Taylor
committed
void testGetMemorySize() { TS_ASSERT_THROWS_NOTHING(ws->getMemorySize()); }
void testHistory() { TS_ASSERT_THROWS_NOTHING(ws->history()); }
void testAxes() { TS_ASSERT_EQUALS(ws->axes(), 2); }
Russell Taylor
committed
void testGetAxis() {
TS_ASSERT_THROWS(ws->getAxis(-1), Exception::IndexError);
TS_ASSERT_THROWS_NOTHING(ws->getAxis(0));
TS_ASSERT(ws->getAxis(0));
TS_ASSERT(ws->getAxis(0)->isNumeric());
TS_ASSERT_THROWS(ws->getAxis(2), Exception::IndexError);
Russell Taylor
committed
void testReplaceAxis() {
Axis *ax = new SpectraAxis(ws.get());
TS_ASSERT_THROWS(ws->replaceAxis(2, ax), Exception::IndexError);
TS_ASSERT_THROWS_NOTHING(ws->replaceAxis(0, ax));
TS_ASSERT(ws->getAxis(0)->isSpectra());
Russell Taylor
committed
}
void testIsDistribution() {
TS_ASSERT(!ws->isDistribution());
ws->setDistribution(true);
TS_ASSERT(ws->isDistribution());
void testGetSetYUnit() {
TS_ASSERT_EQUALS(ws->YUnit(), "");
TS_ASSERT_THROWS_NOTHING(ws->setYUnit("something"));
TS_ASSERT_EQUALS(ws->YUnit(), "something");
Loading
Loading full blame...