2
This file is part of the Okteta Gui library, part of the KDE project.
4
Copyright 2006 Friedrich W. H. Kossebau <kossebau@kde.org>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2.1 of the License, or (at your option) version 3, or any
10
later version accepted by the membership of KDE e.V. (or its
11
successor approved by the membership of KDE e.V.), which shall
12
act as a proxy defined in Section 6 of version 3 of the license.
14
This library is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
Lesser General Public License for more details.
19
You should have received a copy of the GNU Lesser General Public
20
License along with this library. If not, see <http://www.gnu.org/licenses/>.
23
#include "coordrangetest.h"
26
#include <coordrange.h>
28
#include <QtTest/QtTest>
35
static const LinePosition Pos1 = 15;
36
static const LinePosition Pos2 = 25;
37
static const Line Line1 = 10;
38
static const LineSize LineCount = 10;
39
static const Line Line2 = Line1 + LineCount - 1;
40
static Coord Start( Pos1, Line1 );
41
static Coord End( Pos2, Line2 );
43
static const Size BufferWidth = Pos2 + 5;
46
void CoordRangeTest::testConstructorByCoords()
48
CoordRange coordRange( Start, End );
49
QCOMPARE( coordRange.start(), Start );
50
QCOMPARE( coordRange.end(), End );
51
QCOMPARE( coordRange.lines(), LineCount );
52
QVERIFY( coordRange.isValid() );
55
void CoordRangeTest::testConstructorByPosLines()
57
LinePositionRange positions( Pos1, Pos2 );
58
LineRange lines( Line1, Line2 );
60
CoordRange coordRange( positions, lines );
61
QCOMPARE( coordRange.start(), Coord(Pos1,Line1) );
62
QCOMPARE( coordRange.end(), Coord(Pos2,Line2) );
63
QCOMPARE( coordRange.lines(), LineCount );
64
QVERIFY( coordRange.isValid() );
67
void CoordRangeTest::testSimpleConstructor()
69
CoordRange coordRange;
70
QVERIFY( coordRange.isEmpty() );
71
QVERIFY( !coordRange.isValid() );
74
void CoordRangeTest::testSetGetStart()
76
CoordRange coordRange;
77
coordRange.setStart( Start );
78
QCOMPARE( coordRange.start(), Start );
81
void CoordRangeTest::testSetGetEnd()
83
CoordRange coordRange;
84
coordRange.setEnd( End );
85
QCOMPARE( coordRange.end(), End );
88
void CoordRangeTest::testIsEmpty()
90
CoordRange coordRange( Start, End );
91
QVERIFY( !coordRange.isEmpty() );
93
QVERIFY( coordRange.isEmpty() );
96
void CoordRangeTest::testCompare()
98
CoordRange coordRange( Start, End );
100
CoordRange otherCoordRange( Start, End );
101
QVERIFY( coordRange == otherCoordRange );
103
otherCoordRange.set( Coord(Pos1+1,Line1), End );
104
QVERIFY( !(coordRange == otherCoordRange) );
106
otherCoordRange.set(Start, Coord(Pos2+1,Line2));
107
QVERIFY( !(coordRange == otherCoordRange) );
110
void CoordRangeTest::testIncludes()
112
CoordRange coordRange( Start, End );
114
Coord coord = coordRange.start();
115
QVERIFY( coordRange.includes(coord) );
118
QVERIFY( !coordRange.includes(coord) );
120
coord = coordRange.end();
121
QVERIFY( coordRange.includes(coord) );
124
QVERIFY( !coordRange.includes(coord) );
127
void CoordRangeTest::testIncludesLine()
129
CoordRange coordRange( Start, End );
131
Line L = coordRange.start().line();
132
QVERIFY( coordRange.includesLine(L) );
135
QVERIFY( !coordRange.includesLine(L) );
137
L = coordRange.end().line();
138
QVERIFY( coordRange.includesLine(L) );
141
QVERIFY( !coordRange.includesLine(L) );
144
void CoordRangeTest::testWidth()
146
CoordRange coordRange( Start, End );
147
const Size width = BufferWidth*(LineCount-1) - Start.pos() + End.pos()+1;
148
QCOMPARE( coordRange.width(BufferWidth), width );
153
void CoordRangeTest::testAdaptToChange()
155
// adaptToChange, same length, behind
156
CoordRange coordRange( Start, End );
157
CoordRange otherCoordRange( coordRange );
158
coordRange.adaptToChange( End+1, Width, Width );
159
QCOMPARE( coordRange, otherCoordRange );
161
// adaptToChange, same length, before
162
coordRange.set( Start, End );
163
otherCoordRange.set( coordRange );
164
coordRange.adaptToChange( Start-2, 1, 1 );
165
QCOMPARE( coordRange, otherCoordRange );
167
// adaptToChange, same length, -1,-1
168
coordRange.set( Start-1, End-1 );
169
otherCoordRange.set( Start-1, Start-1 );
170
coordRange.adaptToChange( Start, Width, Width );
171
QCOMPARE( coordRange, otherCoordRange );
173
// adaptToChange, same length, -1,0
174
coordRange.set( Start-1, End );
175
otherCoordRange.set( coordRange );
176
coordRange.adaptToChange( Start, Width, Width );
177
QCOMPARE( coordRange, otherCoordRange );
179
// adaptToChange, same length, -1,+1
180
coordRange.set( Start-1, End+1 );
181
otherCoordRange.set( coordRange );
182
coordRange.adaptToChange( Start, Width, Width );
183
QCOMPARE( coordRange, otherCoordRange );
185
// adaptToChange, same length, 0,-1
186
coordRange.set( Start, End-1 );
187
otherCoordRange.set( coordRange );
188
coordRange.adaptToChange( Start, Width, Width );
189
QVERIFY( !coordRange.isValid() );
191
// adaptToChange, same length, 0,0
192
coordRange.set( Start, End );
193
otherCoordRange.set( coordRange );
194
coordRange.adaptToChange( Start, Width, Width );
195
QCOMPARE( coordRange, otherCoordRange );
197
// adaptToChange, same length, 0,+1
198
coordRange.set( Start, End+1 );
199
otherCoordRange.set( coordRange );
200
coordRange.adaptToChange( Start, Width, Width );
201
QCOMPARE( coordRange, otherCoordRange );
203
// adaptToChange, same length, +1,-1
204
coordRange.set( Start+1, End-1 );
205
coordRange.adaptToChange( Start, Width, Width );
206
QVERIFY( !coordRange.isValid() );
208
// adaptToChange, same length, +1,0
209
coordRange.set( Start+1, End );
210
coordRange.adaptToChange( Start, Width, Width );
211
QVERIFY( !coordRange.isValid() );
213
// adaptToChange, same length, +1,+1
214
coordRange.set( Start+1, End+1 );
215
otherCoordRange.set( End+1, End+1 );
216
coordRange.adaptToChange( Start, Width, Width );
217
QCOMPARE( coordRange, otherCoordRange );
220
// adaptToChange, less length, behind
221
coordRange.set( Start, End );
222
otherCoordRange.set( coordRange );
223
coordRange.adaptToChange( End+1, Width, Width-2 );
224
QCOMPARE( coordRange, otherCoordRange );
226
// adaptToChange, less length, before
227
coordRange.set( Start, End );
228
otherCoordRange.set( Start-2, End-2 );
229
coordRange.adaptToChange( Start-Width, Width, Width-2 );
230
QCOMPARE( coordRange, otherCoordRange );
232
// adaptToChange, less length, -1,-1
233
coordRange.set( Start-1, End-1 );
234
otherCoordRange.set( Start-1, Start-1 );
235
coordRange.adaptToChange( Start, Width, Width-2 );
236
QCOMPARE( coordRange, otherCoordRange );
238
// adaptToChange, less length, -1,0
239
coordRange.set( Start-1, End );
240
otherCoordRange.set( Start-1, End-2 );
241
coordRange.adaptToChange( Start, Width, Width-2 );
242
QCOMPARE( coordRange, otherCoordRange );
244
// adaptToChange, less length, -1,+1
245
coordRange.set( Start-1, End+1 );
246
otherCoordRange.set( Start-1, End-1 );
247
coordRange.adaptToChange( Start, Width, Width-2 );
248
QCOMPARE( coordRange, otherCoordRange );
250
// adaptToChange, less length, 0,-1
251
coordRange.set( Start, End-1 );
252
coordRange.adaptToChange( Start, Width, Width-2 );
253
QVERIFY( !coordRange.isValid() );
255
// adaptToChange, less length, 0,0
256
coordRange.set( Start, End );
257
otherCoordRange.set( Start, End-2 );
258
coordRange.adaptToChange( Start, Width, Width-2 );
259
QCOMPARE( coordRange, otherCoordRange );
261
// adaptToChange, less length, 0,+1
262
coordRange.set( Start, End+1 );
263
otherCoordRange.set( Start, End-1 );
264
coordRange.adaptToChange( Start, Width, Width-2 );
265
QCOMPARE( coordRange, otherCoordRange );
267
// adaptToChange, less length, +1,-1
268
coordRange.set( Start+1, End-1 );
269
coordRange.adaptToChange( Start, Width, Width-2 );
270
QVERIFY( !coordRange.isValid() );
272
// adaptToChange, less length, +1,0
273
coordRange.set( Start+1, End );
274
coordRange.adaptToChange( Start, Width, Width-2 );
275
QVERIFY( !coordRange.isValid() );
277
// adaptToChange, less length, +1,+1
278
coordRange.set( Start+1, End+1 );
279
otherCoordRange.set( End-1, End-1 );
280
coordRange.adaptToChange( Start, Width, Width-2 );
281
QCOMPARE( coordRange, otherCoordRange );
284
// adaptToChange, greater length, behind
285
coordRange.set( Start, End );
286
otherCoordRange.set( coordRange );
287
coordRange.adaptToChange( End+1, Width, Width+2 );
288
QCOMPARE( coordRange, otherCoordRange );
290
// adaptToChange, greater length, before
291
coordRange.set( Start, End );
292
otherCoordRange.set( Start+2, End+2 );
293
coordRange.adaptToChange( Start-Width, Width, Width+2 );
294
QCOMPARE( coordRange, otherCoordRange );
296
// adaptToChange, greater length, -1,-1
297
coordRange.set( Start-1, End-1 );
298
otherCoordRange.set( Start-1, Start-1 );
299
coordRange.adaptToChange( Start, Width, Width+2 );
300
QCOMPARE( coordRange, otherCoordRange );
302
// adaptToChange, greater length, -1,0
303
coordRange.set( Start-1, End );
304
otherCoordRange.set( Start-1, End+2 );
305
coordRange.adaptToChange( Start, Width, Width+2 );
306
QCOMPARE( coordRange, otherCoordRange );
308
// adaptToChange, greater length, -1,+1
309
coordRange.set( Start-1, End+1 );
310
otherCoordRange.set( Start-1, End+3 );
311
coordRange.adaptToChange( Start, Width, Width+2 );
312
QCOMPARE( coordRange, otherCoordRange );
314
// adaptToChange, greater length, 0,-1
315
coordRange.set( Start, End-1 );
316
coordRange.adaptToChange( Start, Width, Width+2 );
317
QVERIFY( !coordRange.isValid() );
319
// adaptToChange, greater length, 0,0
320
coordRange.set( Start, End );
321
otherCoordRange.set( Start, End+2 );
322
coordRange.adaptToChange( Start, Width, Width+2 );
323
QCOMPARE( coordRange, otherCoordRange );
325
// adaptToChange, greater length, 0,+1
326
coordRange.set( Start, End+1 );
327
otherCoordRange.set( Start, End+3 );
328
coordRange.adaptToChange( Start, Width, Width+2 );
329
QCOMPARE( coordRange, otherCoordRange );
331
// adaptToChange, greater length, +1,-1
332
coordRange.set( Start+1, End-1 );
333
coordRange.adaptToChange( Start, Width, Width+2 );
334
QVERIFY( !coordRange.isValid() );
336
// adaptToChange, greater length, +1,0
337
coordRange.set( Start+1, End );
338
coordRange.adaptToChange( Start, Width, Width+2 );
339
QVERIFY( !coordRange.isValid() );
341
// adaptToChange, greater length, +1,+1
342
coordRange.set( Start+1, End+1 );
343
otherCoordRange.set( End+3, End+3 );
344
coordRange.adaptToChange( Start, Width, Width+2 );
345
QCOMPARE( coordRange, otherCoordRange );
348
// adaptToChange, insert, behind
349
coordRange.set( Start-1, Start-1 );
350
otherCoordRange.set( Start-1, End );
351
coordRange.adaptToChange( Start, 0, Width );
352
QCOMPARE( coordRange, otherCoordRange );
354
// adaptToChange, insert, middle
355
coordRange.set( Start-1, Start );
356
otherCoordRange.set( Start-1, End+1 );
357
coordRange.adaptToChange( Start, 0, Width );
358
QCOMPARE( coordRange, otherCoordRange );
360
// adaptToChange, insert, before
361
coordRange.set( Start, Start );
362
otherCoordRange.set( End+1, End+1 );
363
coordRange.adaptToChange( Start, 0, Width );
364
QCOMPARE( coordRange, otherCoordRange );
367
void CoordRangeTest::testStartForInclude()
369
// testStartForInclude, same length, start at start
370
const CoordRange coordRange( Start, End );
371
CoordRange otherCoordRange( coordRange );
372
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start() );
374
// testStartForInclude, same length, start before start
375
otherCoordRange.moveBy( -1 );
376
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start() );
378
// testStartForInclude, same length, end behind end
379
otherCoordRange.moveBy( 2 );
380
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start() );
382
// testStartForInclude, smaller length, start at start
383
otherCoordRange.set( Start, End-1 );
384
int Diff = coordRange.width() - otherCoordRange.width();
385
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start() );
387
// testStartForInclude, smaller length, start before start
388
otherCoordRange.moveBy( -1 );
389
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start() );
391
// testStartForInclude, smaller length, end behind end
392
otherCoordRange.moveBy( 2 );
393
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start()-Diff );
395
* // undefined in startForInclude
396
// testStartForInclude, greater length, start at start
397
otherCoordRange.set( Start, End+1 );
398
Diff = coordRange.width() - otherCoordRange.width();
399
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start() );
401
// testStartForInclude, smaller length, start before start
402
otherCoordRange.moveBy( -1 );
403
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start() );
405
// testStartForInclude, smaller length, end behind end
406
otherCoordRange.moveBy( 2 );
407
QCOMPARE( coordRange.startForInclude(otherCoordRange), otherCoordRange.start()-Diff );
412
void CoordRangeTest::testIsJoinable()
414
// testStartForInclude, same length, start at start
415
const CoordRange coordRange( Start, End );
416
CoordRange otherCoordRange( coordRange );
417
QVERIFY( coordRange.isJoinable(otherCoordRange) );
419
// adaptToChange, insert, before
420
otherCoordRange.set( End+1, End+Width );
421
QVERIFY( coordRange.isJoinable(otherCoordRange) );
423
// Overlapping, right shifted
424
otherCoordRange.set( Start+1, End+1 );
425
QVERIFY( coordRange.isJoinable(otherCoordRange) );
427
// Overlapping, left shifted
428
otherCoordRange.set( Start-1, End-1 );
429
QVERIFY( coordRange.isJoinable(otherCoordRange) );
431
// Overlapping, 1 includes 2
432
otherCoordRange.set( Start+1, End-1 );
433
QVERIFY( coordRange.isJoinable(otherCoordRange) );
435
// Overlapping, 2 includes 1
436
otherCoordRange.set( Start-1, End+1 );
437
QVERIFY( coordRange.isJoinable(otherCoordRange) );
439
// Overlapping, identic
440
otherCoordRange.set( coordRange );
441
QVERIFY( coordRange.isJoinable(otherCoordRange) );
443
// Coupled, first, then second
444
otherCoordRange.set( End+1, End+Width );
445
QVERIFY( coordRange.isJoinable(otherCoordRange) );
447
// Coupled, second, then first
448
QVERIFY( otherCoordRange.isJoinable(coordRange) );
450
// NonOverlapping, first, then second
451
otherCoordRange.set( End+2, End+Width+1 );
452
QVERIFY( !coordRange.isJoinable(otherCoordRange) );
454
// NonOverlapping, second, then first
455
QVERIFY( !otherCoordRange.isJoinable(coordRange) );
461
QTEST_MAIN( Okteta::CoordRangeTest )