forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefAxisTest.h
109 lines (93 loc) · 4.1 KB
/
RefAxisTest.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
// 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 +
//-------------------------------------------------------------------------------------------------
// This set of tests has been placed in DataObjects because it really needs to
// use a real workspace
//-------------------------------------------------------------------------------------------------
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidAPI/RefAxis.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/UnitFactory.h"
using namespace Mantid::API;
using namespace Mantid::Kernel;
class RefAxisTest : 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 RefAxisTest *createSuite() { return new RefAxisTest(); }
static void destroySuite(RefAxisTest *suite) { delete suite; }
RefAxisTest() {
// Set up two small workspaces for these tests
m_space.reset(new Mantid::DataObjects::Workspace2D);
m_space->initialize(5, 5, 5);
m_space2.reset(new Mantid::DataObjects::Workspace2D);
m_space2->initialize(1, 5, 5);
// Fill them
Mantid::MantidVec a(25);
for (size_t i = 0; i < a.size(); ++i) {
a[i] = static_cast<double>(i) + 0.1;
}
for (int j = 0; j < 5; ++j) {
m_space->mutableX(j) = Mantid::MantidVec(a.cbegin() + (5 * j), a.cbegin() + (5 * j) + 5);
}
// Create the axis that the tests will be performed on
m_refAxis.reset(new RefAxis(m_space.get()));
m_refAxis->title() = "test axis";
m_refAxis->unit() = UnitFactory::Instance().create("TOF");
}
void testConstructor() {
TS_ASSERT_EQUALS(m_refAxis->title(), "test axis")
TS_ASSERT(m_refAxis->isNumeric())
TS_ASSERT(!m_refAxis->isSpectra())
TS_ASSERT_EQUALS(m_refAxis->unit()->unitID(), "TOF")
TS_ASSERT_THROWS(m_refAxis->spectraNo(0), const std::domain_error &)
}
void testClone() {
std::unique_ptr<Axis> clonedAxis(m_refAxis->clone(m_space2.get()));
TS_ASSERT_DIFFERS(clonedAxis.get(), m_refAxis.get())
TS_ASSERT(clonedAxis)
TS_ASSERT_EQUALS(clonedAxis->title(), "test axis")
TS_ASSERT_EQUALS(clonedAxis->unit()->unitID(), "TOF")
TS_ASSERT(clonedAxis->isNumeric())
TS_ASSERT_EQUALS((*clonedAxis)(0, 0), 1.0)
TS_ASSERT_THROWS((*clonedAxis)(0, 1), const std::range_error &)
}
void testCloneDifferentLength() {
std::unique_ptr<Axis> newRefAxis(m_refAxis->clone(5, m_space2.get()));
TS_ASSERT_DIFFERS(newRefAxis.get(), m_refAxis.get());
TS_ASSERT(newRefAxis->isNumeric());
TS_ASSERT_EQUALS(newRefAxis->title(), "test axis");
TS_ASSERT_EQUALS(newRefAxis->unit()->unitID(), "TOF");
TS_ASSERT_EQUALS(newRefAxis->length(), 5);
m_space2->dataX(0)[1] = 9.9;
TS_ASSERT_EQUALS((*newRefAxis)(1), 9.9);
}
void testOperatorBrackets() {
TS_ASSERT_EQUALS((*m_refAxis)(4, 4), 24.1)
TS_ASSERT_EQUALS((*m_refAxis)(0, 2), 10.1)
TS_ASSERT_EQUALS((*m_refAxis)(2, 0), 2.1)
TS_ASSERT_THROWS((*m_refAxis)(-1, 0), const Exception::IndexError &)
TS_ASSERT_THROWS((*m_refAxis)(5, 0), const Exception::IndexError &)
TS_ASSERT_THROWS((*m_refAxis)(0, -1), const std::range_error &)
TS_ASSERT_THROWS((*m_refAxis)(0, 5), const std::range_error &)
}
void testSetValue() { TS_ASSERT_THROWS(m_refAxis->setValue(0, 9.9), const std::domain_error &) }
void testGetMin() {
std::unique_ptr<Axis> newRefAxis(m_refAxis->clone(5, m_space2.get()));
TS_ASSERT_THROWS(newRefAxis->getMin(), const std::runtime_error &)
}
void testGetMax() {
std::unique_ptr<Axis> newRefAxis(m_refAxis->clone(5, m_space2.get()));
TS_ASSERT_THROWS(newRefAxis->getMax(), const std::runtime_error &)
}
private:
std::unique_ptr<MatrixWorkspace> m_space, m_space2;
std::unique_ptr<RefAxis> m_refAxis;
};