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