SetInstrumentParameterTest.h 6.68 KB
Newer Older
1
2
3
4
5
6
7
#ifndef MANTID_ALGORITHMS_SETINSTRUMENTPARAMETERTEST_H_
#define MANTID_ALGORITHMS_SETINSTRUMENTPARAMETERTEST_H_

#include <cxxtest/TestSuite.h>

#include <string>
#include "MantidAlgorithms/SetInstrumentParameter.h"
8
#include "MantidGeometry/Instrument.h"
9
10
11
12
13
14
15
16
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

using Mantid::Algorithms::SetInstrumentParameter;

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

17
class SetInstrumentParameterTest : public CxxTest::TestSuite {
18
19
20
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
21
22
23
24
  static SetInstrumentParameterTest *createSuite() {
    return new SetInstrumentParameterTest();
  }
  static void destroySuite(SetInstrumentParameterTest *suite) { delete suite; }
25

26
  void test_Init() {
27
    SetInstrumentParameter alg;
28
29
    TS_ASSERT_THROWS_NOTHING(alg.initialize())
    TS_ASSERT(alg.isInitialized())
30
  }
31
32

  void test_cmpt_string_value() {
33
34
35
36
37
    std::string cmptName = "samplePos";
    std::string detList = "";
    std::string paramName = "TestParam";
    std::string paramValue = "Left";

38
39
    MatrixWorkspace_sptr ws =
        WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 3);
40
41
42
    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue);

    auto cmpt = ws->getInstrument()->getComponentByName(cmptName);
43
    TS_ASSERT_EQUALS(paramValue, cmpt->getStringParameter(paramName)[0]);
44
45
  }

46
  void test_default_cmpt_string_value() {
47
48
49
50
51
    std::string cmptName = "";
    std::string detList = "";
    std::string paramName = "TestParam";
    std::string paramValue = "Left";

52
53
    MatrixWorkspace_sptr ws =
        WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 3);
54
55
    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue);

56
57
    TS_ASSERT_EQUALS(paramValue,
                     ws->getInstrument()->getStringParameter(paramName)[0]);
58
59
  }

60
  void test_detlist_string_value() {
61
62
63
64
65
    std::string cmptName = "a value to ignore";
    std::string detList = "1,2";
    std::string paramName = "TestParam";
    std::string paramValue = "Left";

66
67
    MatrixWorkspace_sptr ws =
        WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 3);
68
69
70
    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue);

    auto cmpt = ws->getInstrument()->getDetector(1);
71
    TS_ASSERT_EQUALS(paramValue, cmpt->getStringParameter(paramName)[0]);
72
    cmpt = ws->getInstrument()->getDetector(2);
73
    TS_ASSERT_EQUALS(paramValue, cmpt->getStringParameter(paramName)[0]);
74
75
  }

76
  void test_cmpt_int_value() {
77
78
79
80
81
82
    std::string cmptName = "samplePos";
    std::string detList = "";
    std::string paramName = "TestParam";
    std::string paramType = "Number";
    std::string paramValue = "1";

83
84
    MatrixWorkspace_sptr ws =
        WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 3);
85
86
87
    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue, paramType);

    auto cmpt = ws->getInstrument()->getComponentByName(cmptName);
88
    TS_ASSERT_EQUALS(1, cmpt->getIntParameter(paramName)[0]);
89
90
  }

91
  void test_cmpt_dbl_value() {
92
93
94
95
96
97
    std::string cmptName = "samplePos";
    std::string detList = "";
    std::string paramName = "TestParam";
    std::string paramType = "Number";
    std::string paramValue = "1.12";

98
99
    MatrixWorkspace_sptr ws =
        WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 3);
100
101
102
    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue, paramType);

    auto cmpt = ws->getInstrument()->getComponentByName(cmptName);
103
    TS_ASSERT_EQUALS(1.12, cmpt->getNumberParameter(paramName)[0]);
104
  }
105
106

  void test_overwrite_dbl_value() {
107
108
109
110
111
112
113
    std::string cmptName = "samplePos";
    std::string detList = "";
    std::string paramName = "TestParam";
    std::string paramType = "Number";
    std::string paramValue = "1.12";
    std::string paramValue2 = "3.22";

114
115
    MatrixWorkspace_sptr ws =
        WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 3);
116
117
118
    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue, paramType);

    auto cmpt = ws->getInstrument()->getComponentByName(cmptName);
119
    TS_ASSERT_EQUALS(1.12, cmpt->getNumberParameter(paramName)[0]);
120
121
122
123

    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue2, paramType);

    cmpt = ws->getInstrument()->getComponentByName(cmptName);
124
    TS_ASSERT_EQUALS(3.22, cmpt->getNumberParameter(paramName)[0]);
125
126
  }

127
  void test_overwrite_diff_type() {
128
129
130
131
132
133
134
135
    std::string cmptName = "samplePos";
    std::string detList = "";
    std::string paramName = "TestParam";
    std::string paramType = "Number";
    std::string paramValue = "1.12";
    std::string paramType2 = "String";
    std::string paramValue2 = "A String";

136
137
    MatrixWorkspace_sptr ws =
        WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(3, 3);
138
139
140
    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue, paramType);

    auto cmpt = ws->getInstrument()->getComponentByName(cmptName);
141
    TS_ASSERT_EQUALS(1.12, cmpt->getNumberParameter(paramName)[0]);
142
143
144
145

    ExecuteAlgorithm(ws, cmptName, detList, paramName, paramValue2, paramType2);

    cmpt = ws->getInstrument()->getComponentByName(cmptName);
146
    TS_ASSERT_EQUALS(paramValue2, cmpt->getStringParameter(paramName)[0]);
147
  }
148

149
150
151
152
153
154
155
156
  MatrixWorkspace_sptr
  ExecuteAlgorithm(MatrixWorkspace_sptr testWS, std::string cmptName,
                   std::string detList, std::string paramName,
                   std::string paramValue, std::string paramType = "",
                   bool fails = false) {
    // add the workspace to the ADS
    AnalysisDataService::Instance().addOrReplace(
        "SetInstrumentParameter_Temporary", testWS);
157

158
    // execute algorithm
159
160
    SetInstrumentParameter alg;
    TS_ASSERT_THROWS_NOTHING(alg.initialize());
161
    TS_ASSERT(alg.isInitialized())
162
163

    alg.setPropertyValue("Workspace", "SetInstrumentParameter_Temporary");
164
    if (cmptName.length() > 0) {
165
166
      alg.setPropertyValue("ComponentName", cmptName);
    }
167
    if (detList.length() > 0) {
168
169
      alg.setPropertyValue("DetectorList", detList);
    }
170
    if (paramType.length() > 0) {
171
172
173
174
175
      alg.setPropertyValue("parameterType", paramType);
    }
    alg.setPropertyValue("ParameterName", paramName);
    alg.setPropertyValue("Value", paramValue);
    TS_ASSERT_THROWS_NOTHING(alg.execute())
176
177
    if (fails) {
      TS_ASSERT(!alg.isExecuted())
178
      return testWS;
179
180
    } else {
      TS_ASSERT(alg.isExecuted())
181
182
    }

183
184
185
186
187
188
    // check output
    MatrixWorkspace_sptr output =
        AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
            alg.getProperty("Workspace"));

    // cleanup
189
190
191
192
193
194
195
    AnalysisDataService::Instance().remove(output->getName());

    return output;
  }
};

#endif /* MANTID_ALGORITHMS_SETINSTRUMENTPARAMETERTEST_H_ */