File: CoordinateArraySequenceTest.cpp

package info (click to toggle)
geos 3.4.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,572 kB
  • ctags: 13,989
  • sloc: cpp: 97,030; xml: 32,337; sh: 10,379; ansic: 3,726; php: 1,831; makefile: 1,619; ruby: 1,295; python: 928
file content (637 lines) | stat: -rw-r--r-- 17,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// 
// Test Suite for geos::geom::CoordinateArraySequence class.

#include <tut.hpp>
// geos
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateFilter.h>
#include <geos/geom/CoordinateArraySequence.h>
#include <geos/geom/CoordinateArraySequenceFactory.h>
// std
#include <string>
#include <vector>
#include <iostream>
#include <cmath>

namespace tut
{
    //
    // Test Group
    //

    // Common data used by tests
    struct test_coordinatearraysequence_data
    {
        test_coordinatearraysequence_data() {}

        struct Filter : public geos::geom::CoordinateFilter
        {
          bool is3d;
          Filter() : is3d(false) {}
          void filter_rw(geos::geom::Coordinate* c) const {
            if ( is3d ) {
              if ( ISNAN(c->z) ) c->z = 0.0;
            }
            else c->z = DoubleNotANumber;
          }
        };
    };

    typedef test_group<test_coordinatearraysequence_data> group;
    typedef group::object object;

    group test_coordinatearraysequence_group("geos::geom::CoordinateArraySequence");

    //
    // Test Cases
    //

    // Test of default constructor
    template<>
    template<>
    void object::test<1>()
    {
		const size_t size = 0;
        geos::geom::CoordinateArraySequence sequence;
    
        ensure( sequence.isEmpty() );
        ensure_equals( sequence.getSize(), size );
		ensure_equals( sequence.size(), size );

		ensure_equals( sequence.toString(), std::string("()") );
		
		const size_t dim = 3;
		ensure_equals( sequence.getDimension() , dim);
    }

    // Test of overriden constructor
    template<>
    template<>
    void object::test<2>()
    {
		const size_t size = 3;
        geos::geom::CoordinateArraySequence sequence(size);
    
        ensure( !sequence.isEmpty() );
        ensure_equals( sequence.getSize(), size );
		ensure_equals( sequence.size(), size );

		ensure( sequence.toString() != std::string("()") );

		ensure("Every coodinate in the default sequence should be same.", sequence.hasRepeatedPoints() );

		const size_t dim = 2; // default/empty coordinates now 2D.
		ensure_equals( sequence.getDimension(), dim );
    }

    // Test of overriden constructor taking vector of coordiantes
    template<>
    template<>
    void object::test<3>()
    {
		using geos::geom::Coordinate;
		
		const size_t size = 3;
		const double a = 0;
		const double b = 5.0;
		const double c = 10.0;

		std::vector<Coordinate>* col = new std::vector<Coordinate>();
		col->push_back(Coordinate(a, a, a));
		col->push_back(Coordinate(b, b, b));
		col->push_back(Coordinate(c, c, c));

		// sequence takes ownership of the col
		geos::geom::CoordinateArraySequence sequence(col);

		ensure( !sequence.isEmpty() );
        ensure_equals( sequence.getSize(), size );
		ensure_equals( sequence.size(), size );

		ensure( sequence.toString() != std::string("()") );

		ensure("Coordinate sequence should contain unique coordinates.", !sequence.hasRepeatedPoints() );
	}

    // Test of copy constructor
    template<>
    template<>
    void object::test<4>()
    {
		using geos::geom::Coordinate;

		// Create empty sequence
		const size_t sizeEmpty = 0;
        geos::geom::CoordinateArraySequence empty_original;
      
		ensure( empty_original.isEmpty() );
		ensure_equals( empty_original.size(), sizeEmpty );
		ensure_equals( empty_original.toString(), std::string("()") );

		// Create copy of empty sequence
		geos::geom::CoordinateArraySequence empty_copy(empty_original);

		ensure( empty_copy.isEmpty() );
		ensure_equals( empty_copy.size(), sizeEmpty );
		ensure_equals( empty_copy.toString(), std::string("()") );

		// Create non-empty sequence
		const size_t sizeNonEmpty = 2;
		std::vector<Coordinate>* col = new std::vector<Coordinate>();
		col->push_back(Coordinate(1, 2, 3));
		col->push_back(Coordinate(5, 10, 15));
		geos::geom::CoordinateArraySequence non_empty_original(col);
		
		ensure( !non_empty_original.isEmpty() );
		ensure_equals( non_empty_original.size(), sizeNonEmpty );

		// Create copy of non-empty sequence
		geos::geom::CoordinateArraySequence non_empty_copy(non_empty_original);

		ensure( !non_empty_copy.isEmpty() );
		ensure_equals( non_empty_copy.size(), sizeNonEmpty );

		// Compare non-empty original and copy using equality operators
		ensure_equals( non_empty_original.getAt(0), non_empty_copy.getAt(0) );
		ensure_equals( non_empty_original.getAt(1), non_empty_copy.getAt(1) );
		ensure( non_empty_original.getAt(0) != non_empty_copy.getAt(1) );	
	}

    // Test of getX() and getY()
    template<>
    template<>
    void object::test<5>()
    {
		using geos::geom::Coordinate;

		// Create non-empty sequence
		std::vector<Coordinate>* col = new std::vector<Coordinate>();
		col->push_back(Coordinate(1, 2));
		col->push_back(Coordinate(5, 10));
		
		const size_t size = 2;
		geos::geom::CoordinateArraySequence sequence(col);
		
		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), size );

		ensure_equals( sequence.getX(0), 1 );
		ensure_equals( sequence.getY(0), 2 );
		ensure_equals( sequence.getX(1), 5 );
		ensure_equals( sequence.getY(1), 10 );
	}

    // Test of getAt()
    template<>
    template<>
    void object::test<6>()
    {
		using geos::geom::Coordinate;

		// Create non-empty sequence
		std::vector<Coordinate>* col = new std::vector<Coordinate>();
		col->push_back(Coordinate(1, 2, 3));
		col->push_back(Coordinate(5, 10, 15));
		
		const size_t size = 2;
		geos::geom::CoordinateArraySequence sequence(col);
		
		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), size );
		ensure( sequence.getAt(0) != sequence.getAt(1) );

		// First version of getAt()
		ensure_equals( sequence.getAt(0).x, 1 );
		ensure_equals( sequence.getAt(0).y, 2 );
		ensure_equals( sequence.getAt(0).z, 3 );
		ensure_equals( sequence.getAt(1).x, 5 );
		ensure_equals( sequence.getAt(1).y, 10 );
		ensure_equals( sequence.getAt(1).z, 15 );

		// Second version of getAt()
		Coordinate buf;

		sequence.getAt(0, buf);
		ensure_equals( buf.x, 1 );
		ensure_equals( buf.y, 2 );
		ensure_equals( buf.z, 3 );
		
		sequence.getAt(1, buf);
		ensure_equals( buf.x, 5 );
		ensure_equals( buf.y, 10 );
		ensure_equals( buf.z, 15 );
	}

    // Test of add()
    template<>
    template<>
    void object::test<7>()
    {
		using geos::geom::Coordinate;

		// Create empty sequence to fill with coordinates
		const size_t size = 0;
		geos::geom::CoordinateArraySequence sequence;
		
		ensure( sequence.isEmpty() );
		ensure_equals( sequence.size(), size );

		// Add coordinates
		Coordinate tmp(1, 2, 3);		
		sequence.add(tmp); // insert copy of tmp
		const size_t sizeOne = 1;

		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), sizeOne );

		tmp.x = 5;
		tmp.y = 10;
		tmp.z = 15;
		sequence.add(tmp); // insert copy of tmp
		const size_t sizeTwo = 2;
		
		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), sizeTwo );

		ensure( !sequence.hasRepeatedPoints() );

		// Check elements of sequence
		ensure( sequence.getAt(0) != sequence.getAt(1) );
		
		ensure_equals( sequence.getAt(0).x, 1 );
		ensure_equals( sequence.getAt(0).y, 2 );
		ensure_equals( sequence.getAt(0).z, 3 );
		ensure_equals( sequence.getAt(1).x, 5 );
		ensure_equals( sequence.getAt(1).y, 10 );
		ensure_equals( sequence.getAt(1).z, 15 );
	}

    // Test of setAt()
    template<>
    template<>
    void object::test<8>()
    {
		using geos::geom::Coordinate;

		// Create sequence with 2 default coordinates
		const size_t size = 2;
		geos::geom::CoordinateArraySequence sequence(size);
		
		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), size );
		ensure( sequence.hasRepeatedPoints() );

		// Set new values to first coordinate
		Coordinate first(1, 2, 3);
		sequence.setAt(first, 0);

		ensure_equals( sequence.size(), size );
		ensure_equals( sequence.getAt(0).x, 1 );
		ensure_equals( sequence.getAt(0).y, 2 );
		ensure_equals( sequence.getAt(0).z, 3 );


		// Set new values to second coordinate 
		Coordinate second(5, 10, 15);
		sequence.setAt(second, 1);

		ensure_equals( sequence.size(), size );

		ensure_equals( sequence.getAt(1).x, 5 );
		ensure_equals( sequence.getAt(1).y, 10 );
		ensure_equals( sequence.getAt(1).z, 15 );

		ensure( !sequence.hasRepeatedPoints() );
	}

    // Test of deleteAt()
    template<>
    template<>
    void object::test<9>()
    {
		using geos::geom::Coordinate;

		// Create sequence with only 1 default coordinate
		const size_t sizeOne = 1;
		geos::geom::CoordinateArraySequence sequence(1);
		
		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), sizeOne );

		// Delete the only coordinate in the sequence
		sequence.deleteAt(0);
		const size_t sizeZero = 0;
		
		ensure( sequence.isEmpty() );
		ensure_equals( sequence.size(), sizeZero );

		// Add new 3 coordinates
		Coordinate first(1, 2, 3);
		sequence.add(first);
		Coordinate second(5, 10, 15);
		sequence.add(second);
		Coordinate third(9, 18, 27);
		sequence.add(third);
		const size_t sizeThree = 3;
				
		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), sizeThree );

		// Delete coordinate in the middle of sequence - the second one.
		sequence.deleteAt(1); // (5, 10, 15)
		const size_t sizeTwo = 2;

		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), sizeTwo );
		
		ensure( sequence.getAt(0) != sequence.getAt(1) );
		ensure_equals( sequence.getAt(0), first );
		ensure( "deleteAt() did not remove coordinate.", sequence.getAt(1) != second );
		ensure_equals( sequence.getAt(1), third );
	}

	// Test of setPoints()
    template<>
    template<>
    void object::test<10>()
    {
		using geos::geom::Coordinate;

		// Create empty sequence
		const size_t size = 0;
		geos::geom::CoordinateArraySequence sequence;
		
		ensure( sequence.isEmpty() );
		ensure_equals( sequence.size(), size );

		// Create collection of points
		const std::vector<Coordinate>::size_type sizeCol = 3;
		std::vector<Coordinate> col;
		col.push_back(Coordinate(1, 2, 3));
		col.push_back(Coordinate(5, 10, 15));
		col.push_back(Coordinate(9, 18, 27));

		ensure( "std::vector bug assumed!", !col.empty() );
		ensure_equals( "std::vector bug assumed!", col.size(), sizeCol );

		// Use setPoints()
		sequence.setPoints(col);

		ensure( !sequence.isEmpty() );
		ensure_equals( sequence.size(), col.size() );
		ensure( !sequence.hasRepeatedPoints() );

		// Check inserted points
		ensure_equals( sequence.getAt(0).x, 1 );
		ensure_equals( sequence.getAt(0).y, 2 );
		ensure_equals( sequence.getAt(0).z, 3 );

		ensure_equals( sequence.getAt(1).x, 5 );
		ensure_equals( sequence.getAt(1).y, 10 );
		ensure_equals( sequence.getAt(1).z, 15 );

		ensure_equals( sequence.getAt(2).x, 9 );
		ensure_equals( sequence.getAt(2).y, 18 );
		ensure_equals( sequence.getAt(2).z, 27 );
	}

	// Test of removeRepeatedPoints
	template<>
	template<>
	void object::test<11>()
	{
		using geos::geom::Coordinate;

		geos::geom::CoordinateArraySequence sequence;

		// Add new 3 equal coordinates
		Coordinate c(1, 2, 3);
		sequence.add(c);
		sequence.add(c);
		sequence.add(c);

		Coordinate c2(5, 10, 15);
		sequence.add(c2);
		sequence.add(c2);

		sequence.add(c);

		ensure_equals( sequence.size(), 6u );
		sequence.removeRepeatedPoints();

		ensure_equals( sequence.size(), 3u );
	}

	// Test of equality and inequality operators
	template<>
	template<>
	void object::test<12>()
	{
		using geos::geom::Coordinate;
		using geos::geom::CoordinateArraySequence;
		using geos::geom::CoordinateSequence;

		Coordinate c1(1, 2, 3);
		Coordinate c2(5, 10, 15);
		Coordinate c3(6, 9, 10);

		CoordinateArraySequence sequence1;
		CoordinateArraySequence sequence2;

		sequence1.add(c1);
		sequence1.add(c2);
		sequence1.add(c3);

		sequence2.add(c1);
		sequence2.add(c2);
		sequence2.add(c3);

		ensure_equals( sequence1, sequence2 );
		ensure_equals( sequence2, sequence1 );

		// Add a new coordinate to sequence2
		sequence2.add(c3);

		ensure( sequence1 != sequence2 );
		ensure( sequence2 != sequence1 );

		// Add a new coordinate to sequence1
		sequence1.add(c3);

		ensure_equals( sequence1, sequence2 );
		ensure_equals( sequence2, sequence1 );

		// Add a new coordinate to sequence1
		sequence1.add(c3);
		sequence2.add(c2);

		ensure( sequence1 != sequence2 );
		ensure( sequence2 != sequence1 );

	}

	// Test setOrdinate
	template<>
	template<>
	void object::test<13>()
	{
		using geos::geom::Coordinate;
		using geos::geom::CoordinateArraySequence;
		using geos::geom::CoordinateSequence;

		Coordinate c1(1, 2, 3);

		CoordinateArraySequence sequence1;

		sequence1.add(c1);

		ensure_equals( sequence1[0], c1 );

		sequence1.setOrdinate(0, CoordinateSequence::X, 4);
		ensure_equals( sequence1[0].x, 4 );

		sequence1.setOrdinate(0, CoordinateSequence::Y, 5);
		ensure_equals( sequence1[0].y, 5 );

		sequence1.setOrdinate(0, CoordinateSequence::Z, 6);
		ensure_equals( sequence1[0].z, 6 );

	}

	// Test setOrdinate setting ordinates in arbitrary order
	template<>
	template<>
	void object::test<14>()
	{
		using geos::geom::Coordinate;
		using geos::geom::CoordinateArraySequence;
		using geos::geom::CoordinateSequence;

		Coordinate c1(1, 2, 3);

		CoordinateArraySequence sequence1;

		sequence1.add(c1);

		ensure_equals( sequence1[0], c1 );

		// Order: Y, X, Z

		sequence1.setOrdinate(0, CoordinateSequence::Y, 5);
		ensure_equals( sequence1[0].y, 5 );

		sequence1.setOrdinate(0, CoordinateSequence::X, 4);
		ensure_equals( sequence1[0].x, 4 );

		sequence1.setOrdinate(0, CoordinateSequence::Z, 6);
		ensure_equals( sequence1[0].z, 6 );

	}

	// Test setOrdinate setting ordinates in arbitrary order
	// against a non-initialized CoordinateSequence
	template<>
	template<>
	void object::test<15>()
	{
		using geos::geom::Coordinate;
		using geos::geom::CoordinateArraySequence;
		using geos::geom::CoordinateSequence;

		typedef geos::geom::CoordinateSequenceFactory const* CoordinateFactoryCPtr;
		CoordinateFactoryCPtr factory = geos::geom::CoordinateArraySequenceFactory::instance();

		std::auto_ptr<CoordinateSequence> sequence1ptr(factory->create(4, 2));
		CoordinateSequence& seq = *sequence1ptr;

		// Index: 0 - Order: Y, X, Z

		seq.setOrdinate(0, CoordinateSequence::Y,  5); ensure_equals( seq[0].y, 5 );
		seq.setOrdinate(0, CoordinateSequence::Z,  6); ensure_equals( seq[0].z, 6 );
		seq.setOrdinate(0, CoordinateSequence::X,  4); ensure_equals( seq[0].x, 4 );

		// Index: 1 - Order: Z, X, Y

		seq.setOrdinate(1, CoordinateSequence::Z,  9); ensure_equals( seq[1].z, 9 );
		seq.setOrdinate(1, CoordinateSequence::X,  8); ensure_equals( seq[1].x, 8 );
		seq.setOrdinate(1, CoordinateSequence::Y,  7); ensure_equals( seq[1].y, 7 );

		// Index: 2 - Order: X, Y, Z

		seq.setOrdinate(2, CoordinateSequence::X,  34); ensure_equals( seq[2].x, 34 );
		seq.setOrdinate(2, CoordinateSequence::Y,  -45); ensure_equals( seq[2].y, -45 );
		seq.setOrdinate(2, CoordinateSequence::Z,  152); ensure_equals( seq[2].z, 152 );

		// Index: 3 - Order: Y, Z, X

		seq.setOrdinate(3, CoordinateSequence::Y,  63); ensure_equals( seq[3].y, 63 );
		seq.setOrdinate(3, CoordinateSequence::Z,  13); ensure_equals( seq[3].z, 13 );
		seq.setOrdinate(3, CoordinateSequence::X,  -65); ensure_equals( seq[3].x, -65 );

	}

    // Test of add() in the middle
    template<>
    template<>
    void object::test<16>()
    {
		using geos::geom::Coordinate;
		using geos::geom::CoordinateArraySequence;

		// Create empty sequence to fill with coordinates
		CoordinateArraySequence sequence;
		
		sequence.add(Coordinate(0,0)); 
		sequence.add(Coordinate(1,1)); 
		sequence.add(Coordinate(2,2)); 
		
		ensure_equals( sequence.size(), std::size_t(3));

		sequence.add(0, Coordinate(4,4), false); // don't alow repeated
		ensure_equals( sequence.size(), std::size_t(4) );
		ensure_equals( sequence.getAt(0).x, std::size_t(4) );

		// do not allow repeated
		sequence.add(0, Coordinate(4,4), false); 
		ensure_equals( sequence.size(), std::size_t(4) );

		// allow repeated
		sequence.add(0, Coordinate(4,4), true); 
		ensure_equals( sequence.size(), std::size_t(5) );

		// Now looks like this: 4,4,0,1,2
		// we'll add at position 4 a 2 (equals to the one after)
		sequence.add(4, Coordinate(2,2), false); 
		ensure_equals( sequence.size(), std::size_t(5) );

		// we'll add at position 4 a 1 (equals to the one before)
		sequence.add(4, Coordinate(1,1), false); 
		ensure_equals( sequence.size(), std::size_t(5) );

		// we'll add at position 4 a 1 (equals to the one before)
		// but allowing duplicates
		sequence.add(4, Coordinate(1,1), true); 
		ensure_equals( sequence.size(), std::size_t(6) );
		ensure_equals( sequence.getAt(3).x, 1 );
		ensure_equals( sequence.getAt(4).x, 1 );
		ensure_equals( sequence.getAt(5).x, 2 );
	}

	// Test getDimension and filtering (http://trac.osgeo.org/geos/ticket/435)
	template<>
	template<>
	void object::test<17>()
	{
    geos::geom::CoordinateArraySequence seq(1);
    ensure_equals(seq.getDimension(), 2u);

    Filter f;

    f.is3d = true; seq.apply_rw(&f);
    ensure_equals(seq.getDimension(), 3u);

    f.is3d = false; seq.apply_rw(&f);
    ensure_equals(seq.getDimension(), 2u);
	}

} // namespace tut