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