forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkspaceIteratorTest.h
160 lines (142 loc) · 6.09 KB
/
WorkspaceIteratorTest.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
// 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 <algorithm>
#include <cxxtest/TestSuite.h>
#include <memory>
#include "MantidAPI/LocatedDataRef.h"
#include "MantidAPI/WorkspaceIterator.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceSingleValue.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
using Mantid::MantidVec;
using namespace Mantid::DataObjects;
using namespace Mantid::API;
template <typename T> class FibSeries {
private:
T x1; /// Initial value 1;
T x2; /// Initial value 2;
public:
FibSeries() : x1(1), x2(1) {}
T operator()() {
const T out(x1 + x2);
x1 = x2;
x2 = out;
return out;
}
};
class WorkspaceIteratorTest : public CxxTest::TestSuite {
private:
using parray = std::shared_ptr<MantidVec>;
using W2D = std::shared_ptr<Workspace2D>;
using WSV = std::shared_ptr<WorkspaceSingleValue>;
using Wbase = std::shared_ptr<MatrixWorkspace>;
public:
void testIteratorWorkspace2DAsBase() {
int size = 57;
int histogramCount = 100;
Wbase workspace = WorkspaceCreationHelper::Create2DWorkspace(histogramCount, size);
// workspace->dataX(0) // this is the first spectrum in the workspace with
// real data
int count = 0;
for (MatrixWorkspace::const_iterator ti(*workspace); ti != ti.end(); ++ti) {
TS_ASSERT_THROWS_NOTHING(LocatedDataRef tr = *ti; int datablock = count / size; int blockindex = count % size;
TS_ASSERT_EQUALS(tr.X(), workspace->dataX(datablock)[blockindex]);
TS_ASSERT_EQUALS(tr.Y(), workspace->dataY(datablock)[blockindex]);
TS_ASSERT_EQUALS(tr.E(), workspace->dataE(datablock)[blockindex]);)
count++;
}
TS_ASSERT_EQUALS(count, size * histogramCount);
}
void testHorizontalLoopIteratorWorkspace2D() {
int size = 57;
int histogramCount = 100;
Wbase workspace = WorkspaceCreationHelper::Create2DWorkspace(histogramCount, size);
const int loopCountArrayLength = 4;
int loopCountArray[loopCountArrayLength];
loopCountArray[0] = 1;
loopCountArray[1] = 2;
loopCountArray[2] = 3;
loopCountArray[3] = 0;
for (int i = 0; i < loopCountArrayLength; i++) {
int loopCount = loopCountArray[i];
int count = 0;
for (MatrixWorkspace::const_iterator ti(*workspace, loopCount); ti != ti.end(); ++ti) {
TS_ASSERT_THROWS_NOTHING(LocatedDataRef tr = *ti; int indexPosition = count % (size * histogramCount);
int datablock = indexPosition / size; int blockindex = indexPosition % size;
TS_ASSERT_EQUALS(tr.X(), workspace->dataX(datablock)[blockindex]);
TS_ASSERT_EQUALS(tr.Y(), workspace->dataY(datablock)[blockindex]);
TS_ASSERT_EQUALS(tr.E(), workspace->dataE(datablock)[blockindex]);)
count++;
}
TS_ASSERT_EQUALS(count, size * histogramCount * loopCount);
}
}
void testVerticalLoopIteratorWorkspace2D() {
int size = 50;
int histogramCount = 100;
Wbase workspace = WorkspaceCreationHelper::Create2DWorkspace(histogramCount, size);
const int loopCountArrayLength = 4;
int loopCountArray[loopCountArrayLength];
loopCountArray[0] = 1;
loopCountArray[1] = 2;
loopCountArray[2] = 3;
loopCountArray[3] = 0;
for (int i = 0; i < loopCountArrayLength; i++) {
int loopCount = loopCountArray[i];
int count = 0;
for (MatrixWorkspace::const_iterator ti(*workspace, loopCount, LoopOrientation::Vertical); ti != ti.end(); ++ti) {
// TS_ASSERT_THROWS_NOTHING
//(
// //LocatedDataRef tr = *ti;
// //int datablock = count/(size*loopCount);
// //int blockindex = count/loopCount;
// //TS_ASSERT_EQUALS(tr.X(),workspace->dataX(datablock)[blockindex]);
// //TS_ASSERT_EQUALS(tr.Y(),workspace->dataY(datablock)[blockindex]);
// //TS_ASSERT_EQUALS(tr.E(),workspace->dataE(datablock)[blockindex]);
//)
count++;
}
TS_ASSERT_EQUALS(count, size * histogramCount * loopCount);
}
}
void testIteratorWorkspaceSingleValueLength() {
Wbase workspace = WSV(new WorkspaceSingleValue(1.1, 2.2));
int count = 0;
for (MatrixWorkspace::const_iterator ti(*workspace); ti != ti.end(); ++ti) {
TS_ASSERT_THROWS_NOTHING(LocatedDataRef tr = *ti; TS_ASSERT_EQUALS(tr.X(), workspace->dataX(0)[count]);
TS_ASSERT_EQUALS(tr.Y(), workspace->dataY(0)[count]);
TS_ASSERT_EQUALS(tr.E(), workspace->dataE(0)[count]);)
count++;
}
TS_ASSERT_EQUALS(count, 1);
}
void testHorizontalLoopIteratorWorkspaceSingleValue() {
int size = 1;
int histogramCount = 1;
Wbase workspace = WSV(new WorkspaceSingleValue(1.4, 2.4));
const int loopCountArrayLength = 4;
int loopCountArray[loopCountArrayLength];
loopCountArray[0] = 1;
loopCountArray[1] = 50;
loopCountArray[2] = 200;
loopCountArray[3] = 0;
for (int i = 0; i < loopCountArrayLength; i++) {
int loopCount = loopCountArray[i];
int count = 0;
for (MatrixWorkspace::const_iterator ti(*workspace, loopCount); ti != ti.end(); ++ti) {
TS_ASSERT_THROWS_NOTHING(LocatedDataRef tr = *ti; int indexPosition = count % (size * histogramCount);
int datablock = indexPosition / size; int blockindex = indexPosition % size;
TS_ASSERT_EQUALS(tr.X(), workspace->dataX(datablock)[blockindex]);
TS_ASSERT_EQUALS(tr.Y(), workspace->dataY(datablock)[blockindex]);
TS_ASSERT_EQUALS(tr.E(), workspace->dataE(datablock)[blockindex]);)
count++;
}
TS_ASSERT_EQUALS(count, size * histogramCount * loopCount);
}
}
};