Skip to content

Commit 21a3b8a

Browse files
committed
Use QGSCOMPARENEAR instead of QVERIFY( qgsDoubleNear(... ) )
QGSCOMPARENEAR gives better debugging output when the test fails
1 parent 2af10cb commit 21a3b8a

23 files changed

+295
-279
lines changed

tests/src/analysis/testqgsrastercalculator.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Email : nyall dot dawson at gmail dot com
2121
#include "qgsrastermatrix.h"
2222
#include "qgsapplication.h"
2323
#include "qgsproject.h"
24+
#include "qgstestutils.h"
2425

2526
Q_DECLARE_METATYPE( QgsRasterCalcNode::Operator )
2627

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

214215
qDebug() << "Result: " << result.number() << " expected: " << expected;
215-
QVERIFY( qgsDoubleNear( result.number(), expected, 0.0000000001 ) );
216+
QGSCOMPARENEAR( result.number(), expected, 0.0000000001 );
216217

217218
}
218219

tests/src/app/testqgsattributetable.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "qgsvectorfilewriter.h"
2929

3030
#include "qgstest.h"
31+
#include "qgstestutils.h"
3132

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

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

132133
void TestQgsAttributeTable::testFieldCalculationArea()
@@ -164,7 +165,7 @@ void TestQgsAttributeTable::testFieldCalculationArea()
164165
QgsFeature f;
165166
QVERIFY( fit.nextFeature( f ) );
166167
double expected = 1009089817.0;
167-
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 1.0 ) );
168+
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 1.0 );
168169

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

182183
void TestQgsAttributeTable::testNoGeom()

tests/src/app/testqgsfieldcalculator.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "qgsproject.h"
2525
#include "qgsmapcanvas.h"
2626
#include "qgsunittypes.h"
27+
#include "qgstestutils.h"
2728

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

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

127128
}
128129

@@ -168,7 +169,7 @@ void TestQgsFieldCalculator::testAreaCalculations()
168169
QgsFeature f;
169170
QVERIFY( fit.nextFeature( f ) );
170171
double expected = 1009089817.0;
171-
QVERIFY( qgsDoubleNear( f.attribute( "col1" ).toDouble(), expected, 1.0 ) );
172+
QGSCOMPARENEAR( f.attribute( "col1" ).toDouble(), expected, 1.0 );
172173

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

189190
QGSTEST_MAIN( TestQgsFieldCalculator )

tests/src/app/testqgsmaptoolidentifyaction.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsunittypes.h"
2727
#include "qgsmaptoolidentifyaction.h"
2828
#include "qgssettings.h"
29+
#include "qgstestutils.h"
2930

3031
#include "cpl_conv.h"
3132

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

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

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

164165
void TestQgsMapToolIdentifyAction::perimeterCalculation()
@@ -203,15 +204,15 @@ void TestQgsMapToolIdentifyAction::perimeterCalculation()
203204
QCOMPARE( result.length(), 1 );
204205
derivedPerimeter = result.at( 0 ).mDerivedAttributes[tr( "Perimeter" )];
205206
perimeter = derivedPerimeter.remove( ',' ).split( ' ' ).at( 0 ).toDouble();
206-
QVERIFY( qgsDoubleNear( perimeter, 420896.0, 0.1 ) );
207+
QGSCOMPARENEAR( perimeter, 420896.0, 0.1 );
207208

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

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

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

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

272273
// private

tests/src/core/testqgscomposermap.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "qgsproperty.h"
3030
#include <QObject>
3131
#include "qgstest.h"
32+
#include "qgstestutils.h"
3233

3334
class TestQgsComposerMap : public QObject
3435
{
@@ -176,34 +177,34 @@ void TestQgsComposerMap::worldFileGeneration()
176177
double a, b, c, d, e, f;
177178
mComposition->computeWorldFileParameters( a, b, c, d, e, f );
178179

179-
QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
180-
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
181-
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
182-
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
183-
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
184-
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );
180+
QGSCOMPARENEAR( a, 4.18048, 0.001 );
181+
QGSCOMPARENEAR( b, 2.41331, 0.001 );
182+
QGSCOMPARENEAR( c, 779444, 1 );
183+
QGSCOMPARENEAR( d, 2.4136, 0.001 );
184+
QGSCOMPARENEAR( e, -4.17997, 0.001 );
185+
QGSCOMPARENEAR( f, 3.34241e+06, 1e+03 );
185186

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

190-
QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
191-
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
192-
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
193-
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
194-
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
195-
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );
191+
QGSCOMPARENEAR( a, 4.18048, 0.001 );
192+
QGSCOMPARENEAR( b, 2.41331, 0.001 );
193+
QGSCOMPARENEAR( c, 779444, 1 );
194+
QGSCOMPARENEAR( d, 2.4136, 0.001 );
195+
QGSCOMPARENEAR( e, -4.17997, 0.001 );
196+
QGSCOMPARENEAR( f, 3.34241e+06, 1e+03 );
196197

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

201-
QVERIFY( qgsDoubleNear( a, 4.18061, 0.001 ) );
202-
QVERIFY( qgsDoubleNear( b, 2.41321, 0.001 ) );
203-
QVERIFY( qgsDoubleNear( c, 773810, 1 ) );
204-
QVERIFY( qgsDoubleNear( d, 2.4137, 0.001 ) );
205-
QVERIFY( qgsDoubleNear( e, -4.1798, 0.001 ) );
206-
QVERIFY( qgsDoubleNear( f, 3.35331e+06, 1e+03 ) );
202+
QGSCOMPARENEAR( a, 4.18061, 0.001 );
203+
QGSCOMPARENEAR( b, 2.41321, 0.001 );
204+
QGSCOMPARENEAR( c, 773810, 1 );
205+
QGSCOMPARENEAR( d, 2.4137, 0.001 );
206+
QGSCOMPARENEAR( e, -4.1798, 0.001 );
207+
QGSCOMPARENEAR( f, 3.35331e+06, 1e+03 );
207208

208209
mComposition->setGenerateWorldFile( false );
209210
mComposerMap->setMapRotation( 0.0 );

0 commit comments

Comments
 (0)