Newer
Older
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef MANTID_DATAHANDLING_SAVERMCPROFILETEST_H_
#define MANTID_DATAHANDLING_SAVERMCPROFILETEST_H_
#include <Poco/File.h>
#include <cxxtest/TestSuite.h>
#include <fstream>
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidDataHandling/LoadNexusProcessed.h"
#include "MantidDataHandling/SaveRMCProfile.h"
using Mantid::DataHandling::LoadNexusProcessed;
using Mantid::DataHandling::SaveRMCProfile;
using namespace Mantid::API;
class SaveRMCProfileTest : 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 SaveRMCProfileTest *createSuite() { return new SaveRMCProfileTest(); }
static void destroySuite(SaveRMCProfileTest *suite) { delete suite; }
void test_Init() {
SaveRMCProfile alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
}
size_t read(std::istream &is, std::vector<char> &buff) {
is.read(&buff[0], buff.size());
return is.gcount();
}
size_t countEOL(const std::vector<char> &buff, size_t sz) {
size_t newlines = 0;
const char *p = &buff[0];
for (size_t i = 0; i < sz; i++) {
if (p[i] == '\n') {
newlines++;
}
}
return newlines;
}
size_t countLines(const std::string &filename) {
const size_t BUFFER_SIZE = 1024 * 1024;
std::vector<char> buffer(BUFFER_SIZE);
std::ifstream in(filename.c_str());
size_t n = 0;
while (size_t cc = read(in, buffer)) {
n += countEOL(buffer, cc);
}
return n;
}
bool loadWorkspace(const std::string &filename, const std::string wsName) {
LoadNexusProcessed load;
load.initialize();
load.setProperty("Filename", filename);
load.setProperty("OutputWorkspace", wsName);
load.execute();
return load.isExecuted();
}
void test_exec() {
// name of workspace to create and save
const std::string wsName("SaveRMCProfileTest_OutputWS");
// name of the output file
const std::string outFilename("SaveRMCProfileTest_Output.fq");
// Load a file to save out
TS_ASSERT(loadWorkspace("nom_gr.nxs", wsName));
// save the file
SaveRMCProfile alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspace", wsName));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputType", "S(Q)"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Title", "nom_gr"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Filename", outFilename));
TS_ASSERT_THROWS_NOTHING(alg.execute(););
TS_ASSERT(alg.isExecuted());
// do the checks
Poco::File outFile(outFilename);
TS_ASSERT(outFile.isFile());
TS_ASSERT_EQUALS(countLines(outFilename), 1006);
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(wsName);
// remove the output file
outFile.remove(false);
}
void test_exec_ws_group() {
// Create a group
const std::string groupName("SaveRMCProfileGroup");
TS_ASSERT(loadWorkspace("nom_gr.nxs", groupName + "_1"));
TS_ASSERT(loadWorkspace("nom_gr.nxs", groupName + "_2"));
auto grpAlg =
AlgorithmManager::Instance().createUnmanaged("GroupWorkspaces");
grpAlg->initialize();
grpAlg->setPropertyValue("InputWorkspaces",
groupName + "_1," + groupName + "_2");
grpAlg->setPropertyValue("OutputWorkspace", groupName);
grpAlg->execute();
// name of the output file
const std::string outFilename("SaveRMCProfileGroup.gr");
// run the algorithm with a group
SaveRMCProfile alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspace", groupName));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("Filename", "SaveRMCProfileGroup.gr"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
Poco::File outFile(outFilename);
TS_ASSERT(outFile.isFile());
TS_ASSERT_EQUALS(countLines(outFilename), 1006);
// remove the workspace group
AnalysisDataService::Instance().deepRemoveGroup(groupName);
}
};
#endif /* MANTID_DATAHANDLING_SAVERMCPROFILETEST_H_ */