PostGIS  2.5.1dev-r@@SVN_REVISION@@
cu_in_twkb.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2010 Paul Ramsey <pramsey@cleverelephant.ca>
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "CUnit/Basic.h"
16 
17 #include "liblwgeom_internal.h"
18 #include "cu_tester.h"
19 
20 /*
21 ** Global variable to hold TWKB strings
22 */
23 char *hex_a;
24 char *hex_b;
27 
28 /*
29 ** The suite initialization function.
30 ** Create any re-used objects.
31 */
32 static int init_twkb_in_suite(void)
33 {
34  hex_a = NULL;
35  hex_b = NULL;
36  return 0;
37 }
38 
39 /*
40 ** The suite cleanup function.
41 ** Frees any global objects.
42 */
43 static int clean_twkb_in_suite(void)
44 {
45  if (hex_a) free(hex_a);
46  if (hex_b) free(hex_b);
47  hex_a = NULL;
48  hex_b = NULL;
49  return 0;
50 }
51 
52 
53 static void cu_twkb_in(char *wkt)
54 {
56  LWGEOM *g_a, *g_b;
57  uint8_t *twkb_a, *twkb_b;
58  size_t twkb_size_a, twkb_size_b;
59  /* int i; char *hex; */
60 
61  /* Turn WKT into geom */
63  if ( pr.errcode )
64  {
65  printf("ERROR: %s\n", pr.message);
66  printf("POSITION: %d\n", pr.errlocation);
67  exit(0);
68  }
69 
70  /* Get the geom */
71  g_a = pr.geom;
72 
73  /* Turn geom into TWKB */
74  twkb_a = lwgeom_to_twkb(g_a, variant, precision, precision, precision, &twkb_size_a);
75 
76  // printf("\n Size: %ld\n", twkb_size_a);
77 
78  /* Turn TWKB back into geom */
79  g_b = lwgeom_from_twkb(twkb_a, twkb_size_a, LW_PARSER_CHECK_NONE);
80 
81  // printf("\n Org: %s\n 1st: %s\n 2nd: %s\n", wkt, lwgeom_to_ewkt(g_a), lwgeom_to_ewkt(g_b));
82 
83  /* Turn geom to TWKB again */
84  twkb_b = lwgeom_to_twkb(g_b, variant, precision, precision, precision, &twkb_size_b);
85 
86  /* Turn TWKB into hex for comparisons */
87  if ( hex_a ) lwfree(hex_a);
88  if ( hex_b ) lwfree(hex_b);
89  hex_a = hexbytes_from_bytes(twkb_a, twkb_size_a);
90  hex_b = hexbytes_from_bytes(twkb_b, twkb_size_b);
91 
92  /* Clean up */
93  lwfree(twkb_a);
94  lwfree(twkb_b);
96  lwgeom_free(g_b);
97 }
98 
99 static void test_twkb_in_point(void)
100 {
101  cu_twkb_in("POINT(0 0 0 0)");
102 // printf("old: %s\nnew: %s\n",hex_a, hex_b);
103  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
104 
105  cu_twkb_in("POINT(1 1)");
106  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
107 
108  cu_twkb_in("POINT EMPTY");
109  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
110 }
111 
112 static void test_twkb_in_linestring(void)
113 {
114  cu_twkb_in("LINESTRING(0 0,1 1)");
115  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
116 
117  cu_twkb_in("LINESTRING(0 0 1,1 1 2,2 2 3)");
118  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
119 
120  cu_twkb_in("LINESTRING EMPTY");
121  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
122 }
123 
124 static void test_twkb_in_polygon(void)
125 {
126  cu_twkb_in("POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0))");
127  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
128 
129  cu_twkb_in("POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
130  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
131 
132  cu_twkb_in("POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
133  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
134 
135  cu_twkb_in("POLYGON EMPTY");
136  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
137 }
138 
139 static void test_twkb_in_multipoint(void)
140 {
141  cu_twkb_in("MULTIPOINT Z EMPTY");
142  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
143 
144  cu_twkb_in("MULTIPOINT(1 2, EMPTY, 5 6)");
145  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
146  // printf(" 1st: %s\n 2nd: %s\n", hex_a, hex_b);
147 
148  cu_twkb_in("MULTIPOINT(0 0 0,0 1 0,1 1 0,1 0 0,0 0 1)");
149  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
150 
151  cu_twkb_in("MULTIPOINT(1 2 3, 1 2 3, 4 5 6, -3 -4 -5, -10 -5 -1)");
152  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
153 }
154 
156 {
157  cu_twkb_in("MULTILINESTRING((0 0,0 1),(1 1, 10 10))");
158  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
159 
160  cu_twkb_in("MULTILINESTRING((0 0,0 1),EMPTY,(1 1, 10 10))");
161  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
162 
163  cu_twkb_in("MULTILINESTRING((0 0 200000,0 1 10),(1 100000000 23, 10 10 45))");
164  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
165  // printf(" 1st: %s\n 2nd: %s\n", hex_a, hex_b);
166 
167  cu_twkb_in("MULTILINESTRING EMPTY");
168  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
169 }
170 
171 static void test_twkb_in_multipolygon(void)
172 {
173  cu_twkb_in("MULTIPOLYGON(((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),((-1 -1 0,-1 2 0,2 2 0,2 -1 0,-1 -1 0),(0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)))");
174  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
175  //printf("old: %s\nnew: %s\n",hex_a, hex_b);
176 
177  cu_twkb_in("MULTIPOLYGON EMPTY");
178  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
179  //printf("old: %s\nnew: %s\n",hex_a, hex_b);
180 }
181 
182 static void test_twkb_in_collection(void)
183 {
184  cu_twkb_in("GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0))),POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1),LINESTRING(0 0 0, 1 1 1))");
185  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
186 
187  cu_twkb_in("GEOMETRYCOLLECTION(POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1))");
188  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
189 
190  cu_twkb_in("GEOMETRYCOLLECTION EMPTY");
191  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
192 
193  cu_twkb_in("GEOMETRYCOLLECTION(POINT(1 2 3), LINESTRING EMPTY, POINT(4 5 6))");
194  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
195 
196  cu_twkb_in("GEOMETRYCOLLECTION(POINT(1 2 3), POINT EMPTY, POINT(4 5 6))");
197  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
198 }
199 
200 /*
201 ** PRECISION TESTS HERE: We ALTER THE 'precision' GLOBAL
202 */
203 
204 static void test_twkb_in_precision(void)
205 {
206  /* Try these cases at several different precisions */
207  for ( precision = 1; precision <= 6; precision++ )
208  {
209  cu_twkb_in("MULTILINESTRING((0 0,0 1),EMPTY,(1 1, 10 10))");
210  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
211 
212  cu_twkb_in("MULTIPOLYGON(((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),((-1 -1 0,-1 2 0,2 2 0,2 -1 0,-1 -1 0),(0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)))");
213  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
214 
215  cu_twkb_in("GEOMETRYCOLLECTION(POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1))");
216  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
217 
218  cu_twkb_in("MULTILINESTRING((0 0 200000,0 1 10),(1 100000000 23, 10 10 45))");
219  // printf("old: %s\nnew: %s\n",hex_a, hex_b);
220  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
221  }
222 
223  /* Go back to default precision */
224  precision = 0;
225 }
226 
227 
228 
229 /*
230 ** Used by test harness to register the tests in this file.
231 */
232 void twkb_in_suite_setup(void);
234 {
235  CU_pSuite suite = CU_add_suite("twkb_input", init_twkb_in_suite, clean_twkb_in_suite);
244 }
uint8_t variant
Definition: cu_in_twkb.c:26
static void test_twkb_in_point(void)
Definition: cu_in_twkb.c:99
void lwfree(void *mem)
Definition: lwutil.c:244
static void test_twkb_in_multipolygon(void)
Definition: cu_in_twkb.c:171
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:885
static void test_twkb_in_precision(void)
Definition: cu_in_twkb.c:204
LWGEOM * lwgeom_from_twkb(const uint8_t *twkb, size_t twkb_size, char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
Definition: lwin_twkb.c:656
static int clean_twkb_in_suite(void)
Definition: cu_in_twkb.c:43
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2004
static void test_twkb_in_multilinestring(void)
Definition: cu_in_twkb.c:155
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM...
Definition: liblwgeom.h:2011
#define PG_ADD_TEST(suite, testfunc)
uint8_t precision
Definition: cu_in_twkb.c:25
static void test_twkb_in_multipoint(void)
Definition: cu_in_twkb.c:139
static int init_twkb_in_suite(void)
Definition: cu_in_twkb.c:32
char * hex_b
Definition: cu_in_twkb.c:24
char * hexbytes_from_bytes(const uint8_t *bytes, size_t size)
Definition: lwout_wkb.c:39
static void cu_twkb_in(char *wkt)
Definition: cu_in_twkb.c:53
void free(void *)
static void test_twkb_in_linestring(void)
Definition: cu_in_twkb.c:112
static void test_twkb_in_collection(void)
Definition: cu_in_twkb.c:182
char * hex_a
Definition: cu_in_twkb.c:23
uint8_t * lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
Definition: lwout_twkb.c:614
unsigned char uint8_t
Definition: uthash.h:79
void twkb_in_suite_setup(void)
Definition: cu_in_twkb.c:233
static void test_twkb_in_polygon(void)
Definition: cu_in_twkb.c:124