@@ -25,13 +25,37 @@ class TestQgsRectangle: public QObject
25
25
{
26
26
Q_OBJECT
27
27
private slots:
28
+ void isEmpty ();
28
29
void manipulate ();
29
30
void regression6194 ();
30
31
void operators ();
31
32
void asVariant ();
32
33
void referenced ();
34
+ void minimal ();
35
+ void grow ();
36
+ void include ();
37
+ void buffered ();
38
+ void isFinite ();
39
+ void dataStream ();
33
40
};
34
41
42
+ void TestQgsRectangle::isEmpty ()
43
+ {
44
+ QVERIFY ( QgsRectangle ().isEmpty () );
45
+ QVERIFY ( QgsRectangle ( 1 , 2 , 1 , 4 ).isEmpty () );
46
+ QVERIFY ( QgsRectangle ( 2 , 1 , 4 , 1 ).isEmpty () );
47
+ // constructor should normalize
48
+ QVERIFY ( !QgsRectangle ( 2 , 2 , 1 , 4 ).isEmpty () );
49
+ QVERIFY ( !QgsRectangle ( 1 , 2 , 2 , 1 ).isEmpty () );
50
+
51
+ QgsRectangle r ( 2 , 2 , 3 , 4 );
52
+ r.setXMaximum ( 1 );
53
+ QVERIFY ( r.isEmpty () );
54
+ r = QgsRectangle ( 2 , 2 , 3 , 4 );
55
+ r.setYMaximum ( 1 );
56
+ QVERIFY ( r.isEmpty () );
57
+ }
58
+
35
59
void TestQgsRectangle::manipulate ()
36
60
{
37
61
// Set up two intersecting rectangles and normalize
@@ -155,5 +179,111 @@ void TestQgsRectangle::referenced()
155
179
QCOMPARE ( rect2.crs ().authid (), QStringLiteral ( " EPSG:28356" ) );
156
180
}
157
181
182
+ void TestQgsRectangle::minimal ()
183
+ {
184
+ QgsRectangle rect1 = QgsRectangle ( 10.0 , 20.0 , 110.0 , 220.0 );
185
+ rect1.setMinimal ();
186
+ QVERIFY ( rect1.isEmpty () );
187
+ QVERIFY ( rect1.isNull () );
188
+ }
189
+
190
+ void TestQgsRectangle::grow ()
191
+ {
192
+ QgsRectangle rect1 = QgsRectangle ( 10.0 , 20.0 , 110.0 , 220.0 );
193
+ rect1.grow ( 11 );
194
+ QCOMPARE ( rect1.xMinimum (), -1.0 );
195
+ QCOMPARE ( rect1.yMinimum (), 9.0 );
196
+ QCOMPARE ( rect1.xMaximum (), 121.0 );
197
+ QCOMPARE ( rect1.yMaximum (), 231.0 );
198
+
199
+ QgsRectangle rect2 = QgsRectangle ( -110.0 , -220.0 , -10.0 , -20.0 );
200
+ rect2.grow ( 11 );
201
+ QCOMPARE ( rect2.xMinimum (), -121.0 );
202
+ QCOMPARE ( rect2.yMinimum (), -231.0 );
203
+ QCOMPARE ( rect2.xMaximum (), 1.0 );
204
+ QCOMPARE ( rect2.yMaximum (), -9.0 );
205
+ }
206
+
207
+ void TestQgsRectangle::include ()
208
+ {
209
+ QgsRectangle rect1 = QgsRectangle ( 10.0 , 20.0 , 110.0 , 220.0 );
210
+ // inside
211
+ rect1.include ( QgsPointXY ( 15 , 50 ) );
212
+ QCOMPARE ( rect1.xMinimum (), 10.0 );
213
+ QCOMPARE ( rect1.yMinimum (), 20.0 );
214
+ QCOMPARE ( rect1.xMaximum (), 110.0 );
215
+ QCOMPARE ( rect1.yMaximum (), 220.0 );
216
+
217
+ rect1.include ( QgsPointXY ( 5 , 50 ) );
218
+ QCOMPARE ( rect1.xMinimum (), 5.0 );
219
+ QCOMPARE ( rect1.yMinimum (), 20.0 );
220
+ QCOMPARE ( rect1.xMaximum (), 110.0 );
221
+ QCOMPARE ( rect1.yMaximum (), 220.0 );
222
+
223
+ rect1.include ( QgsPointXY ( 15 , 12 ) );
224
+ QCOMPARE ( rect1.xMinimum (), 5.0 );
225
+ QCOMPARE ( rect1.yMinimum (), 12.0 );
226
+ QCOMPARE ( rect1.xMaximum (), 110.0 );
227
+ QCOMPARE ( rect1.yMaximum (), 220.0 );
228
+
229
+ rect1.include ( QgsPointXY ( 115 , 12 ) );
230
+ QCOMPARE ( rect1.xMinimum (), 5.0 );
231
+ QCOMPARE ( rect1.yMinimum (), 12.0 );
232
+ QCOMPARE ( rect1.xMaximum (), 115.0 );
233
+ QCOMPARE ( rect1.yMaximum (), 220.0 );
234
+
235
+ rect1.include ( QgsPointXY ( 115 , 242 ) );
236
+ QCOMPARE ( rect1.xMinimum (), 5.0 );
237
+ QCOMPARE ( rect1.yMinimum (), 12.0 );
238
+ QCOMPARE ( rect1.xMaximum (), 115.0 );
239
+ QCOMPARE ( rect1.yMaximum (), 242.0 );
240
+
241
+ }
242
+
243
+ void TestQgsRectangle::buffered ()
244
+ {
245
+ QgsRectangle rect = QgsRectangle ( 10.0 , 20.0 , 110.0 , 220.0 );
246
+ QgsRectangle rect1 = rect.buffered ( 11 );
247
+ QCOMPARE ( rect1.xMinimum (), -1.0 );
248
+ QCOMPARE ( rect1.yMinimum (), 9.0 );
249
+ QCOMPARE ( rect1.xMaximum (), 121.0 );
250
+ QCOMPARE ( rect1.yMaximum (), 231.0 );
251
+
252
+ rect = QgsRectangle ( -110.0 , -220.0 , -10.0 , -20.0 );
253
+ QgsRectangle rect2 = rect.buffered ( 11 );
254
+ QCOMPARE ( rect2.xMinimum (), -121.0 );
255
+ QCOMPARE ( rect2.yMinimum (), -231.0 );
256
+ QCOMPARE ( rect2.xMaximum (), 1.0 );
257
+ QCOMPARE ( rect2.yMaximum (), -9.0 );
258
+ }
259
+
260
+ void TestQgsRectangle::isFinite ()
261
+ {
262
+ QVERIFY ( QgsRectangle ( 1 , 2 , 3 , 4 ).isFinite () );
263
+ QVERIFY ( !QgsRectangle ( std::numeric_limits<double >::infinity (), 2 , 3 , 4 ).isFinite () );
264
+ QVERIFY ( !QgsRectangle ( 1 , std::numeric_limits<double >::infinity (), 3 , 4 ).isFinite () );
265
+ QVERIFY ( !QgsRectangle ( 1 , 2 , std::numeric_limits<double >::infinity (), 4 ).isFinite () );
266
+ QVERIFY ( !QgsRectangle ( 1 , 2 , 3 , std::numeric_limits<double >::infinity () ).isFinite () );
267
+ QVERIFY ( !QgsRectangle ( std::numeric_limits<double >::quiet_NaN (), 2 , 3 , 4 ).isFinite () );
268
+ QVERIFY ( !QgsRectangle ( 1 , std::numeric_limits<double >::quiet_NaN (), 3 , 4 ).isFinite () );
269
+ QVERIFY ( !QgsRectangle ( 1 , 2 , std::numeric_limits<double >::quiet_NaN (), 4 ).isFinite () );
270
+ QVERIFY ( !QgsRectangle ( 1 , 2 , 3 , std::numeric_limits<double >::quiet_NaN () ).isFinite () );
271
+ }
272
+
273
+ void TestQgsRectangle::dataStream ()
274
+ {
275
+ QgsRectangle original ( 10.1 , 20.2 , 110.3 , 220.4 );
276
+
277
+ QByteArray ba;
278
+ QDataStream ds ( &ba, QIODevice::ReadWrite );
279
+ ds << original;
280
+
281
+ QgsRectangle result;
282
+ ds.device ()->seek ( 0 );
283
+ ds >> result;
284
+
285
+ QCOMPARE ( result, original );
286
+ }
287
+
158
288
QGSTEST_MAIN ( TestQgsRectangle )
159
289
#include " testqgsrectangle.moc"
0 commit comments