From e4616a87522e3f83941a621563e6e32871cd0356 Mon Sep 17 00:00:00 2001 From: Martyn Gigg Date: Wed, 2 Jul 2014 14:28:37 +0100 Subject: [PATCH] Add a set of tests to define the behaviour that is currently broken. Refs #9802 --- .../Geometry/test/ParameterMapTest.h | 189 ++++++++++++++++-- 1 file changed, 177 insertions(+), 12 deletions(-) diff --git a/Code/Mantid/Framework/Geometry/test/ParameterMapTest.h b/Code/Mantid/Framework/Geometry/test/ParameterMapTest.h index 99857b777506..5ce59245e585 100644 --- a/Code/Mantid/Framework/Geometry/test/ParameterMapTest.h +++ b/Code/Mantid/Framework/Geometry/test/ParameterMapTest.h @@ -8,6 +8,7 @@ #include "MantidKernel/V3D.h" #include +#include #include using Mantid::Geometry::ParameterMap; @@ -189,26 +190,118 @@ class ParameterMapTest : public CxxTest::TestSuite TS_ASSERT_DELTA(finalValue, stored->value(), DBL_EPSILON); } - void test_Replacing_Existing_Parameter_On_A_Copy_Does_Not_Update_Original_Value() + void test_Replacing_Existing_Parameter_On_A_Copy_Does_Not_Update_Original_Value_Using_Generic_Add() { + using namespace Mantid::Kernel; + + // -- General templated function -- + doCopyAndUpdateTestUsingGenericAdd("double", 5.0, 3.5); // no need to check other types + } + + void test_Replacing_Existing_Parameter_On_A_Copy_Does_Not_Update_Original_Value_Using_AddHelpers() + { + using namespace Mantid::Kernel; + // -- Specialized Helper Functions -- + + // double + boost::function faddDouble; + faddDouble = (void (ParameterMap::*)(const IComponent*,const std::string&,double))&ParameterMap::addDouble; + doCopyAndUpdateTestUsingAddHelpers(faddDouble, "name", 5.0, 4.0); + + // int + boost::function faddInt; + faddInt = (void (ParameterMap::*)(const IComponent*,const std::string&,int))&ParameterMap::addInt; + doCopyAndUpdateTestUsingAddHelpers(faddInt, "name", 3, 5); + + // bool + boost::function faddBool; + faddBool = (void (ParameterMap::*)(const IComponent*,const std::string&,bool))&ParameterMap::addBool; + doCopyAndUpdateTestUsingAddHelpers(faddBool, "name", true, false); + + // string + boost::function faddStr; + faddStr = (void (ParameterMap::*)(const IComponent*,const std::string&,const std::string&))&ParameterMap::addString; + doCopyAndUpdateTestUsingAddHelpers(faddStr, "name", std::string("first"), std::string("second")); + + // V3D + boost::function faddV3D; + faddV3D = (void (ParameterMap::*)(const IComponent*,const std::string&,const V3D&))&ParameterMap::addV3D; + doCopyAndUpdateTestUsingAddHelpers(faddV3D, "name", V3D(1,2,3), V3D(4,5,6)); + + // Quat + boost::function faddQuat; + faddQuat = (void (ParameterMap::*)(const IComponent*,const std::string&,const Quat&))&ParameterMap::addQuat; + doCopyAndUpdateTestUsingAddHelpers(faddQuat, "name", Quat(), Quat(45.0,V3D(0,0,1))); + } + + void test_Replacing_Existing_Parameter_On_A_Copy_Does_Not_Update_Original_Value_Using_AddHelpers_As_Strings() + { + // -- Specialized Helper Functions -- + + typedef boost::function AddFuncHelper; + + // double + AddFuncHelper faddDouble; + faddDouble = (void (ParameterMap::*)(const IComponent*,const std::string&,const std::string &))&ParameterMap::addDouble; + doCopyAndUpdateTestUsingAddHelpersAsStrings(faddDouble, "name", 5.0, 4.0); + + // int + AddFuncHelper faddInt; + faddInt = (void (ParameterMap::*)(const IComponent*,const std::string&,const std::string &))&ParameterMap::addInt; + doCopyAndUpdateTestUsingAddHelpersAsStrings(faddInt, "name", 3, 5); + + // bool + AddFuncHelper faddBool; + faddBool = (void (ParameterMap::*)(const IComponent*,const std::string&,const std::string&))&ParameterMap::addBool; + doCopyAndUpdateTestUsingAddHelpersAsStrings(faddBool, "name", true, false); + } + + void test_Replacing_Existing_Parameter_On_A_Copy_Does_Not_Update_Original_Value_Using_AddPosition_Helper() + { + using namespace Mantid::Kernel; + ParameterMap pmap; - const std::string name = "Parameter"; - const double origValue = 5.0; - pmap.addDouble(m_testInstrument.get(), name, origValue); + V3D origValue(1,2,3); + pmap.addV3D(m_testInstrument.get(), ParameterMap::pos(), origValue); ParameterMap copy(pmap); // invoke copy constructor TS_ASSERT_EQUALS(1, copy.size()); - auto parameter = copy.get(m_testInstrument.get(), name); - TS_ASSERT_EQUALS(origValue, parameter->value()); + auto parameter = copy.get(m_testInstrument.get(), ParameterMap::pos()); + TS_ASSERT_EQUALS(origValue, parameter->value()); //change the value on the copy and it should NOT update on the original - const double newValue(3.5); - copy.addDouble(m_testInstrument.get(), name, newValue); + copy.addPositionCoordinate(m_testInstrument.get(), ParameterMap::posy() , 5.0); - auto copyParameter = copy.get(m_testInstrument.get(), name); - TS_ASSERT_EQUALS(newValue, copyParameter->value()); - auto origParameter = pmap.get(m_testInstrument.get(), name); - TS_ASSERT_EQUALS(origValue, origParameter->value()); + V3D newValue(1,5,3); + auto copyParameter = copy.get(m_testInstrument.get(), ParameterMap::pos()); + TS_ASSERT_EQUALS(newValue, copyParameter->value()); + auto origParameter = pmap.get(m_testInstrument.get(), ParameterMap::pos()); + TS_ASSERT_EQUALS(origValue, origParameter->value()); + } + + void test_Replacing_Existing_Parameter_On_A_Copy_Does_Not_Update_Original_Value_Using_AddRotation_Helper() + { + using namespace Mantid::Kernel; + + ParameterMap pmap; + Quat origValue(45.0, V3D(0,0,1)); + pmap.addQuat(m_testInstrument.get(), ParameterMap::rot(), origValue); + + ParameterMap copy(pmap); // invoke copy constructor + + TS_ASSERT_EQUALS(1, copy.size()); + auto parameter = copy.get(m_testInstrument.get(), ParameterMap::rot()); + TS_ASSERT_EQUALS(origValue, parameter->value()); + //change the value on the copy and it should NOT update on the original + copy.addRotationParam(m_testInstrument.get(), ParameterMap::roty(), 30.0); + + Quat newValue = origValue; + newValue.setAngleAxis(30.0, V3D(0,1,0)); + + auto copyParameter = copy.get(m_testInstrument.get(), ParameterMap::rot()); + TS_ASSERT_EQUALS(newValue, copyParameter->value()); + auto origParameter = pmap.get(m_testInstrument.get(), ParameterMap::rot()); + TS_ASSERT_EQUALS(origValue, origParameter->value()); } @@ -441,10 +534,82 @@ class ParameterMapTest : public CxxTest::TestSuite } private: + + template + void doCopyAndUpdateTestUsingGenericAdd(const std::string & type, const ValueType & origValue, const ValueType & newValue) + { + ParameterMap pmap; + const std::string name = "Parameter"; + pmap.add(type, m_testInstrument.get(), name, origValue); + + ParameterMap copy(pmap); // invoke copy constructor + + TS_ASSERT_EQUALS(1, copy.size()); + auto parameter = copy.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(origValue, parameter->value()); + //change the value on the copy and it should NOT update on the original + copy.add(type, m_testInstrument.get(), name, newValue); + + auto copyParameter = copy.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(newValue, copyParameter->value()); + auto origParameter = pmap.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(origValue, origParameter->value()); + } + + template + void doCopyAndUpdateTestUsingAddHelpers(const FuncType & addFunc, + const std::string &name, + const ValueType & origValue, const ValueType & newValue) + { + ParameterMap pmap; + addFunc(&pmap, m_testInstrument.get(), name, origValue); + + ParameterMap copy(pmap); // invoke copy constructor + + TS_ASSERT_EQUALS(1, copy.size()); + auto parameter = copy.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(origValue, parameter->value()); + //change the value on the copy and it should NOT update on the original + addFunc(©, m_testInstrument.get(), name, newValue); + + auto copyParameter = copy.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(newValue, copyParameter->value()); + auto origParameter = pmap.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(origValue, origParameter->value()); + } + + template + void doCopyAndUpdateTestUsingAddHelpersAsStrings(const FuncType & addFunc, + const std::string &name, + const ValueType & origTypedValue, + const ValueType & newTypedValue) + { + std::string origValue = boost::lexical_cast(origTypedValue); + std::string newValue = boost::lexical_cast(newTypedValue); + + ParameterMap pmap; + addFunc(&pmap, m_testInstrument.get(), name, origValue); + + ParameterMap copy(pmap); // invoke copy constructor + + TS_ASSERT_EQUALS(1, copy.size()); + auto parameter = copy.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(origTypedValue, parameter->value()); + //change the value on the copy and it should NOT update on the original + addFunc(©, m_testInstrument.get(), name, newValue); + + auto copyParameter = copy.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(newTypedValue, copyParameter->value()); + auto origParameter = pmap.get(m_testInstrument.get(), name); + TS_ASSERT_EQUALS(origTypedValue, origParameter->value()); + } + + // private instrument Instrument_sptr m_testInstrument; }; + //---------------------------------- Performance Tests ---------------------------------------- class ParameterMapTestPerformance : public CxxTest::TestSuite {