forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeakColumnTest.h
175 lines (146 loc) · 5.87 KB
/
PeakColumnTest.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
// 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 "MantidDataObjects/Peak.h"
#include "MantidDataObjects/PeakColumn.h"
#include "MantidKernel/Exception.h"
#include <cxxtest/TestSuite.h>
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include <locale>
#include <memory>
using namespace Mantid::DataObjects;
// Allow testing of protected methods
class PeakColumnTestHelper : public PeakColumn<Peak> {
public:
PeakColumnTestHelper(std::vector<Peak> &peaks, const std::string &name) : PeakColumn<Peak>(peaks, name) {}
using PeakColumn::insert;
using PeakColumn::remove;
using PeakColumn::resize;
};
class PeakColumnTest : 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 PeakColumnTest *createSuite() { return new PeakColumnTest(); }
static void destroySuite(PeakColumnTest *suite) { delete suite; }
PeakColumnTest() : m_peaks(2) {
m_inst = ComponentCreationHelper::createTestInstrumentCylindrical(1);
m_peaks[0] = Peak(m_inst, 1, 4.0);
m_peaks[1] = Peak(m_inst, 2, 4.1);
}
void test_constructor_create_valid_object_when_given_valid_name() {
PeakColumn<Peak> pc(m_peaks, "h");
TS_ASSERT_EQUALS(pc.name(), "h");
TS_ASSERT_EQUALS(pc.size(), 2);
}
void test_constructor_throws_given_unknown_name() {
TS_ASSERT_THROWS(PeakColumn<Peak>(m_peaks, "NotPeakColumn"), const std::runtime_error &);
}
void test_clone() {
PeakColumn<Peak> pc(m_peaks, "h");
PeakColumn<Peak> *cloned = pc.clone();
TS_ASSERT_EQUALS(pc.name(), cloned->name());
TS_ASSERT_EQUALS(2, cloned->size());
delete cloned;
}
void test_type_info_is_expected_type_based_on_string_type() {
PeakColumnTestHelper pcInt(m_peaks, "DetID");
TS_ASSERT(pcInt.get_type_info() == typeid(int));
TS_ASSERT(pcInt.get_pointer_type_info() == typeid(int *));
PeakColumnTestHelper pcDouble(m_peaks, "h");
TS_ASSERT(pcDouble.get_type_info() == typeid(double));
TS_ASSERT(pcDouble.get_pointer_type_info() == typeid(double *));
PeakColumnTestHelper pcStr(m_peaks, "BankName");
TS_ASSERT(pcStr.get_type_info() == typeid(std::string));
TS_ASSERT(pcStr.get_pointer_type_info() == typeid(std::string *));
PeakColumnTestHelper pcV3D(m_peaks, "QLab");
TS_ASSERT(pcV3D.get_type_info() == typeid(Mantid::Kernel::V3D));
TS_ASSERT(pcV3D.get_pointer_type_info() == typeid(Mantid::Kernel::V3D *));
}
void test_PeakColumn_Cannot_Be_Resized() {
PeakColumnTestHelper pc(m_peaks, "DetID");
TS_ASSERT_THROWS(pc.resize(10), const Mantid::Kernel::Exception::NotImplementedError &);
}
void test_Row_Cannot_Be_Inserted_Into_PeakColumn() {
PeakColumnTestHelper pc(m_peaks, "DetID");
TS_ASSERT_THROWS(pc.insert(0), const Mantid::Kernel::Exception::NotImplementedError &);
}
void test_Row_Cannot_Be_Removed_From_PeakColumn() {
PeakColumnTestHelper pc(m_peaks, "DetID");
TS_ASSERT_THROWS(pc.remove(0), const Mantid::Kernel::Exception::NotImplementedError &);
}
void test_cell_returns_correct_value_from_PeakColumn() {
PeakColumn<Peak> pc1(m_peaks, "DetID");
int detId = pc1.cell<int>(0);
TS_ASSERT_EQUALS(1, detId);
detId = pc1.cell<int>(1);
TS_ASSERT_EQUALS(2, detId);
PeakColumn<Peak> pc2(m_peaks, "QLab");
const Mantid::Kernel::V3D &qlab0 = pc2.cell<Mantid::Kernel::V3D>(0);
TS_ASSERT_EQUALS(qlab0, m_peaks[0].getQLabFrame());
const Mantid::Kernel::V3D &qlab1 = pc2.cell<Mantid::Kernel::V3D>(1);
TS_ASSERT_EQUALS(qlab1, m_peaks[1].getQLabFrame());
}
void test_get_read_only_returns_correct_value() {
PeakColumn<Peak> pc1(m_peaks, "h");
auto readOnly = pc1.getReadOnly();
TS_ASSERT(!readOnly);
PeakColumn<Peak> pc2(m_peaks, "DetID");
readOnly = pc2.getReadOnly();
TS_ASSERT(readOnly);
}
void test_read_locale_awerness() {
const std::vector<std::string> columnNames{"h", "k", "l", "RunNumber"};
const std::vector<double> columnValues{-2.0, 5.0, 12.0, 143290.0};
TestingNumpunctFacet numpunct;
const std::locale testLocale(std::locale::classic(), &numpunct);
for (size_t i = 0; i < columnNames.size(); ++i) {
PeakColumn<Peak> pc(m_peaks, columnNames[i]);
// Use a fake punctuation facet for numeric formatting.
std::ostringstream out;
out.imbue(testLocale);
// Force some decimals to the numbers.
out << std::fixed;
out << std::setprecision(2);
out << columnValues[i];
std::istringstream in(out.str());
in.imbue(testLocale);
pc.read(0, in);
switch (i) {
case 0:
TS_ASSERT_EQUALS(m_peaks[0].getH(), columnValues[i])
break;
case 1:
TS_ASSERT_EQUALS(m_peaks[0].getK(), columnValues[i])
break;
case 2:
TS_ASSERT_EQUALS(m_peaks[0].getL(), columnValues[i])
break;
case 3:
TS_ASSERT_EQUALS(m_peaks[0].getRunNumber(), columnValues[i])
break;
}
}
}
void test_cannot_be_converted_to_double() {
PeakColumn<Peak> col(m_peaks, "DetID");
TS_ASSERT(!col.isNumber());
}
private:
/// A locale facet mocking non-english numerical punctuation.
class TestingNumpunctFacet : public std::numpunct<char> {
public:
/// Informs locales not to delete this facet.
TestingNumpunctFacet() : std::numpunct<char>(1) {}
private:
char_type do_decimal_point() const override { return '%'; }
char_type do_thousands_sep() const override { return '@'; }
string_type do_grouping() const override { return "\03"; }
};
Mantid::Geometry::Instrument_sptr m_inst;
std::vector<Peak> m_peaks;
};