PostGIS  2.2.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 
27 /* ADD YOUR SUITE SETUP FUNCTION HERE (1 of 2) */
28 extern void print_suite_setup();
29 extern void algorithms_suite_setup();
30 extern void buildarea_suite_setup();
31 extern void clean_suite_setup();
32 extern void clip_by_rect_suite_setup();
33 extern void force_sfs_suite_setup(void);
34 extern void geodetic_suite_setup(void);
35 extern void geos_suite_setup(void);
36 extern void geos_cluster_suite_setup(void);
37 extern void unionfind_suite_setup(void);
38 extern void homogenize_suite_setup(void);
39 extern void in_encoded_polyline_suite_setup(void);
40 extern void in_geojson_suite_setup(void);
41 extern void twkb_in_suite_setup(void);
42 extern void libgeom_suite_setup(void);
43 extern void measures_suite_setup(void);
44 extern void effectivearea_suite_setup(void);
45 extern void misc_suite_setup(void);
46 extern void node_suite_setup(void);
47 extern void out_encoded_polyline_suite_setup(void);
48 extern void out_geojson_suite_setup(void);
49 extern void out_gml_suite_setup(void);
50 extern void out_kml_suite_setup(void);
51 extern void out_svg_suite_setup(void);
52 extern void twkb_out_suite_setup(void);
53 extern void out_x3d_suite_setup(void);
54 extern void ptarray_suite_setup(void);
55 extern void sfcgal_suite_setup(void);
56 extern void split_suite_setup(void);
57 extern void stringbuffer_suite_setup(void);
58 extern void tree_suite_setup(void);
59 extern void triangulate_suite_setup(void);
60 extern void varint_suite_setup(void);
61 extern void wkt_out_suite_setup(void);
62 extern void wkb_out_suite_setup(void);
63 extern void surface_suite_setup(void);
64 extern void wkb_in_suite_setup(void);
65 extern void wkt_in_suite_setup(void);
66 
67 
68 /* AND ADD YOUR SUITE SETUP FUNCTION HERE (2 of 2) */
70 {
82 #if HAVE_LIBJSON
84 #endif
99 #if HAVE_SFCGAL
101 #endif
113  NULL
114 };
115 
116 
117 #define MAX_CUNIT_MSG_LENGTH 256
118 
119 /*
120 ** The main() function for setting up and running the tests.
121 ** Returns a CUE_SUCCESS on successful running, another
122 ** CUnit error code on failure.
123 */
124 int main(int argc, char *argv[])
125 {
126  int index;
127  char *suite_name;
128  CU_pSuite suite_to_run;
129  char *test_name;
130  CU_pTest test_to_run;
131  CU_ErrorCode errCode = 0;
132  CU_pTestRegistry registry;
133  int num_run;
134  int num_failed;
135  PG_SuiteSetup *setupfunc = setupfuncs;
136 
137  /* Install the custom error handler */
139 
140  /* Initialize the CUnit test registry */
141  if (CUE_SUCCESS != CU_initialize_registry())
142  {
143  errCode = CU_get_error();
144  printf(" Error attempting to initialize registry: %d. See CUError.h for error code list.\n", errCode);
145  return errCode;
146  }
147 
148  /* Register all the test suites. */
149  while ( *setupfunc )
150  {
151  (*setupfunc)();
152  setupfunc++;
153  }
154 
155  /* Run all tests using the CUnit Basic interface */
156  CU_basic_set_mode(CU_BRM_VERBOSE);
157  if (argc <= 1)
158  {
159  errCode = CU_basic_run_tests();
160  }
161  else
162  {
163  /* NOTE: The cunit functions used here (CU_get_registry, CU_get_suite_by_name, and CU_get_test_by_name) are
164  * listed with the following warning: "Internal CUnit system functions. Should not be routinely called by users."
165  * However, there didn't seem to be any other way to get tests by name, so we're calling them. */
166  registry = CU_get_registry();
167  for (index = 1; index < argc; index++)
168  {
169  suite_name = argv[index];
170  test_name = NULL;
171  suite_to_run = CU_get_suite_by_name(suite_name, registry);
172  if (NULL == suite_to_run)
173  {
174  /* See if it's a test name instead of a suite name. */
175  suite_to_run = registry->pSuite;
176  while (suite_to_run != NULL)
177  {
178  test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
179  if (test_to_run != NULL)
180  {
181  /* It was a test name. */
182  test_name = suite_name;
183  suite_name = suite_to_run->pName;
184  break;
185  }
186  suite_to_run = suite_to_run->pNext;
187  }
188  }
189  if (suite_to_run == NULL)
190  {
191  printf("\n'%s' does not appear to be either a suite name or a test name.\n\n", suite_name);
192  }
193  else
194  {
195  if (test_name != NULL)
196  {
197  /* Run only this test. */
198  printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
199  /* This should be CU_basic_run_test, but that method is broken, see:
200  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
201  * This one doesn't output anything for success, so we have to do it manually. */
202  errCode = CU_run_test(suite_to_run, test_to_run);
203  if (errCode != CUE_SUCCESS)
204  {
205  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
206  }
207  else
208  {
209  num_run = CU_get_number_of_asserts();
210  num_failed = CU_get_number_of_failures();
211  printf("\n %s - asserts - %3d passed, %3d failed, %3d total.\n\n",
212  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
213  }
214  }
215  else
216  {
217  /* Run all the tests in the suite. */
218  printf("\nRunning all tests in suite '%s'.\n", suite_name);
219  /* This should be CU_basic_run_suite, but that method is broken, see:
220  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
221  * This one doesn't output anything for success, so we have to do it manually. */
222  errCode = CU_run_suite(suite_to_run);
223  if (errCode != CUE_SUCCESS)
224  {
225  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
226  }
227  else
228  {
229  num_run = CU_get_number_of_tests_run();
230  num_failed = CU_get_number_of_tests_failed();
231  printf("\n %s - tests - %3d passed, %3d failed, %3d total.\n",
232  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
233  num_run = CU_get_number_of_asserts();
234  num_failed = CU_get_number_of_failures();
235  printf(" - asserts - %3d passed, %3d failed, %3d total.\n\n",
236  (num_run - num_failed), num_failed, num_run);
237  }
238  }
239  }
240  }
241  /* Presumably if the CU_basic_run_[test|suite] functions worked, we wouldn't have to do this. */
242  CU_basic_show_failures(CU_get_failure_list());
243  printf("\n\n"); /* basic_show_failures leaves off line breaks. */
244  }
245  num_failed = CU_get_number_of_failures();
246  CU_cleanup_registry();
247  return num_failed;
248 }
255 static void
256 cu_errorreporter(const char *fmt, va_list ap)
257 {
258  vsnprintf (cu_error_msg, MAX_CUNIT_MSG_LENGTH, fmt, ap);
260  /*fprintf(stderr, "ERROR: %s\n", cu_error_msg);*/
261 }
262 
263 static void
264 cu_noticereporter(const char *fmt, va_list ap)
265 {
266  char buf[MAX_CUNIT_MSG_LENGTH+1];
267  vsnprintf (buf, MAX_CUNIT_MSG_LENGTH, fmt, ap);
268  buf[MAX_CUNIT_MSG_LENGTH]='\0';
269  /*fprintf(stderr, "NOTICE: %s\n", buf);*/
270 }
271 
272 void
274 {
275  memset(cu_error_msg, '\0', MAX_CUNIT_ERROR_LENGTH);
276 }
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 misc_suite_setup(void)
PG_SuiteSetup setupfuncs[]
void clean_suite_setup()
Definition: cu_clean.c:162
void homogenize_suite_setup(void)
void twkb_out_suite_setup(void)
Definition: cu_out_twkb.c:281
#define MAX_CUNIT_MSG_LENGTH
void split_suite_setup(void)
Definition: cu_split.c:285
void out_encoded_polyline_suite_setup(void)
void in_encoded_polyline_suite_setup(void)
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
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:1099
void surface_suite_setup(void)
Definition: cu_surface.c:404
void(* PG_SuiteSetup)(void)
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 sfcgal_suite_setup(void)
Definition: cu_sfcgal.c:95
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
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()
Definition: cu_algorithm.c:989
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
void out_svg_suite_setup(void)
Definition: cu_out_svg.c:321
void unionfind_suite_setup(void)
Definition: cu_unionfind.c:114
int main(int argc, char *argv[])
void node_suite_setup(void)
Definition: cu_node.c:67
void print_suite_setup()
Definition: cu_print.c:164
void in_geojson_suite_setup(void)