forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterpolationTest.h
312 lines (244 loc) · 10.8 KB
/
InterpolationTest.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidKernel/Interpolation.h"
#include <ctime>
#include <cxxtest/TestSuite.h>
using namespace Mantid::Kernel;
class InterpolationTest : public CxxTest::TestSuite {
public:
// This means the constructor isn't called when running other tests
static InterpolationTest *createSuite() { return new InterpolationTest(); }
static void destroySuite(InterpolationTest *suite) { delete suite; }
/* In the constructor some vectors with values are setup,
* which make the tests easier later on.
*
* To check the interpolated values, call the method
* checkInterpolationResults(const Interpolation &interpolation);
* and supply the interpolation object which is to be tested. The method will
*call
* further methods to cover all possible edge-cases. On failure, it will be
*visible
* which case caused the failure.
*/
InterpolationTest() {
// values for setting up the interpolation
m_tableValues.emplace_back(DataXY(200.0, 50));
m_tableValues.emplace_back(DataXY(201.0, 60));
m_tableValues.emplace_back(DataXY(202.0, 100));
m_tableValues.emplace_back(DataXY(203.0, 300));
m_tableValues.emplace_back(DataXY(204.0, 400));
// bulk values for interpolation test
m_interpolationXValues.emplace_back(200.5);
m_interpolationXValues.emplace_back(201.25);
m_interpolationXValues.emplace_back(203.5);
m_expectedYValues.emplace_back(55.0);
m_expectedYValues.emplace_back(70.0);
m_expectedYValues.emplace_back(350.0);
// values outside interpolation range
m_outsideXValues.emplace_back(100.0);
m_outsideXValues.emplace_back(3000.0);
m_outsideYValues.emplace_back(-950.0);
m_outsideYValues.emplace_back(280000.0);
}
void testCopyConstruction() {
Interpolation interpolation;
interpolation.setMethod("linear");
interpolation.setXUnit("Wavelength");
interpolation.setYUnit("dSpacing");
interpolation.addPoint(200.0, 2.0);
interpolation.addPoint(202.0, 3.0);
Interpolation other = interpolation;
TS_ASSERT_EQUALS(other.getMethod(), "linear");
TS_ASSERT_EQUALS(other.getXUnit()->unitID(), "Wavelength");
TS_ASSERT_EQUALS(other.getYUnit()->unitID(), "dSpacing");
TS_ASSERT_EQUALS(other.value(200.0), 2.0);
}
void testContainData() {
Interpolation interpolation;
TS_ASSERT(interpolation.containData() == false);
interpolation.addPoint(200.0, 50);
TS_ASSERT(interpolation.containData() == true);
}
void testResetData() {
Interpolation interpolation = getInitializedInterpolation("Wavelength", "dSpacing");
TS_ASSERT(interpolation.containData());
interpolation.resetData();
TS_ASSERT(interpolation.containData() == false);
}
void testAddPointOrdered() {
Interpolation interpolation;
// Add points from values in vector in correct order.
for (size_t i = 0; i < m_tableValues.size(); ++i) {
interpolation.addPoint(m_tableValues[i].first, m_tableValues[i].second);
}
// Check correctness of interpolation for different cases
checkInterpolationResults(interpolation);
}
void testAddPointArbitrary() {
Interpolation interpolation;
size_t insertionOrderRaw[] = {1, 0, 3, 4, 2};
std::vector<size_t> insertionOrder(insertionOrderRaw, insertionOrderRaw + 5);
for (std::vector<size_t>::const_iterator i = insertionOrder.begin(); i != insertionOrder.end(); ++i) {
interpolation.addPoint(m_tableValues[*i].first, m_tableValues[*i].second);
}
checkInterpolationResults(interpolation);
}
void testAddPointDuplicates() {
TestableInterpolation interpolation;
// Add points from values in vector twice.
for (size_t i = 0; i < m_tableValues.size(); ++i) {
interpolation.addPoint(m_tableValues[i].first, m_tableValues[i].second);
interpolation.addPoint(m_tableValues[i].first, m_tableValues[i].second);
}
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.cend()), 5);
}
void testEmpty() {
Interpolation interpolation;
std::stringstream str;
str << interpolation;
TS_ASSERT(str.str().compare("linear ; TOF ; TOF") == 0);
Interpolation readIn;
str >> readIn;
TS_ASSERT(readIn.containData() == false);
}
void testStreamOperators() {
std::string xUnit = "Wavelength";
std::string yUnit = "dSpacing";
Interpolation interpolation = getInitializedInterpolation(xUnit, yUnit);
// Output stream
std::stringstream str;
str << interpolation;
TS_ASSERT(str.str().compare("linear ; Wavelength ; dSpacing ; 200 50 ; 201 "
"60 ; 202 100 ; 203 300 ; 204 400") == 0);
// Input stream for empty interpolation object
Interpolation readIn;
TS_ASSERT(readIn.getXUnit()->unitID() == "TOF");
TS_ASSERT(readIn.getYUnit()->unitID() == "TOF");
str >> readIn;
TS_ASSERT(readIn.getXUnit()->unitID() == xUnit);
TS_ASSERT(readIn.getYUnit()->unitID() == yUnit);
checkInterpolationResults(readIn);
}
void testStreamOperatorsNonEmpty() {
Interpolation interpolation = getInitializedInterpolation("Wavelength", "dSpacing");
std::stringstream str;
str << interpolation;
// Reconstruct on existing object.
str >> interpolation;
checkInterpolationResults(interpolation);
}
void testFindIndexOfNextLargerValue() {
TestableInterpolation interpolation;
// take values from constructor
for (size_t i = 0; i < m_tableValues.size(); ++i) {
interpolation.addPoint(m_tableValues[i].first, m_tableValues[i].second);
}
// lower limit - can be treated like general case
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.findIndexOfNextLargerValue(200.0)), 1);
// Exact interpolation points
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.findIndexOfNextLargerValue(201.0)), 2);
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.findIndexOfNextLargerValue(202.0)), 3);
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.findIndexOfNextLargerValue(203.0)), 4);
// Arbitrary interpolation points
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.findIndexOfNextLargerValue(200.5)), 1);
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.findIndexOfNextLargerValue(201.25)), 2);
TS_ASSERT_EQUALS(std::distance(interpolation.cbegin(), interpolation.findIndexOfNextLargerValue(203.5)), 4);
}
void testCBegin() {
TestableInterpolation interpolation;
// take values from constructor
for (size_t i = 0; i < m_tableValues.size(); ++i) {
interpolation.addPoint(m_tableValues[i].first, m_tableValues[i].second);
}
TS_ASSERT_EQUALS(interpolation.cbegin()->first, 200.0)
}
void testCEnd() {
TestableInterpolation interpolation;
// take values from constructor
for (size_t i = 0; i < m_tableValues.size(); ++i) {
interpolation.addPoint(m_tableValues[i].first, m_tableValues[i].second);
}
TS_ASSERT_EQUALS(std::prev(interpolation.cend())->first, 204.0)
}
void testInterpolationWithTooFewValues() {
Interpolation interpolationZero;
Interpolation interpolationOne;
interpolationOne.addPoint(200, 2.0);
for (DataXY m_tableValue : m_tableValues) {
// When there are zero values in the interpolation, it returns 0.0
checkValue(interpolationZero, m_tableValue.first, 0.0, "zero interpolation values");
// With one value, it returns this one value for any x.
checkValue(interpolationOne, m_tableValue.first, 2.0, "one interpolation value");
}
}
private:
Interpolation getInitializedInterpolation(const std::string &xUnit, const std::string &yUnit) {
Interpolation interpolation;
// take values from constructor
for (size_t i = 0; i < m_tableValues.size(); ++i) {
interpolation.addPoint(m_tableValues[i].first, m_tableValues[i].second);
}
interpolation.setXUnit(xUnit);
interpolation.setYUnit(yUnit);
return interpolation;
}
void checkInterpolationResults(const Interpolation &interpolation) {
checkValueAtLowerLimit(interpolation);
checkValueAtUpperLimit(interpolation);
checkValuesAtExactBulkPoints(interpolation);
checkValuesInsideInterpolationRange(interpolation);
checkValuesOutsideInterpolationRange(interpolation);
}
void checkValueAtLowerLimit(const Interpolation &interpolation) {
checkValue(interpolation, m_tableValues.front().first, m_tableValues.front().second, "at lower limit");
}
void checkValueAtUpperLimit(const Interpolation &interpolation) {
checkValue(interpolation, m_tableValues.back().first, m_tableValues.back().second, "at upper limit");
}
void checkValuesAtExactBulkPoints(const Interpolation &interpolation) {
for (size_t i = 1; i < m_tableValues.size() - 1; ++i) {
checkValue(interpolation, m_tableValues[i].first, m_tableValues[i].second, "at interpolation point");
}
}
void checkValuesInsideInterpolationRange(const Interpolation &interpolation) {
for (size_t i = 0; i < m_interpolationXValues.size(); ++i) {
checkValue(interpolation, m_interpolationXValues[i], m_expectedYValues[i], "inside interpolation range");
}
}
void checkValuesOutsideInterpolationRange(const Interpolation &interpolation) {
for (size_t i = 0; i < m_outsideXValues.size(); ++i) {
checkValue(interpolation, m_outsideXValues[i], m_outsideYValues[i], "outside interpolation range");
}
}
/* This function performs the actual check.
* It takes a string argument to make it more obvious where the problem is.
*/
void checkValue(const Interpolation &interpolation, double x, double y, const std::string &testedRange) {
std::ostringstream errorString;
errorString << "Interpolation error " << testedRange;
TSM_ASSERT_EQUALS(errorString.str().c_str(), interpolation.value(x), y);
}
// This vector contains the data points from which the interpolation is
// constructed
std::vector<DataXY> m_tableValues;
// Two vectors with test values for the "bulk", e.g. no values at the limits
// and
std::vector<double> m_interpolationXValues;
std::vector<double> m_expectedYValues;
// Values outside interpolation range
std::vector<double> m_outsideXValues;
std::vector<double> m_outsideYValues;
// For the test of findIndexOfNextLargerValue access to protected member is
// needed
class TestableInterpolation : public Interpolation {
friend class InterpolationTest;
public:
TestableInterpolation() : Interpolation() {}
~TestableInterpolation() override = default;
};
};