PeakShapeSphericalTest.h 9.44 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8

9
#ifdef _MSC_VER
Martyn Gigg's avatar
Martyn Gigg committed
10
11
// 'identifier' : class 'type' needs to have dll-interface to be used by clients
// of class 'type2'
Owen Arnold's avatar
Owen Arnold committed
12
#pragma warning(disable : 4251)
Martyn Gigg's avatar
Martyn Gigg committed
13
14
// JSON: non-DLL-interface classkey 'identifier' used as base for
// DLL-interface classkey 'identifier'
15
#pragma warning(disable : 4275)
Owen Arnold's avatar
Owen Arnold committed
16
17
#endif

18
#include <cxxtest/TestSuite.h>
19
#include <json/json.h>
20
21

#include "MantidDataObjects/PeakShapeSpherical.h"
22
#include "MantidKernel/SpecialCoordinateSystem.h"
LamarMoore's avatar
LamarMoore committed
23
#include "MantidKernel/V3D.h"
24
25
26
27

using Mantid::DataObjects::PeakShapeSpherical;
using namespace Mantid::Kernel;

Owen Arnold's avatar
Owen Arnold committed
28
class PeakShapeSphericalTest : public CxxTest::TestSuite {
29
30
31
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
Owen Arnold's avatar
Owen Arnold committed
32
33
34
35
  static PeakShapeSphericalTest *createSuite() {
    return new PeakShapeSphericalTest();
  }
  static void destroySuite(PeakShapeSphericalTest *suite) { delete suite; }
36

Owen Arnold's avatar
Owen Arnold committed
37
  void test_constructor() {
38
    const double radius = 2;
39
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
40
41
42
43
    const std::string algorithmName = "foo";
    const int algorithmVersion = 3;

    // Construct it.
44
    PeakShapeSpherical shape(radius, frame, algorithmName, algorithmVersion);
Owen Arnold's avatar
Owen Arnold committed
45
46
47
48
49

    TS_ASSERT_EQUALS(radius, shape.radius());
    TS_ASSERT_EQUALS(frame, shape.frame());
    TS_ASSERT_EQUALS(algorithmName, shape.algorithmName());
    TS_ASSERT_EQUALS(algorithmVersion, shape.algorithmVersion());
50
51
52
53
    TS_ASSERT(!shape.backgroundInnerRadius().is_initialized());
    TS_ASSERT(!shape.backgroundOuterRadius().is_initialized());
  }

54
55
56
57
  void test_multiple_radii_constructor() {
    const double radius = 2;
    const double backgroundInnerRadius = 3;
    const double backgroundOuterRadius = 4;
58
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
59
60
61
62
63
64
65
66
67
    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());
Hahn, Steven's avatar
Hahn, Steven committed
68
69
70
71
72
73
74
75
76
    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));

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    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 unset since is same as radius",
        !badShape.backgroundInnerRadius().is_initialized());
    TSM_ASSERT(
        "Background outer radius should be unset since is same as radius",
        !badShape.backgroundOuterRadius().is_initialized());
94
95
  }

Owen Arnold's avatar
Owen Arnold committed
96
  void test_copy_constructor() {
97
    const double radius = 2;
98
99
    const double backgroundInnerRadius = 3;
    const double backgroundOuterRadius = 4;
100
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
101
102
103
104
    const std::string algorithmName = "foo";
    const int algorithmVersion = 3;

    // Construct it.
105
106
    PeakShapeSpherical a(radius, backgroundInnerRadius, backgroundOuterRadius,
                         frame, algorithmName, algorithmVersion);
107
108
109
    // Copy construct it
    PeakShapeSpherical b(a);

Owen Arnold's avatar
Owen Arnold committed
110
111
112
113
    TS_ASSERT_EQUALS(radius, b.radius());
    TS_ASSERT_EQUALS(frame, b.frame());
    TS_ASSERT_EQUALS(algorithmName, b.algorithmName());
    TS_ASSERT_EQUALS(algorithmVersion, b.algorithmVersion());
114
115
    TS_ASSERT_EQUALS(backgroundInnerRadius, b.backgroundInnerRadius().get());
    TS_ASSERT_EQUALS(backgroundOuterRadius, b.backgroundOuterRadius().get());
116
117
  }

Owen Arnold's avatar
Owen Arnold committed
118
  void test_assignment() {
119
    const double radius = 2;
120
121
    const double backgroundInnerRadius = 3;
    const double backgroundOuterRadius = 4;
122
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
123
124
125
126
    const std::string algorithmName = "foo";
    const int algorithmVersion = 3;

    // Construct it.
127
128
    PeakShapeSpherical a(radius, backgroundInnerRadius, backgroundOuterRadius,
                         frame, algorithmName, algorithmVersion);
129
    PeakShapeSpherical b(1.0, QSample, "bar", -2);
130
131
132
133
134

    // Assign to it
    b = a;

    // Test the assignments
Owen Arnold's avatar
Owen Arnold committed
135
136
137
138
    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());
139
140
141
142
    TS_ASSERT_EQUALS(a.backgroundInnerRadius(),
                     b.backgroundInnerRadius().get());
    TS_ASSERT_EQUALS(a.backgroundOuterRadius(),
                     b.backgroundOuterRadius().get());
143
144
  }

Owen Arnold's avatar
Owen Arnold committed
145
146
  void test_clone() {
    const double radius = 2;
147
148
    const double backgroundInnerRadius = 3;
    const double backgroundOuterRadius = 4;
149
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
Owen Arnold's avatar
Owen Arnold committed
150
151
    const std::string algorithmName = "foo";
    const int algorithmVersion = 3;
152

Owen Arnold's avatar
Owen Arnold committed
153
    // Construct it.
154
155
    PeakShapeSpherical a(radius, backgroundInnerRadius, backgroundOuterRadius,
                         frame, algorithmName, algorithmVersion);
Owen Arnold's avatar
Owen Arnold committed
156
157
158
159
160
161
    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());
162
163
164
165
    TS_ASSERT_EQUALS(a.backgroundInnerRadius(),
                     clone->backgroundInnerRadius().get());
    TS_ASSERT_EQUALS(a.backgroundOuterRadius(),
                     clone->backgroundOuterRadius().get());
Owen Arnold's avatar
Owen Arnold committed
166
    TS_ASSERT_DIFFERS(clone, &a);
167
168
    delete clone;
  }
169

Owen Arnold's avatar
Owen Arnold committed
170
171
  void test_toJSON() {
    const double radius = 2;
172
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
Owen Arnold's avatar
Owen Arnold committed
173
174
    const std::string algorithmName = "foo";
    const int algorithmVersion = 3;
175

Owen Arnold's avatar
Owen Arnold committed
176
    // Construct it.
177
    PeakShapeSpherical shape(radius, frame, algorithmName, algorithmVersion);
Owen Arnold's avatar
Owen Arnold committed
178
179
180
181
182
183
184
185
186
187
188
    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());
  }
189

190
191
192
193
  void test_toJSON_multiple_radii() {
    const double radius = 2;
    const double backgroundInnerRadius = 3;
    const double backgroundOuterRadius = 4;
194
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    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());
216
217
  }

Owen Arnold's avatar
Owen Arnold committed
218
  void test_equals() {
219
220
    TS_ASSERT_EQUALS(PeakShapeSpherical(1.0, QSample),
                     PeakShapeSpherical(1.0, QSample));
221

222
223
    TS_ASSERT_EQUALS(PeakShapeSpherical(1.0, 2.0, 3.0, QSample),
                     PeakShapeSpherical(1.0, 2.0, 3.0, QSample));
224

225
    TSM_ASSERT_DIFFERS("Different radius", PeakShapeSpherical(1.0, QSample),
226
                       PeakShapeSpherical(2.0, QSample));
227

228
    TSM_ASSERT_DIFFERS("Different frame", PeakShapeSpherical(1.0, QSample),
229
                       PeakShapeSpherical(1.0, QLab));
230
231
232
233
234
235

    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",
236
                       PeakShapeSpherical(1.0, 2.0, 2.0, QSample),
237
                       PeakShapeSpherical(1.0, 2.0, 3.0, QSample));
Owen Arnold's avatar
Owen Arnold committed
238
  }
239

Owen Arnold's avatar
Owen Arnold committed
240
  void test_shape_name() {
241

Owen Arnold's avatar
Owen Arnold committed
242
    const double radius = 1;
243
    const SpecialCoordinateSystem frame = SpecialCoordinateSystem::HKL;
244

Owen Arnold's avatar
Owen Arnold committed
245
    // Construct it.
246
    PeakShapeSpherical shape(radius, frame);
247

Owen Arnold's avatar
Owen Arnold committed
248
249
    TS_ASSERT_EQUALS("spherical", shape.shapeName());
  }
250
};