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
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
#ifndef MANTID_KERNEL_HISTOGRAM_HISTOGRAMXTEST_H_
#define MANTID_KERNEL_HISTOGRAM_HISTOGRAMXTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/Histogram/HistogramX.h"
using Mantid::Kernel::Points;
using Mantid::Kernel::BinEdges;
using Mantid::Kernel::HistogramX;
class HistogramHistogramXTest : 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 HistogramHistogramXTest *createSuite() {
return new HistogramHistogramXTest();
}
static void destroySuite(HistogramHistogramXTest *suite) { delete suite; }
void test_construct_from_Points() {
Points points{0.1, 0.2, 0.4};
const HistogramX histX(points);
TS_ASSERT_EQUALS(histX.size(), 3);
}
void test_construct_from_BinEdges() {
BinEdges binEdges{0.1, 0.2, 0.4};
const HistogramX histX(binEdges);
TS_ASSERT_EQUALS(histX.size(), 3);
}
void test_construct_from_invalid_BinEdges() {
BinEdges binEdges(1);
TS_ASSERT_THROWS(HistogramX{binEdges}, std::logic_error);
}
void test_points_from_edges() {
BinEdges binEdges{0.1, 0.2, 0.4};
const HistogramX histX(binEdges);
TS_ASSERT_EQUALS(histX.size(), 3);
const auto points = histX.points();
TS_ASSERT_DIFFERS(&points[0], &histX[0]);
TS_ASSERT_EQUALS(points.size(), 2);
TS_ASSERT_DELTA(points[0], 0.15, 1e-14);
TS_ASSERT_DELTA(points[1], 0.3, 1e-14);
}
void test_points_from_points() {
const HistogramX histX(Points{0.1, 0.2, 0.4});
const auto points = histX.points();
TS_ASSERT_EQUALS(&points[0], &histX[0]);
}
void test_setPoints_from_vector() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS_NOTHING(x1.setPoints(std::vector<double>{0.1, 0.2}));
TS_ASSERT_EQUALS(x1.size(), 2);
TS_ASSERT_EQUALS(x1[0], 0.1);
TS_ASSERT_EQUALS(x1[1], 0.2);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS_NOTHING(x2.setPoints(std::vector<double>{0.1}));
TS_ASSERT_EQUALS(x2.size(), 1);
TS_ASSERT_EQUALS(x2[0], 0.1);
}
void test_setPoints_from_Points() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS_NOTHING(x1.setPoints(Points{0.1, 0.2}));
TS_ASSERT_EQUALS(x1.size(), 2);
TS_ASSERT_EQUALS(x1[0], 0.1);
TS_ASSERT_EQUALS(x1[1], 0.2);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS_NOTHING(x2.setPoints(Points{0.1}));
TS_ASSERT_EQUALS(x2.size(), 1);
TS_ASSERT_EQUALS(x2[0], 0.1);
}
void test_setPoints_from_BinEdges() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS_NOTHING(x1.setPoints(BinEdges{0.1, 0.2, 0.4}));
TS_ASSERT_EQUALS(x1.size(), 2);
TS_ASSERT_DELTA(x1[0], 0.15, 1e-14);
TS_ASSERT_DELTA(x1[1], 0.3, 1e-14);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS_NOTHING(x2.setPoints(BinEdges{0.1, 0.2}));
TS_ASSERT_EQUALS(x2.size(), 1);
TS_ASSERT_DELTA(x2[0], 0.15, 1e-14);
}
void test_setPoints_degenerate() {
HistogramX x1(Points(0));
TS_ASSERT_THROWS_NOTHING(x1.setPoints(std::vector<double>(0)));
TS_ASSERT_EQUALS(x1.size(), 0);
TS_ASSERT_THROWS_NOTHING(x1.setPoints(Points(0)));
TS_ASSERT_EQUALS(x1.size(), 0);
TS_ASSERT_THROWS_NOTHING(x1.setPoints(BinEdges(0)));
TS_ASSERT_EQUALS(x1.size(), 0);
HistogramX x2(BinEdges(0));
TS_ASSERT_THROWS_NOTHING(x2.setPoints(std::vector<double>(0)));
TS_ASSERT_EQUALS(x2.size(), 0);
TS_ASSERT_THROWS_NOTHING(x2.setPoints(Points(0)));
TS_ASSERT_EQUALS(x2.size(), 0);
TS_ASSERT_THROWS_NOTHING(x2.setPoints(BinEdges(0)));
TS_ASSERT_EQUALS(x2.size(), 0);
}
void test_setPoints_size_mismatch() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS(x1.setPoints(std::vector<double>(1)), std::logic_error);
TS_ASSERT_THROWS(x1.setPoints(std::vector<double>(3)), std::logic_error);
TS_ASSERT_THROWS(x1.setPoints(Points(1)), std::logic_error);
TS_ASSERT_THROWS(x1.setPoints(Points(3)), std::logic_error);
TS_ASSERT_THROWS(x1.setPoints(BinEdges(2)), std::logic_error);
TS_ASSERT_THROWS(x1.setPoints(BinEdges(4)), std::logic_error);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS(x2.setPoints(std::vector<double>(0)), std::logic_error);
TS_ASSERT_THROWS(x2.setPoints(std::vector<double>(2)), std::logic_error);
TS_ASSERT_THROWS(x2.setPoints(Points(0)), std::logic_error);
TS_ASSERT_THROWS(x2.setPoints(Points(2)), std::logic_error);
TS_ASSERT_THROWS(x2.setPoints(BinEdges(1)), std::logic_error);
TS_ASSERT_THROWS(x2.setPoints(BinEdges(3)), std::logic_error);
}
void test_setPoints_size_mismatch_degenerate() {
HistogramX x1(Points(0));
TS_ASSERT_THROWS(x1.setPoints(std::vector<double>(1)), std::logic_error);
TS_ASSERT_THROWS(x1.setPoints(Points(1)), std::logic_error);
TS_ASSERT_THROWS(x1.setPoints(BinEdges(1)), std::logic_error);
HistogramX x2(BinEdges(0));
TS_ASSERT_THROWS(x2.setPoints(std::vector<double>(1)), std::logic_error);
TS_ASSERT_THROWS(x2.setPoints(Points(1)), std::logic_error);
TS_ASSERT_THROWS(x2.setPoints(BinEdges(1)), std::logic_error);
}
void test_edges_from_edges() {
const HistogramX histX(BinEdges{0.1, 0.2, 0.4});
const auto edges = histX.binEdges();
TS_ASSERT_EQUALS(&edges[0], &histX[0]);
TS_ASSERT_EQUALS(edges.size(), 3);
}
void test_edges_from_points() {
const HistogramX histX(Points{0.1, 0.2, 0.4});
const auto edges = histX.binEdges();
TS_ASSERT_DIFFERS(&edges[0], &histX[0]);
TS_ASSERT_EQUALS(edges.size(), 4);
}
void test_setBinEdges() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(std::vector<double>(3)));
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(Points(2)));
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(BinEdges(3)));
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(std::vector<double>(2)));
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(Points(1)));
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(BinEdges(2)));
}
void test_setBinEdges_from_vector() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS_NOTHING(
x1.setBinEdges(std::vector<double>{0.1, 0.2, 0.4}));
TS_ASSERT_EQUALS(x1.size(), 3);
TS_ASSERT_EQUALS(x1[0], 0.1);
TS_ASSERT_EQUALS(x1[1], 0.2);
TS_ASSERT_EQUALS(x1[2], 0.4);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(std::vector<double>{0.1, 0.2}));
TS_ASSERT_EQUALS(x2.size(), 2);
TS_ASSERT_EQUALS(x2[0], 0.1);
TS_ASSERT_EQUALS(x2[1], 0.2);
}
void test_setBinEdges_from_Points() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(Points{0.1, 0.3}));
TS_ASSERT_EQUALS(x1.size(), 3);
TS_ASSERT_DELTA(x1[0], 0.0, 1e-14);
TS_ASSERT_DELTA(x1[1], 0.2, 1e-14);
TS_ASSERT_DELTA(x1[2], 0.4, 1e-14);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(Points{1.0}));
TS_ASSERT_EQUALS(x2.size(), 2);
TS_ASSERT_DELTA(x2[0], 0.5, 1e-14);
TS_ASSERT_DELTA(x2[1], 1.5, 1e-14);
}
void test_setBinEdges_from_BinEdges() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(BinEdges{0.1, 0.2, 0.4}));
TS_ASSERT_EQUALS(x1.size(), 3);
TS_ASSERT_EQUALS(x1[0], 0.1);
TS_ASSERT_EQUALS(x1[1], 0.2);
TS_ASSERT_EQUALS(x1[2], 0.4);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(BinEdges{0.1, 0.2}));
TS_ASSERT_EQUALS(x2.size(), 2);
TS_ASSERT_EQUALS(x2[0], 0.1);
TS_ASSERT_EQUALS(x2[1], 0.2);
}
void test_setBinEdges_degenerate() {
HistogramX x1(Points(0));
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(std::vector<double>(0)));
TS_ASSERT_EQUALS(x1.size(), 0);
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(Points(0)));
TS_ASSERT_EQUALS(x1.size(), 0);
TS_ASSERT_THROWS_NOTHING(x1.setBinEdges(BinEdges(0)));
TS_ASSERT_EQUALS(x1.size(), 0);
HistogramX x2(BinEdges(0));
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(std::vector<double>(0)));
TS_ASSERT_EQUALS(x2.size(), 0);
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(Points(0)));
TS_ASSERT_EQUALS(x2.size(), 0);
TS_ASSERT_THROWS_NOTHING(x2.setBinEdges(BinEdges(0)));
TS_ASSERT_EQUALS(x2.size(), 0);
}
void test_setBinEdges_size_mismatch() {
HistogramX x1(Points(2));
TS_ASSERT_THROWS(x1.setBinEdges(std::vector<double>(2)), std::logic_error);
TS_ASSERT_THROWS(x1.setBinEdges(std::vector<double>(4)), std::logic_error);
TS_ASSERT_THROWS(x1.setBinEdges(Points(1)), std::logic_error);
TS_ASSERT_THROWS(x1.setBinEdges(Points(3)), std::logic_error);
TS_ASSERT_THROWS(x1.setBinEdges(BinEdges(2)), std::logic_error);
TS_ASSERT_THROWS(x1.setBinEdges(BinEdges(4)), std::logic_error);
HistogramX x2(BinEdges(2));
TS_ASSERT_THROWS(x2.setBinEdges(std::vector<double>(1)), std::logic_error);
TS_ASSERT_THROWS(x2.setBinEdges(std::vector<double>(3)), std::logic_error);
TS_ASSERT_THROWS(x2.setBinEdges(Points(0)), std::logic_error);
TS_ASSERT_THROWS(x2.setBinEdges(Points(2)), std::logic_error);
TS_ASSERT_THROWS(x2.setBinEdges(BinEdges(1)), std::logic_error);
TS_ASSERT_THROWS(x2.setBinEdges(BinEdges(3)), std::logic_error);
}
void test_setBinEdges_size_mismatch_degenerate() {
HistogramX x1(Points(0));
TS_ASSERT_THROWS(x1.setBinEdges(std::vector<double>(1)), std::logic_error);
TS_ASSERT_THROWS(x1.setBinEdges(Points(1)), std::logic_error);
TS_ASSERT_THROWS(x1.setBinEdges(BinEdges(1)), std::logic_error);
HistogramX x2(BinEdges(0));
TS_ASSERT_THROWS(x2.setBinEdges(std::vector<double>(1)), std::logic_error);
TS_ASSERT_THROWS(x2.setBinEdges(Points(1)), std::logic_error);
TS_ASSERT_THROWS(x2.setBinEdges(BinEdges(1)), std::logic_error);
}
};
#endif /* MANTID_KERNEL_HISTOGRAM_HISTOGRAMXTEST_H_ */