Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef WEIGHTEDMEANOFWORKSPACETEST_H_
#define WEIGHTEDMEANOFWORKSPACETEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/WeightedMeanOfWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
using namespace Mantid::API;
using namespace Mantid::Algorithms;
using namespace Mantid::DataObjects;
class WeightedMeanOfWorkspaceTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static WeightedMeanOfWorkspaceTest *createSuite() { return new WeightedMeanOfWorkspaceTest(); }
static void destroySuite( WeightedMeanOfWorkspaceTest *suite ) { delete suite; }
void testInit()
{
WeightedMeanOfWorkspace alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void testExec()
{
// Get input workspace
MatrixWorkspace_sptr inputWS = createWorkspace();
// Name of the output workspace.
std::string outWSName("WeightedMeanOfWorkspaceTest_OutputWS");
WeightedMeanOfWorkspace alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", inputWS) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Retrieve the workspace from data service.
MatrixWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outWSName) );
TS_ASSERT(ws);
if (!ws) return;
TS_ASSERT_EQUALS( ws->getNumberHistograms(), 1 );
TS_ASSERT_EQUALS( ws->readY(0)[0], 2.0 );
TS_ASSERT_EQUALS( ws->readE(0)[0], 1.0 );
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}
void testBadValues()
{
// Get input workspace
MatrixWorkspace_sptr inputWS = createWorkspace(false);
// Put bad values into workspace
inputWS->dataY(1)[0] = 0.0/0.0;
inputWS->dataE(1)[1] = 0.0/0.0;
inputWS->dataY(1)[2] = 1.0/0.0;
// Name of the output workspace.
std::string outWSName("WeightedMeanOfWorkspaceTest_OutputWS");
WeightedMeanOfWorkspace alg;
alg.initialize();
alg.isInitialized();
alg.setProperty("InputWorkspace", inputWS);
alg.setPropertyValue("OutputWorkspace", outWSName);
alg.execute();
alg.isExecuted();
// Retrieve the workspace from data service.
MatrixWorkspace_sptr ws;
ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outWSName);
if (!ws) return;
TS_ASSERT_EQUALS( ws->getNumberHistograms(), 1 );
TS_ASSERT_EQUALS( ws->readY(0)[0], 2.0 );
TS_ASSERT_EQUALS( ws->readE(0)[0], 1.0 );
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}
void testEventWs()
{
// Get input workspace
EventWorkspace_sptr inputWS = createEventWorkspace();
// Name of the output workspace.
std::string outWSName("WeightedMeanOfWorkspaceTest_OutputWS");
WeightedMeanOfWorkspace alg;
alg.initialize();
alg.isInitialized();
alg.setProperty("InputWorkspace", inputWS);
alg.setPropertyValue("OutputWorkspace", outWSName);
alg.execute();
TS_ASSERT( !alg.isExecuted() );
}
private:
MatrixWorkspace_sptr createWorkspace(bool doMasked = true)
{
std::set<int64_t> masked;
if (doMasked)
{
masked.insert(0);
}
return WorkspaceCreationHelper::Create2DWorkspace123(4, 3, true, masked);
}
EventWorkspace_sptr createEventWorkspace()
{
return WorkspaceCreationHelper::CreateEventWorkspace();
}
};
#endif /* WEIGHTEDMEANOFWORKSPACETEST_H_ */