Skip to content
Snippets Groups Projects
BinaryOperationTest.h 6.62 KiB
Newer Older
Nick Draper's avatar
Nick Draper committed
#ifndef BINARYOPERATIONTEST_H_
#define BINARYOPERATIONTEST_H_
Nick Draper's avatar
Nick Draper committed

#include <cxxtest/TestSuite.h>
#include <cmath>

Nick Draper's avatar
Nick Draper committed
#include "WorkspaceCreationHelper.hh"
Nick Draper's avatar
Nick Draper committed
#include "MantidAlgorithms/BinaryOperation.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceIterator.h"
Nick Draper's avatar
Nick Draper committed
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/Workspace1D.h"

Nick Draper's avatar
Nick Draper committed
using namespace Mantid::API;

class BinaryOpHelper : public Mantid::Algorithms::BinaryOperation
{
public:
  /// Default constructor
  BinaryOpHelper() : BinaryOperation() {};
  /// Destructor
  virtual ~BinaryOpHelper() {};

  /// function to return a name of the algorithm, must be overridden in all algorithms
  virtual const std::string name() const { return "BinaryOpHelper"; }
  /// function to return a version of the algorithm, must be overridden in all algorithms
  virtual int version() const { return 1; }
  /// function to return a category of the algorithm. A default implementation is provided
  virtual const std::string category() const {return "Helper";}
  
  const bool checkSizeCompatibility(const MatrixWorkspace_sptr ws1,const MatrixWorkspace_sptr ws2) const
  {
    return BinaryOperation::checkSizeCompatibility(ws1,ws2);
  }
  
private:
  // Overridden BinaryOperation methods
  void performBinaryOperation(const MantidVec& lhsX, const MantidVec& lhsY, const MantidVec& lhsE,
                              const MantidVec& rhsY, const MantidVec& rhsE, MantidVec& YOut, MantidVec& EOut)
  {}
  void performBinaryOperation(const MantidVec& lhsX, const MantidVec& lhsY, const MantidVec& lhsE,
                              const double& rhsY, const double& rhsE, MantidVec& YOut, MantidVec& EOut)
  {}
};
Nick Draper's avatar
Nick Draper committed

class BinaryOperationTest : public CxxTest::TestSuite
Nick Draper's avatar
Nick Draper committed
{
public:

  void testcheckSizeCompatibility1D1D()
Nick Draper's avatar
Nick Draper committed
  {
    int sizex = 10;
    // Register the workspace in the data service
Nick Draper's avatar
Nick Draper committed
    Workspace1D_sptr work_in1 = WorkspaceCreationHelper::Create1DWorkspaceFib(sizex);
    Workspace1D_sptr work_in2 = WorkspaceCreationHelper::Create1DWorkspaceFib(20);
    Workspace1D_sptr work_in3 = WorkspaceCreationHelper::Create1DWorkspaceFib(10);
    Workspace1D_sptr work_in4 = WorkspaceCreationHelper::Create1DWorkspaceFib(5);
    Workspace1D_sptr work_in5 = WorkspaceCreationHelper::Create1DWorkspaceFib(3);
    Workspace1D_sptr work_in6 = WorkspaceCreationHelper::Create1DWorkspaceFib(1);
    BinaryOpHelper helper;
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in2));
    TS_ASSERT(helper.checkSizeCompatibility(work_in1,work_in3));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in4));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in5));
    TS_ASSERT(helper.checkSizeCompatibility(work_in1,work_in6));
Nick Draper's avatar
Nick Draper committed
  }

  void testcheckSizeCompatibility2D1D()
Nick Draper's avatar
Nick Draper committed
  {
    // Register the workspace in the data service
Nick Draper's avatar
Nick Draper committed
    Workspace2D_sptr work_in1 = WorkspaceCreationHelper::Create2DWorkspace123(10,10);
Nick Draper's avatar
Nick Draper committed
    Workspace1D_sptr work_in2 = WorkspaceCreationHelper::Create1DWorkspaceFib(20);
    Workspace1D_sptr work_in3 = WorkspaceCreationHelper::Create1DWorkspaceFib(10);
    Workspace1D_sptr work_in4 = WorkspaceCreationHelper::Create1DWorkspaceFib(5);
    Workspace1D_sptr work_in5 = WorkspaceCreationHelper::Create1DWorkspaceFib(3);
    Workspace1D_sptr work_in6 = WorkspaceCreationHelper::Create1DWorkspaceFib(1);
Nick Draper's avatar
Nick Draper committed
    MatrixWorkspace_sptr work_inEvent1 = WorkspaceCreationHelper::CreateEventWorkspace(10,1);
    //will not pass x array does not match
Nick Draper's avatar
Nick Draper committed
    MatrixWorkspace_sptr work_inEvent2 = WorkspaceCreationHelper::CreateEventWorkspace(1,10);
    BinaryOpHelper helper;
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in2));
    TS_ASSERT(helper.checkSizeCompatibility(work_in1,work_in3));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in4));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in5));
    TS_ASSERT(helper.checkSizeCompatibility(work_in1,work_in6));
Nick Draper's avatar
Nick Draper committed
    TS_ASSERT(helper.checkSizeCompatibility(work_in1,work_inEvent1));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_inEvent2));
Nick Draper's avatar
Nick Draper committed
  }

  void testcheckSizeCompatibility2D2D()
Nick Draper's avatar
Nick Draper committed
  {
    // Register the workspace in the data service
Nick Draper's avatar
Nick Draper committed
    Workspace2D_sptr work_in1 = WorkspaceCreationHelper::Create2DWorkspace(10,10);
    Workspace2D_sptr work_in2 = WorkspaceCreationHelper::Create2DWorkspace(20,10);
    Workspace2D_sptr work_in3 = WorkspaceCreationHelper::Create2DWorkspace(10,10);
    Workspace2D_sptr work_in4 = WorkspaceCreationHelper::Create2DWorkspace(5,5);
    Workspace2D_sptr work_in5 = WorkspaceCreationHelper::Create2DWorkspace(3,3);
    Workspace2D_sptr work_in6 = WorkspaceCreationHelper::Create2DWorkspace(1,100);
Nick Draper's avatar
Nick Draper committed
    MatrixWorkspace_sptr work_inEvent1 = WorkspaceCreationHelper::CreateEventWorkspace(5,5);
    MatrixWorkspace_sptr work_inEvent2 = WorkspaceCreationHelper::CreateEventWorkspace(10,10);
    BinaryOpHelper helper;
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in2));
    TS_ASSERT(helper.checkSizeCompatibility(work_in1,work_in3));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in4));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in5));
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_in6));
Nick Draper's avatar
Nick Draper committed
    TS_ASSERT(!helper.checkSizeCompatibility(work_in1,work_inEvent1));
    TS_ASSERT(helper.checkSizeCompatibility(work_in1,work_inEvent2));
Nick Draper's avatar
Nick Draper committed
  }

  void testMaskedSpectraPropagation()
  {
    const int sizex = 10,sizey=20;
    std::set<int> masking;
    masking.insert(0);
    masking.insert(2);
    masking.insert(4);
    
    MatrixWorkspace_sptr work_in1 = WorkspaceCreationHelper::Create2DWorkspace123(sizex,sizey, 0, masking);
    MatrixWorkspace_sptr work_in2 = WorkspaceCreationHelper::Create2DWorkspace154(sizex,sizey);

    BinaryOpHelper helper;
    helper.initialize();
    helper.setProperty("LHSWorkspace", work_in1);
    helper.setProperty("RHSWorkspace", work_in2);
    const std::string outputSpace("test");
    helper.setPropertyValue("OutputWorkspace", outputSpace);
    helper.setRethrows(true);
    helper.execute();

    
    TS_ASSERT(helper.isExecuted());

    MatrixWorkspace_sptr output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace));
    TS_ASSERT(output);
    
    for( int i = 0; i < sizey; ++i )
    {
      IDetector_sptr det;
      try
      {
	det = output->getDetector(i);
      }
      catch(Kernel::Exception::NotFoundError&)
      {
      }
      
      TS_ASSERT(det);
      if( !det ) TS_FAIL("No detector found");
      if( masking.count(i) == 0 )
      {
	TS_ASSERT_EQUALS(det->isMasked(), false);
      }
      else
      {
	TS_ASSERT_EQUALS(det->isMasked(), true);
      }
    }
    
  }

Nick Draper's avatar
Nick Draper committed
};

Nick Draper's avatar
Nick Draper committed
#endif /*BINARYOPERATIONTEST_H_*/