|
| 1 | +/*************************************************************************** |
| 2 | + testqgsgenericspatialindex.cpp |
| 3 | + ------------------------------ |
| 4 | + Date : December 2019 |
| 5 | + Copyright : (C) 2019 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 | +#include "qgstest.h" |
| 16 | + |
| 17 | +#include <QObject> |
| 18 | +#include <QString> |
| 19 | +#include <QStringList> |
| 20 | +#include <QLocale> |
| 21 | + |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +#include "qgsgenericspatialindex.h" |
| 25 | +#include "qgspallabeling.h" |
| 26 | +#include "qgstest.h" |
| 27 | + |
| 28 | +class TestQgsGenericSpatialIndex: public QObject |
| 29 | +{ |
| 30 | + Q_OBJECT |
| 31 | + |
| 32 | + private slots: |
| 33 | + void initTestCase();// will be called before the first testfunction is executed. |
| 34 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 35 | + void init();// will be called before each testfunction is executed. |
| 36 | + void cleanup();// will be called after every testfunction. |
| 37 | + void testIndex(); |
| 38 | + |
| 39 | + private: |
| 40 | +}; |
| 41 | + |
| 42 | +void TestQgsGenericSpatialIndex::initTestCase() |
| 43 | +{ |
| 44 | +} |
| 45 | + |
| 46 | +void TestQgsGenericSpatialIndex::cleanupTestCase() |
| 47 | +{ |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +void TestQgsGenericSpatialIndex::init() |
| 52 | +{ |
| 53 | +} |
| 54 | + |
| 55 | +void TestQgsGenericSpatialIndex::cleanup() |
| 56 | +{ |
| 57 | +} |
| 58 | + |
| 59 | +void TestQgsGenericSpatialIndex::testIndex() |
| 60 | +{ |
| 61 | + QgsGenericSpatialIndex< QgsLabelPosition > index; |
| 62 | + |
| 63 | + QList< QgsLabelPosition * > found; |
| 64 | + index.intersects( QgsRectangle( std::numeric_limits< double >::lowest(), |
| 65 | + std::numeric_limits< double >::lowest(), |
| 66 | + std::numeric_limits< double >::max(), |
| 67 | + std::numeric_limits< double >::max() ), |
| 68 | + [&found]( QgsLabelPosition * p )-> bool |
| 69 | + { |
| 70 | + found.append( p ); |
| 71 | + return true; |
| 72 | + } ); |
| 73 | + |
| 74 | + QVERIFY( found.isEmpty() ); |
| 75 | + |
| 76 | + // remove non-existant feature |
| 77 | + QgsLabelPosition p1; |
| 78 | + QVERIFY( ! index.remove( &p1, QgsRectangle() ) ); |
| 79 | + |
| 80 | + // add data |
| 81 | + QVERIFY( index.insert( &p1, QgsRectangle( 1, 1, 5, 5 ) ) ); |
| 82 | + QgsLabelPosition p2; |
| 83 | + QVERIFY( index.insert( &p2, QgsRectangle( 1, 1, 4, 4 ) ) ); |
| 84 | + QgsLabelPosition p3; |
| 85 | + QVERIFY( index.insert( &p3, QgsRectangle( 11, 11, 14, 14 ) ) ); |
| 86 | + |
| 87 | + index.intersects( QgsRectangle( std::numeric_limits< double >::lowest(), |
| 88 | + std::numeric_limits< double >::lowest(), |
| 89 | + std::numeric_limits< double >::max(), |
| 90 | + std::numeric_limits< double >::max() ), |
| 91 | + [&found]( QgsLabelPosition * p )-> bool |
| 92 | + { |
| 93 | + found.append( p ); |
| 94 | + return true; |
| 95 | + } ); |
| 96 | + QCOMPARE( found.count(), 3 ); |
| 97 | + QVERIFY( found.contains( &p1 ) ); |
| 98 | + QVERIFY( found.contains( &p2 ) ); |
| 99 | + QVERIFY( found.contains( &p3 ) ); |
| 100 | + found.clear(); |
| 101 | + |
| 102 | + index.intersects( QgsRectangle( 0, 0, 3, 3 ), |
| 103 | + [&found]( QgsLabelPosition * p )-> bool |
| 104 | + { |
| 105 | + found.append( p ); |
| 106 | + return true; |
| 107 | + } ); |
| 108 | + QCOMPARE( found.count(), 2 ); |
| 109 | + QVERIFY( found.contains( &p1 ) ); |
| 110 | + QVERIFY( found.contains( &p2 ) ); |
| 111 | + found.clear(); |
| 112 | + |
| 113 | + index.intersects( QgsRectangle( 4.5, 4.5, 5, 5 ), |
| 114 | + [&found]( QgsLabelPosition * p )-> bool |
| 115 | + { |
| 116 | + found.append( p ); |
| 117 | + return true; |
| 118 | + } ); |
| 119 | + QCOMPARE( found.count(), 1 ); |
| 120 | + QVERIFY( found.contains( &p1 ) ); |
| 121 | + found.clear(); |
| 122 | + |
| 123 | + index.intersects( QgsRectangle( 10, 10, 13, 13 ), |
| 124 | + [&found]( QgsLabelPosition * p )-> bool |
| 125 | + { |
| 126 | + found.append( p ); |
| 127 | + return true; |
| 128 | + } ); |
| 129 | + QCOMPARE( found.count(), 1 ); |
| 130 | + QVERIFY( found.contains( &p3 ) ); |
| 131 | + found.clear(); |
| 132 | + |
| 133 | + QVERIFY( index.remove( &p1, QgsRectangle( 1, 1, 5, 5 ) ) ); |
| 134 | + |
| 135 | + index.intersects( QgsRectangle( 0, 0, 3, 3 ), |
| 136 | + [&found]( QgsLabelPosition * p )-> bool |
| 137 | + { |
| 138 | + found.append( p ); |
| 139 | + return true; |
| 140 | + } ); |
| 141 | + QCOMPARE( found.count(), 1 ); |
| 142 | + QVERIFY( found.contains( &p2 ) ); |
| 143 | + found.clear(); |
| 144 | + |
| 145 | + QVERIFY( !index.remove( &p1, QgsRectangle( 1, 1, 5, 5 ) ) ); |
| 146 | + |
| 147 | + index.intersects( QgsRectangle( std::numeric_limits< double >::lowest(), |
| 148 | + std::numeric_limits< double >::lowest(), |
| 149 | + std::numeric_limits< double >::max(), |
| 150 | + std::numeric_limits< double >::max() ), |
| 151 | + [&found]( QgsLabelPosition * p )-> bool |
| 152 | + { |
| 153 | + found.append( p ); |
| 154 | + return true; |
| 155 | + } ); |
| 156 | + QCOMPARE( found.count(), 2 ); |
| 157 | + QVERIFY( found.contains( &p2 ) ); |
| 158 | + QVERIFY( found.contains( &p3 ) ); |
| 159 | + found.clear(); |
| 160 | + |
| 161 | + QVERIFY( index.remove( &p2, QgsRectangle( 1, 1, 4, 4 ) ) ); |
| 162 | + |
| 163 | + index.intersects( QgsRectangle( std::numeric_limits< double >::lowest(), |
| 164 | + std::numeric_limits< double >::lowest(), |
| 165 | + std::numeric_limits< double >::max(), |
| 166 | + std::numeric_limits< double >::max() ), |
| 167 | + [&found]( QgsLabelPosition * p )-> bool |
| 168 | + { |
| 169 | + found.append( p ); |
| 170 | + return true; |
| 171 | + } ); |
| 172 | + QCOMPARE( found.count(), 1 ); |
| 173 | + QVERIFY( found.contains( &p3 ) ); |
| 174 | + found.clear(); |
| 175 | + |
| 176 | + QVERIFY( index.remove( &p3, QgsRectangle( 11, 11, 14, 14 ) ) ); |
| 177 | + |
| 178 | + index.intersects( QgsRectangle( std::numeric_limits< double >::lowest(), |
| 179 | + std::numeric_limits< double >::lowest(), |
| 180 | + std::numeric_limits< double >::max(), |
| 181 | + std::numeric_limits< double >::max() ), |
| 182 | + [&found]( QgsLabelPosition * p )-> bool |
| 183 | + { |
| 184 | + found.append( p ); |
| 185 | + return true; |
| 186 | + } ); |
| 187 | + QVERIFY( found.empty() ); |
| 188 | +} |
| 189 | + |
| 190 | + |
| 191 | +QGSTEST_MAIN( TestQgsGenericSpatialIndex ) |
| 192 | +#include "testqgsgenericspatialindex.moc" |
0 commit comments