Skip to content

Commit

Permalink
Refs #8960 Unit test and exception catching
Browse files Browse the repository at this point in the history
The ILLCosmos algorthim will no longer throw if the workspace is missing one or more logs, it will just not print that heading instead.

The test algorithm has been adapted from SaveANSTOAscii (and could also probably be superclassed) and now checks the headings too.
  • Loading branch information
keithnbrown committed Mar 14, 2014
1 parent 66f5659 commit fb43326
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 25 deletions.
48 changes: 41 additions & 7 deletions Code/Mantid/Framework/DataHandling/src/SaveILLCosmosAscii.cpp
Expand Up @@ -39,16 +39,50 @@ namespace Mantid

void SaveILLCosmosAscii::extraHeaders(std::ofstream & file)
{
auto samp = m_ws->run();
std::string instrument;
std::string user;
std::string title;
std::string subtitle;
std::string startDT;
std::string endDT;
try
{instrument = m_ws->getInstrument()->getName();}
catch (...)
{instrument = "";}

try
{user = getProperty("UserContact");}
catch (...)
{user = "";}

try
{title = getProperty("Title");}
catch (...)
{title = "";}

try
{subtitle = samp.getLogData("run_title")->value();}
catch (...)
{subtitle = "";}

try
{startDT = samp.getLogData("run_start")->value();}
catch (...)
{startDT = "";}

try
{endDT = samp.getLogData("run_end")->value();}
catch (...)
{endDT = "";}

file << "MFT" << std::endl;
file << "Instrument: "<< m_ws->getInstrument()->getName() << std::endl;
std::string user = getProperty("UserContact");
file << "Instrument: "<< instrument << std::endl;
file << "User-local contact: " << user << std::endl; //add optional property
std::string title = getProperty("Title");
file << "Title: " << title << std::endl;
auto samp = m_ws->run();
file << "Subtitle: " << samp.getLogData("run_title")->value() << std::endl;
file << "Start date + time: " << samp.getLogData("run_start")->value() << std::endl;
file << "End date + time: " << samp.getLogData("run_end")->value() << std::endl;
file << "Subtitle: " << subtitle << std::endl;
file << "Start date + time: " << startDT << std::endl;
file << "End date + time: " << endDT << std::endl;

const std::vector<std::string> logList = getProperty("LogList");
///logs
Expand Down
73 changes: 55 additions & 18 deletions Code/Mantid/Framework/DataHandling/test/SaveILLCosmosAsciiTest.h
Expand Up @@ -59,14 +59,18 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite
TS_ASSERT( Poco::File(m_long_filename).exists() );
std::ifstream in(m_long_filename.c_str());
std::string fullline;
headingsTests(in, fullline);
getline(in,fullline);

std::vector<std::string> columns;
boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on);
TS_ASSERT_EQUALS(columns.size(),4);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(0)), 1.5, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 1, 0.01);
TS_ASSERT_EQUALS(columns.size(),5);
//the first is black due to the leading tab
TS_ASSERT(columns.at(0) == "");
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 1.5, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(2)), 1, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(3)), 0.6, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(3)), 1, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(4)), 0.6, 0.01);
in.close();

cleanupafterwards();
Expand All @@ -90,14 +94,17 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite
TS_ASSERT( Poco::File(m_long_filename).exists() );
std::ifstream in(m_long_filename.c_str());
std::string fullline;
headingsTests(in, fullline);
getline(in,fullline);
std::vector<std::string> columns;
boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on);
TS_ASSERT_EQUALS(columns.size(),4);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(0)), 0, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 1, 0.01);
TS_ASSERT_EQUALS(columns.size(),5);
//the first is black due to the leading tab
TS_ASSERT(columns.at(0) == "");
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 0, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(2)), 1, 0.01);
TS_ASSERT((columns.at(3) == "nan") || (columns.at(3) == "inf"));
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(3)), 1, 0.01);
TS_ASSERT((columns.at(4) == "nan") || (columns.at(4) == "inf"));
in.close();

cleanupafterwards();
Expand All @@ -121,14 +128,17 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite
TS_ASSERT( Poco::File(m_long_filename).exists() );
std::ifstream in(m_long_filename.c_str());
std::string fullline;
headingsTests(in, fullline);
getline(in,fullline);
std::vector<std::string> columns;
boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on);
TS_ASSERT_EQUALS(columns.size(),4);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(0)), 1.5, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 0, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(2)), 1, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(3)), 0.6, 0.01);
TS_ASSERT_EQUALS(columns.size(),5);
//the first is black due to the leading tab
TS_ASSERT(columns.at(0) == "");
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 1.5, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(2)), 0, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(3)), 1, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(4)), 0.6, 0.01);
in.close();

cleanupafterwards();
Expand All @@ -152,14 +162,17 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite
TS_ASSERT( Poco::File(m_long_filename).exists() );
std::ifstream in(m_long_filename.c_str());
std::string fullline;
headingsTests(in, fullline);
getline(in,fullline);
std::vector<std::string> columns;
boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on);
TS_ASSERT_EQUALS(columns.size(),4);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(0)), 1.5, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 1, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(2)), 0, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(3)), 0.6, 0.01);
TS_ASSERT_EQUALS(columns.size(),5);
//the first is black due to the leading tab
TS_ASSERT(columns.at(0) == "");
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(1)), 1.5, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(2)), 1, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(3)), 0, 0.01);
TS_ASSERT_DELTA(boost::lexical_cast<double>(columns.at(4)), 0.6, 0.01);
in.close();

cleanupafterwards();
Expand All @@ -179,6 +192,30 @@ class SaveILLCosmosAsciiTest : public CxxTest::TestSuite
TS_ASSERT( !Poco::File(m_long_filename).exists() );
}
private:
void headingsTests(std::ifstream & in,std::string & fullline)
{
getline(in,fullline);
TS_ASSERT(fullline == "MFT");
getline(in,fullline);
TS_ASSERT(fullline == "Instrument: ");
getline(in,fullline);
TS_ASSERT(fullline == "User-local contact: ");
getline(in,fullline);
TS_ASSERT(fullline == "Title: ");
getline(in,fullline);
TS_ASSERT(fullline == "Subtitle: ");
getline(in,fullline);
TS_ASSERT(fullline == "Start date + time: ");
getline(in,fullline);
TS_ASSERT(fullline == "End date + time: ");
getline(in,fullline);
TS_ASSERT(fullline == "Number of file format: 2");
getline(in,fullline);
TS_ASSERT(fullline == "Number of data points:\t9");
getline(in,fullline);
getline(in,fullline);
TS_ASSERT(fullline == "\tq\trefl\trefl_err\tq_res");
}
void createWS(bool zeroX = false, bool zeroY = false, bool zeroE = false)
{
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace(1,10);
Expand Down

0 comments on commit fb43326

Please sign in to comment.