Implemented commutative multiplications as friends in the templates, further test cases (unit tests), fixed Doxygen attributes, renamed test classes

This commit is contained in:
Klaus Basan
2013-04-25 01:56:18 +02:00
parent 919913dd95
commit a31e405b6b
22 changed files with 197 additions and 88 deletions

View File

@@ -11,8 +11,7 @@ int BlackMiscTest::CSamplesVectorMatrix::samples()
CVector3D v2(1, 2, 3);
qDebug() << v1 << "value:" << v2[2] << v2.magnitude();
v2 *= v2; // v2 * v2
qDebug() << v2;
// v2 = v1 * v1;
qDebug() << v2 << 2 * v1 << v1 *v1;
CMatrix3x3 m;
CMatrix3x3 mr = m;

View File

@@ -124,7 +124,7 @@ public:
/*!
* \brief Set longitude
* \param latitude
* \param longitude
*/
void setLongitude(CLongitude longitude)
{

View File

@@ -70,29 +70,24 @@ CCoordinateEcef CCoordinateTransformation::toEcef(const CCoordinateGeodetic &geo
CLatitude lat = geo.latitude();
CLongitude lon = geo.longitude();
double latDeg = lat.value(CAngleUnit::deg());
double lonDeg = lon.value(CAngleUnit::deg());
double phi = lat.value(CAngleUnit::rad());
double lambdaRad = lon.value(CAngleUnit::rad());
double sphi = sin(phi);
double cphi = 0;
if (abs(latDeg) != 90) cphi = cos(phi);
double cphi = cos(phi);
double n = EarthRadiusMeters() / sqrt(1 - e2() * CMath::square(sphi));
double slambda = 0;
if (lonDeg != -180) slambda = sin(lambdaRad);
double slambda = sin(lambdaRad);
double clambda = 0;
if (abs(lonDeg) != 90) clambda = cos(lambdaRad);
double clambda = cos(lambdaRad);
double h = geo.height().convertedSiValueToDouble();
double X = (n + h) * cphi;
double Y = X * slambda;
X *= clambda;
double Z = (e2m() * n + h) * sphi;
CCoordinateEcef result(X, Y, Z);
double x = (n + h) * cphi;
double y = x * slambda;
x *= clambda;
double z = (e2m() * n + h) * sphi;
CCoordinateEcef result(x, y, z);
return result;
}

View File

@@ -121,7 +121,7 @@ public:
/*!
* \brief ECEF to Geodetic
* \param geo
* \param ecef
* \return
*/
static CCoordinateGeodetic toGeodetic(const CCoordinateEcef &ecef);

View File

@@ -27,7 +27,7 @@ public:
/*!
* \brief Copy constructor
* \param other
* \param otherMatrix
*/
CMatrix1x3(const CMatrix1x3 &otherMatrix) : CMatrixBase(otherMatrix) {}

View File

@@ -42,7 +42,7 @@ public:
/*!
* \brief Copy constructor
* \param other
* \param otherMatrix
*/
CMatrix3x1(const CMatrix3x1 &otherMatrix) : CMatrixBase(otherMatrix) {}

View File

@@ -26,7 +26,7 @@ public:
/*!
* \brief Copy constructor
* \param other
* \param otherMatrix
*/
CMatrix3x3(const CMatrix3x3 &otherMatrix) : CMatrixBase(otherMatrix) {}
@@ -101,7 +101,7 @@ public:
/*!
* \brief Operator *
* \param multiply
* \param otherMatrix
* \return
*/
CMatrix3x3 operator *(const CMatrix3x3 &otherMatrix) const

View File

@@ -87,7 +87,7 @@ template<class ImplMatrix, int Rows, int Columns> bool CMatrixBase<ImplMatrix, R
/*
* Round all values
*/
template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, Rows, Columns>::round()
template<class ImplMatrix, int Rows, int Columns> ImplMatrix &CMatrixBase<ImplMatrix, Rows, Columns>::round()
{
for (int r = 0; r < Rows; r++)
{
@@ -96,6 +96,7 @@ template<class ImplMatrix, int Rows, int Columns> void CMatrixBase<ImplMatrix, R
this->m_matrix(r, c) = CMath::roundEpsilon(this->m_matrix(r, c), 1E-10);
}
}
return static_cast<ImplMatrix &>(*this);
}
/*

View File

@@ -39,7 +39,7 @@ public:
/*!
* \brief Copy constructor
* \param other
* \param otherMatrix
*/
CMatrixBase(const CMatrixBase &otherMatrix) : m_matrix(otherMatrix.m_matrix) {}
@@ -81,7 +81,7 @@ public:
/*!
* \brief Assigment operator =
* \param multiply
* \param otherMatrix
* \return
*/
CMatrixBase &operator =(const CMatrixBase &otherMatrix)
@@ -116,11 +116,22 @@ public:
}
/*!
* \brief Multiply with 3D vector operator *
* \param vector
* \brief Operator to support commutative multiplication
* \param factor
* \param otherMatrix
* \return
*/
template<class ImplVector> ImplVector operator*(const ImplVector vector) const;
friend ImplMatrix operator *(double factor, const ImplMatrix &otherMatrix)
{
return otherMatrix * factor;
}
/*!
* \brief Multiply with 3D vector operator *
* \param matrix
* \return
*/
template<class ImplVector> ImplVector operator*(const ImplVector matrix) const;
/*!
* \brief Operator /=
@@ -267,7 +278,7 @@ public:
/*!
* \brief Round all values
*/
void round();
ImplMatrix &round();
/*!
* \brief Get element
@@ -286,7 +297,8 @@ public:
void setElement(size_t row, size_t column, double value);
/*!
* \brief Get element by ()
* \brief Get element by operator () modifying
* \param row
* \param column
* \return
*/
@@ -297,7 +309,8 @@ public:
}
/*!
* \brief Get element by ()
* \brief Get element by operator () read only
* \param row
* \param column
* \return
*/

View File

@@ -33,7 +33,7 @@ protected:
/*!
* \brief Default constructor
*/
CVector3DBase() {}
CVector3DBase() : m_i(0.0), m_j(0.0), m_k(0.0) {}
/*!
* \brief Constructor by values
@@ -124,8 +124,8 @@ public:
/*!
* \brief Get element by ()
* \param column
* \brief Get row element by ()
* \param row
* \return
*/
double operator()(size_t row) const { return this->getElement(row); }
@@ -272,6 +272,17 @@ public:
return v;
}
/*!
* \brief Operator to support commutative multiplication
* \param factor
* \param otherVector
* \return
*/
friend ImplClass operator *(double factor, const ImplClass &otherVector)
{
return otherVector * factor;
}
/*!
* \brief Divide by scalar
* \param divisor
@@ -346,8 +357,7 @@ public:
void matrixMultiplication(const CMatrix3x3 &matrix);
/*!
* \brief Reciporcal value
* \param otherVector
* \brief Reciprocal value
* \return
*/
ImplClass reciprocalValues() const
@@ -394,15 +404,16 @@ public:
/*!
* \brief Round this vector
* \return
*/
void round()
ImplClass &round()
{
const double epsilon = 1E-10;
this->m_i = BlackMisc::Math::CMath::roundEpsilon(this->m_i, epsilon);
this->m_j = BlackMisc::Math::CMath::roundEpsilon(this->m_j, epsilon);
this->m_k = BlackMisc::Math::CMath::roundEpsilon(this->m_k, epsilon);
return static_cast<ImplClass &>(*this);
}
};
} // namespace

View File

@@ -219,7 +219,7 @@ public:
qint32 convertedSiValueToInteger() const
{
return static_cast<qint32>(
BlackMisc::Math::CMath::round(this->m_convertedSiUnitValueD, 0));
BlackMisc::Math::CMath::round(this->m_convertedSiUnitValueD, 0));
}
/*!
@@ -257,11 +257,11 @@ public:
*/
void substractUnitValue(double value);
/*!
* \brief Multiply operator *=
* \param multiply
* \return
*/
/*!
* \brief Multiply operator *=
* \param multiply
* \return
*/
CPhysicalQuantity &operator *=(double multiply);
/*!
@@ -278,6 +278,17 @@ public:
*/
PQ operator *(double multiply) const;
/*!
* \brief Operator to support commutative multiplication
* \param factor
* \param otherQuantity
* \return
*/
friend PQ operator *(double factor, const PQ &otherQuantity)
{
return otherQuantity * factor;
}
/*!
* \brief Operator /
* \param divide

View File

@@ -6,18 +6,20 @@ TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
DEPENDPATH += . ../../src
DEPENDPATH += . ../../src/blackmisc
INCLUDEPATH += . ../../src
SOURCES += main.cpp testmain.cpp \
testphysicalquantitiesbase.cpp \
testaviationbase.cpp \
testvectormatrixbase.cpp
testphysicalquantities.cpp \
testvectormatrix.cpp \
testaviation.cpp \
testgeo.cpp
HEADERS += testmain.h \
testphysicalquantitiesbase.h \
blackmisctest.h \
testaviationbase.h \
testvectormatrixbase.h
testphysicalquantities.h \
testvectormatrix.h \
testaviation.h \
testgeo.h
win32-msvc* {
PRE_TARGETDEPS += ../../lib/blackmisc.lib

View File

@@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "testaviationbase.h"
#include "testaviation.h"
using namespace BlackMisc::Aviation;
using namespace BlackMisc::PhysicalQuantities;
@@ -13,7 +13,7 @@ namespace BlackMiscTest {
/*
* Constructor
*/
CTestAviationBase::CTestAviationBase(QObject *parent): QObject(parent)
CTestAviation::CTestAviation(QObject *parent): QObject(parent)
{
// void
}
@@ -21,7 +21,7 @@ CTestAviationBase::CTestAviationBase(QObject *parent): QObject(parent)
/*
* Basic tests
*/
void CTestAviationBase::headingBasics()
void CTestAviation::headingBasics()
{
CHeading h1(180, true, CAngleUnit::deg());
CHeading h2(180, false, CAngleUnit::deg());
@@ -45,7 +45,7 @@ void CTestAviationBase::headingBasics()
/*
* Vertical positions
*/
void CTestAviationBase::verticalPosition()
void CTestAviation::verticalPosition()
{
CAviationVerticalPositions vp1 = CAviationVerticalPositions::fromAltitudeAndElevationInFt(10000.0, 3000.0);
CAviationVerticalPositions vp2 = vp1;
@@ -55,7 +55,7 @@ void CTestAviationBase::verticalPosition()
/*
* COM and NAV units
*/
void CTestAviationBase::comAndNav()
void CTestAviation::comAndNav()
{
CComSystem c1 = CComSystem::getCom1System(122.8);
CComSystem c2 = CComSystem::getCom2System(122.8);
@@ -72,7 +72,7 @@ void CTestAviationBase::comAndNav()
/*
* COM and NAV units
*/
void CTestAviationBase::transponder()
void CTestAviation::transponder()
{
CTransponder t1 = CTransponder::getStandardTransponder(7000, CTransponder::StateStandby);
CTransponder t2 = t1;

View File

@@ -20,7 +20,7 @@ namespace BlackMiscTest
/*!
* \brief Aviation classes basic tests
*/
class CTestAviationBase : public QObject
class CTestAviation : public QObject
{
Q_OBJECT
@@ -29,7 +29,7 @@ public:
* \brief Standard test case constructor
* \param parent
*/
explicit CTestAviationBase(QObject *parent = 0);
explicit CTestAviation(QObject *parent = 0);
private slots:
/*!

View File

@@ -0,0 +1,32 @@
#include "testgeo.h"
using namespace BlackMisc::Geo;
using namespace BlackMisc::PhysicalQuantities;
namespace BlackMiscTest
{
/*
* Geo classes tests
*/
void CTestGeo::geoBasics()
{
CLatitude lati(10, CAngleUnit::deg());
QVERIFY2(lati * 2 == lati + lati, "Latitude addition should be equal");
lati += CLatitude(20, CAngleUnit::deg());
QVERIFY2(lati.unitValueToDoubleRounded() == 30.0, "Latitude should be 30 degrees");
double lat = 27.999999, lon = 86.999999, h = 8820.999999; // Mt Everest
CCoordinateGeodetic startGeoVec(lat, lon, h);
CCoordinateEcef mediumEcefVec = CCoordinateTransformation::toEcef(startGeoVec);
CCoordinateGeodetic endGeoVec = CCoordinateTransformation::toGeodetic(mediumEcefVec);
QVERIFY2(startGeoVec == endGeoVec, "Reconverted geo vector should be equal ");
CCoordinateNed nedVec = CCoordinateTransformation::toNed(mediumEcefVec, startGeoVec);
CCoordinateEcef ecefReconvert = CCoordinateTransformation::toEcef(nedVec);
QVERIFY2(mediumEcefVec != ecefReconvert, "Reconverted geo vector, expect some minor rounding issues");
QVERIFY2(mediumEcefVec.round() == ecefReconvert.round(), "Reconverted geo vector should be equal");
}
} // namespace

38
tests/blackmisc/testgeo.h Normal file
View File

@@ -0,0 +1,38 @@
/* Copyright (C) 2013 VATSIM Community / contributors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef BLACKMISCTEST_TESTGEO_H
#define BLACKMISCTEST_TESTGEO_H
#include "blackmisc/coordinatetransformation.h"
#include <QtTest/QtTest>
namespace BlackMiscTest
{
/*!
* \brief Geo classes tests
*/
class CTestGeo : public QObject
{
Q_OBJECT
public:
/*!
* \brief Standard test case constructor
* \param parent
*/
explicit CTestGeo(QObject *parent = 0) : QObject(parent) {}
private slots:
/*!
* \brief Basic unit tests for geo classes
*/
void geoBasics();
};
} // namespace
#endif // guard

View File

@@ -14,12 +14,14 @@ int CTestMain::unitMain(int argc, char *argv[])
{
int status = 0;
{
CTestPhysicalQuantitiesBase pqBaseTests ;
CTestAviationBase avBaseTests;
CTestVectorMatrixBase vmTests;
CTestPhysicalQuantities pqBaseTests ;
CTestAviation avBaseTests;
CTestVectorMatrix vmTests;
CTestGeo geoTests;
status |= QTest::qExec(&pqBaseTests, argc, argv);
status |= QTest::qExec(&avBaseTests, argc, argv);
status |= QTest::qExec(&vmTests, argc, argv);
status |= QTest::qExec(&geoTests, argc, argv);
}
return status;
}

View File

@@ -6,9 +6,10 @@
#ifndef BLACKMISCTEST_TESTMAIN_H
#define BLACKMISCTEST_TESTMAIN_H
#include "testphysicalquantitiesbase.h"
#include "testaviationbase.h"
#include "testvectormatrixbase.h"
#include "testphysicalquantities.h"
#include "testaviation.h"
#include "testvectormatrix.h"
#include "testgeo.h"
#include <QtTest/QtTest>
namespace BlackMiscTest

View File

@@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "testphysicalquantitiesbase.h"
#include "testphysicalquantities.h"
using namespace BlackMisc::PhysicalQuantities;
@@ -13,7 +13,7 @@ namespace BlackMiscTest
/*
* Constructor
*/
CTestPhysicalQuantitiesBase::CTestPhysicalQuantitiesBase(QObject *parent) : QObject(parent)
CTestPhysicalQuantities::CTestPhysicalQuantities(QObject *parent) : QObject(parent)
{
// void
}
@@ -21,9 +21,9 @@ CTestPhysicalQuantitiesBase::CTestPhysicalQuantitiesBase(QObject *parent) : QObj
/*
* Basic unit tests for physical units
*/
void CTestPhysicalQuantitiesBase::unitsBasics()
void CTestPhysicalQuantities::unitsBasics()
{
QVERIFY(CMeasurementPrefix::k() > CMeasurementPrefix::h());
QVERIFY2(CMeasurementPrefix::k() > CMeasurementPrefix::h(), "kilo > hecto");
// some tests on units
CLengthUnit du1 = CLengthUnit::m(); // Copy
@@ -42,15 +42,15 @@ void CTestPhysicalQuantitiesBase::unitsBasics()
/*
* Distance tests
*/
void CTestPhysicalQuantitiesBase::lengthBasics()
void CTestPhysicalQuantities::lengthBasics()
{
CLength d1(1, CLengthUnit::m()); // 1m
CLength d2(100, CLengthUnit::cm());
CLength d3(1.852 * 1000, CLengthUnit::m()); // 1852m
CLength d4(1, CLengthUnit::NM());
QVERIFY2(d1 == d2, "1meter shall be 100cm");
QVERIFY2(d3 == d4, "1852meters shall be 1NM");
QVERIFY2(d1 * 2 == 2 * d1, "Commutative multiplication");
d3 *= 2; // SI value
d4 *= 2.0; // SI value !
@@ -73,7 +73,7 @@ void CTestPhysicalQuantitiesBase::lengthBasics()
/*
* Unit tests for speed
*/
void CTestPhysicalQuantitiesBase::speedBasics()
void CTestPhysicalQuantities::speedBasics()
{
CSpeed s1(100, CSpeedUnit::km_h());
CSpeed s2(1000, CSpeedUnit::ft_min());
@@ -84,7 +84,7 @@ void CTestPhysicalQuantitiesBase::speedBasics()
/*
* Frequency unit tests
*/
void CTestPhysicalQuantitiesBase::frequencyTests()
void CTestPhysicalQuantities::frequencyTests()
{
CFrequency f1(1, CFrequencyUnit::MHz());
QVERIFY2(f1.valueRounded(CFrequencyUnit::kHz(), 2) == 1000, "Mega is 1000kHz");
@@ -97,7 +97,7 @@ void CTestPhysicalQuantitiesBase::frequencyTests()
/*
* Angle tests
*/
void CTestPhysicalQuantitiesBase::angleTests()
void CTestPhysicalQuantities::angleTests()
{
CAngle a1(180, CAngleUnit::deg());
CAngle a2(1.5 * CAngle::pi(), CAngleUnit::rad());
@@ -111,7 +111,7 @@ void CTestPhysicalQuantitiesBase::angleTests()
/*
* Weight tests
*/
void CTestPhysicalQuantitiesBase::massTests()
void CTestPhysicalQuantities::massTests()
{
CMass w1(1000, CMassUnit::kg());
CMass w2(w1.unitValueToInteger(), CMassUnit::kg());
@@ -125,7 +125,7 @@ void CTestPhysicalQuantitiesBase::massTests()
/*
* Pressure tests
*/
void CTestPhysicalQuantitiesBase::pressureTests()
void CTestPhysicalQuantities::pressureTests()
{
CPressure p1(1013.25, CPressureUnit::hPa());
CPressure p2(29.92, CPressureUnit::inHg());
@@ -142,7 +142,7 @@ void CTestPhysicalQuantitiesBase::pressureTests()
/*
* Temperature tests
*/
void CTestPhysicalQuantitiesBase::temperatureTests()
void CTestPhysicalQuantities::temperatureTests()
{
CTemperature t1(0, CTemperatureUnit::C()); // 0C
CTemperature t2(1, CTemperatureUnit::F()); // 1F
@@ -157,7 +157,7 @@ void CTestPhysicalQuantitiesBase::temperatureTests()
/*
* Temperature tests
*/
void CTestPhysicalQuantitiesBase::timeTests()
void CTestPhysicalQuantities::timeTests()
{
CTime t1(1, CTimeUnit::h());
QVERIFY2(t1.convertedSiValueToInteger() == 3600, "1hour shall be 3600s");
@@ -166,7 +166,7 @@ void CTestPhysicalQuantitiesBase::timeTests()
/*
* Just testing obvious memory create / destruct flaws
*/
void CTestPhysicalQuantitiesBase::memoryTests()
void CTestPhysicalQuantities::memoryTests()
{
CLength *c = new CLength(100, CLengthUnit::m());
c->switchUnit(CLengthUnit::NM());
@@ -184,7 +184,7 @@ void CTestPhysicalQuantitiesBase::memoryTests()
/*
* @brief Just testing obvious memory create / destruct flaws
*/
void CTestPhysicalQuantitiesBase::basicArithmetic()
void CTestPhysicalQuantities::basicArithmetic()
{
CPressure p1 = CPhysicalQuantitiesConstants::InternationalStandardSeaLevelPressure();
CPressure p2(p1);

View File

@@ -13,9 +13,9 @@ namespace BlackMiscTest
{
/*!
* \brief Physical quantities,basic tests
* \brief Physical quantities, basic tests
*/
class CTestPhysicalQuantitiesBase : public QObject
class CTestPhysicalQuantities : public QObject
{
Q_OBJECT
@@ -24,7 +24,8 @@ public:
* \brief Standard test case constructor
* \param parent
*/
explicit CTestPhysicalQuantitiesBase(QObject *parent = 0);
explicit CTestPhysicalQuantities(QObject *parent = 0);
private slots:
/*!
* \brief Basic unit tests for physical units

View File

@@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "testvectormatrixbase.h"
#include "testvectormatrix.h"
using namespace BlackMisc::Math;
@@ -13,12 +13,13 @@ namespace BlackMiscTest
/*
* Basic tests vector
*/
void CTestVectorMatrixBase::vectorBasics()
void CTestVectorMatrix::vectorBasics()
{
CVector3D v1(1);
v1 *= 2.0;
CVector3D v2(2);
QVERIFY2(v1 == v2, "Vectors should be equal");
QVERIFY2(v1 * 2 == 2 * v2, "Commutative vector multiplication failed");
CVector3D v3(1, 1, 1);
CVector3D v4(2, 2, 2);
CVector3D v5 = v3.crossProduct(v4);
@@ -33,7 +34,7 @@ void CTestVectorMatrixBase::vectorBasics()
* Matrix tests
* http://www.bluebit.gr/matrix-calculator/
*/
void CTestVectorMatrixBase::matrixBasics()
void CTestVectorMatrix::matrixBasics()
{
CMatrix3x3 m1;
CMatrix3x3 m2 = m1 - m1;
@@ -55,6 +56,7 @@ void CTestVectorMatrixBase::matrixBasics()
m2 = m1 + m1;
m1 = m1 * 2.0;
QVERIFY2(m1 == m2, "2* Identity should be Identity + Identity");
QVERIFY2(m1 * 2 == 2 * m1, "Commutative matrix multiplication failed");
m1 /= 2.0;
m2 -= m1;

View File

@@ -13,9 +13,9 @@ namespace BlackMiscTest
{
/*!
* \brief Vector and Matrix classes basic tests
* \brief Vector and Matrix classes tests
*/
class CTestVectorMatrixBase : public QObject
class CTestVectorMatrix : public QObject
{
Q_OBJECT
@@ -24,13 +24,14 @@ public:
* \brief Standard test case constructor
* \param parent
*/
explicit CTestVectorMatrixBase(QObject *parent = 0) : QObject(parent) {}
explicit CTestVectorMatrix(QObject *parent = 0) : QObject(parent) {}
private slots:
/*!
* \brief Basic unit tests for physical units
*/
void vectorBasics();
/*!
* \brief Vertical positions
*/