Add MAX range object

This commit is contained in:
Danny Robson 2012-06-15 16:38:10 +10:00
parent 7d61acc8c1
commit c0dcdd89d9
3 changed files with 26 additions and 6 deletions

View File

@ -177,10 +177,16 @@ range<T>::operator ==(const range<T> &rhs) const
template <typename T>
const range<T>
range<T>::UNLIMITED (numeric_limits <T>::is_integer ? numeric_limits <T>::min () :
-numeric_limits <T>::infinity (),
numeric_limits <T>::is_integer ? numeric_limits <T>::max () :
numeric_limits <T>::infinity ());
range<T>::UNLIMITED (numeric_limits<T>::has_infinity ? -numeric_limits<T>::infinity () :
numeric_limits<T>::lowest (),
numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity () :
numeric_limits<T>::max ());
template <typename T>
const range<T>
range<T>::MAX (numeric_limits<T>::lowest (),
numeric_limits<T>::max ());
template <typename T>
const range<T>

View File

@ -73,9 +73,10 @@ namespace util {
{ return !(*this == rhs); }
/// A range which is guaranteed to contain all elements type T
static const range <T> UNLIMITED;
static const range<T> UNLIMITED;
static const range<T> MAX;
/// A range which only contains elements between 0 and 1 inclusive
static const range <T> UNIT;
static const range<T> UNIT;
void sanity (void) const;
};

View File

@ -9,25 +9,38 @@ using namespace util;
int
main (int, char **) {
// Check some simple cases close to the edges of a unit range. Tests float rounding.
CHECK_HARD ( range<double>::UNIT.contains ( 0.0));
CHECK_HARD ( range<double>::UNIT.contains ( 0.5));
CHECK_HARD ( range<double>::UNIT.contains ( 1.0));
CHECK_HARD ( range<double>::UNIT.contains (std::numeric_limits<double>::min ()));
CHECK_HARD (!range<double>::UNIT.contains (-0.00001));
CHECK_HARD (!range<double>::UNIT.contains ( 1.00001));
// Check edge cases of unit with integer values
CHECK_HARD ( range<uint16_t>::UNIT.contains (0));
CHECK_HARD ( range<uint16_t>::UNIT.contains (1));
CHECK_HARD (!range<uint16_t>::UNIT.contains (2));
CHECK_HARD (!range<uint16_t>::UNIT.contains (numeric_limits <uint16_t>::max ()));
// Check the inclusivity of UNLIMITED with special floating values
CHECK_HARD ( range<double>::UNLIMITED.contains (0.0));
CHECK_HARD ( range<double>::UNLIMITED.contains (+numeric_limits<double>::infinity ()));
CHECK_HARD ( range<double>::UNLIMITED.contains (-numeric_limits<double>::infinity ()));
CHECK_HARD (!range<double>::UNLIMITED.contains ( numeric_limits<double>::quiet_NaN ()));
// Check the inclusivity of UNLIMITED with some large numbers
CHECK_HARD ( range<uint16_t>::UNLIMITED.contains (numeric_limits<uint16_t>::min()));
CHECK_HARD ( range<uint16_t>::UNLIMITED.contains (numeric_limits<uint16_t>::max()));
// Check inclusivity of MAX
CHECK_HARD (!range<double>::MAX.contains ( numeric_limits<double>::infinity ()));
CHECK_HARD (!range<double>::MAX.contains (-numeric_limits<double>::infinity ()));
CHECK_HARD ( range<uint16_t>::MAX.contains (numeric_limits<uint16_t>::min()));
CHECK_HARD ( range<uint16_t>::MAX.contains (numeric_limits<uint16_t>::max()));
// Check that expansion via NaN is a noop
{
range<double> initial_nan (numeric_limits<double>::quiet_NaN (),
numeric_limits<double>::quiet_NaN ());