-
Notifications
You must be signed in to change notification settings - Fork 122
/
SaveReflTBLTest.h
264 lines (214 loc) · 9.09 KB
/
SaveReflTBLTest.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#ifndef SAVEREFLTBLTEST_H_
#define SAVEREFLTBLTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidDataHandling/SaveReflTBL.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/TableRow.h"
#include <fstream>
#include <Poco/File.h>
using namespace Mantid::API;
using namespace Mantid::DataHandling;
using namespace Mantid::DataObjects;
class SaveReflTBLTest : public CxxTest::TestSuite
{
public:
static SaveReflTBLTest *createSuite() { return new SaveReflTBLTest(); }
static void destroySuite(SaveReflTBLTest *suite) { delete suite; }
SaveReflTBLTest(): m_name("SaveReflTBLTestWS"), m_filename("SaveReflTBLTest.tbl"), m_abspath()
{
}
~SaveReflTBLTest()
{
}
void testNoQuotes()
{
ITableWorkspace_sptr ws = CreateWorkspace();
Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL");
alg->setRethrows(true);
alg->setPropertyValue("InputWorkspace", m_name);
alg->setPropertyValue("Filename", m_filename);
m_abspath = alg->getPropertyValue("Filename"); //Get absolute path
TS_ASSERT_THROWS_NOTHING(alg->execute());
if ( ! alg->isExecuted() )
{
TS_FAIL("Could not run SaveReflTBL");
}
TS_ASSERT( Poco::File(m_abspath).exists() );
std::ifstream file(m_abspath.c_str());
std::string line = "";
getline(file,line);
TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,13470,2.3,13463,0.035,0.3,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,,,,,,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,13462,2.3,13463,0.035,0.3,,,,,,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,,,,,,,,,,,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,,,,,,,,,,,0.04,2");
file.close();
cleanupafterwards();
}
void testQuotes()
{
ITableWorkspace_sptr ws = CreateWorkspace();
TableRow row = ws->appendRow();
row << "13460" << "0.7" << "13463,13464" << "0.01" << "0.06" << "0.04" << "2" << 4;
row = ws->appendRow();
row << "13470" << "2.3" << "13463,13464" << "0.035" << "0.3" << "0.04" << "2" << 5;
Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL");
alg->setRethrows(true);
alg->setPropertyValue("InputWorkspace", m_name);
alg->setPropertyValue("Filename", m_filename);
m_abspath = alg->getPropertyValue("Filename"); //Get absolute path
TS_ASSERT_THROWS_NOTHING(alg->execute());
if ( ! alg->isExecuted() )
{
TS_FAIL("Could not run SaveReflTBL");
}
TS_ASSERT( Poco::File(m_abspath).exists() );
std::ifstream file(m_abspath.c_str());
std::string line = "";
getline(file,line);
TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,13470,2.3,13463,0.035,0.3,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,13462,2.3,13463,0.035,0.3,,,,,,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,13462,2.3,13463,0.035,0.3,,,,,,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13470,2.3,13463,0.035,0.3,13460,0.7,\"13463,13464\",0.01,0.06,,,,,,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13470,2.3,\"13463,13464\",0.035,0.3,,,,,,,,,,,0.04,2");
getline(file,line);
TS_ASSERT_EQUALS(line,"13460,0.7,13463,0.01,0.06,,,,,,,,,,,0.04,2");
file.close();
cleanupafterwards();
}
void testFourGroupFail()
{
ITableWorkspace_sptr ws = CreateWorkspace();
TableRow row = ws->appendRow();
row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 1;
Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL");
alg->setRethrows(true);
alg->setPropertyValue("InputWorkspace", m_name);
alg->setPropertyValue("Filename", m_filename);
m_abspath = alg->getPropertyValue("Filename"); //Get absolute path
TS_ASSERT_THROWS_ANYTHING(alg->execute());
// the algorithm shouldn't have written a file to disk
TS_ASSERT( !Poco::File(m_abspath).exists() );
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name));
}
void testNotEnoughColumns()
{
ITableWorkspace_sptr ws = WorkspaceFactory::Instance().createTable();
AnalysisDataService::Instance().addOrReplace(m_name, ws);
auto colRuns = ws->addColumn("str","Run(s)");
auto colTheta = ws->addColumn("str","ThetaIn");
auto colTrans = ws->addColumn("str","TransRun(s)");
auto colQmin = ws->addColumn("str","Qmin");
auto colQmax = ws->addColumn("str","Qmax");
auto colDqq = ws->addColumn("str","dq/q");
auto colScale = ws->addColumn("str","Scale");
colRuns->setPlotType(0);
colTheta->setPlotType(0);
colTrans->setPlotType(0);
colQmin->setPlotType(0);
colQmax->setPlotType(0);
colDqq->setPlotType(0);
colScale->setPlotType(0);
TableRow row = ws->appendRow();
row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2";
row = ws->appendRow();
row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2";
row = ws->appendRow();
row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2";
Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL");
alg->setRethrows(true);
alg->setPropertyValue("InputWorkspace", m_name);
alg->setPropertyValue("Filename", m_filename);
m_abspath = alg->getPropertyValue("Filename"); //Get absolute path
TS_ASSERT_THROWS_ANYTHING(alg->execute());
// the algorithm shouldn't have written a file to disk
TS_ASSERT( !Poco::File(m_abspath).exists() );
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name));
}
void testLoadWithLoadReflTBL()
{
ITableWorkspace_sptr ws = CreateWorkspace();
Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveReflTBL");
alg->setRethrows(true);
alg->setPropertyValue("InputWorkspace", m_name);
alg->setPropertyValue("Filename", m_filename);
m_abspath = alg->getPropertyValue("Filename"); //Get absolute path
TS_ASSERT_THROWS_NOTHING(alg->execute());
if ( ! alg->isExecuted() )
{
TS_FAIL("Could not run SaveReflTBL");
}
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name));
Mantid::API::IAlgorithm_sptr algLoad = Mantid::API::AlgorithmManager::Instance().create("LoadReflTBL");
algLoad->setRethrows(true);
algLoad->setPropertyValue("OutputWorkspace", m_name);
algLoad->setPropertyValue("Filename", m_abspath);
TS_ASSERT_THROWS_NOTHING(algLoad->execute());
if ( ! alg->isExecuted() )
{
TS_FAIL("Could not run LoadReflTBL");
}
cleanupafterwards();
}
private:
void cleanupafterwards()
{
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_name));
TS_ASSERT_THROWS_NOTHING(Poco::File(m_abspath).remove());
}
ITableWorkspace_sptr CreateWorkspace()
{
ITableWorkspace_sptr ws = WorkspaceFactory::Instance().createTable();
AnalysisDataService::Instance().addOrReplace(m_name, ws);
auto colRuns = ws->addColumn("str","Run(s)");
auto colTheta = ws->addColumn("str","ThetaIn");
auto colTrans = ws->addColumn("str","TransRun(s)");
auto colQmin = ws->addColumn("str","Qmin");
auto colQmax = ws->addColumn("str","Qmax");
auto colDqq = ws->addColumn("str","dq/q");
auto colScale = ws->addColumn("str","Scale");
auto colStitch = ws->addColumn("int","StitchGroup");
colRuns->setPlotType(0);
colTheta->setPlotType(0);
colTrans->setPlotType(0);
colQmin->setPlotType(0);
colQmax->setPlotType(0);
colDqq->setPlotType(0);
colScale->setPlotType(0);
colStitch->setPlotType(0);
TableRow row = ws->appendRow();
row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 1;
row = ws->appendRow();
row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 1;
row = ws->appendRow();
row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 1;
row = ws->appendRow();
row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 2;
row = ws->appendRow();
row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 2;
row = ws->appendRow();
row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 3;
row = ws->appendRow();
row << "13460" << "0.7" << "13463" << "0.01" << "0.06" << "0.04" << "2" << 0;
//this row's last two cells will show in the tableworkspace, but the first row in stich group 3's will take priority when saving
row = ws->appendRow();
row << "13462" << "2.3" << "13463" << "0.035" << "0.3" << "0.4" << "3" << 3;
row = ws->appendRow();
row << "13470" << "2.3" << "13463" << "0.035" << "0.3" << "0.04" << "2" << 4;
return ws;
}
std::string m_name;
std::string m_filename;
std::string m_abspath;
};
#endif /*SAVEREFLTBLTEST_H_*/