Skip to content

Commit

Permalink
Re #9682. Added resetData method to Interpolation
Browse files Browse the repository at this point in the history
Cleaned up the unit tests a bit so that it's easier to spot where errors are coming from.
  • Loading branch information
Michael Wedel committed Jun 23, 2014
1 parent b769e9d commit a451716
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/Interpolation.h
Expand Up @@ -97,6 +97,8 @@ class MANTID_KERNEL_DLL Interpolation
/// Prints object to stream
void printSelf(std::ostream& os) const;

/// Clear interpolation values
void resetData();
};

// defining operator << and >>
Expand Down
10 changes: 10 additions & 0 deletions Code/Mantid/Framework/Kernel/src/Interpolation.cpp
Expand Up @@ -132,6 +132,15 @@ namespace Kernel
}
}

/**
Resets interpolation data by clearing the internal storage for x- and y-values
*/
void Interpolation::resetData()
{
m_x.clear();
m_y.clear();
}

/**
Prints the value of parameter
@param os :: the Stream to output to
Expand Down Expand Up @@ -161,6 +170,7 @@ namespace Kernel
f.setMethod(values[0]);
f.setXUnit(values[1]);
f.setYUnit(values[2]);
f.resetData(); // Reset data, in case the interpolation table is not empty

for ( unsigned int i = 3; i < values.count(); i++)
{
Expand Down
184 changes: 130 additions & 54 deletions Code/Mantid/Framework/Kernel/test/InterpolationTest.h
Expand Up @@ -10,65 +10,141 @@ using namespace Mantid::Kernel;
class InterpolationTest : public CxxTest::TestSuite
{
public:

void test1()
{
Interpolation interpolation;

TS_ASSERT ( interpolation.containData() == false );

interpolation.addPoint(200.0, 50);

TS_ASSERT ( interpolation.containData() == true );

interpolation.addPoint(201.0, 60);
interpolation.addPoint(202.0, 100);
interpolation.addPoint(204.0, 400);
interpolation.addPoint(203.0, 300);

// Test that all the base class member variables are correctly assigned to
TS_ASSERT_DELTA( interpolation.value(100), -950.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(3000), 280000.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(200.5), 55.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(201.25), 70.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(203.5), 350.0 ,0.000000001);

TS_ASSERT_EQUALS(interpolation.value(204.0), 400.0);

interpolation.setXUnit("Wavelength");
interpolation.setYUnit("dSpacing");
std::stringstream str;
str << interpolation;
TS_ASSERT( str.str().compare("linear ; Wavelength ; dSpacing ; 200 50 ; 201 60 ; 202 100 ; 203 300 ; 204 400") == 0 );

Interpolation readIn;
TS_ASSERT( readIn.getXUnit()->unitID() == "TOF" );
TS_ASSERT( readIn.getYUnit()->unitID() == "TOF" );
str >> readIn;
TS_ASSERT( readIn.getXUnit()->unitID() == "Wavelength" );
TS_ASSERT( readIn.getYUnit()->unitID() == "dSpacing" );

// Test that all the base class member variables are correctly assigned to
TS_ASSERT_DELTA( readIn.value(100), -950.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(3000), 280000.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(200.5), 55.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(201.25), 70.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(203.5), 350.0 ,0.000000001);
void testCopyConstruction()
{
Interpolation interpolation;
interpolation.setMethod("linear");
interpolation.setXUnit("Wavelength");
interpolation.setYUnit("dSpacing");

interpolation.addPoint(200.0, 2.0);

Interpolation other = interpolation;

TS_ASSERT_EQUALS(other.getMethod(), "linear");
TS_ASSERT_EQUALS(other.getXUnit()->unitID(), "Wavelength");
TS_ASSERT_EQUALS(other.getYUnit()->unitID(), "dSpacing");
TS_ASSERT_EQUALS(other.value(200.0), 2.0);
}

void testContainData()
{
Interpolation interpolation;

TS_ASSERT ( interpolation.containData() == false );

interpolation.addPoint(200.0, 50);

TS_ASSERT ( interpolation.containData() == true );
}

void testResetData()
{
Interpolation interpolation = getInitializedInterpolation("Wavelength", "dSpacing");

TS_ASSERT(interpolation.containData());
interpolation.resetData();
TS_ASSERT(interpolation.containData() == false);
}

void testAddPoint()
{
Interpolation interpolation;

// insertion order does not matter, interpolation table is always sorted internally
interpolation.addPoint(200.0, 50);
interpolation.addPoint(201.0, 60);
interpolation.addPoint(202.0, 100);
interpolation.addPoint(204.0, 400);
interpolation.addPoint(203.0, 300);

// Test that all the base class member variables are correctly assigned to
TS_ASSERT_DELTA( interpolation.value(100), -950.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(3000), 280000.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(200.5), 55.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(201.25), 70.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(203.5), 350.0 ,0.000000001);

TS_ASSERT_EQUALS(interpolation.value(204.0), 400.0);
}

void testEmpty()
{
Interpolation interpolation;
void testEmpty()
{
Interpolation interpolation;

std::stringstream str;
str << interpolation;
TS_ASSERT( str.str().compare("linear ; TOF ; TOF") == 0 );
std::stringstream str;
str << interpolation;
TS_ASSERT( str.str().compare("linear ; TOF ; TOF") == 0 );

Interpolation readIn;
str >> readIn;
Interpolation readIn;
str >> readIn;

TS_ASSERT( readIn.containData() == false );
}
TS_ASSERT( readIn.containData() == false );
}

void testStreamOperators()
{
std::string xUnit = "Wavelength";
std::string yUnit = "dSpacing";

Interpolation interpolation = getInitializedInterpolation(xUnit, yUnit);

// Output stream
std::stringstream str;
str << interpolation;
TS_ASSERT( str.str().compare("linear ; Wavelength ; dSpacing ; 200 50 ; 201 60 ; 202 100 ; 203 300 ; 204 400") == 0 );

// Input stream for empty interpolation object
Interpolation readIn;
TS_ASSERT( readIn.getXUnit()->unitID() == "TOF" );
TS_ASSERT( readIn.getYUnit()->unitID() == "TOF" );
str >> readIn;
TS_ASSERT( readIn.getXUnit()->unitID() == xUnit );
TS_ASSERT( readIn.getYUnit()->unitID() == yUnit );

// Test that all the base class member variables are correctly assigned to
TS_ASSERT_DELTA( readIn.value(100), -950.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(3000), 280000.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(200.5), 55.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(201.25), 70.0 ,0.000000001);
TS_ASSERT_DELTA( readIn.value(203.5), 350.0 ,0.000000001);
}

void testStreamOperatorsNonEmpty()
{
Interpolation interpolation = getInitializedInterpolation("Wavelength", "dSpacing");

std::stringstream str;
str << interpolation;

// Reconstruct on existing object.
str >> interpolation;

TS_ASSERT_DELTA( interpolation.value(100), -950.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(3000), 280000.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(200.5), 55.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(201.25), 70.0 ,0.000000001);
TS_ASSERT_DELTA( interpolation.value(203.5), 350.0 ,0.000000001);

TS_ASSERT_EQUALS( interpolation.value(201.0), 60.0)
}

private:
Interpolation getInitializedInterpolation(std::string xUnit, std::string yUnit)
{
Interpolation interpolation;

interpolation.addPoint(200.0, 50);
interpolation.addPoint(201.0, 60);
interpolation.addPoint(202.0, 100);
interpolation.addPoint(203.0, 300);
interpolation.addPoint(204.0, 400);

interpolation.setXUnit(xUnit);
interpolation.setYUnit(yUnit);

return interpolation;
}

};

Expand Down

0 comments on commit a451716

Please sign in to comment.