Skip to content

Commit 7ac0d56

Browse files
committedJul 7, 2018
Unit tests for QgsSpatialIndexKDBush
1 parent 004dc18 commit 7ac0d56

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
 

‎src/core/qgsspatialindexkdbush.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class CORE_EXPORT QgsSpatialIndexKDBush
120120

121121
//! Implicitly shared data pointer
122122
QgsSpatialIndexKDBushPrivate *d = nullptr;
123+
124+
friend class TestQgsSpatialIndexKdBush;
123125
};
124126

125127
#endif // QGSSPATIALINDEXKDBUSH_H

‎tests/src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# the UI file won't be wrapped!
44
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
55
${CMAKE_CURRENT_BINARY_DIR}
6+
${CMAKE_SOURCE_DIR}/external/kdbush/include
67
${CMAKE_SOURCE_DIR}/src/core
78
${CMAKE_SOURCE_DIR}/src/annotations
89
${CMAKE_SOURCE_DIR}/src/core/auth
@@ -175,6 +176,7 @@ SET(TESTS
175176
testqgssimplemarker.cpp
176177
testqgssnappingutils.cpp
177178
testqgsspatialindex.cpp
179+
testqgsspatialindexkdbush.cpp
178180
testqgsstatisticalsummary.cpp
179181
testqgsstringutils.cpp
180182
testqgsstyle.cpp
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/***************************************************************************
2+
TestQgsSpatialIndexKdBushkdbush.cpp
3+
--------------------------------------
4+
Date : July 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgstest.h"
17+
#include <QObject>
18+
#include <QString>
19+
20+
#include <qgsapplication.h>
21+
#include "qgsfeatureiterator.h"
22+
#include "qgsgeometry.h"
23+
#include "qgsspatialindexkdbush.h"
24+
#include "qgsvectordataprovider.h"
25+
#include "qgsvectorlayer.h"
26+
#include "qgsspatialindexkdbush_p.h"
27+
28+
static QgsFeature _pointFeature( QgsFeatureId id, qreal x, qreal y )
29+
{
30+
QgsFeature f( id );
31+
QgsGeometry g = QgsGeometry::fromPointXY( QgsPointXY( x, y ) );
32+
f.setGeometry( g );
33+
return f;
34+
}
35+
36+
static QList<QgsFeature> _pointFeatures()
37+
{
38+
/*
39+
* 2 | 1
40+
* |
41+
* -----+-----
42+
* |
43+
* 3 | 4
44+
*/
45+
46+
QList<QgsFeature> feats;
47+
feats << _pointFeature( 1, 1, 1 )
48+
<< _pointFeature( 2, -1, 1 )
49+
<< _pointFeature( 3, -1, -1 )
50+
<< _pointFeature( 4, 1, -1 );
51+
return feats;
52+
}
53+
54+
class TestQgsSpatialIndexKdBush : public QObject
55+
{
56+
Q_OBJECT
57+
58+
private slots:
59+
60+
void initTestCase()
61+
{
62+
QgsApplication::init();
63+
QgsApplication::initQgis();
64+
}
65+
void cleanupTestCase()
66+
{
67+
QgsApplication::exitQgis();
68+
}
69+
70+
void testQuery()
71+
{
72+
std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) );
73+
for ( QgsFeature f : _pointFeatures() )
74+
vl->dataProvider()->addFeature( f );
75+
QgsSpatialIndexKDBush index( *vl->dataProvider() );
76+
77+
QList<QgsFeatureId> fids = index.intersect( QgsRectangle( 0, 0, 10, 10 ) );
78+
QVERIFY( fids.count() == 1 );
79+
QCOMPARE( fids[0], 1 );
80+
81+
QList<QgsFeatureId> fids2 = index.intersect( QgsRectangle( -10, -10, 0, 10 ) );
82+
QCOMPARE( fids2.count(), 2 );
83+
QVERIFY( fids2.contains( 2 ) );
84+
QVERIFY( fids2.contains( 3 ) );
85+
86+
QList<QgsFeatureId> fids3 = index.within( QgsPointXY( 0, 0 ), 2 );
87+
QCOMPARE( fids3.count(), 4 );
88+
QVERIFY( fids3.contains( 1 ) );
89+
QVERIFY( fids3.contains( 2 ) );
90+
QVERIFY( fids3.contains( 3 ) );
91+
QVERIFY( fids3.contains( 4 ) );
92+
93+
QList<QgsFeatureId> fids4 = index.within( QgsPointXY( 0, 0 ), 1 );
94+
QCOMPARE( fids4.count(), 0 );
95+
96+
QList<QgsFeatureId> fids5 = index.within( QgsPointXY( -1, -1 ), 2.1 );
97+
QCOMPARE( fids5.count(), 3 );
98+
QVERIFY( fids5.contains( 2 ) );
99+
QVERIFY( fids5.contains( 3 ) );
100+
QVERIFY( fids5.contains( 4 ) );
101+
102+
QgsPointXY p;
103+
QVERIFY( !index.point( -1, p ) );
104+
QVERIFY( !index.point( 5, p ) );
105+
QVERIFY( index.point( 1, p ) );
106+
QCOMPARE( p, QgsPointXY( 1, 1 ) );
107+
QVERIFY( index.point( 2, p ) );
108+
QCOMPARE( p, QgsPointXY( -1, 1 ) );
109+
QVERIFY( index.point( 3, p ) );
110+
QCOMPARE( p, QgsPointXY( -1, -1 ) );
111+
QVERIFY( index.point( 4, p ) );
112+
QCOMPARE( p, QgsPointXY( 1, -1 ) );
113+
}
114+
115+
void testCopy()
116+
{
117+
std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) );
118+
for ( QgsFeature f : _pointFeatures() )
119+
vl->dataProvider()->addFeature( f );
120+
121+
std::unique_ptr< QgsSpatialIndexKDBush > index( new QgsSpatialIndexKDBush( *vl->dataProvider() ) );
122+
123+
// create copy of the index
124+
std::unique_ptr< QgsSpatialIndexKDBush > indexCopy( new QgsSpatialIndexKDBush( *index ) );
125+
126+
QCOMPARE( index->d, indexCopy->d );
127+
QVERIFY( index->d->ref == 2 );
128+
129+
// test that copied index works
130+
QList<QgsFeatureId> fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
131+
QVERIFY( fids.count() == 1 );
132+
QCOMPARE( fids[0], 1 );
133+
134+
// check that the index is still shared
135+
QCOMPARE( index->d, indexCopy->d );
136+
QVERIFY( index->d->ref == 2 );
137+
138+
index.reset();
139+
140+
// test that copied index still works
141+
fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
142+
QVERIFY( fids.count() == 1 );
143+
QCOMPARE( fids[0], 1 );
144+
QVERIFY( indexCopy->d->ref == 1 );
145+
146+
// assignment operator
147+
std::unique_ptr< QgsVectorLayer > vl2 = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) );
148+
QgsSpatialIndexKDBush index3( *vl2->dataProvider() );
149+
fids = index3.intersect( QgsRectangle( 0, 0, 10, 10 ) );
150+
QCOMPARE( fids.count(), 0 );
151+
QVERIFY( index3.d->ref == 1 );
152+
153+
index3 = *indexCopy;
154+
QCOMPARE( index3.d, indexCopy->d );
155+
QVERIFY( index3.d->ref == 2 );
156+
fids = index3.intersect( QgsRectangle( 0, 0, 10, 10 ) );
157+
QVERIFY( fids.count() == 1 );
158+
QCOMPARE( fids[0], 1 );
159+
160+
indexCopy.reset();
161+
QCOMPARE( index3.d->ref, 1 );
162+
}
163+
164+
};
165+
166+
QGSTEST_MAIN( TestQgsSpatialIndexKdBush )
167+
168+
#include "testqgsspatialindexkdbush.moc"
169+
170+

0 commit comments

Comments
 (0)
Please sign in to comment.