File: cu_iterator.c

package info (click to toggle)
postgis 2.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,660 kB
  • ctags: 10,181
  • sloc: ansic: 132,858; sql: 131,148; xml: 46,460; sh: 4,832; perl: 4,476; makefile: 2,749; python: 1,198; yacc: 442; lex: 131
file content (265 lines) | stat: -rw-r--r-- 7,426 bytes parent folder | download | duplicates (8)
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
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 * Copyright 2015 Daniel Baston
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public Licence. See the COPYING file.
 *
 **********************************************************************/

#include "CUnit/Basic.h"
#include "cu_tester.h"

#include "../liblwgeom_internal.h"

char* inputs[] =
{
	"POINT (17 253)",
	"POINT Z (17 253 018)",
	"TRIANGLE ((0 0, 10 0, 10 10, 0 0))",
	"LINESTRING (17 253, -44 28, 33 11, 26 44)",
	"LINESTRING M (17 253 0, -44 28 1, 33 11 2, 26 44 3)",
	"POLYGON((26426 65078,26531 65242,26075 65136,26096 65427,26426 65078))",
	"MULTIPOINT ((1 1), (1 1))",
	"MULTILINESTRING Z ((1 1 0, 2 2 0), (3 3 1, 4 4 1))",
	"MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1)), ((20 20, 20 30, 30 30, 20 20)))",
	"POINT EMPTY",
	"LINESTRING M EMPTY",
	"POLYGON Z EMPTY",
	"GEOMETRYCOLLECTION EMPTY",
	"GEOMETRYCOLLECTION (MULTIPOINT ((14 80), (22 12)))",
	"GEOMETRYCOLLECTION (POINT (3 7), LINESTRING (0 0, 14 3), GEOMETRYCOLLECTION(POINT (2 8)))",
	"GEOMETRYCOLLECTION (POINT (3 7), GEOMETRYCOLLECTION(MULTIPOINT ((2 8))))",
	"GEOMETRYCOLLECTION (POINT (3 7), GEOMETRYCOLLECTION(LINESTRING (2 8, 4 3), POLYGON EMPTY, MULTIPOINT ((2 8), (17 3), EMPTY)))",
	"CIRCULARSTRING(0 0,2 0, 2 1, 2 3, 4 3)",
	"COMPOUNDCURVE(CIRCULARSTRING(0 0,2 0, 2 1, 2 3, 4 3),(4 3, 4 5, 1 4, 0 0))",
	"MULTICURVE((0 0, 5 5),CIRCULARSTRING(4 0, 4 4, 8 4))",
	"CURVEPOLYGON(COMPOUNDCURVE(CIRCULARSTRING(0 0,2 0, 2 1, 2 3, 4 3),(4 3, 4 5, 1 4, 0 0)), LINESTRING (0.1 0.1, 0.3 0.1, 0.3 0.3, 0.1 0.1) )",
	"MULTISURFACE(CURVEPOLYGON(CIRCULARSTRING(0 0, 4 0, 4 4, 0 4, 0 0),(1 1, 3 3, 3 1, 1 1)),((10 10, 14 12, 11 10, 10 10),(11 11, 11.5 11, 11 11.5, 11 11)))",
	"POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)), ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)), ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)), ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )",
	"TIN(((80 130,50 160,80 70,80 130)),((50 160,10 190,10 70,50 160)), ((80 70,50 160,10 70,80 70)),((120 160,120 190,50 160,120 160)), ((120 190,10 190,50 160,120 190)))"
};

static uint32_t
count_points_using_iterator(LWGEOM* g)
{
	POINT4D p;
	uint32_t count = 0;
	LWPOINTITERATOR* it = lwpointiterator_create(g);

	while (lwpointiterator_has_next(it))
	{
		CU_ASSERT_TRUE(lwpointiterator_next(it, &p));
		count++;
	}

	lwpointiterator_destroy(it);

	return count;
}

static void
test_point_count(void)
{
	char* types_visited = lwalloc(NUMTYPES * sizeof(char));
	memset(types_visited, LW_FALSE, NUMTYPES * sizeof(char));

	uint32_t i;
	for (i = 0; i < sizeof(inputs)/sizeof(char*); i++)
	{
		LWGEOM* input = lwgeom_from_wkt(inputs[i], LW_PARSER_CHECK_NONE);
		types_visited[lwgeom_get_type(input)] = LW_TRUE;

		uint32_t itercount = count_points_using_iterator(input);

		CU_ASSERT_EQUAL(lwgeom_count_vertices(input), itercount);

		lwgeom_free(input);
	}

	/* Assert that every valid LWGEOM type has been tested */
	for (i = 1; i < NUMTYPES; i++)
	{
		CU_ASSERT_TRUE(types_visited[i]);
	}

	lwfree(types_visited);
}

static void
test_cannot_modify_read_only(void)
{
	LWGEOM* input = lwgeom_from_wkt(inputs[0], LW_PARSER_CHECK_NONE);
	LWPOINTITERATOR* it = lwpointiterator_create(input);

	POINT4D p;
	p.x = 3.2;
	p.y = 4.8;

	CU_ASSERT_EQUAL(LW_FAILURE, lwpointiterator_modify_next(it, &p));

	lwgeom_free(input);
	lwpointiterator_destroy(it);
}

static void
test_modification(void)
{
	uint32_t i;
	uint32_t j = 0;

	for (i = 0; i < sizeof(inputs)/sizeof(char*); i++)
	{
		LWGEOM* input = lwgeom_from_wkt(inputs[i], LW_PARSER_CHECK_NONE);
		LWPOINTITERATOR* it1 = lwpointiterator_create_rw(input);
		LWPOINTITERATOR* it2 = lwpointiterator_create(input);;

		while (lwpointiterator_has_next(it1))
		{
			/* Make up a coordinate, assign it to the next spot in it1,
			 * read it from it2 to verify that it was assigned correctly. */
			POINT4D p1, p2;
			p1.x = sqrt(j++);
			p1.y = sqrt(j++);
			p1.z = sqrt(j++);
			p1.m = sqrt(j++);

			CU_ASSERT_TRUE(lwpointiterator_modify_next(it1, &p1));
			CU_ASSERT_TRUE(lwpointiterator_next(it2, &p2));

			CU_ASSERT_EQUAL(p1.x, p2.x);
			CU_ASSERT_EQUAL(p1.y, p2.y);

			if (lwgeom_has_z(input))
				CU_ASSERT_EQUAL(p1.z, p2.z);

			if (lwgeom_has_m(input))
				CU_ASSERT_EQUAL(p1.m, p2.m);
		}

		lwgeom_free(input);

		lwpointiterator_destroy(it1);
		lwpointiterator_destroy(it2);
	}
}

static void
test_no_memory_leaked_when_iterator_is_partially_used(void)
{
	LWGEOM* g = lwgeom_from_wkt("GEOMETRYCOLLECTION (POINT (3 7), GEOMETRYCOLLECTION(LINESTRING (2 8, 4 3), POLYGON EMPTY, MULTIPOINT ((2 8), (17 3), EMPTY)))", LW_PARSER_CHECK_NONE);

	LWPOINTITERATOR* it = lwpointiterator_create(g);
	lwpointiterator_next(it, NULL);
	lwpointiterator_next(it, NULL);

	lwpointiterator_destroy(it);
	lwgeom_free(g);
}

static void
test_mixed_rw_access(void)
{
	uint32_t i = 0;
	LWGEOM* g = lwgeom_from_wkt("GEOMETRYCOLLECTION (POINT (3 7), GEOMETRYCOLLECTION(LINESTRING (2 8, 4 3), POLYGON EMPTY, MULTIPOINT ((2 8), (17 3), EMPTY)))", LW_PARSER_CHECK_NONE);
	LWPOINTITERATOR* it1 = lwpointiterator_create_rw(g);
	LWPOINTITERATOR* it2 = lwpointiterator_create(g);

	/* Flip the coordinates of the 3rd point */
	while(lwpointiterator_has_next(it1))
	{
		if (i == 2)
		{
			POINT4D p;
			double tmp;

			lwpointiterator_peek(it1, &p);
			tmp = p.x;
			p.x = p.y;
			p.y = tmp;

			lwpointiterator_modify_next(it1, &p);
		}
		else
		{
			lwpointiterator_next(it1, NULL);
		}
		i++;
	}
	CU_ASSERT_EQUAL(5, i); /* Every point was visited */
	lwpointiterator_destroy(it1);

	/* Verify that the points are as expected */
	POINT2D points[] =
	{
		{ .x = 3, .y = 7 },
		{ .x = 2, .y = 8 },
		{ .x = 3, .y = 4 },
		{ .x = 2, .y = 8 },
		{ .x = 17, .y = 3}
	};

	for (i = 0; lwpointiterator_has_next(it2); i++)
	{
		POINT4D p;

		lwpointiterator_next(it2, &p);

		CU_ASSERT_EQUAL(p.x, points[i].x);
		CU_ASSERT_EQUAL(p.y, points[i].y);
	}

	lwpointiterator_destroy(it2);
	lwgeom_free(g);
}

static void
test_ordering(void)
{
	uint32_t i = 0;
	LWGEOM* g = lwgeom_from_wkt("GEOMETRYCOLLECTION (POLYGON ((0 0, 0 10, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1)), MULTIPOINT((4 4), (3 3)))", LW_PARSER_CHECK_NONE);

	POINT2D points[] = { {.x = 0,  .y = 0},
		{.x = 0,  .y = 10},
		{.x = 10, .y = 10},
		{.x = 0,  .y = 10},
		{.x = 0,  .y = 0},
		{.x = 1,  .y = 1},
		{.x = 1,  .y = 2},
		{.x = 2,  .y = 2},
		{.x = 2,  .y = 1},
		{.x = 1,  .y = 1},
		{.x = 4,  .y = 4},
		{.x = 3,  .y = 3}
	};

	LWPOINTITERATOR* it = lwpointiterator_create(g);
	POINT4D p;

	for (i = 0; lwpointiterator_has_next(it); i++)
	{
		CU_ASSERT_EQUAL(LW_SUCCESS, lwpointiterator_next(it, &p));
		CU_ASSERT_EQUAL(p.x, points[i].x);
		CU_ASSERT_EQUAL(p.y, points[i].y);
	}

	lwpointiterator_destroy(it);
	lwgeom_free(g);
}

/*
** Used by test harness to register the tests in this file.
*/
void iterator_suite_setup(void);
void iterator_suite_setup(void)
{
	CU_pSuite suite = CU_add_suite("iterator", NULL, NULL);
	PG_ADD_TEST(suite, test_point_count);
	PG_ADD_TEST(suite, test_ordering);
	PG_ADD_TEST(suite, test_modification);
	PG_ADD_TEST(suite, test_mixed_rw_access);
	PG_ADD_TEST(suite, test_cannot_modify_read_only);
	PG_ADD_TEST(suite, test_no_memory_leaked_when_iterator_is_partially_used);
}