Skip to content
Permalink
Browse files

Add qgstestutils.h file with test helpers (#3208)

Initially containing QGSCOMPARENEAR(a,b,epsilon) which prints
debug information if the comparison fails
  • Loading branch information
nyalldawson authored and m-kuhn committed Jun 23, 2016
1 parent 904dc21 commit b63f7887e93bdd26d5b62a259ca5814ffc184a43
Showing with 75 additions and 54 deletions.
  1. +1 −0 src/core/CMakeLists.txt
  2. +29 −0 src/core/qgstestutils.h
  3. +18 −17 tests/src/core/testqgsdistancearea.cpp
  4. +27 −37 tests/src/gui/testqgsmapcanvas.cpp
@@ -707,6 +707,7 @@ SET(QGIS_CORE_HDRS
qgsstatisticalsummary.h
qgsstringstatisticalsummary.h
qgsstringutils.h
qgstestutils.h
qgstextlabelfeature.h
qgstolerance.h
qgstracer.h
@@ -0,0 +1,29 @@
/***************************************************************************
qgstestutils.h
---------------------
begin : June 2016
copyright : (C) 2016 by Nyall Dawson
email : nyalld dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSTESTUTILS_H
#define QGSTESTUTILS_H

#include "qgis.h"
#include "QtTest/qtestcase.h"

/**
* Assorted helper methods for unit testing.
* @note added in QGIS 2.16
*/

#define QGSCOMPARENEAR(a,b,epsilon) { bool _xxxresult = qgsDoubleNear( a, b, epsilon ); if ( !_xxxresult ) { qDebug( "Expecting %f got %f (diff %f > %f)", static_cast< double >( a ), static_cast< double >( b ), qAbs( static_cast< double >( a ) - b ), static_cast< double >( epsilon ) ); } QVERIFY( _xxxresult ); }


#endif // QGSTESTUTILS_H
@@ -26,6 +26,7 @@
#include "qgsgeometryfactory.h"
#include "qgsgeometry.h"
#include "qgis.h"
#include "qgstestutils.h"


class TestQgsDistanceArea: public QObject
@@ -186,7 +187,7 @@ void TestQgsDistanceArea::regression13601()
calc.setEllipsoid( "NONE" );
calc.setSourceCrs( 1108L );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( "Polygon ((252000 1389000, 265000 1389000, 265000 1385000, 252000 1385000, 252000 1389000))" ) );
QVERIFY( qgsDoubleNear( calc.measureArea( &geom ), 52000000, 0.0001 ) );
QGSCOMPARENEAR( calc.measureArea( &geom ), 52000000, 0.0001 );
}

void TestQgsDistanceArea::collections()
@@ -201,31 +202,31 @@ void TestQgsDistanceArea::collections()
//collection of lines, should be sum of line length
QgsGeometry lines( QgsGeometryFactory::geomFromWkt( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70) )" ) );
double result = myDa.measure( &lines ); //should measure length
QVERIFY( qgsDoubleNear( result, 12006159, 1 ) );
QGSCOMPARENEAR( result, 12006159, 1 );
result = myDa.measureLength( &lines );
QVERIFY( qgsDoubleNear( result, 12006159, 1 ) );
QGSCOMPARENEAR( result, 12006159, 1 );
result = myDa.measureArea( &lines );
QVERIFY( qgsDoubleNear( result, 0 ) );

//collection of polygons
QgsGeometry polys( QgsGeometryFactory::geomFromWkt( "GeometryCollection( Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) );
result = myDa.measure( &polys ); //should measure area
QVERIFY( qgsDoubleNear( result, 670434859475LL, 1 ) );
QGSCOMPARENEAR( result, 670434859475LL, 1 );
result = myDa.measureArea( &polys );
QVERIFY( qgsDoubleNear( result, 670434859475LL, 1 ) );
QGSCOMPARENEAR( result, 670434859475LL, 1 );
result = myDa.measureLength( &polys );
QVERIFY( qgsDoubleNear( result, 0 ) );

//mixed collection
QgsGeometry mixed( QgsGeometryFactory::geomFromWkt( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70), Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) );
result = myDa.measure( &mixed ); //should measure area
QVERIFY( qgsDoubleNear( result, 670434859475LL, 1 ) );
QGSCOMPARENEAR( result, 670434859475LL, 1 );
//measure area specifically
result = myDa.measureArea( &mixed );
QVERIFY( qgsDoubleNear( result, 670434859475LL, 1 ) );
QGSCOMPARENEAR( result, 670434859475LL, 1 );
//measure length
result = myDa.measureLength( &mixed );
QVERIFY( qgsDoubleNear( result, 12006159, 1 ) );
QGSCOMPARENEAR( result, 12006159, 1 );

Q_NOWARN_DEPRECATED_POP
}
@@ -244,14 +245,14 @@ void TestQgsDistanceArea::measureUnits()
double result = calc.measureLine( p1, p2, units );
//no OTF, result will be in CRS unit (feet)
QCOMPARE( units, QGis::Feet );
QVERIFY( qgsDoubleNear( result, 7637.7952755903825, 0.001 ) );
QGSCOMPARENEAR( result, 7637.7952755903825, 0.001 );

calc.setEllipsoidalMode( true );
calc.setEllipsoid( "WGS84" );
result = calc.measureLine( p1, p2, units );
//OTF, result will be in meters
QCOMPARE( units, QGis::Meters );
QVERIFY( qgsDoubleNear( result, 2328.0988253106957, 0.001 ) );
QGSCOMPARENEAR( result, 2328.0988253106957, 0.001 );
}

void TestQgsDistanceArea::measureAreaAndUnits()
@@ -299,12 +300,12 @@ void TestQgsDistanceArea::measureAreaAndUnits()

QgsDebugMsg( QString( "measured %1 in %2" ).arg( area ).arg( QgsUnitTypes::toString( units ) ) );
// should always be in Meters Squared
QVERIFY( qgsDoubleNear( area, 37416879192.9, 0.1 ) );
QGSCOMPARENEAR( area, 37416879192.9, 0.1 );
QCOMPARE( units, QgsUnitTypes::SquareMeters );

// test converting the resultant area
area = da.convertAreaMeasurement( area, QgsUnitTypes::SquareMiles );
QVERIFY( qgsDoubleNear( area, 14446.7378, 0.001 ) );
QGSCOMPARENEAR( area, 14446.7378, 0.001 );

// now try with a source CRS which is in feet
ring.clear();
@@ -325,25 +326,25 @@ void TestQgsDistanceArea::measureAreaAndUnits()
area = da.measureArea( polygon.data() );
units = da.areaUnits();
QgsDebugMsg( QString( "measured %1 in %2" ).arg( area ).arg( QgsUnitTypes::toString( units ) ) );
QVERIFY( qgsDoubleNear( area, 2000000, 0.001 ) );
QGSCOMPARENEAR( area, 2000000, 0.001 );
QCOMPARE( units, QgsUnitTypes::SquareFeet );

// test converting the resultant area
area = da.convertAreaMeasurement( area, QgsUnitTypes::SquareYards );
QVERIFY( qgsDoubleNear( area, 222222.2222, 0.001 ) );
QGSCOMPARENEAR( area, 222222.2222, 0.001 );

da.setEllipsoidalMode( true );
// now should be in Square Meters again
area = da.measureArea( polygon.data() );
units = da.areaUnits();
QgsDebugMsg( QString( "measured %1 in %2" ).arg( area ).arg( QgsUnitTypes::toString( units ) ) );
QVERIFY( qgsDoubleNear( area, 184149.37, 1.0 ) );
QGSCOMPARENEAR( area, 184149.37, 1.0 );
QCOMPARE( units, QgsUnitTypes::SquareMeters );

// test converting the resultant area
area = da.convertAreaMeasurement( area, QgsUnitTypes::SquareYards );
QgsDebugMsg( QString( "measured %1 in sq yrds" ).arg( area ) );
QVERIFY( qgsDoubleNear( area, 220240.8172549, 0.00001 ) );
QGSCOMPARENEAR( area, 220240.8172549, 0.00001 );
}

void TestQgsDistanceArea::emptyPolygon()
@@ -365,7 +366,7 @@ void TestQgsDistanceArea::regression14675()
calc.setEllipsoid( "GRS80" );
calc.setSourceCrs( 145L );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( "Polygon ((917593.5791854317067191 6833700.00807378999888897, 917596.43389983859378844 6833700.67099479306489229, 917599.53056440979707986 6833700.78673478215932846, 917593.5791854317067191 6833700.00807378999888897))" ) );
QVERIFY( qgsDoubleNear( calc.measureArea( &geom ), 0.83301, 0.0001 ) );
QGSCOMPARENEAR( calc.measureArea( &geom ), 0.83301, 0.0001 );
}

QTEST_MAIN( TestQgsDistanceArea )
@@ -23,6 +23,7 @@
#include <qgsrenderchecker.h>
#include <qgsvectordataprovider.h>
#include <qgsmaptoolpan.h>
#include "qgstestutils.h"

namespace QTest
{
@@ -34,17 +35,6 @@ namespace QTest
}
}

inline bool qgsDoubleNearDebug( double a, double b, double epsilon = 4 * DBL_EPSILON )
{
if ( !qgsDoubleNear( a, b, epsilon ) )
{
qDebug( "Expecting %f got %f (diff %f > %f)", a, b, qAbs( a - b ), epsilon );
return false;
}
return true;
}


class QgsMapToolTest : public QgsMapTool
{
public:
@@ -116,20 +106,20 @@ void TestQgsMapCanvas::testMapRendererInteraction()
QgsRectangle r1( 10, 10, 20, 20 );
mr->setExtent( r1 );
QgsRectangle r2 = mr->extent();
QVERIFY( qgsDoubleNear( mCanvas->extent().xMinimum(), r2.xMinimum(), 0.0000000001 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().yMinimum(), r2.yMinimum(), 0.0000000001 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().xMaximum(), r2.xMaximum(), 0.0000000001 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().yMaximum(), r2.yMaximum(), 0.0000000001 ) );
QGSCOMPARENEAR( mCanvas->extent().xMinimum(), r2.xMinimum(), 0.0000000001 );
QGSCOMPARENEAR( mCanvas->extent().yMinimum(), r2.yMinimum(), 0.0000000001 );
QGSCOMPARENEAR( mCanvas->extent().xMaximum(), r2.xMaximum(), 0.0000000001 );
QGSCOMPARENEAR( mCanvas->extent().yMaximum(), r2.yMaximum(), 0.0000000001 );
QCOMPARE( spy2.count(), 1 );

QgsRectangle r3( 100, 100, 200, 200 );
QSignalSpy spy3( mr, SIGNAL( extentsChanged() ) );
mCanvas->setExtent( r3 );
QgsRectangle r4 = mCanvas->extent();
QVERIFY( qgsDoubleNear( mr->extent().xMinimum(), r4.xMinimum(), 0.0000000001 ) );
QVERIFY( qgsDoubleNear( mr->extent().yMinimum(), r4.yMinimum(), 0.0000000001 ) );
QVERIFY( qgsDoubleNear( mr->extent().xMaximum(), r4.xMaximum(), 0.0000000001 ) );
QVERIFY( qgsDoubleNear( mr->extent().yMaximum(), r4.yMaximum(), 0.0000000001 ) );
QGSCOMPARENEAR( mr->extent().xMinimum(), r4.xMinimum(), 0.0000000001 );
QGSCOMPARENEAR( mr->extent().yMinimum(), r4.yMinimum(), 0.0000000001 );
QGSCOMPARENEAR( mr->extent().xMaximum(), r4.xMaximum(), 0.0000000001 );
QGSCOMPARENEAR( mr->extent().yMaximum(), r4.yMaximum(), 0.0000000001 );
QCOMPARE( spy3.count(), 1 );

// Destination CRS
@@ -278,10 +268,10 @@ void TestQgsMapCanvas::testMagnification()
void compareExtent( const QgsRectangle &initialExtent,
const QgsRectangle &extent )
{
QVERIFY( qgsDoubleNear( initialExtent.xMinimum(), extent.xMinimum(), 0.00000000001 ) );
QVERIFY( qgsDoubleNear( initialExtent.xMaximum(), extent.xMaximum(), 0.00000000001 ) );
QVERIFY( qgsDoubleNear( initialExtent.yMinimum(), extent.yMinimum(), 0.00000000001 ) );
QVERIFY( qgsDoubleNear( initialExtent.yMaximum(), extent.yMaximum(), 0.00000000001 ) );
QGSCOMPARENEAR( initialExtent.xMinimum(), extent.xMinimum(), 0.00000000001 );
QGSCOMPARENEAR( initialExtent.xMaximum(), extent.xMaximum(), 0.00000000001 );
QGSCOMPARENEAR( initialExtent.yMinimum(), extent.yMinimum(), 0.00000000001 );
QGSCOMPARENEAR( initialExtent.yMaximum(), extent.yMaximum(), 0.00000000001 );
}

void TestQgsMapCanvas::testMagnificationExtent()
@@ -393,26 +383,26 @@ void TestQgsMapCanvas::testZoomByWheel()
//test zoom out
QWheelEvent e( QPoint( 0, 0 ), -1, Qt::NoButton, Qt::NoModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth * 2.0, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight * 2.0, 0.1 ) );
QGSCOMPARENEAR( mCanvas->extent().width(), originalWidth * 2.0, 0.1 );
QGSCOMPARENEAR( mCanvas->extent().height(), originalHeight * 2.0, 0.1 );

//test zoom in
e = QWheelEvent( QPoint( 0, 0 ), 1, Qt::NoButton, Qt::NoModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight, 0.1 ) );
QGSCOMPARENEAR( mCanvas->extent().width(), originalWidth, 0.1 );
QGSCOMPARENEAR( mCanvas->extent().height(), originalHeight, 0.1 );

// test zoom out with ctrl
e = QWheelEvent( QPoint( 0, 0 ), -1, Qt::NoButton, Qt::ControlModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), 1.05 * originalWidth, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), 1.05 * originalHeight, 0.1 ) );
QGSCOMPARENEAR( mCanvas->extent().width(), 1.05 * originalWidth, 0.1 );
QGSCOMPARENEAR( mCanvas->extent().height(), 1.05 * originalHeight, 0.1 );

//test zoom in with ctrl
e = QWheelEvent( QPoint( 0, 0 ), 1, Qt::NoButton, Qt::ControlModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight, 0.1 ) );
QGSCOMPARENEAR( mCanvas->extent().width(), originalWidth, 0.1 );
QGSCOMPARENEAR( mCanvas->extent().height(), originalHeight, 0.1 );
}

void TestQgsMapCanvas::testShiftZoom()
@@ -440,8 +430,8 @@ void TestQgsMapCanvas::testShiftZoom()
Qt::LeftButton, Qt::LeftButton, Qt::ShiftModifier );
mCanvas->mouseReleaseEvent( &e );

QVERIFY( qgsDoubleNearDebug( mCanvas->extent().width(), originalWidth / 2.0, 0.2 ) );
QVERIFY( qgsDoubleNearDebug( mCanvas->extent().height(), originalHeight / 2.0, 0.2 ) );
QGSCOMPARENEAR( mCanvas->extent().width(), originalWidth / 2.0, 0.2 );
QGSCOMPARENEAR( mCanvas->extent().height(), originalHeight / 2.0, 0.2 );

//reset
mCanvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
@@ -457,8 +447,8 @@ void TestQgsMapCanvas::testShiftZoom()
Qt::LeftButton, Qt::LeftButton, Qt::ShiftModifier );
mCanvas->mouseReleaseEvent( &e );

QVERIFY( qgsDoubleNearDebug( mCanvas->extent().width(), originalWidth, 0.0001 ) );
QVERIFY( qgsDoubleNearDebug( mCanvas->extent().height(), originalHeight, 0.0001 ) );
QGSCOMPARENEAR( mCanvas->extent().width(), originalWidth, 0.0001 );
QGSCOMPARENEAR( mCanvas->extent().height(), originalHeight, 0.0001 );

//reset
mCanvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
@@ -477,8 +467,8 @@ void TestQgsMapCanvas::testShiftZoom()
Qt::LeftButton, Qt::LeftButton, Qt::ShiftModifier );
mCanvas->mouseReleaseEvent( &e );

QVERIFY( qgsDoubleNearDebug( mCanvas->extent().width(), originalWidth, 0.00001 ) );
QVERIFY( qgsDoubleNearDebug( mCanvas->extent().height(), originalHeight, 0.00001 ) );
QGSCOMPARENEAR( mCanvas->extent().width(), originalWidth, 0.00001 );
QGSCOMPARENEAR( mCanvas->extent().height(), originalHeight, 0.00001 );
}

QTEST_MAIN( TestQgsMapCanvas )

0 comments on commit b63f788

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