From a4e3ac6f407f0a1fe7e623139b4ae411af1979c7 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Tue, 8 Apr 2014 16:55:11 +0100 Subject: [PATCH] refs #197 fixing (hopefully) comparison between null quantities and related rationalization of PQ comparison. --- src/blackmisc/pqbase.cpp | 2 +- src/blackmisc/pqbase.h | 3 ++- src/blackmisc/pqphysicalquantity.cpp | 22 ++++++++++++++-------- src/blackmisc/pqphysicalquantity.h | 9 ++++++++- tests/blackmisc/testvariantandmap.cpp | 8 ++++++++ 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/blackmisc/pqbase.cpp b/src/blackmisc/pqbase.cpp index 597cbcaa2..e1384185d 100644 --- a/src/blackmisc/pqbase.cpp +++ b/src/blackmisc/pqbase.cpp @@ -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)); } diff --git a/src/blackmisc/pqbase.h b/src/blackmisc/pqbase.h index 654527411..80ec73734 100644 --- a/src/blackmisc/pqbase.h +++ b/src/blackmisc/pqbase.h @@ -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; } diff --git a/src/blackmisc/pqphysicalquantity.cpp b/src/blackmisc/pqphysicalquantity.cpp index 8bf4d92cf..fd21f8aa5 100644 --- a/src/blackmisc/pqphysicalquantity.cpp +++ b/src/blackmisc/pqphysicalquantity.cpp @@ -16,7 +16,7 @@ namespace BlackMisc * Constructor by double */ template CPhysicalQuantity::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 bool CPhysicalQuantity::operator ==(const CPhysicalQuantity &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 bool CPhysicalQuantity::operator <(const CPhysicalQuantity &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 bool CPhysicalQuantity::operator >(const CPhysicalQuantity &other) const { - if (this == &other) return false; return other < *this; } @@ -200,8 +203,8 @@ namespace BlackMisc */ template bool CPhysicalQuantity::operator >=(const CPhysicalQuantity &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 bool CPhysicalQuantity::operator <=(const CPhysicalQuantity &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(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; } diff --git a/src/blackmisc/pqphysicalquantity.h b/src/blackmisc/pqphysicalquantity.h index b9fe55791..98ea75b10 100644 --- a/src/blackmisc/pqphysicalquantity.h +++ b/src/blackmisc/pqphysicalquantity.h @@ -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 diff --git a/tests/blackmisc/testvariantandmap.cpp b/tests/blackmisc/testvariantandmap.cpp index 5cf979f6b..c00877e8d 100644 --- a/tests/blackmisc/testvariantandmap.cpp +++ b/tests/blackmisc/testvariantandmap.cpp @@ -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");