PostGIS  3.4.0dev-r@@SVN_REVISION@@
liblwgeom/cunit/cu_tester.c
Go to the documentation of this file.
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 
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 boundary_suite_setup();
36 extern void buildarea_suite_setup();
37 extern void clean_suite_setup();
38 extern void clip_by_rect_suite_setup();
39 extern void force_dims_suite_setup(void);
40 extern void force_sfs_suite_setup(void);
41 extern void geodetic_suite_setup(void);
42 extern void geos_suite_setup(void);
43 extern void geos_cluster_suite_setup(void);
44 extern void unionfind_suite_setup(void);
45 extern void homogenize_suite_setup(void);
46 extern void in_encoded_polyline_suite_setup(void);
47 extern void in_geojson_suite_setup(void);
48 extern void iterator_suite_setup(void);
49 extern void twkb_in_suite_setup(void);
50 extern void gserialized1_suite_setup(void);
51 extern void gserialized2_suite_setup(void);
52 extern void lwstroke_suite_setup(void);
53 extern void measures_suite_setup(void);
54 extern void effectivearea_suite_setup(void);
55 extern void chaikin_suite_setup(void);
56 extern void filterm_suite_setup(void);
57 extern void minimum_bounding_circle_suite_setup(void);
58 extern void misc_suite_setup(void);
59 extern void node_suite_setup(void);
60 extern void out_encoded_polyline_suite_setup(void);
61 extern void out_geojson_suite_setup(void);
62 extern void out_gml_suite_setup(void);
63 extern void out_kml_suite_setup(void);
64 extern void out_svg_suite_setup(void);
65 extern void twkb_out_suite_setup(void);
66 extern void out_x3d_suite_setup(void);
67 extern void ptarray_suite_setup(void);
68 #if HAVE_SFCGAL
69 extern void sfcgal_suite_setup(void);
70 #endif
71 extern void split_suite_setup(void);
72 extern void stringbuffer_suite_setup(void);
73 extern void tree_suite_setup(void);
74 extern void triangulate_suite_setup(void);
75 extern void varint_suite_setup(void);
76 extern void wkt_out_suite_setup(void);
77 extern void wkb_out_suite_setup(void);
78 extern void surface_suite_setup(void);
79 extern void wkb_in_suite_setup(void);
80 extern void wkt_in_suite_setup(void);
81 extern void wrapx_suite_setup(void);
82 
83 
84 /* AND ADD YOUR SUITE SETUP FUNCTION HERE (2 of 2) */
98 #if HAVE_LIBJSON
100 #endif
121 #if HAVE_SFCGAL
123 #endif
136  NULL};
137 
138 
139 #define MAX_CUNIT_MSG_LENGTH 256
140 
141 /*
142 ** The main() function for setting up and running the tests.
143 ** Returns a CUE_SUCCESS on successful running, another
144 ** CUnit error code on failure.
145 */
146 int main(int argc, char *argv[])
147 {
148  int index;
149  char *suite_name;
150  CU_pSuite suite_to_run;
151  char *test_name;
152  CU_pTest test_to_run = NULL;
153  CU_ErrorCode errCode = 0;
154  CU_pTestRegistry registry;
155  int num_run;
156  int num_failed;
157  PG_SuiteSetup *setupfunc = setupfuncs;
158 
159  /* Install the custom error handler */
162 
163  /* Initialize the CUnit test registry */
164  if (CUE_SUCCESS != CU_initialize_registry())
165  {
166  errCode = CU_get_error();
167  printf(" Error attempting to initialize registry: %d. See CUError.h for error code list.\n", errCode);
168  return errCode;
169  }
170 
171  /* Register all the test suites. */
172  while ( *setupfunc )
173  {
174  (*setupfunc)();
175  setupfunc++;
176  }
177 
178  /* Run all tests using the CUnit Basic interface */
179  CU_basic_set_mode(CU_BRM_VERBOSE);
180  if (argc <= 1)
181  {
182  errCode = CU_basic_run_tests();
183  }
184  else
185  {
186  /* NOTE: The cunit functions used here (CU_get_registry, CU_get_suite_by_name, and CU_get_test_by_name) are
187  * listed with the following warning: "Internal CUnit system functions. Should not be routinely called by users."
188  * However, there didn't seem to be any other way to get tests by name, so we're calling them. */
189  registry = CU_get_registry();
190  for (index = 1; index < argc; index++)
191  {
192  suite_name = argv[index];
193  test_name = NULL;
194  suite_to_run = CU_get_suite_by_name(suite_name, registry);
195  if (NULL == suite_to_run)
196  {
197  /* See if it's a test name instead of a suite name. */
198  suite_to_run = registry->pSuite;
199  while (suite_to_run != NULL)
200  {
201  test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
202  if (test_to_run != NULL)
203  {
204  /* It was a test name. */
205  test_name = suite_name;
206  suite_name = suite_to_run->pName;
207  break;
208  }
209  suite_to_run = suite_to_run->pNext;
210  }
211  }
212  if (suite_to_run == NULL)
213  {
214  printf("\n'%s' does not appear to be either a suite name or a test name.\n\n", suite_name);
215  }
216  else
217  {
218  if (test_name != NULL && test_to_run != NULL)
219  {
220  /* Run only this test. */
221  printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
222  /* This should be CU_basic_run_test, but that method is broken, see:
223  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
224  * This one doesn't output anything for success, so we have to do it manually. */
225  errCode = CU_run_test(suite_to_run, test_to_run);
226  if (errCode != CUE_SUCCESS)
227  {
228  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
229  }
230  else
231  {
232  num_run = CU_get_number_of_asserts();
233  num_failed = CU_get_number_of_failures();
234  printf("\n %s - asserts - %3d passed, %3d failed, %3d total.\n\n",
235  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
236  }
237  }
238  else
239  {
240  /* Run all the tests in the suite. */
241  printf("\nRunning all tests in suite '%s'.\n", suite_name);
242  /* This should be CU_basic_run_suite, but that method is broken, see:
243  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
244  * This one doesn't output anything for success, so we have to do it manually. */
245  errCode = CU_run_suite(suite_to_run);
246  if (errCode != CUE_SUCCESS)
247  {
248  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
249  }
250  else
251  {
252  num_run = CU_get_number_of_tests_run();
253  num_failed = CU_get_number_of_tests_failed();
254  printf("\n %s - tests - %3d passed, %3d failed, %3d total.\n",
255  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
256  num_run = CU_get_number_of_asserts();
257  num_failed = CU_get_number_of_failures();
258  printf(" - asserts - %3d passed, %3d failed, %3d total.\n\n",
259  (num_run - num_failed), num_failed, num_run);
260  }
261  }
262  }
263  }
264  /* Presumably if the CU_basic_run_[test|suite] functions worked, we wouldn't have to do this. */
265  CU_basic_show_failures(CU_get_failure_list());
266  printf("\n\n"); /* basic_show_failures leaves off line breaks. */
267  }
268  num_failed = CU_get_number_of_failures();
269  CU_cleanup_registry();
270  return num_failed;
271 }
278 static void
279 cu_errorreporter(const char *fmt, va_list ap)
280 {
281  vsnprintf (cu_error_msg, MAX_CUNIT_MSG_LENGTH, fmt, ap);
283  /*fprintf(stderr, "ERROR: %s\n", cu_error_msg);*/
284 }
285 
286 static void
287 cu_noticereporter(const char *fmt, va_list ap)
288 {
289  char buf[MAX_CUNIT_MSG_LENGTH+1];
290  vsnprintf (buf, MAX_CUNIT_MSG_LENGTH, fmt, ap);
291  buf[MAX_CUNIT_MSG_LENGTH]='\0';
292  fprintf(stderr, "NOTICE: %s\n", buf);
293 }
294 
295 static void
296 cu_debuglogger(int level, const char *fmt, va_list ap)
297 {
298  char buf[MAX_CUNIT_MSG_LENGTH+1];
299  vsnprintf (buf, MAX_CUNIT_MSG_LENGTH, fmt, ap);
300  buf[MAX_CUNIT_MSG_LENGTH]='\0';
301  fprintf(stderr, "DEBUG%d: %s\n", level, buf);
302 }
303 
304 void
306 {
307  memset(cu_error_msg, '\0', MAX_CUNIT_ERROR_LENGTH);
308 }
309 
310 /* Utility functions for testing */
311 
312 /* do_transformation_test
313  * - reads input_wkt and expected_wkt
314  * - asserts output of transfn(input) = expected
315  * - cleans up
316  */
317 void
318 do_fn_test(LWGEOM* (*transfn)(LWGEOM*), char *input_wkt, char *expected_wkt)
319 {
320  LWGEOM* input = lwgeom_from_wkt(input_wkt, LW_PARSER_CHECK_NONE);
321  LWGEOM* expected = lwgeom_from_wkt(expected_wkt, LW_PARSER_CHECK_NONE);
322  LWGEOM* observed = transfn(input);
323 
324  ASSERT_LWGEOM_EQUAL(observed, expected);
325 
326  lwgeom_free(input);
327  lwgeom_free(expected);
328  lwgeom_free(observed);
329 }
330 
void sfcgal_suite_setup(void)
Definition: cu_sfcgal.c:95
void out_x3d_suite_setup(void)
Definition: cu_out_x3d.c:169
void in_encoded_polyline_suite_setup(void)
void clip_by_rect_suite_setup()
int main(int argc, char *argv[])
#define MAX_CUNIT_MSG_LENGTH
void out_encoded_polyline_suite_setup(void)
void geos_cluster_suite_setup(void)
void boundary_suite_setup()
Definition: cu_boundary.c:207
void force_sfs_suite_setup(void)
Definition: cu_force_sfs.c:167
void buildarea_suite_setup()
Definition: cu_buildarea.c:327
static void cu_debuglogger(int level, const char *fmt, va_list ap)
void unionfind_suite_setup(void)
Definition: cu_unionfind.c:166
void do_fn_test(LWGEOM *(*transfn)(LWGEOM *), char *input_wkt, char *expected_wkt)
void wkt_out_suite_setup(void)
Definition: cu_out_wkt.c:220
void twkb_in_suite_setup(void)
Definition: cu_in_twkb.c:231
void homogenize_suite_setup(void)
void misc_suite_setup(void)
void gserialized2_suite_setup(void)
void measures_suite_setup(void)
Definition: cu_measures.c:1549
void wrapx_suite_setup(void)
Definition: cu_wrapx.c:194
void cu_error_msg_reset()
PG_SuiteSetup setupfuncs[]
void iterator_suite_setup(void)
Definition: cu_iterator.c:256
void triangulate_suite_setup(void)
void stringbuffer_suite_setup(void)
void chaikin_suite_setup(void)
Definition: cu_chaikin.c:61
void split_suite_setup(void)
Definition: cu_split.c:287
void clean_suite_setup()
Definition: cu_clean.c:177
void tree_suite_setup(void)
Definition: cu_tree.c:426
void out_gml_suite_setup(void)
Definition: cu_out_gml.c:1020
static void cu_errorreporter(const char *fmt, va_list ap)
CUnit error handler Log message in a global var instead of printing in stderr.
void twkb_out_suite_setup(void)
Definition: cu_out_twkb.c:277
void out_svg_suite_setup(void)
Definition: cu_out_svg.c:313
void geos_suite_setup(void)
Definition: cu_geos.c:199
void effectivearea_suite_setup(void)
void in_geojson_suite_setup(void)
void print_suite_setup()
Definition: cu_print.c:568
void wkb_in_suite_setup(void)
Definition: cu_in_wkb.c:286
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
void wkb_out_suite_setup(void)
Definition: cu_out_wkb.c:214
void geodetic_suite_setup(void)
Definition: cu_geodetic.c:1621
void ptarray_suite_setup(void)
Definition: cu_ptarray.c:840
void node_suite_setup(void)
Definition: cu_node.c:73
void out_kml_suite_setup(void)
Definition: cu_out_kml.c:219
void surface_suite_setup(void)
Definition: cu_surface.c:404
void varint_suite_setup(void)
Definition: cu_varint.c:251
void out_geojson_suite_setup(void)
void wkt_in_suite_setup(void)
Definition: cu_in_wkt.c:428
void algorithms_suite_setup()
void minimum_bounding_circle_suite_setup(void)
void gserialized1_suite_setup(void)
void filterm_suite_setup(void)
Definition: cu_filterm.c:59
static void cu_noticereporter(const char *fmt, va_list ap)
void force_dims_suite_setup(void)
void lwstroke_suite_setup(void)
Definition: cu_lwstroke.c:525
void(* PG_SuiteSetup)(void)
#define MAX_CUNIT_ERROR_LENGTH
#define ASSERT_LWGEOM_EQUAL(o, e)
void(*) typedef void(*) voi lwgeom_set_handlers)(lwallocator allocator, lwreallocator reallocator, lwfreeor freeor, lwreporter errorreporter, lwreporter noticereporter)
Install custom memory management and error handling functions you want your application to use.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2114
void lwgeom_set_debuglogger(lwdebuglogger debuglogger)
Definition: lwutil.c:171
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:905
def fmt
Definition: pixval.py:93