Skip to content

Commit

Permalink
refs #5592. Calculates the correct sizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Jul 9, 2012
1 parent eab6fbe commit 32301fd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ std::string toString(const std::vector<std::vector<T> >& value)
return result.str();
}

/// Specialisation for any type, should be appropriate for properties with a single value.
template <typename T>
int findSize(const T& value)
{
return 1;
}

/// Specialisation for properties that are of type vector.
template <typename T>
int findSize(const std::vector<T>& value)
{
return static_cast<int>(value.size());
}

// ------------- Convert strings to values
template <typename T>
inline void appendValue(const std::string& strvalue, std::vector<T>& value)
Expand Down Expand Up @@ -306,6 +320,13 @@ class PropertyWithValue : public Property
return toString(m_value);
}

/** Get the size of the property.
*/
virtual int size() const
{
return findSize(m_value);
}

/** Get the value the property was initialised with -its default value
* @return The default value
*/
Expand Down
19 changes: 19 additions & 0 deletions Code/Mantid/Framework/Kernel/test/ArrayPropertyTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ class ArrayPropertyTest : public CxxTest::TestSuite
TS_ASSERT( ! sp.operator()()[2].compare("yyy") )
}

void testSize()
{
TS_ASSERT_EQUALS(0, iProp->size());
TS_ASSERT_EQUALS(0, dProp->size());
TS_ASSERT_EQUALS(0, sProp->size());

// Make something bigger and test that.
Property* a = new ArrayProperty<int>("int_property", "1, 2, 3");
TS_ASSERT_EQUALS(3, a->size());
delete a;

// Test vector of vector.
std::vector<std::vector<int> > input;
input.push_back(std::vector<int>(10)); // Make it 10 elements long, but should only be the size of the parent vector that is counted.
Property* b = new ArrayProperty<std::vector<int> >("vec_property", input);
TS_ASSERT_EQUALS(1, b->size());
delete b;
}

void testConstructorByString()
{
ArrayProperty<int> i("i","1,2,3");
Expand Down
19 changes: 19 additions & 0 deletions Code/Mantid/Framework/Kernel/test/PropertyWithValueTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ class PropertyWithValueTest : public CxxTest::TestSuite
TS_ASSERT( ! lProp->value().compare("-9876543210987654") );
}

void testSize()
{
// Test single value property.
TS_ASSERT_EQUALS(1, iProp->size());
TS_ASSERT_EQUALS(1, dProp->size());
TS_ASSERT_EQUALS(1, sProp->size());
TS_ASSERT_EQUALS(1, lProp->size());

// Test vector value property.
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
PropertyWithValue< std::vector<int> > * pv = new PropertyWithValue< std::vector<int> >("some_array", v);
TS_ASSERT_EQUALS(int(v.size()), pv->size());

delete pv;
}

void testSetValue()
{
PropertyWithValue<int> i("test", 1);
Expand Down

0 comments on commit 32301fd

Please sign in to comment.