Skip to content

Commit

Permalink
re #10657 added protection around xmin and xmax values + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
NickDraper committed Nov 28, 2014
1 parent 855d1c4 commit 918da90
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Code/Mantid/Framework/Algorithms/src/CreateSampleWorkspace.cpp
Expand Up @@ -19,6 +19,7 @@
#include <cmath>
#include <ctime>
#include <numeric>
#include <stdexcept>

namespace Mantid
{
Expand Down Expand Up @@ -91,7 +92,7 @@ namespace Algorithms
declareProperty("UserDefinedFunction","","Parameters defining the fitting function and its initial values");

declareProperty("NumBanks", 2,boost::make_shared<BoundedValidator<int> >(0,100), "The Number of banks in the instrument (default:2)");
declareProperty("BankPixelWidth", 10,boost::make_shared<BoundedValidator<int> >(0,1000), "The width & height of each bank in pixels (default:10).");
declareProperty("BankPixelWidth", 10,boost::make_shared<BoundedValidator<int> >(0,10000), "The width & height of each bank in pixels (default:10).");
declareProperty("NumEvents", 1000,boost::make_shared<BoundedValidator<int> >(0,100000), "The number of events per detector, this is only used for EventWorkspaces (default:1000).");
declareProperty("Random", false, "Whether to randomise the placement of events and data (default:false).");

Expand All @@ -117,7 +118,19 @@ namespace Algorithms
const std::string xUnit = getProperty("XUnit");
const double xMin = getProperty("XMin");
const double xMax = getProperty("XMax");
const double binWidth = getProperty("BinWidth");
double binWidth = getProperty("BinWidth");

if (xMax <= xMin)
{
throw std::invalid_argument("XMax must be larger than XMin");
}

if (binWidth > (xMax-xMin))
{
//the bin width is so large that there is less than one bin - so adjust it down
binWidth = xMax-xMin;
g_log.warning()<<"The bin width is so large that there is less than one bin - it has been changed to " << binWidth << std::endl;
}

std::string functionString = "";
if (m_preDefinedFunctionmap.find(preDefinedFunction) != m_preDefinedFunctionmap.end())
Expand Down
32 changes: 32 additions & 0 deletions Code/Mantid/Framework/Algorithms/test/CreateSampleWorkspaceTest.h
Expand Up @@ -263,6 +263,38 @@ class CreateSampleWorkspaceTest : public CxxTest::TestSuite
AnalysisDataService::Instance().remove(outWSName);
}

void test_failure_due_to_bad_bin_width()
{
/* Equivalent of this python command:
mono_ws = CreateSampleWorkspace(NumBanks=1, BankPixelWidth=4, NumEvents=10000,XUnit='DeltaE',XMin=-5,XMax=15)
*/
std::string outWSName = "CreateSampleWorkspaceTest_test_failure_due_to_bad_bin_width";
CreateSampleWorkspace alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
alg.setPropertyValue("OutputWorkspace", outWSName);
TS_ASSERT_THROWS_NOTHING( alg.setProperty("NumBanks", 1) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("BankPixelWidth", 4) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("NumEvents", 10000) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("XUnit", "DeltaE") );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("XMin", -5.0) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("XMax", 15.0) );
//leave the default bin width of 200, which is inappropriate

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

// Retrieve the workspace from data service. TODO: Change to your desired type
MatrixWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outWSName) );
TS_ASSERT(ws);
//just one bin
TS_ASSERT_EQUALS(ws->blocksize(),1);

// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}

};


Expand Down

0 comments on commit 918da90

Please sign in to comment.