PostGIS  2.1.10dev-r@@SVN_REVISION@@
raster/test/cunit/cu_tester.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id$
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2008 Paul Ramsey <pramsey@cleverelephant.ca>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  **********************************************************************/
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include "CUnit/Basic.h"
27 #include "cu_tester.h"
28 
29 /* Internal funcs */
30 static void
31 cu_error_reporter(const char *fmt, va_list ap);
32 
33 /* ADD YOUR SUITE SETUP FUNCTION HERE (1 of 2) */
34 extern void pixtype_suite_setup(void);
35 extern void raster_basics_suite_setup(void);
36 extern void band_basics_suite_setup(void);
37 extern void raster_wkb_suite_setup(void);
38 extern void gdal_suite_setup(void);
39 extern void raster_geometry_suite_setup(void);
40 extern void raster_misc_suite_setup(void);
41 extern void band_stats_suite_setup(void);
42 extern void band_misc_suite_setup(void);
43 extern void mapalgebra_suite_setup(void);
44 extern void spatial_relationship_suite_setup(void);
45 extern void misc_suite_setup(void);
46 
47 /* AND ADD YOUR SUITE SETUP FUNCTION HERE (2 of 2) */
49 {
62  NULL
63 };
64 
65 
66 /*
67 ** The main() function for setting up and running the tests.
68 ** Returns a CUE_SUCCESS on successful running, another
69 ** CUnit error code on failure.
70 */
71 int main(int argc, char *argv[])
72 {
73  int index;
74  char *suite_name;
75  CU_pSuite suite_to_run;
76  char *test_name;
77  CU_pTest test_to_run;
78  CU_ErrorCode errCode = 0;
79  CU_pTestRegistry registry;
80  int num_run;
81  int num_failed;
82  PG_SuiteSetup *setupfunc = setupfuncs;
83 
84  /* install the custom error handler */
86 
94  );
95 
96  /* initialize the CUnit test registry */
97  if (CUE_SUCCESS != CU_initialize_registry())
98  {
99  errCode = CU_get_error();
100  printf(" Error attempting to initialize registry: %d. See CUError.h for error code list.\n", errCode);
101  return errCode;
102  }
103 
104  /* Register all the test suites. */
105  while ( *setupfunc )
106  {
107  (*setupfunc)();
108  setupfunc++;
109  }
110 
111  /* Run all tests using the CUnit Basic interface */
112  CU_basic_set_mode(CU_BRM_VERBOSE);
113  if (argc <= 1)
114  {
115  errCode = CU_basic_run_tests();
116  }
117  else
118  {
119  /* NOTE: The cunit functions used here (CU_get_registry, CU_get_suite_by_name, and CU_get_test_by_name) are
120  * listed with the following warning: "Internal CUnit system functions. Should not be routinely called by users."
121  * However, there didn't seem to be any other way to get tests by name, so we're calling them. */
122  registry = CU_get_registry();
123  for (index = 1; index < argc; index++)
124  {
125  suite_name = argv[index];
126  test_name = NULL;
127  suite_to_run = CU_get_suite_by_name(suite_name, registry);
128  if (NULL == suite_to_run)
129  {
130  /* See if it's a test name instead of a suite name. */
131  suite_to_run = registry->pSuite;
132  while (suite_to_run != NULL)
133  {
134  test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
135  if (test_to_run != NULL)
136  {
137  /* It was a test name. */
138  test_name = suite_name;
139  suite_name = suite_to_run->pName;
140  break;
141  }
142  suite_to_run = suite_to_run->pNext;
143  }
144  }
145  if (suite_to_run == NULL)
146  {
147  printf("\n'%s' does not appear to be either a suite name or a test name.\n\n", suite_name);
148  }
149  else
150  {
151  if (test_name != NULL)
152  {
153  /* Run only this test. */
154  printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
155  /* This should be CU_basic_run_test, but that method is broken, see:
156  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
157  * This one doesn't output anything for success, so we have to do it manually. */
158  errCode = CU_run_test(suite_to_run, test_to_run);
159  if (errCode != CUE_SUCCESS)
160  {
161  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
162  }
163  else
164  {
165  num_run = CU_get_number_of_asserts();
166  num_failed = CU_get_number_of_failures();
167  printf("\n %s - asserts - %3d passed, %3d failed, %3d total.\n\n",
168  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
169  }
170  }
171  else
172  {
173  /* Run all the tests in the suite. */
174  printf("\nRunning all tests in suite '%s'.\n", suite_name);
175  /* This should be CU_basic_run_suite, but that method is broken, see:
176  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
177  * This one doesn't output anything for success, so we have to do it manually. */
178  errCode = CU_run_suite(suite_to_run);
179  if (errCode != CUE_SUCCESS)
180  {
181  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
182  }
183  else
184  {
185  num_run = CU_get_number_of_tests_run();
186  num_failed = CU_get_number_of_tests_failed();
187  printf("\n %s - tests - %3d passed, %3d failed, %3d total.\n",
188  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
189  num_run = CU_get_number_of_asserts();
190  num_failed = CU_get_number_of_failures();
191  printf(" - asserts - %3d passed, %3d failed, %3d total.\n\n",
192  (num_run - num_failed), num_failed, num_run);
193  }
194  }
195  }
196  }
197  /* Presumably if the CU_basic_run_[test|suite] functions worked, we wouldn't have to do this. */
198  CU_basic_show_failures(CU_get_failure_list());
199  printf("\n\n"); /* basic_show_failures leaves off line breaks. */
200  }
201  num_failed = CU_get_number_of_failures();
202  CU_cleanup_registry();
203  return num_failed;
204 }
205 
212 static void cu_error_reporter(const char *fmt, va_list ap) {
213  char *msg;
214 
218  if (!lw_vasprintf (&msg, fmt, ap)) {
219  va_end (ap);
220  return;
221  }
222 
223  strncpy(cu_error_msg, msg, MAX_CUNIT_MSG_LENGTH);
224  rtdealloc(msg);
225 }
226 
228  memset(cu_error_msg, '\0', MAX_CUNIT_MSG_LENGTH);
229 }
230 
232  uint16_t i;
233  uint16_t nbands = rt_raster_get_num_bands(raster);
234 
235  for (i = 0; i < nbands; ++i) {
236  rt_band band = rt_raster_get_band(raster, i);
237  rt_band_destroy(band);
238  }
239 
240  rt_raster_destroy(raster);
241  raster = NULL;
242 }
243 
244 rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval) {
245  void* mem = NULL;
246  int32_t bandNum = 0;
247  size_t datasize = 0;
248  rt_band band = NULL;
249  uint16_t width = 0;
250  uint16_t height = 0;
251 
252  width = rt_raster_get_width(raster);
253  height = rt_raster_get_height(raster);
254 
255  datasize = rt_pixtype_size(pixtype) * width * height;
256  mem = rtalloc(datasize);
257  CU_ASSERT(mem != NULL);
258 
259  if (hasnodata)
260  memset(mem, nodataval, datasize);
261  else
262  memset(mem, 0, datasize);
263 
264  band = rt_band_new_inline(width, height, pixtype, hasnodata, nodataval, mem);
265  CU_ASSERT(band != NULL);
266  rt_band_set_ownsdata_flag(band, 1);
267 
268  bandNum = rt_raster_add_band(raster, band, rt_raster_get_num_bands(raster));
269  CU_ASSERT(bandNum >= 0);
270 
271  return band;
272 }
273 
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_api.c:5677
static void cu_error_reporter(const char *fmt, va_list ap)
CUnit error handler Log message in a global var instead of printing in stderr.
void rtdealloc(void *mem)
Definition: rt_api.c:882
void pixtype_suite_setup(void)
Definition: cu_pixtype.c:232
void raster_wkb_suite_setup(void)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_api.c:5387
void band_stats_suite_setup(void)
void rt_set_handlers(rt_allocator allocator, rt_reallocator reallocator, rt_deallocator deallocator, rt_message_handler error_handler, rt_message_handler info_handler, rt_message_handler warning_handler)
This function is called when the PostgreSQL backend is taking care of the memory and we want to use p...
Definition: rt_api.c:846
void spatial_relationship_suite_setup(void)
tuple fmt
Definition: pixval.py:92
int lw_vasprintf(char **result, const char *format, va_list args)
Definition: vsprintf.c:148
tuple band
Definition: ovdump.py:57
void cu_error_msg_reset()
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:123
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
Definition: rt_api.c:1466
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_api.c:1097
PG_SuiteSetup setupfuncs[]
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
Definition: rt_api.c:5706
void(* PG_SuiteSetup)(void)
void cu_free_raster(rt_raster raster)
rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval)
void raster_basics_suite_setup(void)
rt_pixtype
Definition: rt_api.h:172
void raster_geometry_suite_setup(void)
void default_rt_info_handler(const char *fmt, va_list ap)
Definition: rt_api.c:788
void rt_band_set_ownsdata_flag(rt_band band, int flag)
Definition: rt_api.c:1936
void default_rt_deallocator(void *mem)
Definition: rt_api.c:756
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_api.c:1650
void * default_rt_reallocator(void *mem, size_t size)
Definition: rt_api.c:749
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_api.c:5434
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_api.c:867
void * default_rt_allocator(size_t size)
The default memory/logging handlers installed by lwgeom_install_default_allocators() ...
Definition: rt_api.c:742
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition: rt_api.c:5686
void gdal_suite_setup(void)
Definition: cu_gdal.c:646
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
void misc_suite_setup(void)
void band_misc_suite_setup(void)
Definition: cu_band_misc.c:372
#define MAX_CUNIT_MSG_LENGTH
void band_basics_suite_setup(void)
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
void default_rt_warning_handler(const char *fmt, va_list ap)
Definition: rt_api.c:775
void mapalgebra_suite_setup(void)
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_api.c:5426
void raster_misc_suite_setup(void)
int main(int argc, char *argv[])