Skip to content

Commit

Permalink
Use QGSCOMPARENEAR instead of QVERIFY( qgsDoubleNear(... ) )
Browse files Browse the repository at this point in the history
QGSCOMPARENEAR gives better debugging output when the
test fails
  • Loading branch information
nyalldawson committed Aug 29, 2017
1 parent 2af10cb commit 21a3b8a
Show file tree
Hide file tree
Showing 23 changed files with 295 additions and 279 deletions.
3 changes: 2 additions & 1 deletion tests/src/analysis/testqgsrastercalculator.cpp
Expand Up @@ -21,6 +21,7 @@ Email : nyall dot dawson at gmail dot com
#include "qgsrastermatrix.h"
#include "qgsapplication.h"
#include "qgsproject.h"
#include "qgstestutils.h"

Q_DECLARE_METATYPE( QgsRasterCalcNode::Operator )

Expand Down Expand Up @@ -212,7 +213,7 @@ void TestQgsRasterCalculator::singleOp()
QVERIFY( node.calculate( rasterData, result ) );

qDebug() << "Result: " << result.number() << " expected: " << expected;
QVERIFY( qgsDoubleNear( result.number(), expected, 0.0000000001 ) );
QGSCOMPARENEAR( result.number(), expected, 0.0000000001 );

}

Expand Down
9 changes: 5 additions & 4 deletions tests/src/app/testqgsattributetable.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsvectorfilewriter.h"

#include "qgstest.h"
#include "qgstestutils.h"

/** \ingroup UnitTests
* This is a unit test for the attribute table dialog
Expand Down Expand Up @@ -114,7 +115,7 @@ void TestQgsAttributeTable::testFieldCalculation()
QgsFeature f;
QVERIFY( fit.nextFeature( f ) );
double expected = 26932.156;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 0.001 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 0.001 );

// change project length unit, check calculation respects unit
QgsProject::instance()->setDistanceUnits( QgsUnitTypes::DistanceFeet );
Expand All @@ -126,7 +127,7 @@ void TestQgsAttributeTable::testFieldCalculation()
fit = tempLayer->dataProvider()->getFeatures();
QVERIFY( fit.nextFeature( f ) );
expected = 88360.0918635;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 0.001 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 0.001 );
}

void TestQgsAttributeTable::testFieldCalculationArea()
Expand Down Expand Up @@ -164,7 +165,7 @@ void TestQgsAttributeTable::testFieldCalculationArea()
QgsFeature f;
QVERIFY( fit.nextFeature( f ) );
double expected = 1009089817.0;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 1.0 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 1.0 );

// change project area unit, check calculation respects unit
QgsProject::instance()->setAreaUnits( QgsUnitTypes::AreaSquareMiles );
Expand All @@ -176,7 +177,7 @@ void TestQgsAttributeTable::testFieldCalculationArea()
fit = tempLayer->dataProvider()->getFeatures();
QVERIFY( fit.nextFeature( f ) );
expected = 389.6117565069;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 0.001 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 0.001 );
}

void TestQgsAttributeTable::testNoGeom()
Expand Down
9 changes: 5 additions & 4 deletions tests/src/app/testqgsfieldcalculator.cpp
Expand Up @@ -24,6 +24,7 @@
#include "qgsproject.h"
#include "qgsmapcanvas.h"
#include "qgsunittypes.h"
#include "qgstestutils.h"

/** \ingroup UnitTests
* This is a unit test for the field calculator
Expand Down Expand Up @@ -107,7 +108,7 @@ void TestQgsFieldCalculator::testLengthCalculations()
QgsFeature f;
QVERIFY( fit.nextFeature( f ) );
double expected = 26932.156;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 0.001 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 0.001 );

// change project length unit, check calculation respects unit
QgsProject::instance()->setDistanceUnits( QgsUnitTypes::DistanceFeet );
Expand All @@ -122,7 +123,7 @@ void TestQgsFieldCalculator::testLengthCalculations()
fit = tempLayer->dataProvider()->getFeatures();
QVERIFY( fit.nextFeature( f ) );
expected = 88360.0918635;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 0.001 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 0.001 );

}

Expand Down Expand Up @@ -168,7 +169,7 @@ void TestQgsFieldCalculator::testAreaCalculations()
QgsFeature f;
QVERIFY( fit.nextFeature( f ) );
double expected = 1009089817.0;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 1.0 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 1.0 );

// change project area unit, check calculation respects unit
QgsProject::instance()->setAreaUnits( QgsUnitTypes::AreaSquareMiles );
Expand All @@ -183,7 +184,7 @@ void TestQgsFieldCalculator::testAreaCalculations()
fit = tempLayer->dataProvider()->getFeatures();
QVERIFY( fit.nextFeature( f ) );
expected = 389.6117565069;
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 0.001 ) );
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 0.001 );
}

QGSTEST_MAIN( TestQgsFieldCalculator )
Expand Down
17 changes: 9 additions & 8 deletions tests/src/app/testqgsmaptoolidentifyaction.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsunittypes.h"
#include "qgsmaptoolidentifyaction.h"
#include "qgssettings.h"
#include "qgstestutils.h"

#include "cpl_conv.h"

Expand Down Expand Up @@ -142,23 +143,23 @@ void TestQgsMapToolIdentifyAction::lengthCalculation()
QCOMPARE( result.length(), 1 );
QString derivedLength = result.at( 0 ).mDerivedAttributes[tr( "Length" )];
double length = derivedLength.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( length, 26932.2, 0.1 ) );
QGSCOMPARENEAR( length, 26932.2, 0.1 );

//check that project units are respected
QgsProject::instance()->setDistanceUnits( QgsUnitTypes::DistanceFeet );
result = action->identify( mapPoint.x(), mapPoint.y(), QList<QgsMapLayer *>() << tempLayer.get() );
QCOMPARE( result.length(), 1 );
derivedLength = result.at( 0 ).mDerivedAttributes[tr( "Length" )];
length = derivedLength.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( length, 88360.1, 0.1 ) );
QGSCOMPARENEAR( length, 88360.1, 0.1 );

//test unchecked "keep base units" setting
s.setValue( QStringLiteral( "/qgis/measure/keepbaseunit" ), false );
result = action->identify( mapPoint.x(), mapPoint.y(), QList<QgsMapLayer *>() << tempLayer.get() );
QCOMPARE( result.length(), 1 );
derivedLength = result.at( 0 ).mDerivedAttributes[tr( "Length" )];
length = derivedLength.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( length, 16.735, 0.001 ) );
QGSCOMPARENEAR( length, 16.735, 0.001 );
}

void TestQgsMapToolIdentifyAction::perimeterCalculation()
Expand Down Expand Up @@ -203,15 +204,15 @@ void TestQgsMapToolIdentifyAction::perimeterCalculation()
QCOMPARE( result.length(), 1 );
derivedPerimeter = result.at( 0 ).mDerivedAttributes[tr( "Perimeter" )];
perimeter = derivedPerimeter.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( perimeter, 420896.0, 0.1 ) );
QGSCOMPARENEAR( perimeter, 420896.0, 0.1 );

//test unchecked "keep base units" setting
s.setValue( QStringLiteral( "/qgis/measure/keepbaseunit" ), false );
result = action->identify( mapPoint.x(), mapPoint.y(), QList<QgsMapLayer *>() << tempLayer.get() );
QCOMPARE( result.length(), 1 );
derivedPerimeter = result.at( 0 ).mDerivedAttributes[tr( "Perimeter" )];
perimeter = derivedPerimeter.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( perimeter, 79.715, 0.001 ) );
QGSCOMPARENEAR( perimeter, 79.715, 0.001 );
}

void TestQgsMapToolIdentifyAction::areaCalculation()
Expand Down Expand Up @@ -249,15 +250,15 @@ void TestQgsMapToolIdentifyAction::areaCalculation()
QCOMPARE( result.length(), 1 );
QString derivedArea = result.at( 0 ).mDerivedAttributes[tr( "Area" )];
double area = derivedArea.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( area, 1009089817.0, 1.0 ) );
QGSCOMPARENEAR( area, 1009089817.0, 1.0 );

//check that project units are respected
QgsProject::instance()->setAreaUnits( QgsUnitTypes::AreaSquareMiles );
result = action->identify( mapPoint.x(), mapPoint.y(), QList<QgsMapLayer *>() << tempLayer.get() );
QCOMPARE( result.length(), 1 );
derivedArea = result.at( 0 ).mDerivedAttributes[tr( "Area" )];
area = derivedArea.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( area, 389.6117, 0.001 ) );
QGSCOMPARENEAR( area, 389.6117, 0.001 );

//test unchecked "keep base units" setting
s.setValue( QStringLiteral( "/qgis/measure/keepbaseunit" ), false );
Expand All @@ -266,7 +267,7 @@ void TestQgsMapToolIdentifyAction::areaCalculation()
QCOMPARE( result.length(), 1 );
derivedArea = result.at( 0 ).mDerivedAttributes[tr( "Area" )];
area = derivedArea.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
QVERIFY( qgsDoubleNear( area, 389.6117, 0.001 ) );
QGSCOMPARENEAR( area, 389.6117, 0.001 );
}

// private
Expand Down
37 changes: 19 additions & 18 deletions tests/src/core/testqgscomposermap.cpp
Expand Up @@ -29,6 +29,7 @@
#include "qgsproperty.h"
#include <QObject>
#include "qgstest.h"
#include "qgstestutils.h"

class TestQgsComposerMap : public QObject
{
Expand Down Expand Up @@ -176,34 +177,34 @@ void TestQgsComposerMap::worldFileGeneration()
double a, b, c, d, e, f;
mComposition->computeWorldFileParameters( a, b, c, d, e, f );

QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );
QGSCOMPARENEAR( a, 4.18048, 0.001 );
QGSCOMPARENEAR( b, 2.41331, 0.001 );
QGSCOMPARENEAR( c, 779444, 1 );
QGSCOMPARENEAR( d, 2.4136, 0.001 );
QGSCOMPARENEAR( e, -4.17997, 0.001 );
QGSCOMPARENEAR( f, 3.34241e+06, 1e+03 );

//test with map on second page. Parameters should be the same
mComposerMap->setItemPosition( 20, 20, QgsComposerItem::UpperLeft, 2 );
mComposition->computeWorldFileParameters( a, b, c, d, e, f );

QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );
QGSCOMPARENEAR( a, 4.18048, 0.001 );
QGSCOMPARENEAR( b, 2.41331, 0.001 );
QGSCOMPARENEAR( c, 779444, 1 );
QGSCOMPARENEAR( d, 2.4136, 0.001 );
QGSCOMPARENEAR( e, -4.17997, 0.001 );
QGSCOMPARENEAR( f, 3.34241e+06, 1e+03 );

//test computing parameters for specific region
mComposerMap->setItemPosition( 20, 20, QgsComposerItem::UpperLeft, 2 );
mComposition->computeWorldFileParameters( QRectF( 10, 5, 260, 200 ), a, b, c, d, e, f );

QVERIFY( qgsDoubleNear( a, 4.18061, 0.001 ) );
QVERIFY( qgsDoubleNear( b, 2.41321, 0.001 ) );
QVERIFY( qgsDoubleNear( c, 773810, 1 ) );
QVERIFY( qgsDoubleNear( d, 2.4137, 0.001 ) );
QVERIFY( qgsDoubleNear( e, -4.1798, 0.001 ) );
QVERIFY( qgsDoubleNear( f, 3.35331e+06, 1e+03 ) );
QGSCOMPARENEAR( a, 4.18061, 0.001 );
QGSCOMPARENEAR( b, 2.41321, 0.001 );
QGSCOMPARENEAR( c, 773810, 1 );
QGSCOMPARENEAR( d, 2.4137, 0.001 );
QGSCOMPARENEAR( e, -4.1798, 0.001 );
QGSCOMPARENEAR( f, 3.35331e+06, 1e+03 );

mComposition->setGenerateWorldFile( false );
mComposerMap->setMapRotation( 0.0 );
Expand Down

0 comments on commit 21a3b8a

Please sign in to comment.