Skip to content
Snippets Groups Projects
MatrixWorkspaceTest.h 6.16 KiB
Newer Older
#ifndef WORKSPACETEST_H_
#define WORKSPACETEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAPI/SpectraDetectorMap.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/SpectraAxis.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;

class WorkspaceTester : public MatrixWorkspace
  virtual ~WorkspaceTester() {}
  // Empty overrides of virtual methods
  virtual const int getNumberHistograms() const { return 1;}
  const std::string id() const {return "WorkspaceTester";}
  void init(const int& i, const int& j, const int& k)
    vec.resize(j,1.0);
    // Put an 'empty' axis in to test the getAxis method
    m_axes.resize(1);
    m_axes[0] = new NumericAxis(1);
  int size() const {return vec.size();}
  int blocksize() const {return vec.size();}
  MantidVec& dataX(int const index) {return vec;}
  MantidVec& dataY(int const index) {return vec;}
  MantidVec& dataE(int const index) {return vec;}
  const MantidVec& dataX(int const index) const {return vec;}
  const MantidVec& dataY(int const index) const {return vec;}
  const MantidVec& dataE(int const index) const {return vec;}
  Kernel::cow_ptr<MantidVec> refX(const int index) const {return Kernel::cow_ptr<MantidVec>();}
  void setX(const int index, const Kernel::cow_ptr<MantidVec>& X) {}
DECLARE_WORKSPACE(WorkspaceTester)

class MatrixWorkspaceTest : public CxxTest::TestSuite
  MatrixWorkspaceTest() : ws(new Mantid::DataObjects::WorkspaceTester)
  {
    ws->initialize(1,1,1);
  }
  
  void testGetSetTitle()
  {
    TS_ASSERT_EQUALS( ws->getTitle(), "" );
    TS_ASSERT_EQUALS( ws->getTitle(), "something" );
  void testGetSetComment()
  {
    TS_ASSERT_EQUALS( ws->getComment(), "" );
    TS_ASSERT_EQUALS( ws->getComment(), "commenting" );
  void testGetInstrument()
  {
    boost::shared_ptr<IInstrument> i = ws->getInstrument();
    TS_ASSERT_EQUALS( ws->getInstrument()->type(), "Instrument" );
    MatrixWorkspace_sptr ws2 = WorkspaceFactory::Instance().create(ws,1,1,1);
    const SpectraDetectorMap &specs = ws2->spectraMap();
    TS_ASSERT_EQUALS( &(ws->spectraMap()), &specs );
    SpectraDetectorMap &specs2 = ws2->mutableSpectraMap();
    TS_ASSERT_DIFFERS( &(ws->spectraMap()), &specs2 );
    TS_ASSERT_EQUALS( ws->sample().getName(), "test" );
  void testGetMemorySize()
  {
    TS_ASSERT_THROWS_NOTHING( ws->getMemorySize() );
    TS_ASSERT_THROWS_NOTHING( WorkspaceHistory& h = ws->history() );
    TS_ASSERT_THROWS_NOTHING( const WorkspaceHistory& hh = wsc.getHistory() );
  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(1), Exception::IndexError );
    Axis* axBad = new SpectraAxis(5);
    TS_ASSERT_THROWS( ws->replaceAxis(0,axBad), std::runtime_error );
    Axis* ax = new SpectraAxis(1);
    TS_ASSERT_THROWS( ws->replaceAxis(1,ax), Exception::IndexError );
    TS_ASSERT_THROWS_NOTHING( ws->replaceAxis(0,ax) );
    TS_ASSERT( ws->getAxis(0)->isSpectra() );
  void testIsDistribution()
  {
    TS_ASSERT( ! ws->isDistribution() );
    TS_ASSERT( ws->isDistribution(true) );
    TS_ASSERT( ws->isDistribution() );
    TS_ASSERT_THROWS_NOTHING( ws->setYUnit("something") )
    TS_ASSERT_EQUALS( ws->YUnit(), "something" )
  void testMasking()
  {
    MatrixWorkspace *ws2 = new Mantid::DataObjects::WorkspaceTester;
    ws2->initialize(1,2,2);
    // Doesn't throw on invalid spectrum index, just returns false
    TS_ASSERT( !ws2->hasMaskedBins(1) );
    TS_ASSERT( !ws2->hasMaskedBins(-1) );
    
    // Will throw if nothing masked for spectrum
    TS_ASSERT_THROWS( ws2->maskedBins(0), Mantid::Kernel::Exception::IndexError );
    // Will throw if attempting to mask invalid spectrum
    TS_ASSERT_THROWS( ws2->maskBin(-1,1), Mantid::Kernel::Exception::IndexError );
    TS_ASSERT_THROWS( ws2->maskBin(1,1), Mantid::Kernel::Exception::IndexError );
    // ...or an invalid bin
    TS_ASSERT_THROWS( ws2->maskBin(0,-1), Mantid::Kernel::Exception::IndexError );
    TS_ASSERT_THROWS( ws2->maskBin(0,2), Mantid::Kernel::Exception::IndexError );
    
    // Now do a valid masking
    TS_ASSERT_THROWS_NOTHING( ws2->maskBin(0,1,0.5) );
    TS_ASSERT( ws2->hasMaskedBins(0) );
    TS_ASSERT_EQUALS( ws2->maskedBins(0).size(), 1 );
    TS_ASSERT_EQUALS( ws2->maskedBins(0).begin()->first, 1 );
    TS_ASSERT_EQUALS( ws2->maskedBins(0).begin()->second, 0.5 );
    // This will be 0.25 (1*0.5*0.5) because in the test class the same vector is used for both E & Y
    TS_ASSERT_EQUALS( ws2->dataY(0)[1], 0.25 );
    
    // Now mask a bin earlier than above and check it's sorting properly
    TS_ASSERT_THROWS_NOTHING( ws2->maskBin(0,0) );
    TS_ASSERT( ws2->hasMaskedBins(0) );
    TS_ASSERT_EQUALS( ws2->maskedBins(0).size(), 2 );
    TS_ASSERT_EQUALS( ws2->maskedBins(0).begin()->first, 0 );
    TS_ASSERT_EQUALS( ws2->maskedBins(0).begin()->second, 1.0 );
    // This will be 0.25 (1*0.5*0.5) because in the test class the same vector is used for both E & Y
    TS_ASSERT_EQUALS( ws2->dataY(0)[0], 0.0 );
    // Check the previous masking is still OK
    TS_ASSERT_EQUALS( ws2->maskedBins(0).rbegin()->first, 1 );
    TS_ASSERT_EQUALS( ws2->maskedBins(0).rbegin()->second, 0.5 );
    TS_ASSERT_EQUALS( ws2->dataY(0)[1], 0.25 );
};

#endif /*WORKSPACETEST_H_*/