Skip to content
Snippets Groups Projects
Commit 61fccfb2 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

doku, add unit tests, minor refactorings

parent 28d11807
No related branches found
No related tags found
1 merge request!65Refactor Distribution Handling
...@@ -19,23 +19,6 @@ ...@@ -19,23 +19,6 @@
#include "Param/Base/RealParameter.h" #include "Param/Base/RealParameter.h"
namespace {
void setGuarded(double* p, const RealLimits& limits, const std::string& paramName, double value)
{
if (value == *p)
return; // nothing to do
if (!limits.isInRange(value)) {
std::ostringstream message;
message << "Cannot set " << paramName << " to value " << value << ": out of bounds ["
<< limits << "]\n";
throw std::runtime_error(message.str());
}
*p = value;
}
} // namespace
// Allow for 90 degrees by adding a relatively small constant to pi/2 // Allow for 90 degrees by adding a relatively small constant to pi/2
static constexpr double INCLINATION_LIMIT = M_PI_2 + 1e-10; static constexpr double INCLINATION_LIMIT = M_PI_2 + 1e-10;
...@@ -133,17 +116,20 @@ const IFootprintFactor* Beam::footprintFactor() const ...@@ -133,17 +116,20 @@ const IFootprintFactor* Beam::footprintFactor() const
void Beam::setInclinationAngleGuarded(double value) void Beam::setInclinationAngleGuarded(double value)
{ {
setGuarded(&m_alpha, m_alphaLimits, "inclination angle", value); checkLimits("inclination angle", value, m_alphaLimits);
m_alpha = value;
} }
void Beam::setAzimuthalAngleGuarded(double value) void Beam::setAzimuthalAngleGuarded(double value)
{ {
setGuarded(&m_phi, m_phiLimits, "azimuthal angle", value); checkLimits("azimuthal angle", value, m_phiLimits);
m_phi = value;
} }
void Beam::setWavelengthGuarded(double value) void Beam::setWavelengthGuarded(double value)
{ {
setGuarded(&m_wavelength, m_wavelengthLimits, "wavelength", value); checkLimits("wavelength", value, m_wavelengthLimits);
m_wavelength = value;
} }
void Beam::setFootprintFactor(const IFootprintFactor& shape_factor) void Beam::setFootprintFactor(const IFootprintFactor& shape_factor)
......
...@@ -51,14 +51,17 @@ public: ...@@ -51,14 +51,17 @@ public:
//! Returns footprint factor. //! Returns footprint factor.
const IFootprintFactor* footprintFactor() const; const IFootprintFactor* footprintFactor() const;
//! set the value and check its limits //! Check for limits, set the value within limits.
void setInclinationAngleGuarded(double d); // #baPool + add unit tests //! Throws if limits are violated.
void setInclinationAngleGuarded(double d);
//! set the value and check its limits //! Check for limits, set the value within limits.
void setAzimuthalAngleGuarded(double d); // #baPool + add unit tests //! Throws if limits are violated.
void setAzimuthalAngleGuarded(double d);
//! set the value and check its limits //! Check for limits, set the value within limits.
void setWavelengthGuarded(double d); // #baPool + add unit tests //! Throws if limits are violated.
void setWavelengthGuarded(double d);
#ifndef SWIG #ifndef SWIG
//! Returns the polarization density matrix (in spin basis along z-axis) //! Returns the polarization density matrix (in spin basis along z-axis)
......
...@@ -25,7 +25,8 @@ class RealLimits; ...@@ -25,7 +25,8 @@ class RealLimits;
class ParameterPool; class ParameterPool;
class RealParameter; class RealParameter;
//! Manages a local parameter pool, and a tree of child pools. //! Originally managed registered parameters. Is somehow a left-over of the big refactoring when
//! these parameters have been taken out. Maybe could be refactored to be put into IComponent.
//! @ingroup tools_internal //! @ingroup tools_internal
class IParametricComponent : public IComponent { class IParametricComponent : public IComponent {
...@@ -38,14 +39,18 @@ public: ...@@ -38,14 +39,18 @@ public:
const std::string& getName() const { return m_name; } const std::string& getName() const { return m_name; }
//! Action to be taken in inherited class when a parameter has changed. //! Action to be taken in inherited class when a value has changed. Formerly this was also
//! called when a change via registered parameter took place. This is now obsolete, but the
//! onChange() call is still relevant for calculations when a value is changed by different
//! means (e.g. after initialization).
virtual void onChange() {} virtual void onChange() {}
protected: protected:
void setName(const std::string& name) { m_name = name; } void setName(const std::string& name) { m_name = name; }
// #bapool add docu and unit tests //! Check a certain value against limits.
//! Throws if not within the limits.
//! Name is only for exception message.
void checkLimits(const std::string& name, const double value, const RealLimits& limits) const; void checkLimits(const std::string& name, const double value, const RealLimits& limits) const;
private: private:
......
...@@ -76,15 +76,6 @@ protected: ...@@ -76,15 +76,6 @@ protected:
//! Returns weighted samples from given interpolation points and probabilityDensity(). //! Returns weighted samples from given interpolation points and probabilityDensity().
std::vector<ParameterSample> std::vector<ParameterSample>
generateSamplesFromValues(const std::vector<double>& sample_values) const; generateSamplesFromValues(const std::vector<double>& sample_values) const;
friend class DistributionsTest_DistributionGateParameters_Test; // #baPool o remove; for
// setUnits
friend class DistributionsTest_DistributionLorentzParameters_Test; // #baPool o remove; for
// setUnits
friend class DistributionsTest_DistributionGaussianParameters_Test; // #baPool o remove; for
// setUnits
friend class DistributionsTest_DistributionCosineParameters_Test; // #baPool o remove; for
// setUnits
}; };
#endif // USER_API #endif // USER_API
......
...@@ -66,7 +66,7 @@ void MultiLayer::addLayerWithTopRoughness(const Layer& layer, const LayerRoughne ...@@ -66,7 +66,7 @@ void MultiLayer::addLayerWithTopRoughness(const Layer& layer, const LayerRoughne
interface = LayerInterface::createRoughInterface(last_layer, new_layer, roughness); interface = LayerInterface::createRoughInterface(last_layer, new_layer, roughness);
else else
interface = LayerInterface::createSmoothInterface(last_layer, new_layer); interface = LayerInterface::createSmoothInterface(last_layer, new_layer);
addAndRegisterInterface(interface); addInterface(interface);
} else { } else {
// the top layer // the top layer
if (new_layer->thickness() != 0.0) { if (new_layer->thickness() != 0.0) {
...@@ -83,7 +83,7 @@ void MultiLayer::addLayerWithTopRoughness(const Layer& layer, const LayerRoughne ...@@ -83,7 +83,7 @@ void MultiLayer::addLayerWithTopRoughness(const Layer& layer, const LayerRoughne
"cannot have roughness."); "cannot have roughness.");
} }
} }
addAndRegisterLayer(new_layer); addLayer(new_layer);
} }
const Layer* MultiLayer::layer(size_t i_layer) const const Layer* MultiLayer::layer(size_t i_layer) const
...@@ -128,13 +128,13 @@ std::vector<const INode*> MultiLayer::getChildren() const ...@@ -128,13 +128,13 @@ std::vector<const INode*> MultiLayer::getChildren() const
return ret; return ret;
} }
void MultiLayer::addAndRegisterLayer(Layer* child) // #baPool + rename to addLayer void MultiLayer::addLayer(Layer* child)
{ {
m_layers.push_back(child); m_layers.push_back(child);
registerChild(child); registerChild(child);
} }
void MultiLayer::addAndRegisterInterface(LayerInterface* child) // #baPool + rename to addInterface void MultiLayer::addInterface(LayerInterface* child)
{ {
m_interfaces.push_back(child); m_interfaces.push_back(child);
registerChild(child); registerChild(child);
......
...@@ -73,10 +73,10 @@ public: ...@@ -73,10 +73,10 @@ public:
private: private:
//! Adds the layer with simultaneous registration in parent class //! Adds the layer with simultaneous registration in parent class
void addAndRegisterLayer(Layer* child); void addLayer(Layer* child);
//! Adds the interface with simultaneous registration in parent class //! Adds the interface with simultaneous registration in parent class
void addAndRegisterInterface(LayerInterface* child); void addInterface(LayerInterface* child);
//! Checks index of layer w.r.t. vector length //! Checks index of layer w.r.t. vector length
size_t check_layer_index(size_t i_layer) const; size_t check_layer_index(size_t i_layer) const;
......
#include "Fit/Param/RealLimits.h"
#include "Param/Base/IParametricComponent.h" #include "Param/Base/IParametricComponent.h"
#include "Param/Base/RealParameter.h"
#include "Tests/GTestWrapper/google_test.h" #include "Tests/GTestWrapper/google_test.h"
#include <stdexcept> #include <stdexcept>
class IParametricComponentTest : public ::testing::Test {
protected:
class TestClass : public IParametricComponent {
public:
using IParametricComponent::checkLimits; // make it public
};
};
TEST_F(IParametricComponentTest, checkLimits)
{
// this checks only the function if IParametricComponentTest::checkLimits() in principle. More
// tests regarding RealLimits shall be done in dedicated RealLimits unit tests
TestClass t;
EXPECT_NO_THROW(t.checkLimits("A", 0, RealLimits::nonnegative()));
EXPECT_THROW(t.checkLimits("A", 0, RealLimits::positive()), std::runtime_error);
EXPECT_NO_THROW(t.checkLimits("A", -1, RealLimits::limitless()));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment