Skip to content
Snippets Groups Projects
LoadTBLTest.h 17.5 KiB
Newer Older
#ifndef LOADTBLTEST_H_
#define LOADTBLTEST_H_

#include "cxxtest/TestSuite.h"
#include "MantidDataHandling/LoadTBL.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/TableRow.h"
#include <Poco/File.h>
#include <fstream>

using namespace Mantid::API;
using namespace Mantid::DataHandling;
using namespace Mantid::Kernel;
using namespace Mantid::DataObjects;

class LoadTBLTest : public CxxTest::TestSuite {
  static LoadTBLTest *createSuite() { return new LoadTBLTest(); }
  static void destroySuite(LoadTBLTest *suite) { delete suite; }
  LoadTBLTest()
      : m_filename("LoadTBLTest.tbl"), m_wsName("LoadTBLTestWS"), m_abspath() {}
  ~LoadTBLTest() override {}
  void testFileNoQuotes() {
    // create a file with each line containing different but valid data format
    std::ofstream file(m_filename.c_str());
    file << "13460,0.7,13463,0.01,0.06,,,,,,,,,,,0.04,2" << std::endl;
    file << "13469,0.7,13463,0.01,0.06,13470,2.3,13463,0.035,0.3,,,,,,0.04,2"
         << std::endl;
    file << "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" << std::endl;
    file << "13460,0.7,13463,0.01,0.06,,,,,,13470,2.3,13463,0.035,0.3,0.04,2"
         << std::endl;
    file << ",,,,,13470,2.3,13463,0.035,0.3,,,,,,0.04,2" << std::endl;
    file << ",,,,,,,,,,13462,2.3,13463,0.035,0.3,0.04,2" << std::endl;
    file.close();
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS_NOTHING(alg->execute());

    TS_ASSERT(alg->isExecuted());

    TS_ASSERT_EQUALS(AnalysisDataService::Instance().doesExist(m_wsName), true);
    Workspace_sptr output;
    TS_ASSERT_THROWS_NOTHING(
        output = AnalysisDataService::Instance().retrieve(m_wsName));
    TableWorkspace_sptr outputWS =
        boost::dynamic_pointer_cast<TableWorkspace>(output);
    TS_ASSERT_EQUALS(outputWS->columnCount(), 9);
    TS_ASSERT_EQUALS(outputWS->rowCount(), 10);

    // test the first three rows, equivalent to the first two rows of the file.
    TableRow row = outputWS->getRow(0);
    TS_ASSERT_EQUALS(row.cell<std::string>(0), "13460");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(1)), 0.7,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<std::string>(2), "13463");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(3)), 0.01,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(4)), 0.06,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(5)), 0.04,
                    0.001);
Raquel Alvarez's avatar
Raquel Alvarez committed
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(6)), 2,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<int>(7), 1);

    row = outputWS->getRow(1);
    TS_ASSERT_EQUALS(row.cell<std::string>(0), "13469");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(1)), 0.7,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<std::string>(2), "13463");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(3)), 0.01,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(4)), 0.06,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(5)), 0.04,
                    0.001);
Raquel Alvarez's avatar
Raquel Alvarez committed
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(6)), 2,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<int>(7), 2);

    row = outputWS->getRow(2);
    TS_ASSERT_EQUALS(row.cell<std::string>(0), "13470");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(1)), 2.3,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<std::string>(2), "13463");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(3)),
                    0.035, 0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(4)), 0.3,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(5)), 0.04,
                    0.001);
Raquel Alvarez's avatar
Raquel Alvarez committed
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(6)), 2,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<int>(7), 2);

    cleanupafterwards();
  void testQuotedFile() {
    // create a file with each line containing different but valid data format
    std::ofstream file(m_filename.c_str());
    file << "13460,0.7,\"13463,13464\",0.01,0.06,,,,,,,,,,,0.04,2" << std::endl;
    file << "13469,0.7,\"13463,13464\",0.01,0.06,13470,2.3,\"13463,13464\",0."
            "035,0.3,,,,,,0.04,2" << std::endl;
    file << "13460,0.7,\"13463,13464\",0.01,0.06,13462,2.3,\"13463,13464\",0."
            "035,0.3,13470,2.3,\"13463,13464\",0.035,0.3,0.04,2" << std::endl;
    file << "13460,0.7,\"13463,13464\",0.01,0.06,,,,,,13470,2.3,\"13463,"
            "13464\",0.035,0.3,0.04,2" << std::endl;
    file << ",,,,,13470,2.3,\"13463,13464\",0.035,0.3,,,,,,0.04,2" << std::endl;
    file << ",,,,,,,,,,13462,2.3,\"13463,13464\",0.035,0.3,0.04,2" << std::endl;
    file.close();

    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS_NOTHING(alg->execute());

    TS_ASSERT(alg->isExecuted());
    TS_ASSERT_EQUALS(AnalysisDataService::Instance().doesExist(m_wsName), true);
    Workspace_sptr output;
    TS_ASSERT_THROWS_NOTHING(
        output = AnalysisDataService::Instance().retrieve(m_wsName));
    TableWorkspace_sptr outputWS =
        boost::dynamic_pointer_cast<TableWorkspace>(output);
    TS_ASSERT_EQUALS(outputWS->columnCount(), 9);
    TS_ASSERT_EQUALS(outputWS->rowCount(), 10);

    // test the first three rows, equivalent to the first two rows of the file.
    TableRow row = outputWS->getRow(0);
    TS_ASSERT_EQUALS(row.cell<std::string>(0), "13460");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(1)), 0.7,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<std::string>(2), "13463,13464");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(3)), 0.01,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(4)), 0.06,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(5)), 0.04,
                    0.001);
Raquel Alvarez's avatar
Raquel Alvarez committed
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(6)), 2,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<int>(7), 1);

    row = outputWS->getRow(1);
    TS_ASSERT_EQUALS(row.cell<std::string>(0), "13469");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(1)), 0.7,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<std::string>(2), "13463,13464");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(3)), 0.01,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(4)), 0.06,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(5)), 0.04,
                    0.001);
Raquel Alvarez's avatar
Raquel Alvarez committed
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(6)), 2,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<int>(7), 2);

    row = outputWS->getRow(2);
    TS_ASSERT_EQUALS(row.cell<std::string>(0), "13470");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(1)), 2.3,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<std::string>(2), "13463,13464");
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(3)),
                    0.035, 0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(4)), 0.3,
                    0.001);
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(5)), 0.04,
                    0.001);
Raquel Alvarez's avatar
Raquel Alvarez committed
    TS_ASSERT_DELTA(boost::lexical_cast<double>(row.cell<std::string>(6)), 2,
                    0.01);
    TS_ASSERT_EQUALS(row.cell<int>(7), 2);

    cleanupafterwards();
  void testFewColumns() {
    // create a file with each line containing too few columns
    std::ofstream file(m_filename.c_str());
    file << "13460,0.7,\"13463,13464\",0.01,0.06,,,,,,,0.04,2" << std::endl;
    file << "13469,0.7,\"13463,13464\",0.01,0.06,13470,2.3,\"13463,13464\",0."
            "035,0.3,,0.04,2" << std::endl;
    file << "13460,0.7,\"13463,13464\",,\"13463,13464\",,13470,2.3,\"13463,"
            "13464\",0.035,0.04,2" << std::endl;
    file << "13460,0.7,\"13463,13464\",0.01,0.06,,13470,2.3,\"13463,13464\",0."
            "035,0.3,0.04,2" << std::endl;
    file << "13470,2.3,\"13463,13464\",0.035,0.3,,0.04,2" << std::endl;
    file << ",,,,13462,2.3,\"13463,13464\",0.035,0.3,0.04,2" << std::endl;
    file.close();
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS_ANYTHING(alg->execute());

    TS_ASSERT_THROWS_NOTHING(Poco::File(m_abspath).remove());
  void testManyColumns() {
    // create a file with each line containing too many columns
    std::ofstream file(m_filename.c_str());
    file << "13460,0.7,13463,0.01,0.06,,,,,,,,,,,0.04,2,,,,0.04,2" << std::endl;
    file << "13469,0.7,13463,0.01,0.06,13470,2.3,13463,0.035,0.3,,,,,,0.04,2,,,"
            ",0.04,2" << std::endl;
    file << "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,,,,0.04,2" << std::endl;
    file << "13460,0.7,13463,0.01,0.06,,,,,,13470,2.3,13463,0.035,0.3,0.04,2,,,"
            ",0.04,2" << std::endl;
    file << ",,,,,13470,2.3,13463,0.035,0.3,,,,,,0.04,2,,,,0.04,2" << std::endl;
    file << ",,,,,,,,,13462,2.3,13463,0.035,0.3,0.04,2,,,,0.04,2" << std::endl;
    file.close();

    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS_ANYTHING(alg->execute());

    TS_ASSERT_THROWS_NOTHING(Poco::File(m_abspath).remove());
  }

  void testManyColumnsTwo() {
    // create a file with each line containing too many columns
    std::ofstream file(m_filename.c_str());
    file << "13460,0.7,\"13463,0.01\",0.06,,,,,,,,,,,0.04,2,,,,0.04,2"
         << std::endl;
    file << "13469,0.7,13463,\"0.01,0.06\",13470,2.3,13463,0.06,\"13470,0.06,"
            "13470\",2.3,13463,0.035,0.3,,,,,,,,,0.04,2,,,,0.04,2" << std::endl;
    file << "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,,,,0.04,2" << std::endl;
    file << "13460,0.7,\"13463,0.01\",0.06,,,,,,,,,,13470,2.3,\"13463,0.035\","
            "0.3,0.04,2,,,,0.04,2" << std::endl;
    file << ",,,,,13470,2.3,\"13463,0.035\",0.3,,,,,,,,,0.04,2,,,,0.04,2"
         << std::endl;
    file << ",,,,,,,,,,,,13462,2.3,\"13463,0.035\",0.3,0.04,2,,,,0.04,2"
         << std::endl;
    file.close();
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS_ANYTHING(alg->execute());

    TS_ASSERT_THROWS_NOTHING(Poco::File(m_abspath).remove());
  void testTBLWithColumnHeadingsRowAndData() {
    std::ofstream file(m_filename.c_str());
    file << "Runs,Angle,QMin,QMax,Group,Options" << std::endl
         << "14456,0.7,1.443,8.992,1," << std::endl
         << "18553,0.3,1.233,4.388,3," << std::endl;
    file.close();
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS_NOTHING(alg->execute());
    TS_ASSERT_EQUALS(AnalysisDataService::Instance().doesExist(m_wsName), true);
    Workspace_sptr output;
    TS_ASSERT_THROWS_NOTHING(
        output = AnalysisDataService::Instance().retrieve(m_wsName));
    TableWorkspace_sptr outputWS =
        boost::dynamic_pointer_cast<TableWorkspace>(output);
    std::vector<std::string> cols{"Runs", "Angle", "QMin",
                                  "QMax", "Group", "Options"};
    TS_ASSERT_EQUALS(outputWS->getColumnNames(), cols);
    TableRow firstRow = outputWS->getRow(0);
    TS_ASSERT_EQUALS(firstRow.cell<std::string>(0), "14456");
    TS_ASSERT_EQUALS(firstRow.cell<std::string>(1), "0.7");
    TS_ASSERT_EQUALS(firstRow.cell<std::string>(3), "8.992");
    TS_ASSERT_EQUALS(firstRow.cell<std::string>(5), "");
    TableRow secondRow = outputWS->getRow(1);
    std::string entryOne = secondRow.cell<std::string>(0);
    std::string entryTwo = secondRow.cell<std::string>(1);
    std::string entryFour = secondRow.cell<std::string>(3);
    std::string entrySix = secondRow.cell<std::string>(5);
    TS_ASSERT_EQUALS(secondRow.cell<std::string>(0), "18553");
    TS_ASSERT_EQUALS(secondRow.cell<std::string>(1), "0.3");
    TS_ASSERT_EQUALS(secondRow.cell<std::string>(3), "4.388");

    TS_ASSERT_THROWS_NOTHING(Poco::File(m_abspath).remove());
  }

  void testTBLWithColumnHeadingsRowOnly() {
    std::ofstream file(m_filename.c_str());
    file << "Runs,Angle,Transmission,Energy,Spin,Group,Options" << std::endl;
    file.close();
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS_NOTHING(alg->execute());
    TS_ASSERT_EQUALS(AnalysisDataService::Instance().doesExist(m_wsName), true);
    Workspace_sptr output;
    TS_ASSERT_THROWS_NOTHING(
        output = AnalysisDataService::Instance().retrieve(m_wsName));
    TableWorkspace_sptr outputWS =
        boost::dynamic_pointer_cast<TableWorkspace>(output);
    std::vector<std::string> cols{"Runs", "Angle", "Transmission", "Energy",
                                  "Spin", "Group", "Options"};
    TS_ASSERT_EQUALS(outputWS->getColumnNames(), cols);
    TableRow row = outputWS->getRow(0);
    // we added no rows so we should get runtime error when we try to acheive a
    // cell from a row that doesn't exist
    TS_ASSERT_THROWS_ANYTHING(row.cell<std::string>(0));
  }

  void testBlankFile() {
    std::ofstream file(m_filename.c_str());
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS(alg->execute(), std::runtime_error);

    cleanupafterwards();

  void testNoDataFile() {
    // create a file with content, and the right amount of delimiters, but no
    // valid data
    std::ofstream file(m_filename.c_str());
    file << ",,,,,,,,,,,,,,,," << std::endl;
    file << ",,,,,,,,,,,,,,,," << std::endl;
    file << ",,,,,,,,,,,,,,,," << std::endl;
    file << ",,,,,,,,,,,,,,,," << std::endl;
    file << ",,,,,,,,,,,,,,,," << std::endl;
    file << ",,,,,,,,,,,,,,,," << std::endl;
    file << ",,,,,,,,,,,,,,,," << std::endl;
    file.close();
    Mantid::API::IAlgorithm_sptr alg =
        Mantid::API::AlgorithmManager::Instance().create("LoadTBL");
    alg->setRethrows(true);
    TS_ASSERT(alg->isInitialized());
    TS_ASSERT_THROWS_NOTHING(alg->setPropertyValue("Filename", m_filename));
    m_abspath = alg->getPropertyValue("Filename"); // Get absolute path
    TS_ASSERT_THROWS_NOTHING(
        alg->setPropertyValue("OutputWorkspace", m_wsName));
    TS_ASSERT_THROWS(alg->execute(), std::runtime_error);
    cleanupafterwards();
  }

private:
  const std::string m_filename;
  const std::string m_wsName;
  std::string m_abspath;
  void cleanupafterwards() {
    TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(m_wsName));
    TS_ASSERT_THROWS_NOTHING(Poco::File(m_abspath).remove());
#endif // LOADTBLTEST_H_