-
Notifications
You must be signed in to change notification settings - Fork 122
/
NumericAxisTest.h
195 lines (165 loc) · 5.22 KB
/
NumericAxisTest.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef NUMERICAXISTEST_H_
#define NUMERICAXISTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAPI/NumericAxis.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/Exception.h"
#include <cfloat>
#include "MantidTestHelpers/FakeObjects.h"
using namespace Mantid::API;
using namespace Mantid::Kernel;
class NumericAxisTester : public NumericAxis
{
public:
NumericAxisTester() : NumericAxis(1) {}
NumericAxisTester(const NumericAxisTester& right) : NumericAxis(right) {}
};
// Now the unit test class itself
class NumericAxisTest : 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 NumericAxisTest *createSuite() { return new NumericAxisTest(); }
static void destroySuite( NumericAxisTest *suite ) { delete suite; }
NumericAxisTest()
{
numericAxis = new NumericAxis(5);
numericAxis->title() = "A numeric axis";
}
~NumericAxisTest()
{
delete numericAxis;
}
void testConstructor()
{
TS_ASSERT_EQUALS( numericAxis->title(), "A numeric axis" );
TS_ASSERT( numericAxis->unit().get() );
for (int i=0; i<5; ++i)
{
TS_ASSERT_EQUALS( (*numericAxis)(i), 0.0 );
}
}
void testCopyConstructor()
{
NumericAxisTester axistester;
axistester.title() = "tester";
axistester.unit() = UnitFactory::Instance().create("Wavelength");
axistester.setValue(0,5.5);
NumericAxisTester copiedAxis = axistester;
TS_ASSERT_EQUALS( copiedAxis.title(), "tester" );
TS_ASSERT_EQUALS( copiedAxis.unit()->unitID(), "Wavelength" );
TS_ASSERT( copiedAxis.isNumeric() );
TS_ASSERT_EQUALS( copiedAxis(0), 5.5 );
TS_ASSERT_THROWS( copiedAxis(1), Exception::IndexError );
}
void testClone()
{
WorkspaceTester ws; // Fake workspace to pass to clone
Axis* newNumAxis = numericAxis->clone(&ws);
TS_ASSERT_DIFFERS( newNumAxis, numericAxis );
delete newNumAxis;
}
void testCloneDifferentLength()
{
numericAxis->setValue(0,9.9);
WorkspaceTester ws; // Fake workspace to pass to clone
Axis* newNumAxis = numericAxis->clone(1,&ws);
TS_ASSERT_DIFFERS( newNumAxis, numericAxis );
TS_ASSERT( newNumAxis->isNumeric() );
TS_ASSERT_EQUALS( newNumAxis->title(), "A numeric axis" );
TS_ASSERT_EQUALS( newNumAxis->unit()->unitID(), "Empty" );
TS_ASSERT_EQUALS( newNumAxis->length(), 1 );
TS_ASSERT_EQUALS( (*newNumAxis)(0), 0.0 );
delete newNumAxis;
}
void testTitle()
{
numericAxis->title() = "something else";
TS_ASSERT_EQUALS( numericAxis->title(), "something else" );
}
void testUnit()
{
numericAxis->unit() = UnitFactory::Instance().create("Energy");
TS_ASSERT_EQUALS( numericAxis->unit()->unitID(), "Energy" );
}
void testIsSpectra()
{
TS_ASSERT( ! numericAxis->isSpectra() );
}
void testIsNumeric()
{
TS_ASSERT( numericAxis->isNumeric() );
}
void testIsText()
{
TS_ASSERT( !numericAxis->isText() );
}
void testOperatorBrackets()
{
TS_ASSERT_THROWS( (*numericAxis)(-1), Exception::IndexError );
TS_ASSERT_THROWS( (*numericAxis)(5), Exception::IndexError );
}
void testSetValue()
{
TS_ASSERT_THROWS( numericAxis->setValue(-1, 1.1), Exception::IndexError );
TS_ASSERT_THROWS( numericAxis->setValue(5, 1.1), Exception::IndexError );
for (int i=0; i<5; ++i)
{
TS_ASSERT_THROWS_NOTHING( numericAxis->setValue(i, i+0.5) );
TS_ASSERT_EQUALS( (*numericAxis)(i), i+0.5 );
}
}
void testSpectraNo()
{
TS_ASSERT_THROWS( numericAxis->spectraNo(-1), std::domain_error );
TS_ASSERT_THROWS( numericAxis->spectraNo(5), std::domain_error );
}
void testConversionToBins()
{
const size_t npoints(5);
NumericAxis axis(npoints);
for(size_t i = 0; i < npoints; ++i)
{
axis.setValue(i, static_cast<double>(i));
}
std::vector<double> boundaries = axis.createBinBoundaries();
const size_t nvalues(boundaries.size());
TS_ASSERT_EQUALS(nvalues, npoints + 1);
// Easier to read the expected values with static sized array so
// live with the hard-coded size
double expectedValues[6] = {-0.5, 0.5, 1.5, 2.5, 3.5, 4.5};
for( size_t i = 0; i < nvalues; ++i )
{
TS_ASSERT_DELTA(boundaries[i],expectedValues[i], DBL_EPSILON);
}
}
void test_indexOfValue_Gives_Correct_Value_When_Input_In_Range()
{
const size_t npoints(5);
NumericAxis axis(npoints);
for(size_t i = 0; i < npoints; ++i)
{
axis.setValue(i, static_cast<double>(i));
}
TS_ASSERT_EQUALS(0, axis.indexOfValue(0.0));
TS_ASSERT_EQUALS(4, axis.indexOfValue(4.0));
TS_ASSERT_EQUALS(3, axis.indexOfValue(2.5));
}
//-------------------------------- Failure cases ----------------------------
void test_indexOfValue_Throws_When_Input_Not_In_Axis_Range()
{
const size_t npoints(5);
NumericAxis axis(npoints);
for(size_t i = 0; i < npoints; ++i)
{
axis.setValue(i, static_cast<double>(i));
}
TS_ASSERT_THROWS(axis.indexOfValue(-0.1), std::out_of_range);
TS_ASSERT_THROWS(axis.indexOfValue(4.1), std::out_of_range);
}
private:
Axis *numericAxis;
};
#endif /*NUMERICAXISTEST_H_*/