Skip to content
Snippets Groups Projects
BinaryOperationTest.h 10 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"

using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::Algorithms;
using namespace Mantid::DataObjects;

Nick Draper's avatar
Nick Draper committed
 class BinaryOpHelper : public BinaryOperation
    {
    public:
      /// Default constructor
      BinaryOpHelper() : BinaryOperation() {};
      /// Destructor
      virtual ~BinaryOpHelper() {};
      const bool checkSizeCompatibility(const MatrixWorkspace_sptr ws1,const MatrixWorkspace_sptr ws2) const
Nick Draper's avatar
Nick Draper committed
      {
        return BinaryOperation::checkSizeCompatibility(ws1,ws2);
Nick Draper's avatar
Nick Draper committed
      }
      const bool checkXarrayCompatibility(const MatrixWorkspace_sptr ws1, const MatrixWorkspace_sptr ws2) const
Nick Draper's avatar
Nick Draper committed
      {
        return BinaryOperation::checkXarrayCompatibility(ws1,ws2);
Nick Draper's avatar
Nick Draper committed
      }
      const int getRelativeLoopCount(const MatrixWorkspace_sptr ws1, const MatrixWorkspace_sptr ws2) const
Nick Draper's avatar
Nick Draper committed
      {
        return BinaryOperation::getRelativeLoopCount(ws1,ws2);
      }
      MatrixWorkspace_sptr createOutputWorkspace(const MatrixWorkspace_sptr ws1, const MatrixWorkspace_sptr ws2) const
Nick Draper's avatar
Nick Draper committed
      {
        return BinaryOperation::createOutputWorkspace(ws1,ws2);
      }



    private:
      // Overridden BinaryOperation methods
      void performBinaryOperation(MatrixWorkspace::const_iterator it_in1, MatrixWorkspace::const_iterator it_in2,
        MatrixWorkspace::iterator it_out)
Nick Draper's avatar
Nick Draper committed
      {}
      /// Static reference to the logger class
      static Mantid::Kernel::Logger& g_log;
    };

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);
Nick Draper's avatar
Nick Draper committed
    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::Create2DWorkspace(10,10);
    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
    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 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
    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 testcreateOutputWorkspace1D1D()
  {
    // Register the workspace in the data service
Nick Draper's avatar
Nick Draper committed
    Workspace1D_sptr work_in1 = WorkspaceCreationHelper::Create1DWorkspaceFib(10);
    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
    BinaryOpHelper helper;
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in2),work_in1,work_in2);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in3),work_in1,work_in3);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in4),work_in1,work_in4);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in5),work_in1,work_in5);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in6),work_in1,work_in6);
  }
Nick Draper's avatar
Nick Draper committed
  void testcreateOutputWorkspace2D1D()
  {
    // Register the workspace in the data service
Nick Draper's avatar
Nick Draper committed
    Workspace2D_sptr work_in1 = WorkspaceCreationHelper::Create2DWorkspace(5,2);
    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
    BinaryOpHelper helper;
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in2),work_in1,work_in2);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in3),work_in1,work_in3);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in4),work_in1,work_in4);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in5),work_in1,work_in5);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in6),work_in1,work_in6);
  }

Nick Draper's avatar
Nick Draper committed
  void testcreateOutputWorkspace2D2D()
  {
    // 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
    BinaryOpHelper helper;
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in2),work_in1,work_in2);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in3),work_in1,work_in3);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in4),work_in1,work_in4);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in5),work_in1,work_in5);
    checkOutputWorkspace(helper.createOutputWorkspace(work_in1,work_in6),work_in1,work_in6);
  }

Nick Draper's avatar
Nick Draper committed
  void testgetRelativeLoopCount()
  {
    // Register the workspace in the data service
    Workspace1D_sptr work_in1 = WorkspaceCreationHelper::Create1DWorkspaceFib(10);
    Workspace1D_sptr work_in2 = WorkspaceCreationHelper::Create1DWorkspaceFib(20);
    Workspace1D_sptr work_in3 = WorkspaceCreationHelper::Create1DWorkspaceFib(1);
    Workspace2D_sptr work_in4 = WorkspaceCreationHelper::Create2DWorkspace(4,5);
    Workspace2D_sptr work_in5 = WorkspaceCreationHelper::Create2DWorkspace(3,3);
    Workspace2D_sptr work_in6 = WorkspaceCreationHelper::Create2DWorkspace(1,100);
    BinaryOpHelper helper;
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in1,work_in2),2);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in2,work_in1),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in2,work_in2),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in1,work_in3),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in3,work_in1),10);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in2,work_in4),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in4,work_in2),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in5,work_in3),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in3,work_in5),9);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in6,work_in1),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in1,work_in6),10);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in6,work_in4),1);
    TS_ASSERT_EQUALS(helper.getRelativeLoopCount(work_in4,work_in6),5);
  }

Nick Draper's avatar
Nick Draper committed

  void checkOutputWorkspace(MatrixWorkspace_sptr ws, MatrixWorkspace_sptr wsIn1,MatrixWorkspace_sptr wsIn2 ) const
Nick Draper's avatar
Nick Draper committed
  {
    int targetsize = (wsIn1->size()>wsIn2->size())?wsIn1->size():wsIn2->size();
    TS_ASSERT_EQUALS(ws->size(),targetsize);
    //check they arre all 0
    for(MatrixWorkspace::iterator ti(*ws); ti != ti.end(); ++ti)
Nick Draper's avatar
Nick Draper committed
    {
      TS_ASSERT_THROWS_NOTHING
      (
Nick Draper's avatar
Nick Draper committed
        LocatedDataRef tr = *ti;
Nick Draper's avatar
Nick Draper committed
        TS_ASSERT_DELTA(tr.X(),0,0.0001);
        TS_ASSERT_DELTA(tr.Y(),0,0.0001);
        TS_ASSERT_DELTA(tr.E(),0,0.0001);
Nick Draper's avatar
Nick Draper committed
      )
    }
  }
Nick Draper's avatar
Nick Draper committed
};

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