First major wave of PQ refactoring, including but not limited to:

* Default unit is more clearly stated in one place, not restated in many different places, and is not always the SI unit
* Converter strategy pattern in CMeasurementUnit, covering linear, affine, and different kinds of sexagesimal units
* General reorganization of CMeasurementUnit construction and CPhysicalQuantity methods, not removing any behvaiour
* Move duplicated method unitFromSymbol from derived classes into base class CMeasurementUnit
* For DBus, CPhysicalQuantity marshals both in its own unit and in the default unit
This commit is contained in:
Mathew Sutcliffe
2013-08-18 23:40:52 +01:00
parent a8036f1ce5
commit da8ba9aac3
26 changed files with 809 additions and 957 deletions

View File

@@ -15,8 +15,6 @@ namespace BlackMiscTest
*/
void CTestPhysicalQuantities::unitsBasics()
{
QVERIFY2(CMeasurementPrefix::k() > CMeasurementPrefix::h(), "kilo > hecto");
// some tests on units
CLengthUnit du1 = CLengthUnit::m(); // Copy
CLengthUnit du2 = CLengthUnit::m(); // Copy
@@ -25,7 +23,6 @@ void CTestPhysicalQuantities::unitsBasics()
du2 = CLengthUnit::m(); // Copy
QVERIFY2(du1 == du2, "Compare by value 2");
QVERIFY2(CLengthUnit::m() == CLengthUnit::m(), "Compare by value");
QVERIFY2(CMeasurementPrefix::h() < CMeasurementPrefix::M(), "hecto < mega");
CFrequencyUnit fu1 = CFrequencyUnit::Hz();
QVERIFY2(fu1 != du1, "Hz must not be meter");
@@ -54,12 +51,12 @@ void CTestPhysicalQuantities::lengthBasics()
// epsilon tests
d1 = d2; // both in same unit
d1.addUnitValue(d1.getUnit().getEpsilon()); // this should be still the same
d1.addValueSameUnit(d1.getUnit().getEpsilon() / 2.0); // this should be still the same
QVERIFY2(d1 == d2, "Epsilon: 100cm + epsilon shall be 100cm");
QVERIFY2(!(d1 != d2), "Epsilon: 100cm + epsilon shall be still 100cm");
QVERIFY2(!(d1 > d2), "d1 shall not be greater");
d1.addUnitValue(d1.getUnit().getEpsilon()); // now over epsilon threshold
d1.addValueSameUnit(d1.getUnit().getEpsilon()); // now over epsilon threshold
QVERIFY2(d1 != d2, "Epsilon exceeded: 100 cm + 2 epsilon shall not be 100cm");
QVERIFY2(d1 > d2, "d1 shall be greater");
}
@@ -82,8 +79,8 @@ void CTestPhysicalQuantities::frequencyTests()
{
CFrequency f1(1, CFrequencyUnit::MHz());
QVERIFY2(f1.valueRounded(CFrequencyUnit::kHz(), 2) == 1000, "Mega is 1000kHz");
QVERIFY2(f1.unitValueToDouble() == 1 , "1MHz");
QVERIFY2(f1.convertedSiValueToDouble() == 1000000 , "1E6 Hz");
QVERIFY2(f1.value() == 1 , "1MHz");
QVERIFY2(f1.value(CFrequencyUnit::defaultUnit()) == 1000000 , "1E6 Hz");
CFrequency f2(CMeasurementPrefix::M().toDouble(), CFrequencyUnit::Hz()) ; // 1 Megahertz
QVERIFY2(f1 == f2 , "MHz is 1E6 Hz");
}
@@ -96,9 +93,13 @@ void CTestPhysicalQuantities::angleTests()
CAngle a1(180, CAngleUnit::deg());
CAngle a2(1.5 * CAngle::PI(), CAngleUnit::rad());
CAngle a3(35.4336, CAngleUnit::sexagesimalDeg()); // 35.72666
CAngle a4(35.436, CAngleUnit::sexagesimalDegMin()); // 35.72666
CAngle a5(-60.3015, CAngleUnit::sexagesimalDeg()); // negative angles = west longitude or south latitude
a2.switchUnit(CAngleUnit::deg());
QVERIFY2(a1.piFactor() == 1, qPrintable(QString("Pi should be 1PI, not %1").arg(a1.piFactor())));
QVERIFY2(a3.valueRounded(CAngleUnit::deg()) == 35.73, "Expecting 35.73");
QVERIFY2(a4.valueRounded(CAngleUnit::deg()) == 35.73, "Expecting 35.73");
QVERIFY2(a5.valueRounded(CAngleUnit::deg(), 4) == -60.5042, "Expecting -60.5042");
}
/*
@@ -107,11 +108,11 @@ void CTestPhysicalQuantities::angleTests()
void CTestPhysicalQuantities::massTests()
{
CMass w1(1000, CMassUnit::kg());
CMass w2(w1.unitValueToDouble(), CMassUnit::kg());
CMass w2(w1.value(), CMassUnit::kg());
w2.switchUnit(CMassUnit::t());
QVERIFY2(w2.unitValueToDouble() == 1, "1tonne shall be 1000kg");
QVERIFY2(w2.value() == 1, "1tonne shall be 1000kg");
w2.switchUnit(CMassUnit::lb());
QVERIFY2(w2.unitValueToDoubleRounded(2) == 2204.62, "1tonne shall be 2204pounds");
QVERIFY2(w2.valueRounded(2) == 2204.62, "1tonne shall be 2204pounds");
QVERIFY2(w1 == w2, "Masses shall be equal");
}
@@ -127,7 +128,7 @@ void CTestPhysicalQuantities::pressureTests()
// does not match exactly
QVERIFY2(p1 != p2, "Standard pressure test little difference");
QVERIFY2(p1.unitValueToDouble() == p4.unitValueToDouble(), "mbar/hPa test");
QVERIFY2(p1.value() == p4.value(), "mbar/hPa test");
}
/*
@@ -139,19 +140,25 @@ void CTestPhysicalQuantities::temperatureTests()
CTemperature t2(1, CTemperatureUnit::F()); // 1F
CTemperature t3(220.15, CTemperatureUnit::F());
CTemperature t4(10, CTemperatureUnit::F());
QVERIFY2(t1.convertedSiValueToDoubleRounded() == 273.15, qPrintable(QString("0C shall be 273.15K, not %1 K").arg(t1.convertedSiValueToDoubleRounded())));
QVERIFY2(t1.valueRounded(CTemperatureUnit::K()) == 273.15, qPrintable(QString("0C shall be 273.15K, not %1 K").arg(t1.valueRounded(CTemperatureUnit::K()))));
QVERIFY2(t2.valueRounded(CTemperatureUnit::C()) == -17.22, qPrintable(QString("1F shall be -17.22C, not %1 C").arg(t2.valueRounded(CTemperatureUnit::C()))));
QVERIFY2(t3.valueRounded(CTemperatureUnit::C()) == 104.53, qPrintable(QString("220.15F shall be 104.53C, not %1 C").arg(t3.valueRounded(CTemperatureUnit::C()))));
QVERIFY2(t4.valueRounded(CTemperatureUnit::K()) == 260.93, qPrintable(QString("10F shall be 260.93K, not %1 K").arg(t4.valueRounded(CTemperatureUnit::K()))));
}
/*
* Temperature tests
* Time tests
*/
void CTestPhysicalQuantities::timeTests()
{
CTime t1(1, CTimeUnit::h());
QVERIFY2(t1.convertedSiValueToDouble() == 3600, "1hour shall be 3600s");
CTime t2(1.5, CTimeUnit::h());
CTime t3(1.25, CTimeUnit::min());
CTime t4(1.0101, CTimeUnit::hms());
QVERIFY2(t1.value(CTimeUnit::defaultUnit()) == 3600, "1hour shall be 3600s");
QVERIFY2(t2.value(CTimeUnit::hrmin()) == 1.30, "1.5hour shall be 1h30m");
QVERIFY2(t3.value(CTimeUnit::minsec()) == 1.15, "1.25min shall be 1m15s");
QVERIFY2(t4.value(CTimeUnit::s()) == 3661, "1h01m01s shall be 3661s");
}
/*
@@ -160,13 +167,13 @@ void CTestPhysicalQuantities::timeTests()
void CTestPhysicalQuantities::accelerationTests()
{
CLength oneMeter(1, CLengthUnit::m());
double ftFactor = oneMeter.switchUnit(CLengthUnit::ft()).unitValueToDouble();
double ftFactor = oneMeter.switchUnit(CLengthUnit::ft()).value();
CAcceleration a1(10.0, CAccelerationUnit::m_s2());
CAcceleration a2(a1);
a1.switchUnit(CAccelerationUnit::ft_s2());
QVERIFY2(a1 == a2, "Accelerations should be similar");
QVERIFY2(BlackMisc::Math::CMath::round(a1.unitValueToDouble() * ftFactor, 6) == a2.unitValueToDoubleRounded(6),
QVERIFY2(BlackMisc::Math::CMath::round(a1.value() * ftFactor, 6) == a2.valueRounded(6),
"Numerical values should be equal");
}
@@ -177,13 +184,13 @@ void CTestPhysicalQuantities::memoryTests()
{
CLength *c = new CLength(100, CLengthUnit::m());
c->switchUnit(CLengthUnit::NM());
QVERIFY2(c->getUnit() == CLengthUnit::NM() && c->getConversionSiUnit() == CLengthUnit::m(),
QVERIFY2(c->getUnit() == CLengthUnit::NM() && CLengthUnit::defaultUnit() == CLengthUnit::m(),
"Testing distance units failed");
delete c;
CAngle *a = new CAngle(100, CAngleUnit::rad());
a->switchUnit(CAngleUnit::deg());
QVERIFY2(a->getUnit() == CAngleUnit::deg() && c->getConversionSiUnit() == CAngleUnit::rad(),
QVERIFY2(a->getUnit() == CAngleUnit::deg() && CAngleUnit::defaultUnit() == CAngleUnit::deg(),
"Testing angle units failed");
delete a;
}
@@ -201,7 +208,7 @@ void CTestPhysicalQuantities::basicArithmetic()
p3 /= 2.0;
QVERIFY2(p3 == p1, "Pressure needs to be the same (1time)");
p3 = p3 - p3;
QVERIFY2(p3.unitValueToDouble() == 0, "Value needs to be zero");
QVERIFY2(p3.value() == 0, "Value needs to be zero");
p3 = CPressure(1013, CPressureUnit::hPa());
QVERIFY2(p3 * 1.5 == 1.5 * p3, "Basic commutative test on PQ failed");
}