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