refs #197 fixing (hopefully) comparison between null quantities

and related rationalization of PQ comparison.
This commit is contained in:
Mathew Sutcliffe
2014-04-08 16:55:11 +01:00
committed by Klaus Basan
parent 8ba2badb61
commit a4e3ac6f40
5 changed files with 33 additions and 11 deletions

View File

@@ -39,7 +39,7 @@ namespace BlackMisc
*/
double CMeasurementUnit::convertFrom(double value, const CMeasurementUnit &unit) const
{
if (this->isNull() || unit.isNull()) return -1; // models the previous behaviour of using -1 as a sentinel value
if (this->isNull() || unit.isNull()) return 0;
if (this->m_converter == unit.m_converter) return value;
return this->m_converter->fromDefault(unit.m_converter->toDefault(value));
}

View File

@@ -262,7 +262,7 @@ namespace BlackMisc
* \brief Default constructor for meta system
* \remarks Only public because the need, to use this with the metasystem
*/
CMeasurementUnit() : m_name("none"), m_symbol(""), m_epsilon(0), m_displayDigits(0), m_converter(new NilConverter())
CMeasurementUnit() : m_name("none"), m_symbol(""), m_epsilon(0), m_displayDigits(0)
{}
/*!
@@ -352,6 +352,7 @@ namespace BlackMisc
*/
bool isEpsilon(double value) const
{
if (this->isNull()) return false;
if (value == 0) return true;
return abs(value) <= this->m_epsilon;
}

View File

@@ -16,7 +16,7 @@ namespace BlackMisc
* Constructor by double
*/
template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(double value, const MU &unit) :
m_value(value), m_unit(unit)
m_value(unit.isNull() ? 0.0 : value), m_unit(unit)
{
// void
}
@@ -37,7 +37,10 @@ namespace BlackMisc
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator ==(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return true;
if (this->isNull() && other.isNull()) return true; // preliminary fix
if (this->isNull()) return other.isNull();
if (other.isNull()) return false;
double diff = std::abs(this->m_value - other.value(this->m_unit));
return diff <= this->m_unit.getEpsilon();
}
@@ -181,7 +184,8 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator <(const CPhysicalQuantity<MU, PQ> &other) const
{
if ((*this) == other) return false;
if (*this == other) return false;
if (this->isNull() || other.isNull()) return false;
return (this->m_value < other.value(this->m_unit));
}
@@ -191,7 +195,6 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator >(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return false;
return other < *this;
}
@@ -200,8 +203,8 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator >=(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return true;
return !(*this < other);
if (*this == other) return true;
return *this > other;
}
/*
@@ -209,8 +212,8 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator <=(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return true;
return !(*this > other);
if (*this == other) return true;
return *this < other;
}
/*
@@ -325,6 +328,9 @@ namespace BlackMisc
{
const auto &other = static_cast<const CPhysicalQuantity &>(otherBase);
if (this->isNull() > other.isNull()) { return -1; }
if (this->isNull() < other.isNull()) { return 1; }
if (*this < other) { return -1; }
else if (*this > other) { return 1; }
else { return 0; }

View File

@@ -109,13 +109,20 @@ namespace BlackMisc
//! \brief Value in current unit
double value() const
{
if (this->isNull())
{
return 0.0;
}
return this->m_value;
}
//! \brief Set value in current unit
void setCurrentUnitValue(double value)
{
this->m_value = value;
if (! this->isNull())
{
this->m_value = value;
}
}
//! \brief Rounded value in given unit

View File

@@ -44,6 +44,14 @@ namespace BlackMiscTest
CLength l2(0, CLengthUnit::nullUnit());
QVERIFY2(l1 == l2, "Null lengths should be equal");
CLength l3(0, CLengthUnit::m());
CLength l4(-1, CLengthUnit::m());
QVERIFY2(l1 != l3, "Null length and non-null length should not be equal");
QVERIFY2(l1 != l4, "Null length and non-null length should not be equal");
QVERIFY2(!(l1 < l4), "Null length and non-null length should not be comparable");
QVERIFY2(!(l1 > l4), "Null length and non-null length should not be comparable");
QVERIFY2(compare(l1, l4) < 0, "Null length and non-null length should be sortable");
QVariant station1qv = QVariant::fromValue(station1);
QVERIFY2(station1 == station1, "Station should be equal");