diff --git a/src/blackmisc/pq/physicalquantity.cpp b/src/blackmisc/pq/physicalquantity.cpp index 764646cdf..65009227a 100644 --- a/src/blackmisc/pq/physicalquantity.cpp +++ b/src/blackmisc/pq/physicalquantity.cpp @@ -222,7 +222,10 @@ namespace BlackMisc bool CPhysicalQuantity::lessThan(const CPhysicalQuantity &other) const { if (*this == other) return false; - if (this->isNull() || other.isNull()) return false; + + if (isNull() < other.isNull()) { return true; } + if (isNull() > other.isNull()) { return false; } + if (isNull() && other.isNull()) { return false; } return (m_value < other.value(m_unit)); } diff --git a/tests/blackmisc/pq/testphysicalquantities/testphysicalquantities.cpp b/tests/blackmisc/pq/testphysicalquantities/testphysicalquantities.cpp index c350faaa4..7fd415003 100644 --- a/tests/blackmisc/pq/testphysicalquantities/testphysicalquantities.cpp +++ b/tests/blackmisc/pq/testphysicalquantities/testphysicalquantities.cpp @@ -96,6 +96,14 @@ namespace BlackMiscTest CFrequencyUnit fu1 = CFrequencyUnit::Hz(); QVERIFY2(fu1 != du1, "Hz must not be meter"); + + // null comparisons + const CLength null(CLength::null()); + const CLength nonNull(1, CLengthUnit::m()); + QVERIFY2(null == CLength::null(), "null is equal to null"); + QVERIFY2(!(null < CLength::null()), "null is equivalent to null"); + QVERIFY2(null != nonNull, "null is not equal to non-null"); + QVERIFY2((null < nonNull) != (null > nonNull), "null is ordered wrt non-null"); } void CTestPhysicalQuantities::lengthBasics() diff --git a/tests/blackmisc/testvariantandmap/testvariantandmap.cpp b/tests/blackmisc/testvariantandmap/testvariantandmap.cpp index 52a740395..e55cdba4f 100644 --- a/tests/blackmisc/testvariantandmap/testvariantandmap.cpp +++ b/tests/blackmisc/testvariantandmap/testvariantandmap.cpp @@ -85,9 +85,8 @@ namespace BlackMiscTest 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"); + QVERIFY2((l1 < l4) != (l1 > l4), "Null length and non-null length should be comparable"); + QVERIFY2(compare(l1, l4) != 0, "Null length and non-null length should be sortable"); CVariant station1qv = CVariant::fromValue(station1); QVERIFY2(station1 == station1, "Station should be equal");