From 96295010d39aba072b097c7c3a8830fa91ee07f9 Mon Sep 17 00:00:00 2001 From: Magnus Homann Date: Sun, 16 Sep 2012 12:07:26 +0200 Subject: [PATCH] Improved QgsClipper test, and ran prepare-commit --- python/core/qgsrectangle.sip | 2 +- src/core/qgsrectangle.cpp | 6 ++-- tests/src/core/testqgsclipper.cpp | 56 +++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/python/core/qgsrectangle.sip b/python/core/qgsrectangle.sip index 69462ed2d81..13bcbb79416 100644 --- a/python/core/qgsrectangle.sip +++ b/python/core/qgsrectangle.sip @@ -10,7 +10,7 @@ class QgsRectangle %TypeHeaderCode #include %End - + public: //! Constructor QgsRectangle(double xmin=0, double ymin=0, double xmax=0, double ymax=0); diff --git a/src/core/qgsrectangle.cpp b/src/core/qgsrectangle.cpp index 53d49271adf..3b0dbc811c7 100644 --- a/src/core/qgsrectangle.cpp +++ b/src/core/qgsrectangle.cpp @@ -196,7 +196,7 @@ QString QgsRectangle::asWktCoordinates() const QString QgsRectangle::asWktPolygon() const { QString rep = - QString("POLYGON((") + + QString( "POLYGON((" ) + QString::number( xmin, 'f', 16 ) + " " + QString::number( ymin, 'f', 16 ) + ", " + QString::number( xmax, 'f', 16 ) + " " + @@ -207,7 +207,7 @@ QString QgsRectangle::asWktPolygon() const QString::number( ymax, 'f', 16 ) + ", " + QString::number( xmin, 'f', 16 ) + " " + QString::number( ymin, 'f', 16 ) + - QString("))"); + QString( "))" ); return rep; } @@ -216,7 +216,7 @@ QString QgsRectangle::asWktPolygon() const //@note added in 2.0 QRectF QgsRectangle::toRectF() const { - return QRectF( (qreal)xmin, (qreal)ymin, (qreal)xmax - xmin, (qreal)ymax - ymin ); + return QRectF(( qreal )xmin, ( qreal )ymin, ( qreal )xmax - xmin, ( qreal )ymax - ymin ); } // Return a string representation of the rectangle with automatic or high precision diff --git a/tests/src/core/testqgsclipper.cpp b/tests/src/core/testqgsclipper.cpp index 2b6fb247808..31ed704ecc1 100644 --- a/tests/src/core/testqgsclipper.cpp +++ b/tests/src/core/testqgsclipper.cpp @@ -34,36 +34,56 @@ class TestQgsClipper: public QObject void init() {};// will be called before each testfunction is executed. void cleanup() {};// will be called after every testfunction. void basic(); + private: + bool TestQgsClipper::checkBoundingBox( QPolygonF polygon, QgsRectangle clipRect ); }; void TestQgsClipper::initTestCase() { - // - // Runs once before any tests are run - // - // init QGIS's paths - true means that all path will be inited from prefix - // QgsApplication::init(); - // QgsApplication::initQgis(); - // QgsApplication::showSettings(); + } void TestQgsClipper::basic() { - // CQgsClipper is static only - //QgsClipper snipsnip; + // QgsClipper is static only QPolygonF polygon; - polygon << QPointF(10.4, 20.5) << QPointF(20.2, 30.2); - - QgsRectangle clipRect(10, 10, 25, 30 ); - - QgsClipper::trimPolygon( polygon, clipRect ); - - QRectF bBox( polygon.boundingRect() ); - QgsRectangle boundingRect( bBox.bottomLeft().x(), bBox.bottomLeft().y(), bBox.topRight().x(), bBox.topRight().y() ); + polygon << QPointF( 10.4, 20.5 ) << QPointF( 20.2, 30.2 ); - QVERIFY( clipRect.contains( boundingRect ) ); + QgsRectangle clipRect( 10, 10, 25, 30 ); + + QgsClipper::trimPolygon( polygon, clipRect ); + + // Check nothing sticks out. + QVERIFY( checkBoundingBox( polygon , clipRect ) ); + // Check that it didn't clip too much + QgsRectangle clipRectInner( clipRect ); + clipRectInner.scale( 0.999 ); + QVERIFY( ! checkBoundingBox( polygon , clipRectInner ) ); + + // A more complex example + polygon.clear(); + polygon << QPointF( 1.0, 9.0 ) << QPointF( 11.0, 11.0 ) << QPointF( 9.0, 1.0 ); + clipRect.set( 0.0, 0.0, 10.0, 10.0 ); + + QgsClipper::trimPolygon( polygon, clipRect ); + + // We should have 5 vertices now? + QCOMPARE( polygon.size(), 5 ); + // Check nothing sticks out. + QVERIFY( checkBoundingBox( polygon , clipRect ) ); + // Check that it didn't clip too much + clipRectInner = clipRect; + clipRectInner.scale( 0.999 ); + QVERIFY( ! checkBoundingBox( polygon , clipRectInner ) ); }; +bool TestQgsClipper::checkBoundingBox( QPolygonF polygon, QgsRectangle clipRect ) +{ + QgsRectangle bBox( polygon.boundingRect() ); + + return clipRect.contains( bBox ); +} + QTEST_MAIN( TestQgsClipper ) #include "moc_testqgsclipper.cxx"