Skip to content
Permalink
Browse files

Added simple text unit test

  • Loading branch information
homann committed Sep 3, 2012
1 parent 6154f8e commit a4d5c88524bfc304a99603cb1909691bcb59427b
Showing with 43 additions and 20 deletions.
  1. +3 −3 src/core/qgsdistancearea.cpp
  2. +2 −2 src/core/qgsdistancearea.h
  3. +38 −15 tests/src/core/testqgsdistancearea.cpp
@@ -49,7 +49,7 @@ QgsDistanceArea::QgsDistanceArea()


//! Copy constructor
QgsDistanceArea::QgsDistanceArea(const QgsDistanceArea & origDA )
QgsDistanceArea::QgsDistanceArea( const QgsDistanceArea & origDA )
{
_copy( origDA );
}
@@ -60,9 +60,9 @@ QgsDistanceArea::~QgsDistanceArea()
}

//! Assignment operator
QgsDistanceArea & QgsDistanceArea::operator=(const QgsDistanceArea & origDA )
QgsDistanceArea & QgsDistanceArea::operator=( const QgsDistanceArea & origDA )
{
if (this == & origDA )
if ( this == & origDA )
{
// Do not copy unto self
return *this;
@@ -41,10 +41,10 @@ class CORE_EXPORT QgsDistanceArea
~QgsDistanceArea();

//! Copy constructor
QgsDistanceArea(const QgsDistanceArea & origDA );
QgsDistanceArea( const QgsDistanceArea & origDA );

//! Assignment operator
QgsDistanceArea & operator=(const QgsDistanceArea & origDA );
QgsDistanceArea & operator=( const QgsDistanceArea & origDA );

//! sets whether coordinates must be projected to ellipsoid before measuring
void setEllipsoidalMode( bool flag );
@@ -29,10 +29,10 @@ class TestQgsDistanceArea: public QObject

Q_OBJECT;
private slots:
void initTestCase();
void basic();
void test_distances();
void unit_conversions();
void initTestCase();
void basic();
void test_distances();
void unit_conversions();
};

void TestQgsDistanceArea::initTestCase()
@@ -48,14 +48,14 @@ void TestQgsDistanceArea::initTestCase()

void TestQgsDistanceArea::basic()
{
QgsPoint p1( 1.0, 3.0 ), p2(-2.0, -1.0 );
QgsPoint p1( 1.0, 3.0 ), p2( -2.0, -1.0 );
QgsDistanceArea daA;
double resultA, resultB, resultC;

daA.setEllipsoid( "NONE" );
resultA = daA.measureLine( p1, p2 );
QCOMPARE( resultA, 5.0 );

// Now, on an ellipsoid. Always less?
daA.setSourceCrs( 3006 );
daA.setEllipsoid( "WGS84" );
@@ -69,7 +69,7 @@ void TestQgsDistanceArea::basic()
QCOMPARE( resultA, resultB );

// Different Ellipsoid
daB.setEllipsoid( "WGS72" );
daB.setEllipsoid( "WGS72" );
resultB = daB.measureLine( p1, p2 );
QVERIFY( ! qFuzzyCompare( resultA, resultB ) );

@@ -101,41 +101,64 @@ void TestQgsDistanceArea::test_distances()
// Set up DA
QgsDistanceArea myDa;
myDa.setSourceAuthId( "EPSG:4030" );
myDa.setEllipsoidalMode ( true );
myDa.setEllipsoidalMode( true );
myDa.setEllipsoid( "WGS84" );

QString myFileName = QString( TEST_DATA_DIR ) + QDir::separator() + "GeodTest-nano.dat";

QFile myFile( myFileName );
if ( ! myFile.open( QIODevice::ReadOnly | QIODevice::Text))
QFile myFile( myFileName );
if ( ! myFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QFAIL( "Couldn't open file" );
return;
}
QTextStream in( & myFile );
while (!in.atEnd()) {
while ( !in.atEnd() )
{
QString line = in.readLine();
// Some test points (antipodal) does not converge with the chosen algorithm!
// See calcaulator at http://www.movable-type.co.uk/scripts/latlong-vincenty.html
// These are commented out.
if (line[0] != '#' )
if ( line[0] != '#' )
{
QStringList myLineList = line.split(' '); // Split fields on space.
QStringList myLineList = line.split( ' ' ); // Split fields on space.
// Create points
QgsPoint p1( myLineList[1].toDouble(), myLineList[0].toDouble() );
QgsPoint p2( myLineList[4].toDouble(), myLineList[3].toDouble() );
double result = myDa.measureLine( p1, p2 );
// QgsDebugMsg( QString( "Distance from %1 to %2 is %3" ).arg( p1.toString( 15 ) ).arg( p2.toString( 15 ) ).arg( result, 0, 'g', 15 ) );
// QgsDebugMsg( QString( "Distance should be %1" ).arg( myLineList[6] ) );
// Check result is less than 0.5mm from expected.
QVERIFY ( qAbs( result - myLineList[6].toDouble() ) < 0.0005 );
QVERIFY( qAbs( result - myLineList[6].toDouble() ) < 0.0005 );
}
}

};

void TestQgsDistanceArea::unit_conversions()
{
// Do some very simple test of conversion and units
QgsDistanceArea myDa;
myDa.setEllipsoidalMode( "NONE" );

double inputValue;
QGis::UnitType inputUnit;
QGis::UnitType outputUnit;

inputValue = 10000.0;
inputUnit = QGis::Meters;
outputUnit = QGis::Feet;
//outputUnit = QGis::Meters;

// First, convert from sq.meter to sq.feet
myDa.convertMeasurement( inputValue, inputUnit, outputUnit, true );
QVERIFY( qAbs( inputValue - 107639.1041671 ) <= 0.0000001 );

// The print a text unit. This is i18n, so we should ignore the unit
// and use the locale settings for separation of digits.
QString myTxt = QgsDistanceArea::textUnit( inputValue, 7, inputUnit, true, false );
QString expectedTxt = QLocale::system().toString( 2.4710538146717, 'g', 1 + 7 );
QVERIFY( myTxt.startsWith( expectedTxt ) ); // Ignore units for now.
};

QTEST_MAIN( TestQgsDistanceArea )

0 comments on commit a4d5c88

Please sign in to comment.
You can’t perform that action at this time.