From 10f8c12d8c0e15b7fd6dce188450e50fab5938fb Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Thu, 13 Feb 2014 12:17:37 +0000 Subject: [PATCH] Refs #8958 Written unit test SaveANSTO now has a basic unit test. Still needs a few more to push it a bit. --- .../DataHandling/test/SaveANSTOTest.h | 207 +++++++++++++++++- 1 file changed, 204 insertions(+), 3 deletions(-) diff --git a/Code/Mantid/Framework/DataHandling/test/SaveANSTOTest.h b/Code/Mantid/Framework/DataHandling/test/SaveANSTOTest.h index 5b63d793ed89..b0c24acd73c8 100644 --- a/Code/Mantid/Framework/DataHandling/test/SaveANSTOTest.h +++ b/Code/Mantid/Framework/DataHandling/test/SaveANSTOTest.h @@ -4,8 +4,9 @@ #include #include "MantidDataHandling/SaveANSTO.h" #include "MantidDataObjects/Workspace2D.h" -#include "MantidAPI/FrameworkManager.h" +#include "MantidAPI/AlgorithmManager.h" #include +#include #include using namespace Mantid::API; @@ -22,16 +23,216 @@ class SaveANSTOTest : public CxxTest::TestSuite SaveANSTOTest() { - + m_filename = "SaveANSTOTestFile.txt"; + m_name = "SaveANSTOWS"; + for (int i = 1; i < 11; ++i) + { + //X, Y and E get [1,2,3,4,5,6,7,8,9,10] + //0 gets [0,0,0,0,0,0,0,0,0,0] and is used to make sure there is no problem with divide by zero + m_dataX.push_back(i); + m_dataY.push_back(i); + m_dataE.push_back(i); + m_data0.push_back(0); + } } ~SaveANSTOTest() { - FrameworkManager::Instance().deleteWorkspace("SaveANSTOWS"); } void testExec() { + //create a new workspace and then delete it later on + Mantid::API::IAlgorithm_sptr makews = Mantid::API::AlgorithmManager::Instance().create("CreateWorkspace",1); + makews->setProperty("OutputWorkspace", m_name); + + makews->setProperty< std::vector >("DataX", m_dataX); + makews->setProperty< std::vector >("DataY", m_dataY); + makews->setProperty< std::vector >("DataE", m_dataE); + // execute the algorithm + makews->execute(); + if ( ! makews->isExecuted() ) + { + TS_FAIL("Could not create workspace"); + } + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveANSTO"); + alg->initialize(); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveANSTO"); + } + std::string filename = alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(filename).exists() ); + std::ifstream in(filename.c_str()); + double readX, readY, readE, readDQ; + // Test that the first few column headers, separator and first two bins are as expected + in >> readX >> readY >> readE >> readDQ; + TS_ASSERT_EQUALS(readX, 1.5); + TS_ASSERT_EQUALS(readY, 1); + TS_ASSERT_EQUALS(readE, 1); + TS_ASSERT_EQUALS(readDQ, 0.6); + std::string fullline; + getline(in,fullline); + getline(in,fullline); + std::list columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + in.close(); + + Poco::File(filename).remove(); + AnalysisDataService::Instance().remove(m_name); + } + void testNoX() + { + //create a new workspace and then delete it later on + Mantid::API::IAlgorithm_sptr makews = Mantid::API::AlgorithmManager::Instance().create("CreateWorkspace",1); + makews->setProperty("OutputWorkspace", m_name); + + makews->setProperty< std::vector >("DataX", m_data0); + makews->setProperty< std::vector >("DataY", m_dataY); + makews->setProperty< std::vector >("DataE", m_dataE); + // execute the algorithm + makews->execute(); + if ( ! makews->isExecuted() ) + { + TS_FAIL("Could not create workspace"); + } + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveANSTO"); + alg->initialize(); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveANSTO"); + } + std::string filename = alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(filename).exists() ); + std::ifstream in(filename.c_str()); + double readX, readY, readE, readDQ; + // Test that the first few column headers, separator and first two bins are as expected + in >> readX >> readY >> readE >> readDQ; + TS_ASSERT_EQUALS(readX, 0); + TS_ASSERT_EQUALS(readY, 1); + TS_ASSERT_EQUALS(readE, 1); + TS_ASSERT_EQUALS(readDQ, -1); + std::string fullline; + getline(in,fullline); + getline(in,fullline); + std::list columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + in.close(); + + Poco::File(filename).remove(); + AnalysisDataService::Instance().remove(m_name); + } + void testNoY() + { + //create a new workspace and then delete it later on + Mantid::API::IAlgorithm_sptr makews = Mantid::API::AlgorithmManager::Instance().create("CreateWorkspace",1); + makews->setProperty("OutputWorkspace", m_name); + + makews->setProperty< std::vector >("DataX", m_dataX); + makews->setProperty< std::vector >("DataY", m_data0); + makews->setProperty< std::vector >("DataE", m_dataE); + // execute the algorithm + makews->execute(); + if ( ! makews->isExecuted() ) + { + TS_FAIL("Could not create workspace"); + } + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveANSTO"); + alg->initialize(); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveANSTO"); + } + std::string filename = alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(filename).exists() ); + std::ifstream in(filename.c_str()); + double readX, readY, readE, readDQ; + // Test that the first few column headers, separator and first two bins are as expected + in >> readX >> readY >> readE >> readDQ; + TS_ASSERT_EQUALS(readX, 1.5); + TS_ASSERT_EQUALS(readY, 0); + TS_ASSERT_EQUALS(readE, 1); + TS_ASSERT_EQUALS(readDQ, 0.6); + std::string fullline; + getline(in,fullline); + getline(in,fullline); + std::list columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + in.close(); + + Poco::File(filename).remove(); + AnalysisDataService::Instance().remove(m_name); + } + void testNoE() + { + //create a new workspace and then delete it later on + Mantid::API::IAlgorithm_sptr makews = Mantid::API::AlgorithmManager::Instance().create("CreateWorkspace",1); + makews->setProperty("OutputWorkspace", m_name); + + makews->setProperty< std::vector >("DataX", m_dataX); + makews->setProperty< std::vector >("DataY", m_dataY); + makews->setProperty< std::vector >("DataE", m_data0); + // execute the algorithm + makews->execute(); + if ( ! makews->isExecuted() ) + { + TS_FAIL("Could not create workspace"); + } + + Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SaveANSTO"); + alg->initialize(); + alg->setPropertyValue("InputWorkspace", m_name); + alg->setPropertyValue("Filename", m_filename); + TS_ASSERT_THROWS_NOTHING(alg->execute()); + + if ( ! alg->isExecuted() ) + { + TS_FAIL("Could not run SaveANSTO"); + } + std::string filename = alg->getPropertyValue("Filename"); + // has the algorithm written a file to disk? + TS_ASSERT( Poco::File(filename).exists() ); + std::ifstream in(filename.c_str()); + double readX, readY, readE, readDQ; + // Test that the first few column headers, separator and first two bins are as expected + in >> readX >> readY >> readE >> readDQ; + TS_ASSERT_EQUALS(readX, 1.5); + TS_ASSERT_EQUALS(readY, 1); + TS_ASSERT_EQUALS(readE, 0); + TS_ASSERT_EQUALS(readDQ, 0.6); + std::string fullline; + getline(in,fullline); + getline(in,fullline); + std::list columns; + boost::split(columns, fullline, boost::is_any_of("\t"), boost::token_compress_on); + TS_ASSERT_EQUALS(columns.size(),4); + in.close(); + + Poco::File(filename).remove(); + AnalysisDataService::Instance().remove(m_name); } + std::string m_filename, m_name; + std::vector m_dataX, m_dataY, m_dataE, m_data0; };