Skip to content

Commit

Permalink
Merge pull request mantidproject#36970 from mantidproject/fix_domain_…
Browse files Browse the repository at this point in the history
…check_bug

Fix domain check bug
  • Loading branch information
SilkeSchomann authored Mar 11, 2024
2 parents 0deaeac + e4105f2 commit d3979da
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Framework/API/src/CompositeFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ size_t CompositeFunction::getNumberDomains() const {
}
size_t nd = getFunction(0)->getNumberDomains();
for (size_t iFun = 1; iFun < n; ++iFun) {
if (getFunction(0)->getNumberDomains() != nd) {
if (getFunction(iFun)->getNumberDomains() != nd) {
throw std::runtime_error("CompositeFunction has members with "
"inconsistent domain numbers.");
}
Expand Down
40 changes: 39 additions & 1 deletion Framework/API/test/CompositeFunctionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ template <bool withAttributes = false> class Gauss : public IPeakFunction {
void setFwhm(const double w) override { setParameter(2, w); }
};

// Create class with configurable number of domains for ease of testing.
class FunctionWithNDomains : public IPeakFunction {
public:
FunctionWithNDomains(int nDomains) : m_nDomains(nDomains) {}

size_t getNumberDomains() const override { return m_nDomains; }

// Override pure virtual functions.
std::string name() const override { return "FunctionWithNDomains"; }
double fwhm() const override { return 1; }
void setFwhm(const double w) override { UNUSED_ARG(w); }
void functionLocal(double *out, const double *xValues, const size_t nData) const override {
UNUSED_ARG(out);
UNUSED_ARG(xValues);
UNUSED_ARG(nData);
}
double centre() const override { return 1; }
double height() const override { return 1; }
void setCentre(const double c) override { UNUSED_ARG(c); }
void setHeight(const double h) override { UNUSED_ARG(h); }

private:
size_t m_nDomains;
};

template <bool withAttributes = false> class Linear : public ParamFunction, public IFunction1D {
public:
Linear() {
Expand Down Expand Up @@ -1334,7 +1359,7 @@ class CompositeFunctionTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(mfun->getAttribute("NumDeriv").asBool(), true);
}

void test_set_attribute_thorws_if_attribute_not_recongized() {
void test_set_attribute_throws_if_attribute_not_recongized() {
auto mfun = std::make_unique<CompositeFunction>();
auto gauss = std::make_shared<Gauss<true>>();
auto background = std::make_shared<Linear<true>>();
Expand Down Expand Up @@ -1517,6 +1542,19 @@ class CompositeFunctionTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(composite->getFunction(1)->calculateStepSize(parameterValue2), parameterValue2 * sqrtEpsilon);
}

void test_getNumberDomains_throws_with_inconsistent_domain_numbers() {
IFunction_sptr f1 = IFunction_sptr(new FunctionWithNDomains(7));
IFunction_sptr f2 = IFunction_sptr(new FunctionWithNDomains(13));

CompositeFunction fun;

fun.addFunction(f1);
fun.addFunction(f2);

TS_ASSERT_THROWS_EQUALS(fun.getNumberDomains(), const std::runtime_error &e, std::string(e.what()),
"CompositeFunction has members with inconsistent domain numbers.");
}

private:
CompositeFunction_sptr createComposite() {
auto composite = std::make_unique<CompositeFunction>();
Expand Down
1 change: 0 additions & 1 deletion buildconfig/CMake/CppCheck_Suppressions.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ constParameterPointer:${CMAKE_SOURCE_DIR}/Framework/API/src/BoostOptionalToAlgor
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/BoostOptionalToAlgorithmProperty.cpp:33
constParameterPointer:${CMAKE_SOURCE_DIR}/Framework/API/src/BoostOptionalToAlgorithmProperty.cpp:30
unreadVariable:${CMAKE_SOURCE_DIR}/Framework/Algorithms/src/SmoothNeighbours.cpp:317
knownConditionTrueFalse:${CMAKE_SOURCE_DIR}/Framework/API/src/CompositeFunction.cpp:968
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/CompositeFunction.cpp:560
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/CompositeFunction.cpp:879
constVariablePointer:${CMAKE_SOURCE_DIR}/Framework/API/src/CompositeFunction.cpp:906
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- CompositeFunction will now throw an exception if getNumberDomains() is called and there is an inconsistent number of domains in any of the member functions.

0 comments on commit d3979da

Please sign in to comment.