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