PostGIS  2.1.10dev-r@@SVN_REVISION@@
liblwgeom/cunit/cu_tester.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cu_tester.c 13242 2015-02-19 00:22:22Z pramsey $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2008 Paul Ramsey <pramsey@cleverelephant.ca>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU General Public Licence. See the COPYING file.
10  *
11  **********************************************************************/
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include "CUnit/Basic.h"
16 #include "liblwgeom_internal.h"
17 #include "cu_tester.h"
18 #include "../postgis_config.h"
19 
20 /* Internal funcs */
21 static void
22 cu_errorreporter(const char *fmt, va_list ap);
23 
24 
25 /* ADD YOUR SUITE SETUP FUNCTION HERE (1 of 2) */
26 extern void print_suite_setup();
27 extern void algorithms_suite_setup();
28 extern void buildarea_suite_setup();
29 extern void clean_suite_setup();
30 extern void force_sfs_suite_setup(void);
31 extern void geodetic_suite_setup(void);
32 extern void geos_suite_setup(void);
33 extern void homogenize_suite_setup(void);
34 extern void in_geojson_suite_setup(void);
35 extern void libgeom_suite_setup(void);
36 extern void measures_suite_setup(void);
37 extern void misc_suite_setup(void);
38 extern void node_suite_setup(void);
39 extern void out_geojson_suite_setup(void);
40 extern void out_gml_suite_setup(void);
41 extern void out_kml_suite_setup(void);
42 extern void out_svg_suite_setup(void);
43 extern void out_x3d_suite_setup(void);
44 extern void ptarray_suite_setup(void);
45 extern void sfcgal_suite_setup(void);
46 extern void split_suite_setup(void);
47 extern void stringbuffer_suite_setup(void);
48 extern void tree_suite_setup(void);
49 extern void triangulate_suite_setup(void);
50 extern void wkt_out_suite_setup(void);
51 extern void wkb_out_suite_setup(void);
52 extern void surface_suite_setup(void);
53 extern void wkb_in_suite_setup(void);
54 extern void wkt_in_suite_setup(void);
55 
56 
57 /* AND ADD YOUR SUITE SETUP FUNCTION HERE (2 of 2) */
59 {
68 #if HAVE_LIBJSON
70 #endif
81 #if HAVE_SFCGAL
83 #endif
93  NULL
94 };
95 
96 
97 /*
98 ** The main() function for setting up and running the tests.
99 ** Returns a CUE_SUCCESS on successful running, another
100 ** CUnit error code on failure.
101 */
102 int main(int argc, char *argv[])
103 {
104  int index;
105  char *suite_name;
106  CU_pSuite suite_to_run;
107  char *test_name;
108  CU_pTest test_to_run;
109  CU_ErrorCode errCode = 0;
110  CU_pTestRegistry registry;
111  int num_run;
112  int num_failed;
113  PG_SuiteSetup *setupfunc = setupfuncs;
114 
115  /* install the custom error handler */
117 
118  /* initialize the CUnit test registry */
119  if (CUE_SUCCESS != CU_initialize_registry())
120  {
121  errCode = CU_get_error();
122  printf(" Error attempting to initialize registry: %d. See CUError.h for error code list.\n", errCode);
123  return errCode;
124  }
125 
126  /* Register all the test suites. */
127  while ( *setupfunc )
128  {
129  (*setupfunc)();
130  setupfunc++;
131  }
132 
133 
134  /* Run all tests using the CUnit Basic interface */
135  CU_basic_set_mode(CU_BRM_VERBOSE);
136  if (argc <= 1)
137  {
138  errCode = CU_basic_run_tests();
139  }
140  else
141  {
142  /* NOTE: The cunit functions used here (CU_get_registry, CU_get_suite_by_name, and CU_get_test_by_name) are
143  * listed with the following warning: "Internal CUnit system functions. Should not be routinely called by users."
144  * However, there didn't seem to be any other way to get tests by name, so we're calling them. */
145  registry = CU_get_registry();
146  for (index = 1; index < argc; index++)
147  {
148  suite_name = argv[index];
149  test_name = NULL;
150  suite_to_run = CU_get_suite_by_name(suite_name, registry);
151  if (NULL == suite_to_run)
152  {
153  /* See if it's a test name instead of a suite name. */
154  suite_to_run = registry->pSuite;
155  while (suite_to_run != NULL)
156  {
157  test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
158  if (test_to_run != NULL)
159  {
160  /* It was a test name. */
161  test_name = suite_name;
162  suite_name = suite_to_run->pName;
163  break;
164  }
165  suite_to_run = suite_to_run->pNext;
166  }
167  }
168  if (suite_to_run == NULL)
169  {
170  printf("\n'%s' does not appear to be either a suite name or a test name.\n\n", suite_name);
171  }
172  else
173  {
174  if (test_name != NULL)
175  {
176  /* Run only this test. */
177  printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
178  /* This should be CU_basic_run_test, but that method is broken, see:
179  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
180  * This one doesn't output anything for success, so we have to do it manually. */
181  errCode = CU_run_test(suite_to_run, test_to_run);
182  if (errCode != CUE_SUCCESS)
183  {
184  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
185  }
186  else
187  {
188  num_run = CU_get_number_of_asserts();
189  num_failed = CU_get_number_of_failures();
190  printf("\n %s - asserts - %3d passed, %3d failed, %3d total.\n\n",
191  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
192  }
193  }
194  else
195  {
196  /* Run all the tests in the suite. */
197  printf("\nRunning all tests in suite '%s'.\n", suite_name);
198  /* This should be CU_basic_run_suite, but that method is broken, see:
199  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
200  * This one doesn't output anything for success, so we have to do it manually. */
201  errCode = CU_run_suite(suite_to_run);
202  if (errCode != CUE_SUCCESS)
203  {
204  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
205  }
206  else
207  {
208  num_run = CU_get_number_of_tests_run();
209  num_failed = CU_get_number_of_tests_failed();
210  printf("\n %s - tests - %3d passed, %3d failed, %3d total.\n",
211  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
212  num_run = CU_get_number_of_asserts();
213  num_failed = CU_get_number_of_failures();
214  printf(" - asserts - %3d passed, %3d failed, %3d total.\n\n",
215  (num_run - num_failed), num_failed, num_run);
216  }
217  }
218  }
219  }
220  /* Presumably if the CU_basic_run_[test|suite] functions worked, we wouldn't have to do this. */
221  CU_basic_show_failures(CU_get_failure_list());
222  printf("\n\n"); /* basic_show_failures leaves off line breaks. */
223  }
224  num_failed = CU_get_number_of_failures();
225  CU_cleanup_registry();
226  return num_failed;
227 }
234 static void
235 cu_errorreporter(const char *fmt, va_list ap)
236 {
237  char *msg;
238 
242  if (!lw_vasprintf (&msg, fmt, ap))
243  {
244  va_end (ap);
245  return;
246  }
247 
248  strncpy(cu_error_msg, msg, MAX_CUNIT_ERROR_LENGTH);
249  lwfree(msg);
250 }
251 
252 void
254 {
255  memset(cu_error_msg, '\0', MAX_CUNIT_ERROR_LENGTH);
256 }
void stringbuffer_suite_setup(void)
void geos_suite_setup(void)
Definition: cu_geos.c:73
void out_kml_suite_setup(void)
Definition: cu_out_kml.c:240
void geodetic_suite_setup(void)
Definition: cu_geodetic.c:1504
void misc_suite_setup(void)
PG_SuiteSetup setupfuncs[]
void clean_suite_setup()
Definition: cu_clean.c:143
void homogenize_suite_setup(void)
void lwfree(void *mem)
Definition: lwutil.c:190
void split_suite_setup(void)
Definition: cu_split.c:164
void wkb_out_suite_setup(void)
Definition: cu_out_wkb.c:179
tuple fmt
Definition: pixval.py:92
void tree_suite_setup(void)
Definition: cu_tree.c:312
int lw_vasprintf(char **result, const char *format, va_list args)
Definition: vsprintf.c:148
void wkt_out_suite_setup(void)
Definition: cu_out_wkt.c:212
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:958
void surface_suite_setup(void)
Definition: cu_surface.c:405
void(* PG_SuiteSetup)(void)
void buildarea_suite_setup()
Definition: cu_buildarea.c:327
void ptarray_suite_setup(void)
Definition: cu_ptarray.c:695
void out_gml_suite_setup(void)
Definition: cu_out_gml.c:1084
void cu_error_msg_reset()
void out_x3d_suite_setup(void)
Definition: cu_out_x3d.c:163
void triangulate_suite_setup(void)
void sfcgal_suite_setup(void)
Definition: cu_sfcgal.c:95
void force_sfs_suite_setup(void)
Definition: cu_force_sfs.c:168
void out_geojson_suite_setup(void)
void measures_suite_setup(void)
Definition: cu_measures.c:803
void wkb_in_suite_setup(void)
Definition: cu_in_wkb.c:217
void 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...
Definition: lwutil.c:151
#define MAX_CUNIT_ERROR_LENGTH
void wkt_in_suite_setup(void)
Definition: cu_in_wkt.c:353
void algorithms_suite_setup()
Definition: cu_algorithm.c:950
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
void out_svg_suite_setup(void)
Definition: cu_out_svg.c:322
int main(int argc, char *argv[])
void node_suite_setup(void)
Definition: cu_node.c:67
void print_suite_setup()
Definition: cu_print.c:165
void in_geojson_suite_setup(void)