forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeakShapeSphericalTest.h
221 lines (179 loc) · 8.93 KB
/
PeakShapeSphericalTest.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
// 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
#ifdef _MSC_VER
// 'identifier' : class 'type' needs to have dll-interface to be used by clients
// of class 'type2'
#pragma warning(disable : 4251)
// JSON: non-DLL-interface classkey 'identifier' used as base for
// DLL-interface classkey 'identifier'
#pragma warning(disable : 4275)
#endif
#include <cxxtest/TestSuite.h>
#include <json/json.h>
#include "MantidDataObjects/PeakShapeSpherical.h"
#include "MantidKernel/SpecialCoordinateSystem.h"
#include "MantidKernel/V3D.h"
using Mantid::DataObjects::PeakShapeSpherical;
using namespace Mantid::Kernel;
class PeakShapeSphericalTest : 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 PeakShapeSphericalTest *createSuite() { return new PeakShapeSphericalTest(); }
static void destroySuite(PeakShapeSphericalTest *suite) { delete suite; }
void test_constructor() {
const double radius = 2;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
const std::string algorithmName = "foo";
const int algorithmVersion = 3;
// Construct it.
PeakShapeSpherical shape(radius, frame, algorithmName, algorithmVersion);
TS_ASSERT_EQUALS(radius, shape.radius());
TS_ASSERT_EQUALS(frame, shape.frame());
TS_ASSERT_EQUALS(algorithmName, shape.algorithmName());
TS_ASSERT_EQUALS(algorithmVersion, shape.algorithmVersion());
TS_ASSERT(!shape.backgroundInnerRadius().is_initialized());
TS_ASSERT(!shape.backgroundOuterRadius().is_initialized());
}
void test_multiple_radii_constructor() {
const double radius = 2;
const double backgroundInnerRadius = 3;
const double backgroundOuterRadius = 4;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
const std::string algorithmName = "foo";
const int algorithmVersion = 3;
// Construct it.
PeakShapeSpherical shape(radius, backgroundInnerRadius, backgroundOuterRadius, frame, algorithmName,
algorithmVersion);
TS_ASSERT_EQUALS(radius, shape.radius());
TS_ASSERT_EQUALS(radius, shape.radius(Mantid::Geometry::PeakShape::RadiusType::Radius));
TS_ASSERT_EQUALS(backgroundInnerRadius, shape.radius(Mantid::Geometry::PeakShape::RadiusType::InnerRadius));
TS_ASSERT_EQUALS(backgroundOuterRadius, shape.radius(Mantid::Geometry::PeakShape::RadiusType::OuterRadius));
TS_ASSERT_EQUALS(frame, shape.frame());
TS_ASSERT_EQUALS(algorithmName, shape.algorithmName());
TS_ASSERT_EQUALS(algorithmVersion, shape.algorithmVersion());
TS_ASSERT_EQUALS(backgroundInnerRadius, shape.backgroundInnerRadius().get());
TS_ASSERT_EQUALS(backgroundOuterRadius, shape.backgroundOuterRadius().get());
PeakShapeSpherical badShape(radius, radius, radius, frame, algorithmName, algorithmVersion);
TSM_ASSERT("Background inner radius should be set even when same as radius",
badShape.backgroundInnerRadius().is_initialized());
TSM_ASSERT("Background outer radius should be unset since is same as radius",
!badShape.backgroundOuterRadius().is_initialized());
}
void test_copy_constructor() {
const double radius = 2;
const double backgroundInnerRadius = 3;
const double backgroundOuterRadius = 4;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
const std::string algorithmName = "foo";
const int algorithmVersion = 3;
// Construct it.
PeakShapeSpherical a(radius, backgroundInnerRadius, backgroundOuterRadius, frame, algorithmName, algorithmVersion);
// Copy construct it
PeakShapeSpherical b(a);
TS_ASSERT_EQUALS(radius, b.radius());
TS_ASSERT_EQUALS(frame, b.frame());
TS_ASSERT_EQUALS(algorithmName, b.algorithmName());
TS_ASSERT_EQUALS(algorithmVersion, b.algorithmVersion());
TS_ASSERT_EQUALS(backgroundInnerRadius, b.backgroundInnerRadius().get());
TS_ASSERT_EQUALS(backgroundOuterRadius, b.backgroundOuterRadius().get());
}
void test_assignment() {
const double radius = 2;
const double backgroundInnerRadius = 3;
const double backgroundOuterRadius = 4;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
const std::string algorithmName = "foo";
const int algorithmVersion = 3;
// Construct it.
PeakShapeSpherical a(radius, backgroundInnerRadius, backgroundOuterRadius, frame, algorithmName, algorithmVersion);
PeakShapeSpherical b(1.0, QSample, "bar", -2);
// Assign to it
b = a;
// Test the assignments
TS_ASSERT_EQUALS(a.radius(), b.radius());
TS_ASSERT_EQUALS(a.frame(), b.frame());
TS_ASSERT_EQUALS(a.algorithmName(), b.algorithmName());
TS_ASSERT_EQUALS(a.algorithmVersion(), b.algorithmVersion());
TS_ASSERT_EQUALS(a.backgroundInnerRadius(), b.backgroundInnerRadius().get());
TS_ASSERT_EQUALS(a.backgroundOuterRadius(), b.backgroundOuterRadius().get());
}
void test_clone() {
const double radius = 2;
const double backgroundInnerRadius = 3;
const double backgroundOuterRadius = 4;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
const std::string algorithmName = "foo";
const int algorithmVersion = 3;
// Construct it.
PeakShapeSpherical a(radius, backgroundInnerRadius, backgroundOuterRadius, frame, algorithmName, algorithmVersion);
PeakShapeSpherical *clone = a.clone();
TS_ASSERT_EQUALS(a.radius(), clone->radius());
TS_ASSERT_EQUALS(a.frame(), clone->frame());
TS_ASSERT_EQUALS(a.algorithmName(), clone->algorithmName());
TS_ASSERT_EQUALS(a.algorithmVersion(), clone->algorithmVersion());
TS_ASSERT_EQUALS(a.backgroundInnerRadius(), clone->backgroundInnerRadius().get());
TS_ASSERT_EQUALS(a.backgroundOuterRadius(), clone->backgroundOuterRadius().get());
TS_ASSERT_DIFFERS(clone, &a);
delete clone;
}
void test_toJSON() {
const double radius = 2;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
const std::string algorithmName = "foo";
const int algorithmVersion = 3;
// Construct it.
PeakShapeSpherical shape(radius, frame, algorithmName, algorithmVersion);
const std::string json = shape.toJSON();
Json::Reader reader;
Json::Value output;
TSM_ASSERT("Should parse as JSON", reader.parse(json, output));
TS_ASSERT_EQUALS(algorithmName, output["algorithm_name"].asString());
TS_ASSERT_EQUALS(algorithmVersion, output["algorithm_version"].asInt());
TS_ASSERT_EQUALS(frame, output["frame"].asInt());
TS_ASSERT_EQUALS(radius, output["radius"].asDouble());
}
void test_toJSON_multiple_radii() {
const double radius = 2;
const double backgroundInnerRadius = 3;
const double backgroundOuterRadius = 4;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
const std::string algorithmName = "foo";
const int algorithmVersion = 3;
// Construct it.
PeakShapeSpherical shape(radius, backgroundInnerRadius, backgroundOuterRadius, frame, algorithmName,
algorithmVersion);
const std::string json = shape.toJSON();
Json::Reader reader;
Json::Value output;
TSM_ASSERT("Should parse as JSON", reader.parse(json, output));
TS_ASSERT_EQUALS(algorithmName, output["algorithm_name"].asString());
TS_ASSERT_EQUALS(algorithmVersion, output["algorithm_version"].asInt());
TS_ASSERT_EQUALS(frame, output["frame"].asInt());
TS_ASSERT_EQUALS(radius, output["radius"].asDouble());
TS_ASSERT_EQUALS(backgroundInnerRadius, output["background_inner_radius"].asDouble());
TS_ASSERT_EQUALS(backgroundOuterRadius, output["background_outer_radius"].asDouble());
}
void test_equals() {
TS_ASSERT_EQUALS(PeakShapeSpherical(1.0, QSample), PeakShapeSpherical(1.0, QSample));
TS_ASSERT_EQUALS(PeakShapeSpherical(1.0, 2.0, 3.0, QSample), PeakShapeSpherical(1.0, 2.0, 3.0, QSample));
TSM_ASSERT_DIFFERS("Different radius", PeakShapeSpherical(1.0, QSample), PeakShapeSpherical(2.0, QSample));
TSM_ASSERT_DIFFERS("Different frame", PeakShapeSpherical(1.0, QSample), PeakShapeSpherical(1.0, QLab));
TSM_ASSERT_DIFFERS("Different background inner", PeakShapeSpherical(1.0, 1.0, 3.0, QSample),
PeakShapeSpherical(1.0, 2.0, 3.0, QSample));
TSM_ASSERT_DIFFERS("Different background outer", PeakShapeSpherical(1.0, 2.0, 2.0, QSample),
PeakShapeSpherical(1.0, 2.0, 3.0, QSample));
}
void test_shape_name() {
const double radius = 1;
const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
// Construct it.
PeakShapeSpherical shape(radius, frame);
TS_ASSERT_EQUALS("spherical", shape.shapeName());
}
};