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