Skip to content

Commit

Permalink
Add more unit tests. Refs #4749.
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzhou committed Mar 21, 2013
1 parent 956698f commit 7fb8fbf
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 15 deletions.
22 changes: 7 additions & 15 deletions Code/Mantid/Framework/DataHandling/src/LoadFullprofResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace DataHandling
<< datafile << ".\n";
errmsg << "In input .irf file, Bank ID ";
for (size_t i = 0; i < banks.size(); ++i)
errmsg << i << ", ";
errmsg << banks[i] << ", ";
errmsg << " are found.";
g_log.error(errmsg.str());
throw runtime_error(errmsg.str());
Expand Down Expand Up @@ -166,14 +166,6 @@ namespace DataHandling
throw runtime_error(errmsg.str());
}

/*
cout << "[DB1115] Lines of input file: \n";
for (size_t i = 0; i < lines.size(); ++i)
{
cout << "Line " << i << "\t\t" << lines[i] << "\n";
}
*/

return;
}

Expand Down Expand Up @@ -223,11 +215,11 @@ namespace DataHandling
bankendindexmap.insert(make_pair(banks.back(), endindex));
}

cout << "[DB1112] Number of bank IDs = " << banks.size() << ", "
g_log.debug() << "[DB1112] Number of bank IDs = " << banks.size() << ", "
<< "Number of ranges = " << bankstartindexmap.size() << endl;
for (size_t i = 0; i < banks.size(); ++i)
{
cout << "Bank " << banks[i] << " From line " << bankstartindexmap[banks[i]] << " to "
g_log.debug() << "Bank " << banks[i] << " From line " << bankstartindexmap[banks[i]] << " to "
<< bankendindexmap[banks[i]] << endl;
}

Expand All @@ -243,7 +235,7 @@ namespace DataHandling
double cwl;
int tmpbankid;
parseBankLine(bankline, cwl, tmpbankid);
cout << "Found CWL = " << cwl << ", Bank ID = " << tmpbankid << "\n";
g_log.debug() << "Found CWL = " << cwl << ", Bank ID = " << tmpbankid << "\n";
if (bankid != tmpbankid)
{
throw runtime_error("Scanned bank is not same as bank found in the specified region.");
Expand All @@ -259,7 +251,7 @@ namespace DataHandling
continue;

// Parse
cout << "Parse Line " << i << "\t\t" << line << "\n";
g_log.debug() << "Parse Line " << i << "\t\t" << line << "\n";

if (boost::starts_with(line, "TOFRG"))
{
Expand Down Expand Up @@ -525,7 +517,7 @@ namespace DataHandling
boost::split(v, infostr, boost::is_any_of("=A"));
for (size_t i = 0; i < v.size(); ++i)
{
cout << "Last CWL splitted. Term " << i << ": \t\t" << "'" << v[i] << "'\n";
g_log.debug() << "Last CWL splitted. Term " << i << ": \t\t" << "'" << v[i] << "'\n";
string candidate = v[i];
boost::algorithm::trim(candidate);
if (candidate.size() > 0)
Expand All @@ -551,7 +543,7 @@ namespace DataHandling

// 2. Add rows
size_t numparams = parammap.size();
cout << "[DBx240] Number of imported parameters is " << numparams << "\n";
g_log.debug() << "[DBx240] Number of imported parameters is " << numparams << "\n";

map<string, double>::iterator mapiter;
for (mapiter = parammap.begin(); mapiter != parammap.end(); ++mapiter)
Expand Down
135 changes: 135 additions & 0 deletions Code/Mantid/Framework/DataHandling/test/LoadFullprofResolutionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidAPI/TableRow.h"
#include <fstream>
#include <Poco/File.h>

using Mantid::DataHandling::LoadFullprofResolution;

Expand Down Expand Up @@ -59,9 +60,82 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
TS_ASSERT_DELTA(parammap["Sig2"], 514.546, 0.0001);
TS_ASSERT_DELTA(parammap["Beta0t"], 85.918922, 0.00001);

// 4. Clean
AnalysisDataService::Instance().remove("TestBank1Table");
Poco::File("Test1Bank.irf").remove();

return;
}

/** Test import from a 1-bank irf file
*/
void test_2BankCase()
{
// 1. Generate file
string filename("Test2Bank.irf");
generate2BankIrfFile(filename);

// 2. Load
LoadFullprofResolution alg;
alg.initialize();

alg.setProperty("Filename", filename);
alg.setProperty("Bank", 3);
alg.setProperty("OutputWorkspace", "TestBank3Table");

TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());

TableWorkspace_sptr outws = boost::dynamic_pointer_cast<TableWorkspace>(
AnalysisDataService::Instance().retrieve("TestBank3Table"));
TS_ASSERT(outws);

TS_ASSERT_EQUALS(outws->columnCount(), 2);
TS_ASSERT_EQUALS(outws->rowCount(), 26);

// 3. Verify value
map<string, double> parammap;
parseTableWorkspace(outws, parammap);

TS_ASSERT_DELTA(parammap["Dtt1"], 22586.10156, 0.0001);
TS_ASSERT_DELTA(parammap["Sig1"], 10.00, 0.0001);
TS_ASSERT_DELTA(parammap["Alph0t"], 86.059, 0.00001);

// 4. Clean
AnalysisDataService::Instance().remove("TestBank3Table");
Poco::File("Test2Bank.irf").remove();

return;
}

/** Test Exception
*/
void test_WrongInputBankCase()
{
// 1. Generate file
string filename("Test2Bank.irf");
generate2BankIrfFile(filename);

// 2. Load
LoadFullprofResolution alg;
alg.initialize();

alg.setProperty("Filename", filename);
alg.setProperty("Bank", 2);
alg.setProperty("OutputWorkspace", "TestBank3Table");

alg.execute();

// 3. Check if failed
TS_ASSERT(!alg.isExecuted());

// 4. Clean
Poco::File("Test2Bank.irf").remove();

return;
}


/** Parse a TableWorkspace to a map
*/
void parseTableWorkspace(TableWorkspace_sptr tablews, map<string, double>& parammap)
Expand Down Expand Up @@ -123,6 +197,67 @@ class LoadFullprofResolutionTest : public CxxTest::TestSuite
return;
}

/** Generate a 2 bank .irf file
*/
void generate2BankIrfFile(string filename)
{
ofstream ofile;
ofile.open(filename.c_str());

if (ofile.is_open())
{
ofile << " Instrumental resolution function for POWGEN/SNS A Huq 2013-12-03 ireso: 6 \n";
ofile << "! To be used with function NPROF=10 in FullProf (Res=6) \n";
ofile << "! ---------------------------------------------- Bank 1 CWL = 0.5330A \n";
ofile << "! Type of profile function: back-to-back exponentials * pseudo-Voigt \n";
ofile << "NPROF 10 \n";
ofile << "! Tof-min(us) step Tof-max(us) \n";
ofile << "TOFRG 5000.2300 4.0002 51000.0000 \n";
ofile << "! Zero Dtt1 \n";
ofile << "ZD2TOF -1.00 22580.59157 \n";
ofile << "! Zerot Dtt1t Dtt2t x-cross Width \n";
ofile << "ZD2TOT 933.50214 22275.21084 1.0290 0.0000002 5.0957 \n";
ofile << "! TOF-TWOTH of the bank \n";
ofile << "TWOTH 90.00 \n";
ofile << "! Sig-2 Sig-1 Sig-0 \n";
ofile << "SIGMA 514.546 0.00044 0.355 \n";
ofile << "! Gam-2 Gam-1 Gam-0 \n";
ofile << "GAMMA 0.000 0.000 0.000 \n";
ofile << "! alph0 beta0 alph1 beta1 \n";
ofile << "ALFBE 0.000008 6.251096 0.000000 0.000000 \n";
ofile << "! alph0t beta0t alph1t beta1t \n";
ofile << "ALFBT 0.010156 85.918922 0.000000 0.000000 \n";
ofile << "END \n";
ofile << "! ---------------------------------------------- Bank 3 CWL = 1.3330A\n";
ofile << "! Type of profile function: back-to-back exponentials * pseudo-Voigt \n";
ofile << "NPROF 10 \n";
ofile << "! Tof-min(us) step Tof-max(us) \n";
ofile << "TOFRG 9800.0000 5.0000 86000.0000 \n";
ofile << "! Zero Dtt1 \n";
ofile << "ZD2TOF 0.00 22586.10156 \n";
ofile << "! Zerot Dtt1t Dtt2t x-cross Width \n";
ofile << "ZD2TOT -42.76068 22622.76953 0.30 0.3560 2.4135 \n";
ofile << "! TOF-TWOTH of the bank \n";
ofile << "TWOTH 90.000 \n";
ofile << "! Sig-2 Sig-1 Sig-0 \n";
ofile << "SIGMA 72.366 10.000 0.000 \n";
ofile << "! Gam-2 Gam-1 Gam-0 \n";
ofile << "GAMMA 0.000 2.742 0.000 \n";
ofile << "! alph0 beta0 alph1 beta1 \n";
ofile << "ALFBE 1.500 3.012 5.502 9.639 \n";
ofile << "! alph0t beta0t alph1t beta1t \n";
ofile << "ALFBT 86.059 96.487 13.445 3.435 \n";

ofile.close();
}
else
{
throw runtime_error("Unable to open file to write.");
}

return;
}


};

Expand Down

0 comments on commit 7fb8fbf

Please sign in to comment.