Skip to content

Commit

Permalink
Array length validator now accepts a minimum/maximum range. Refs #4845
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiSavici committed Feb 23, 2012
1 parent d9b3b72 commit f447e1c
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace Kernel
public:
ArrayLengthValidator();
ArrayLengthValidator(const size_t len);
ArrayLengthValidator(const size_t lenmin, const size_t lenmax);
virtual ~ArrayLengthValidator();

IValidator<std::vector<TYPE> >* clone();
Expand All @@ -46,19 +47,43 @@ namespace Kernel

/// Return if it has a length
bool hasLength() const;
/// Return if it has a length
bool hasMinLength() const;
/// Return if it has a length
bool hasMaxLength() const;
/// Return the length
const size_t& getLength() const;
/// Set lower bound value
/// Return the minimum length
const size_t& getMinLength() const;
/// Return the maximum length
const size_t& getMaxLength() const;
/// Set length
void setLength( const size_t& value );
/// Clear the length
void clearLength();
/// Set length min
void setLengthMin( const size_t& value );
/// Set length max
void setLengthMax( const size_t& value );
/// Clear minimum
void clearLengthMin();
/// Clear maximum
void clearLengthMax();

private:
std::string checkValidity( const std::vector<TYPE> &value ) const;
/// private variable containing the size of the array
size_t m_arraySize;
/// private variable, true if size is set, false if not
bool m_hasArraySize;
/// private variable containing the minimum size of the array
size_t m_arraySizeMin;
/// private variable, true if min size is set, false if not
bool m_hasArraySizeMin;
/// private variable containing the size max of the array
size_t m_arraySizeMax;
/// private variable, true if size max is set, false if not
bool m_hasArraySizeMax;
};


Expand Down
119 changes: 113 additions & 6 deletions Code/Mantid/Framework/Kernel/src/ArrayLengthValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ namespace Kernel
/** No arg constructor
*/
template <typename TYPE>
ArrayLengthValidator<TYPE>::ArrayLengthValidator():IValidator<std::vector<TYPE> >(),m_arraySize(size_t(0)),m_hasArraySize(false)
ArrayLengthValidator<TYPE>::ArrayLengthValidator():IValidator<std::vector<TYPE> >(),m_arraySize(size_t(0)),m_hasArraySize(false),
m_arraySizeMin(size_t(0)),m_hasArraySizeMin(false),m_arraySizeMax(size_t(0)),m_hasArraySizeMax(false)
{
}
//----------------------------------------------------------------------------------------------
/** Constructor
* @param len:: the legth of the array
*/
template <typename TYPE>
ArrayLengthValidator<TYPE>::ArrayLengthValidator(const size_t len):IValidator<std::vector<TYPE> >(),m_arraySize(size_t(len)),m_hasArraySize(true)
ArrayLengthValidator<TYPE>::ArrayLengthValidator(const size_t len):IValidator<std::vector<TYPE> >(),m_arraySize(size_t(len)),m_hasArraySize(true),
m_arraySizeMin(size_t(0)),m_hasArraySizeMin(false),m_arraySizeMax(size_t(0)),m_hasArraySizeMax(false)
{
}
//----------------------------------------------------------------------------------------------
/** Constructor
* @param lenmin:: the minimum legth of the array
* @param lenmax:: the maximum legth of the array
*/
template <typename TYPE>
ArrayLengthValidator<TYPE>::ArrayLengthValidator(const size_t lenmin, const size_t lenmax):IValidator<std::vector<TYPE> >(),m_arraySize(size_t(0)),
m_hasArraySize(false), m_arraySizeMin(size_t(lenmin)),m_hasArraySizeMin(true),m_arraySizeMax(size_t(lenmax)),m_hasArraySizeMax(true)
{
}
//----------------------------------------------------------------------------------------------
Expand All @@ -42,6 +54,27 @@ namespace Kernel
return this->m_hasArraySize;
}

/**
Check if minimum length is set
@returns true/false
*/
template <typename TYPE>
bool ArrayLengthValidator<TYPE>::hasMinLength() const
{
return this->m_hasArraySizeMin;
}

/**
Check if maximum length is set
@returns true/false
*/
template <typename TYPE>
bool ArrayLengthValidator<TYPE>::hasMaxLength() const
{
return this->m_hasArraySizeMax;
}


/**
Function to retun the set length
@returns m_arraySize
Expand All @@ -53,14 +86,59 @@ namespace Kernel
}

/**
Function to set the length
Function to retun the set minimum length
@returns m_arraySize
*/
template <typename TYPE>
const size_t& ArrayLengthValidator<TYPE>:: getMinLength() const
{
return this->m_arraySizeMin;
}

/**
Function to retun the set maximum length
@returns m_arraySize
*/
template <typename TYPE>
const size_t& ArrayLengthValidator<TYPE>:: getMaxLength() const
{
return this->m_arraySizeMax;
}

/**
Function to set the length. It will automatically clear the minimum and maximum
@param value:: size_t type
*/
template <typename TYPE>
void ArrayLengthValidator<TYPE>::setLength(const size_t &value)
{
this->m_hasArraySize=true;
this->m_arraySize=value;
this->clearLengthMax();
this->clearLengthMin();
}

/**
Function to set the minimum length. It will automatically clear the set length
@param value:: size_t type
*/
template <typename TYPE>
void ArrayLengthValidator<TYPE>::setLengthMin(const size_t &value)
{
this->m_hasArraySizeMin=true;
this->m_arraySizeMin=value;
this->clearLength();
}
/**
Function to set the maximum length. It will automatically clear the set length
@param value:: size_t type
*/
template <typename TYPE>
void ArrayLengthValidator<TYPE>::setLengthMax(const size_t &value)
{
this->m_hasArraySizeMax=true;
this->m_arraySizeMax=value;
this->clearLength();
}

/**
Expand All @@ -72,6 +150,27 @@ namespace Kernel
this->m_hasArraySize=false;
this->m_arraySize=size_t(0);
}

/**
Function to unset the minimum length. It sets m_hasArraySizeMin to false, and the m_arraySizeMin to 0
*/
template <typename TYPE>
void ArrayLengthValidator<TYPE>::clearLengthMin()
{
this->m_hasArraySizeMin=false;
this->m_arraySizeMin=size_t(0);
}

/**
Function to unset the maximum length. It sets m_hasArraySizeMax to false, and the m_arraySizeMax to 0
*/
template <typename TYPE>
void ArrayLengthValidator<TYPE>::clearLengthMax()
{
this->m_hasArraySizeMax=false;
this->m_arraySizeMax=size_t(0);
}

/**
Clone function
@returns a clone of the validator
Expand Down Expand Up @@ -99,11 +198,19 @@ namespace Kernel
template <typename TYPE>
std::string ArrayLengthValidator<TYPE>::checkValidity( const std::vector<TYPE> &value ) const
{
if (value.size()==this->m_arraySize )
if (this->hasLength()&&value.size()!=this->m_arraySize )
{
return "Incorrect size";
}
if (this->hasMinLength() && value.size()<this->m_arraySizeMin)
{
return "Array size too short";
}
if (this->hasMaxLength() && value.size()>this->m_arraySizeMax)
{
return "";
return "Array size too long";
}
else return"Incorrect size";
return "";
}
// Required explicit instantiations
template class ArrayLengthValidator<double>;
Expand Down
45 changes: 44 additions & 1 deletion Code/Mantid/Framework/Kernel/test/ArrayLengthValidatorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ class ArrayLengthValidatorTest : public CxxTest::TestSuite
/// test constructors, both empty and with length, also hasLength and getLength
void testConstructor()
{
ArrayLengthValidator<int> av1,av2(3);
ArrayLengthValidator<int> av1,av2(3),av3(4,5);
TS_ASSERT_EQUALS(av1.hasLength(),false);
TS_ASSERT_EQUALS(av1.hasMinLength(),false);
TS_ASSERT_EQUALS(av1.hasMaxLength(),false);
TS_ASSERT_EQUALS(av2.hasLength(),true);
TS_ASSERT_EQUALS(av2.hasMinLength(),false);
TS_ASSERT_EQUALS(av2.hasMaxLength(),false);
TS_ASSERT_EQUALS(av2.getLength(),3);
TS_ASSERT_EQUALS(av3.hasLength(),false);
TS_ASSERT_EQUALS(av3.hasMinLength(),true);
TS_ASSERT_EQUALS(av3.hasMaxLength(),true);
TS_ASSERT_EQUALS(av3.getMinLength(),4);
TS_ASSERT_EQUALS(av3.getMaxLength(),5);
}

/// test the clone function
Expand All @@ -45,12 +54,32 @@ class ArrayLengthValidatorTest : public CxxTest::TestSuite
{
ArrayLengthValidator<int> av1;
TS_ASSERT_EQUALS(av1.hasLength(),false);
TS_ASSERT_EQUALS(av1.hasMinLength(),false);
TS_ASSERT_EQUALS(av1.hasMaxLength(),false);
av1.setLength(4);
TS_ASSERT_EQUALS(av1.hasLength(),true);
TS_ASSERT_EQUALS(av1.getLength(),4);
TS_ASSERT_EQUALS(av1.hasMinLength(),false);
TS_ASSERT_EQUALS(av1.hasMaxLength(),false);
av1.clearLength();
TS_ASSERT_EQUALS(av1.hasLength(),false);
TS_ASSERT_EQUALS(av1.getLength(),0);
TS_ASSERT_EQUALS(av1.hasMinLength(),false);
TS_ASSERT_EQUALS(av1.hasMaxLength(),false);
av1.setLengthMax(4);
TS_ASSERT_EQUALS(av1.hasLength(),false);
TS_ASSERT_EQUALS(av1.getMaxLength(),4);
TS_ASSERT_EQUALS(av1.hasMinLength(),false);
TS_ASSERT_EQUALS(av1.hasMaxLength(),true);
av1.setLengthMin(2);
TS_ASSERT_EQUALS(av1.hasLength(),false);
TS_ASSERT_EQUALS(av1.getMinLength(),2);
TS_ASSERT_EQUALS(av1.hasMinLength(),true);
TS_ASSERT_EQUALS(av1.hasMaxLength(),true);
av1.clearLengthMax();
TS_ASSERT_EQUALS(av1.hasLength(),false);
TS_ASSERT_EQUALS(av1.hasMinLength(),true);
TS_ASSERT_EQUALS(av1.hasMaxLength(),false);
}

/// test validator, both for OK and for different length
Expand All @@ -64,6 +93,20 @@ class ArrayLengthValidatorTest : public CxxTest::TestSuite
a.push_back(11);
TS_ASSERT_EQUALS(vi.isValid(a).length(),0);
}

void testValidatorRange()
{
ArrayLengthValidator<int> vi(2,3);
std::vector<int> a;
a.push_back(3);
TS_ASSERT_DIFFERS(vi.isValid(a).length(),0);
a.push_back(11);
TS_ASSERT_EQUALS(vi.isValid(a).length(),0);
a.push_back(12);
TS_ASSERT_EQUALS(vi.isValid(a).length(),0);
a.push_back(21);
TS_ASSERT_DIFFERS(vi.isValid(a).length(),0);
}
};


Expand Down

0 comments on commit f447e1c

Please sign in to comment.