Skip to content

Commit

Permalink
Refs #4111 Refs #3204. The parameter map now searches case insensitiv…
Browse files Browse the repository at this point in the history
…ely.

There was also no unit test for the ParameterMap so I have added one.
  • Loading branch information
martyngigg committed Nov 14, 2011
1 parent ebbd844 commit 3f2e8d5
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 4 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/Geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ set ( TEST_FILES
test/ObjectTest.h
test/OneToOneSpectraDetectorMapTest.h
test/OrientedLatticeTest.h
test/ParameterMapTest.h
test/ParCompAssemblyTest.h
test/ParComponentFactoryTest.h
test/ParDetectorTest.h
Expand Down
10 changes: 6 additions & 4 deletions Code/Mantid/Framework/Geometry/src/Instrument/ParameterMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "MantidKernel/MultiThreaded.h"
#include "MantidGeometry/Instrument.h"
#include <cstring>
#include <boost/algorithm/string.hpp>


namespace Mantid
{
Expand Down Expand Up @@ -322,7 +324,7 @@ namespace Mantid
std::pair<pmap_cit,pmap_cit> components = m_map.equal_range(id);
for( pmap_cit itr = components.first; itr != components.second; ++itr )
{
if( strcmp(itr->second->nameAsCString(), name) == 0 )
if( boost::iequals(itr->second->nameAsCString(), name) )
{
return true;
}
Expand All @@ -349,7 +351,7 @@ namespace Mantid
for( pmap_cit itr = components.first; itr != components.second; ++itr )
{
boost::shared_ptr<Parameter> param = itr->second;
if( param->name() == name && (anytype || param->type() == type) )
if( boost::iequals(param->name(),name) && (anytype || param->type() == type) )
{
return true;
}
Expand All @@ -376,7 +378,7 @@ namespace Mantid
for( ; itr != itr_end; ++itr )
{
Parameter_sptr param = itr->second;
if( strcmp(param->nameAsCString(), name) == 0 )
if( boost::iequals(param->nameAsCString(), name) )
{
return param;
}
Expand Down Expand Up @@ -408,7 +410,7 @@ namespace Mantid
for( ; itr != itr_end; ++itr )
{
Parameter_sptr param = itr->second;
if( param->name() == name && (anytype || param->type() == type) )
if( boost::iequals(param->name(), name) && (anytype || param->type() == type) )
{
return param;
}
Expand Down
144 changes: 144 additions & 0 deletions Code/Mantid/Framework/Geometry/test/ParameterMapTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#ifndef PARAMETERMAPTEST_H_
#define PARAMETERMAPTEST_H_

#include "MantidGeometry/Instrument/ParameterMap.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include <cxxtest/TestSuite.h>

using Mantid::Geometry::ParameterMap;
using Mantid::Geometry::ParameterMap_sptr;
using Mantid::Geometry::Parameter_sptr;
using Mantid::Geometry::Instrument_sptr;
using Mantid::Geometry::IComponent;
using Mantid::Geometry::IComponent_sptr;

class ParameterMapTest : public CxxTest::TestSuite
{
public:

static ParameterMapTest *createSuite() { return new ParameterMapTest(); }
static void destroySuite( ParameterMapTest *suite ) { delete suite; }

ParameterMapTest()
{
m_testInstrument = ComponentCreationHelper::createTestInstrumentCylindrical(1);
}

void testConstructor_Does_Not_Throw()
{
TS_ASSERT_THROWS_NOTHING(ParameterMap());
}

void testAdding_A_Parameter_That_Is_Not_Present_Puts_The_Parameter_In()
{
// Add a parameter for the first component of the instrument
IComponent_sptr comp = m_testInstrument->getChild(0);
const std::string name("TestName");
const double value(5.1);
ParameterMap pmap;
TS_ASSERT_EQUALS(pmap.size(), 0);
pmap.addDouble(comp.get(), name, value);
TS_ASSERT_EQUALS(pmap.size(), 1);
// Check that the correct one went in
Parameter_sptr fetchedValue = pmap.get(comp.get(), name);
TS_ASSERT(fetchedValue);
TS_ASSERT_DELTA(value, fetchedValue->value<double>(), DBL_EPSILON);
}

void testAdding_A_Parameter_Of_Same_Name_Overwrites_The_First()
{
// Add a parameter for the first component of the instrument
IComponent_sptr comp = m_testInstrument->getChild(0);
const std::string name("TestName");
ParameterMap pmap;
pmap.addDouble(comp.get(), name, 5.1);
TS_ASSERT_EQUALS(pmap.size(), 1);
const double finalValue(10.1);
pmap.addDouble(comp.get(), name, finalValue);
// Should have overwritten
TS_ASSERT_EQUALS(pmap.size(), 1);
Parameter_sptr stored = pmap.get(comp.get(), name);
TS_ASSERT(stored);
TS_ASSERT_DELTA(finalValue, stored->value<double>(), DBL_EPSILON);
}

void testMap_Contains_Newly_Added_Value_For_Correct_Component()
{
ParameterMap pmap;
const std::string name("NewValue");
pmap.addInt(m_testInstrument.get(), name, 1);
TS_ASSERT_EQUALS(pmap.contains(m_testInstrument.get(), name), true);
IComponent_sptr parametrized = m_testInstrument->getChild(0);
TS_ASSERT_EQUALS(pmap.contains(parametrized.get(), name), false);
}

void testMap_Contains_Newly_Added_Value_For_Correct_Component_Of_Correct_Type()
{
ParameterMap pmap;
const std::string name("MyValue");
const std::string type("int");
const int value(1);
pmap.add<int>("int", m_testInstrument.get(),name,value);
TS_ASSERT_EQUALS(pmap.contains(m_testInstrument.get(), name, "int"), true);
TS_ASSERT_EQUALS(pmap.contains(m_testInstrument.get(), name, "double"), false);
}

void testParameter_Name_Matching_Is_Case_Insensitive()
{
IComponent_sptr parametrized = m_testInstrument->getChild(0);
const std::string camelCase("TestCase");
const double value(10.01);
ParameterMap pmap;
pmap.addDouble(parametrized.get(), camelCase, value);
Parameter_sptr fetched = pmap.get(parametrized.get(), "TESTCASE");
TSM_ASSERT("The parameter should be found by a case insensitive search",fetched);
}

void testRecursive_Parameter_Search_Moves_Up_The_Instrument_Tree()
{
// Attach a parameter to the instrument
const std::string topLevel("TopLevelParameter");
const int value(2);
ParameterMap pmap;
pmap.addInt(m_testInstrument.get(), topLevel, value);
//Ask for the parameter on a child
IComponent_sptr comp = m_testInstrument->getChild(0);
// Non-recursive should not find the parameter
Parameter_sptr fetched = pmap.get(comp.get(), topLevel);
TS_ASSERT_EQUALS(fetched, Parameter_sptr());
fetched = pmap.getRecursive(comp.get(), topLevel);
TS_ASSERT(fetched);
TS_ASSERT_EQUALS(fetched->value<int>(), value);
}

void testClearByName_Only_Removes_Named_Parameter()
{
ParameterMap pmap;
pmap.addDouble(m_testInstrument.get(), "first", 5.4);
pmap.addDouble(m_testInstrument.get(), "second", 10.3);
TS_ASSERT_EQUALS(pmap.size(), 2);
pmap.clearParametersByName("first");
TS_ASSERT_EQUALS(pmap.size(), 1);
// Has the correct one gone?
Parameter_sptr stored = pmap.get(m_testInstrument.get(), "second");
TSM_ASSERT("Parameter called second should still exist", stored);
stored = pmap.get(m_testInstrument.get(), "first");
TSM_ASSERT_EQUALS("Parameter called first should not exist", stored, Parameter_sptr());
}

void testClear_Results_In_Empty_Map()
{
ParameterMap pmap;
pmap.addInt(m_testInstrument.get(), "P1", 1);
pmap.addInt(m_testInstrument.get(), "P2", 2);
TS_ASSERT_EQUALS(pmap.size(), 2);
pmap.clear();
TS_ASSERT_EQUALS(pmap.size(), 0);
}

private:
Instrument_sptr m_testInstrument;
};


#endif /* PARAMETERMAPTEST_H_ */

0 comments on commit 3f2e8d5

Please sign in to comment.