-
Notifications
You must be signed in to change notification settings - Fork 122
/
CalculateChiSquaredTest.h
114 lines (91 loc) · 3.09 KB
/
CalculateChiSquaredTest.h
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
#ifndef MANTID_CURVEFITTING_CALCULATECHISQUAREDTEST_H_
#define MANTID_CURVEFITTING_CALCULATECHISQUAREDTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidCurveFitting/CalculateChiSquared.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidKernel/EmptyValues.h"
using Mantid::CurveFitting::CalculateChiSquared;
using namespace Mantid;
using namespace Mantid::API;
class CalculateChiSquaredTest : 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 CalculateChiSquaredTest *createSuite() { return new CalculateChiSquaredTest(); }
static void destroySuite( CalculateChiSquaredTest *suite ) { delete suite; }
void test_Init()
{
CalculateChiSquared alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()
{
Tester tester;
tester.set1DFunction();
tester.set1DSpectrumEmpty();
tester.runAlgorithm();
}
private:
class Tester{
// input parameters
size_t nParams;
size_t nData;
bool isHisto;
double xMin;
double xMax;
std::vector<double> xBins;
std::vector<double> xValues;
// values for algorithm input properties
IFunction_sptr function;
Workspace_sptr workspace;
int workspaceIndex;
double StartX;
double EndX;
// algorithm output
double chiSquared;
void makeXValues(){
size_t dlt = isHisto ? 1 : 0;
xBins.resize(nData + dlt);
double dx = (xMax - xMin) / double(xBins.size() - 1);
for(size_t i = 0; i < xBins.size(); ++i)
{
xBins[i] = xMin + i * dx;
}
if (isHisto){
xValues.resize(nData);
std::transform(xBins.begin(),xBins.end()-1, xValues.begin(), std::bind2nd(std::plus<double>(),dx/2));
} else {
xValues = xBins;
}
}
public:
Tester(size_t np = 3, size_t nd = 10, bool histo = true)
: nParams(np), nData(nd), isHisto(histo), workspaceIndex(0), xMin(-10),
xMax(10), StartX(EMPTY_DBL()), EndX(EMPTY_DBL()) {}
void runAlgorithm(){
CalculateChiSquared alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setProperty("Function", function) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", workspace) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
chiSquared = alg.getProperty("ChiSquared");
}
void set1DFunction(){
const std::string fun = "name=UserFunction,Formula=a+b*x+c*x^2,a=1,b=1,c=1";
function = FunctionFactory::Instance().createInitialized(fun);
TS_ASSERT_EQUALS(function->nParams(), nParams);
}
void set1DSpectrumEmpty(){
makeXValues();
const size_t nSpec = 1;
auto space = WorkspaceFactory::Instance().create("Workspace2D", nSpec, nData+1, nData);
space->dataX(0).assign(xBins.begin(),xBins.end());
workspace = space;
}
};
};
#endif /* MANTID_CURVEFITTING_CALCULATECHISQUAREDTEST_H_ */