Commit bd14fe21 authored by Roman Tolchenov's avatar Roman Tolchenov
Browse files

Re #15702. Better checks for bad input by FunctionFactory.

parent 807c1809
......@@ -213,8 +213,14 @@ void Expression::tokenize() {
if (lvl == 0 && !isNumber && is_op_symbol(c)) // insert new token
{
if (i == last) {
break;
// throw std::runtime_error("Expression: syntax error");
if (c == ',' || c == ';') {
m_expr.resize(last);
break;
} else {
throw std::runtime_error("Syntax error in expression.\n\nA binary "
"operator isn't followed by a value:\n " +
m_expr);
}
}
if (is_op_symbol(m_expr[i + 1])) {
......
......@@ -9,6 +9,7 @@
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/LibraryManager.h"
#include <MantidKernel/StringTokenizer.h>
#include <boost/lexical_cast.hpp>
#include <sstream>
namespace Mantid {
......@@ -118,7 +119,13 @@ IFunction_sptr FunctionFactoryImpl::createSimple(
parentAttributes[parName] = parValue;
} else {
// set initial parameter value
fun->setParameter(parName, atof(parValue.c_str()));
try {
fun->setParameter(parName, boost::lexical_cast<double>(parValue));
} catch (boost::bad_lexical_cast &e) {
throw std::runtime_error(
"Error in value of parameter " + parName + ".\n" + parValue +
" cannot be interpreted as a floating point value.");
}
}
} // for term
......
......@@ -4,6 +4,7 @@
#include <cxxtest/TestSuite.h>
#include "MantidAPI/Expression.h"
#include <iostream>
using namespace Mantid;
using namespace Mantid::API;
......@@ -353,6 +354,33 @@ public:
e.toList();
TS_ASSERT_EQUALS(e.name(), ",");
}
void testEndOnOperator() {
{
Expression e;
TS_ASSERT_THROWS(e.parse("x+y+z + "), std::runtime_error);
}
{
Expression e;
TS_ASSERT_THROWS(e.parse("x*z-"), std::runtime_error);
}
{
Expression e;
TS_ASSERT_THROWS(e.parse("x*z="), std::runtime_error);
}
{
Expression e;
TS_ASSERT_THROWS_NOTHING(e.parse("x;y;z;"));
TS_ASSERT_EQUALS(e.size(), 3);
TS_ASSERT_EQUALS(e[2].str(), "z");
}
{
Expression e;
TS_ASSERT_THROWS_NOTHING(e.parse("z,y,x,"));
TS_ASSERT_EQUALS(e.size(), 3);
TS_ASSERT_EQUALS(e[2].str(), "x");
}
}
};
#endif /*EXPRESSIONTEST_H_*/
......@@ -816,5 +816,35 @@ class CrystalFieldFitTest(unittest.TestCase):
fun = FunctionFactory.createInitialized(s)
def test_bad_input(self):
from CrystalField import CrystalField
from mantid.simpleapi import FunctionFactory
cf = CrystalField('Ce', 'C2v', B20='aaa', B22=3.97, B40=-0.0317, B42=-0.116, B44=-0.12,
Temperature=44.0, FWHM=1.0)
s = cf.makeSpectrumFunction()
self.assertRaises(RuntimeError, FunctionFactory.createInitialized, s)
cf = CrystalField('Ce', 'C2v', B20=1, B22=3.97, B40=[-0.0317], B42=-0.116, B44=-0.12,
Temperature=44.0, FWHM=1.0)
s = cf.makeSpectrumFunction()
self.assertRaises(RuntimeError, FunctionFactory.createInitialized, s)
cf = CrystalField('Ce', 'C2v', B20=1, B22=3.97, B40=np.array([-0.0317]), B42=-0.116, B44=-0.12,
Temperature=44.0, FWHM=1.0)
s = cf.makeSpectrumFunction()
self.assertRaises(RuntimeError, FunctionFactory.createInitialized, s)
cf = CrystalField('Ce', 'C2v', B20=1, B22=3.97, B40=np.array([1.2, 2.3]), B42=-0.116, B44=-0.12,
Temperature=44.0, FWHM=1.0)
s = cf.makeSpectrumFunction()
self.assertRaises(RuntimeError, FunctionFactory.createInitialized, s)
cf = CrystalField('Ce', 'C2v', B20=1, B22=3.97, B40=-0.0317, B42=-0.116, B44=-0.12,
Temperature=44.0, FWHM=1.0)
cf.peaks.param[1]["FWHM"] = 'aaa'
s = cf.makeSpectrumFunction()
self.assertRaises(RuntimeError, FunctionFactory.createInitialized, s)
if __name__ == "__main__":
unittest.main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment