1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2008 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 <string.h>
14 #include "CUnit/Basic.h"
15 #include "liblwgeom_internal.h"
16 #include "cu_tester.h"
17 #include "../postgis_config.h"
18 
19 char cu_error_msg[MAX_CUNIT_ERROR_LENGTH + 1] = {0};
20 
21 /* Internal funcs */
22 static void
23 cu_errorreporter(const char *fmt, va_list ap);
24 
25 static void
26 cu_noticereporter(const char *fmt, va_list ap);
27 
28 static void
29 cu_debuglogger(int level, const char *fmt, va_list ap);
30 
31 
32 /* ADD YOUR SUITE SETUP FUNCTION HERE (1 of 2) */
33 extern void print_suite_setup();
34 extern void algorithms_suite_setup();
35 extern void buildarea_suite_setup();
36 extern void clean_suite_setup();
37 extern void clip_by_rect_suite_setup();
38 extern void force_sfs_suite_setup(void);
39 extern void geodetic_suite_setup(void);
40 extern void geos_suite_setup(void);
41 extern void geos_cluster_suite_setup(void);
42 extern void unionfind_suite_setup(void);
43 extern void homogenize_suite_setup(void);
44 extern void in_encoded_polyline_suite_setup(void);
45 extern void in_geojson_suite_setup(void);
46 extern void iterator_suite_setup(void);
47 extern void twkb_in_suite_setup(void);
48 extern void gserialized1_suite_setup(void);
49 extern void gserialized2_suite_setup(void);
50 extern void lwstroke_suite_setup(void);
51 extern void measures_suite_setup(void);
52 extern void effectivearea_suite_setup(void);
53 extern void chaikin_suite_setup(void);
54 extern void filterm_suite_setup(void);
55 extern void minimum_bounding_circle_suite_setup(void);
56 extern void misc_suite_setup(void);
57 extern void node_suite_setup(void);
58 extern void out_encoded_polyline_suite_setup(void);
59 extern void out_geojson_suite_setup(void);
60 extern void out_gml_suite_setup(void);
61 extern void out_kml_suite_setup(void);
62 extern void out_svg_suite_setup(void);
63 extern void twkb_out_suite_setup(void);
64 extern void out_x3d_suite_setup(void);
65 extern void ptarray_suite_setup(void);
66 #if HAVE_SFCGAL
67 extern void sfcgal_suite_setup(void);
68 #endif
69 extern void split_suite_setup(void);
70 extern void stringbuffer_suite_setup(void);
71 extern void tree_suite_setup(void);
72 extern void triangulate_suite_setup(void);
73 extern void varint_suite_setup(void);
74 extern void wkt_out_suite_setup(void);
75 extern void wkb_out_suite_setup(void);
76 extern void surface_suite_setup(void);
77 extern void wkb_in_suite_setup(void);
78 extern void wkt_in_suite_setup(void);
79 extern void wrapx_suite_setup(void);
80 
81 
82 /* AND ADD YOUR SUITE SETUP FUNCTION HERE (2 of 2) */
83 PG_SuiteSetup setupfuncs[] =
84 {
85 	algorithms_suite_setup,
86 	buildarea_suite_setup,
87 	clean_suite_setup,
88 	clip_by_rect_suite_setup,
89 	force_sfs_suite_setup,
90 	geodetic_suite_setup,
91 	geos_suite_setup,
92 	geos_cluster_suite_setup,
93 	unionfind_suite_setup,
94 	homogenize_suite_setup,
95 	in_encoded_polyline_suite_setup,
96 #if HAVE_LIBJSON
97 	in_geojson_suite_setup,
98 #endif
99     iterator_suite_setup,
100 	twkb_in_suite_setup,
101 	gserialized1_suite_setup,
102 	gserialized2_suite_setup,
103 	lwstroke_suite_setup,
104 	measures_suite_setup,
105 	effectivearea_suite_setup,
106 	chaikin_suite_setup,
107 	filterm_suite_setup,
108 	minimum_bounding_circle_suite_setup,
109 	misc_suite_setup,
110 	node_suite_setup,
111 	out_encoded_polyline_suite_setup,
112 	out_geojson_suite_setup,
113 	out_gml_suite_setup,
114 	out_kml_suite_setup,
115 	out_svg_suite_setup,
116 	out_x3d_suite_setup,
117 	ptarray_suite_setup,
118 	print_suite_setup,
119 #if HAVE_SFCGAL
120 	sfcgal_suite_setup,
121 #endif
122 	split_suite_setup,
123 	stringbuffer_suite_setup,
124 	surface_suite_setup,
125 	tree_suite_setup,
126 	triangulate_suite_setup,
127 	twkb_out_suite_setup,
128 	varint_suite_setup,
129 	wkb_in_suite_setup,
130 	wkb_out_suite_setup,
131 	wkt_in_suite_setup,
132 	wkt_out_suite_setup,
133 	wrapx_suite_setup,
134 	NULL
135 };
136 
137 
138 #define MAX_CUNIT_MSG_LENGTH 256
139 
140 /*
141 ** The main() function for setting up and running the tests.
142 ** Returns a CUE_SUCCESS on successful running, another
143 ** CUnit error code on failure.
144 */
main(int argc,char * argv[])145 int main(int argc, char *argv[])
146 {
147 	int index;
148 	char *suite_name;
149 	CU_pSuite suite_to_run;
150 	char *test_name;
151 	CU_pTest test_to_run = NULL;
152 	CU_ErrorCode errCode = 0;
153 	CU_pTestRegistry registry;
154 	int num_run;
155 	int num_failed;
156 	PG_SuiteSetup *setupfunc = setupfuncs;
157 
158 	/* Install the custom error handler */
159 	lwgeom_set_handlers(0, 0, 0, cu_errorreporter, cu_noticereporter);
160 	lwgeom_set_debuglogger(cu_debuglogger);
161 
162 	/* Initialize the CUnit test registry */
163 	if (CUE_SUCCESS != CU_initialize_registry())
164 	{
165 		errCode = CU_get_error();
166 		printf("    Error attempting to initialize registry: %d.  See CUError.h for error code list.\n", errCode);
167 		return errCode;
168 	}
169 
170 	/* Register all the test suites. */
171 	while ( *setupfunc )
172 	{
173 		(*setupfunc)();
174 		setupfunc++;
175 	}
176 
177 	/* Run all tests using the CUnit Basic interface */
178 	CU_basic_set_mode(CU_BRM_VERBOSE);
179 	if (argc <= 1)
180 	{
181 		errCode = CU_basic_run_tests();
182 	}
183 	else
184 	{
185 		/* NOTE: The cunit functions used here (CU_get_registry, CU_get_suite_by_name, and CU_get_test_by_name) are
186 		 *       listed with the following warning: "Internal CUnit system functions.  Should not be routinely called by users."
187 		 *       However, there didn't seem to be any other way to get tests by name, so we're calling them. */
188 		registry = CU_get_registry();
189 		for (index = 1; index < argc; index++)
190 		{
191 			suite_name = argv[index];
192 			test_name = NULL;
193 			suite_to_run = CU_get_suite_by_name(suite_name, registry);
194 			if (NULL == suite_to_run)
195 			{
196 				/* See if it's a test name instead of a suite name. */
197 				suite_to_run = registry->pSuite;
198 				while (suite_to_run != NULL)
199 				{
200 					test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
201 					if (test_to_run != NULL)
202 					{
203 						/* It was a test name. */
204 						test_name = suite_name;
205 						suite_name = suite_to_run->pName;
206 						break;
207 					}
208 					suite_to_run = suite_to_run->pNext;
209 				}
210 			}
211 			if (suite_to_run == NULL)
212 			{
213 				printf("\n'%s' does not appear to be either a suite name or a test name.\n\n", suite_name);
214 			}
215 			else
216 			{
217 				if (test_name != NULL && test_to_run != NULL)
218 				{
219 					/* Run only this test. */
220 					printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
221 					/* This should be CU_basic_run_test, but that method is broken, see:
222 					 *     https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
223 					 * This one doesn't output anything for success, so we have to do it manually. */
224 					errCode = CU_run_test(suite_to_run, test_to_run);
225 					if (errCode != CUE_SUCCESS)
226 					{
227 						printf("    Error attempting to run tests: %d.  See CUError.h for error code list.\n", errCode);
228 					}
229 					else
230 					{
231 						num_run = CU_get_number_of_asserts();
232 						num_failed = CU_get_number_of_failures();
233 						printf("\n    %s - asserts - %3d passed, %3d failed, %3d total.\n\n",
234 						       (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
235 					}
236 				}
237 				else
238 				{
239 					/* Run all the tests in the suite. */
240 					printf("\nRunning all tests in suite '%s'.\n", suite_name);
241 					/* This should be CU_basic_run_suite, but that method is broken, see:
242 					 *     https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
243 					 * This one doesn't output anything for success, so we have to do it manually. */
244 					errCode = CU_run_suite(suite_to_run);
245 					if (errCode != CUE_SUCCESS)
246 					{
247 						printf("    Error attempting to run tests: %d.  See CUError.h for error code list.\n", errCode);
248 					}
249 					else
250 					{
251 						num_run = CU_get_number_of_tests_run();
252 						num_failed = CU_get_number_of_tests_failed();
253 						printf("\n    %s -   tests - %3d passed, %3d failed, %3d total.\n",
254 						       (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
255 						num_run = CU_get_number_of_asserts();
256 						num_failed = CU_get_number_of_failures();
257 						printf("           - asserts - %3d passed, %3d failed, %3d total.\n\n",
258 						       (num_run - num_failed), num_failed, num_run);
259 					}
260 				}
261 			}
262 		}
263 		/* Presumably if the CU_basic_run_[test|suite] functions worked, we wouldn't have to do this. */
264 		CU_basic_show_failures(CU_get_failure_list());
265 		printf("\n\n"); /* basic_show_failures leaves off line breaks. */
266 	}
267 	num_failed = CU_get_number_of_failures();
268 	CU_cleanup_registry();
269 	return num_failed;
270 }
271 /**
272  * CUnit error handler
273  * Log message in a global var instead of printing in stderr
274  *
275  * CAUTION: Not stop execution on lwerror case !!!
276  */
277 static void
cu_errorreporter(const char * fmt,va_list ap)278 cu_errorreporter(const char *fmt, va_list ap)
279 {
280   vsnprintf (cu_error_msg, MAX_CUNIT_MSG_LENGTH, fmt, ap);
281   cu_error_msg[MAX_CUNIT_MSG_LENGTH]='\0';
282   /*fprintf(stderr, "ERROR: %s\n", cu_error_msg);*/
283 }
284 
285 static void
cu_noticereporter(const char * fmt,va_list ap)286 cu_noticereporter(const char *fmt, va_list ap)
287 {
288   char buf[MAX_CUNIT_MSG_LENGTH+1];
289   vsnprintf (buf, MAX_CUNIT_MSG_LENGTH, fmt, ap);
290   buf[MAX_CUNIT_MSG_LENGTH]='\0';
291   fprintf(stderr, "NOTICE: %s\n", buf);
292 }
293 
294 static void
cu_debuglogger(int level,const char * fmt,va_list ap)295 cu_debuglogger(int level, const char *fmt, va_list ap)
296 {
297   char buf[MAX_CUNIT_MSG_LENGTH+1];
298   vsnprintf (buf, MAX_CUNIT_MSG_LENGTH, fmt, ap);
299   buf[MAX_CUNIT_MSG_LENGTH]='\0';
300   fprintf(stderr, "DEBUG%d: %s\n", level, buf);
301 }
302 
303 void
cu_error_msg_reset()304 cu_error_msg_reset()
305 {
306 	memset(cu_error_msg, '\0', MAX_CUNIT_ERROR_LENGTH);
307 }
308 
309 /* Utility functions for testing */
310 
311 /* do_transformation_test
312  * - reads input_wkt and expected_wkt
313  * - asserts output of transfn(input) = expected
314  * - cleans up
315  */
316 void
do_fn_test(LWGEOM * (* transfn)(LWGEOM *),char * input_wkt,char * expected_wkt)317 do_fn_test(LWGEOM* (*transfn)(LWGEOM*), char *input_wkt, char *expected_wkt)
318 {
319 	LWGEOM* input = lwgeom_from_wkt(input_wkt, LW_PARSER_CHECK_NONE);
320 	LWGEOM* expected = lwgeom_from_wkt(expected_wkt, LW_PARSER_CHECK_NONE);
321 	LWGEOM* observed = transfn(input);
322 
323 	ASSERT_LWGEOM_EQUAL(observed, expected);
324 
325 	lwgeom_free(input);
326 	lwgeom_free(expected);
327 	lwgeom_free(observed);
328 }
329 
330