ConvertToMatrixWorkspaceTest.h 4.17 KB
Newer Older
1
2
3
4
5
6
#ifndef CONVERTTOMATRIXWORKSPACETEST_H_
#define CONVERTTOMATRIXWORKSPACETEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAlgorithms/CheckWorkspacesMatch.h"
7
8
9
#include "MantidAlgorithms/ConvertToMatrixWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataObjects/EventWorkspace.h"
10
#include "MantidGeometry/Instrument.h"
11
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
12
13
14

using namespace Mantid;
using namespace Mantid::Kernel;
15

16
class ConvertToMatrixWorkspaceTest : public CxxTest::TestSuite {
17
public:
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
  void testName() {
    Mantid::Algorithms::ConvertToMatrixWorkspace cloner;
    TS_ASSERT_EQUALS(cloner.name(), "ConvertToMatrixWorkspace")
  }

  void testVersion() {
    Mantid::Algorithms::ConvertToMatrixWorkspace cloner;
    TS_ASSERT_EQUALS(cloner.version(), 1)
  }

  void testInit() {
    Mantid::Algorithms::ConvertToMatrixWorkspace cloner;
    TS_ASSERT_THROWS_NOTHING(cloner.initialize())
    TS_ASSERT(cloner.isInitialized())
  }

  void testExec_2D_to_2D() {
    Mantid::Algorithms::ConvertToMatrixWorkspace cloner;
    cloner.setChild(true);
    cloner.initialize();
    // create 2D input workspace
    Mantid::API::MatrixWorkspace_sptr in =
        WorkspaceCreationHelper::Create2DWorkspace(5, 10);
    // add instance to variable 'in'

    Mantid::API::MatrixWorkspace_sptr out;

    TS_ASSERT_THROWS_NOTHING(cloner.setProperty("InputWorkspace", in))
    TS_ASSERT_THROWS_NOTHING(cloner.setProperty("OutputWorkspace", "out"))
    TS_ASSERT(cloner.execute())

    // retrieve OutputWorkspace produced by execute and set it to out
    TS_ASSERT_THROWS_NOTHING(out = cloner.getProperty("OutputWorkspace"));
    TS_ASSERT(out);
    if (!out)
      return;

    // Best way to test this is to use the CheckWorkspacesMatch algorithm
    Mantid::Algorithms::CheckWorkspacesMatch checker;
    checker.initialize();
    checker.setProperty("Workspace1", in);
    checker.setProperty("Workspace2", out);
    checker.execute();

    TS_ASSERT_EQUALS(checker.getPropertyValue("Result"),
                     checker.successString());
  }

  void testExec_Event_to_2D() {
    Mantid::Algorithms::ConvertToMatrixWorkspace cloner;
    cloner.setChild(true);
    cloner.initialize();
    Mantid::DataObjects::EventWorkspace_sptr in =
        WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 10);

    TS_ASSERT_THROWS_NOTHING(cloner.setProperty("InputWorkspace", in))
    TS_ASSERT_THROWS_NOTHING(cloner.setPropertyValue("OutputWorkspace", "out"))
    TS_ASSERT(cloner.execute())

    Mantid::API::MatrixWorkspace_sptr out;
    TS_ASSERT_THROWS_NOTHING(out = cloner.getProperty("OutputWorkspace"));
    TS_ASSERT(out);
    if (!out)
      return;

    TS_ASSERT_EQUALS(in->getNumberHistograms(), out->getNumberHistograms());
    TS_ASSERT_EQUALS(in->getInstrument()->getName(),
                     out->getInstrument()->getName());
    TS_ASSERT_EQUALS(in->getInstrument()->isParametrized(),
                     out->getInstrument()->isParametrized());
    for (size_t i = 0; i < out->getNumberHistograms(); i++) {
      const Mantid::API::ISpectrum *inSpec = in->getSpectrum(i);
      const Mantid::API::ISpectrum *outSpec = out->getSpectrum(i);
91
      TSM_ASSERT_EQUALS("Failed on comparing Spectrum Number for Histogram: " +
92
                            boost::lexical_cast<std::string>(i),
93
94
                        inSpec->getSpectrumNo(), outSpec->getSpectrumNo());
      TSM_ASSERT_EQUALS("Failed on comparing Detector ID for Histogram: " +
95
                            boost::lexical_cast<std::string>(i),
96
97
                        *inSpec->getDetectorIDs().begin(),
                        *outSpec->getDetectorIDs().begin());
98
99
      TSM_ASSERT_EQUALS("Failed on readX for Histogram: " +
                            boost::lexical_cast<std::string>(i),
100
                        in->readX(i), out->readX(i));
101
102
      TSM_ASSERT_EQUALS("Failed on readY for Histogram: " +
                            boost::lexical_cast<std::string>(i),
103
                        in->readY(i), out->readY(i));
104
105
      TSM_ASSERT_EQUALS("Failed on readE for Histogram: " +
                            boost::lexical_cast<std::string>(i),
106
                        in->readE(i), out->readE(i));
107
108
    }
  }
109
110
111
};

#endif /*CONVERTTOMATRIXWORKSPACETEST_H_*/