PostGIS  2.4.9dev-r@@SVN_REVISION@@
raster/test/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 "cu_tester.h"
16 
17 /* Internal funcs */
18 static void
19 cu_error_reporter(const char *fmt, va_list ap);
20 
21 /* ADD YOUR SUITE SETUP FUNCTION HERE (1 of 2) */
22 extern void pixtype_suite_setup(void);
23 extern void raster_basics_suite_setup(void);
24 extern void band_basics_suite_setup(void);
25 extern void raster_wkb_suite_setup(void);
26 extern void gdal_suite_setup(void);
27 extern void raster_geometry_suite_setup(void);
28 extern void raster_misc_suite_setup(void);
29 extern void band_stats_suite_setup(void);
30 extern void band_misc_suite_setup(void);
31 extern void mapalgebra_suite_setup(void);
32 extern void spatial_relationship_suite_setup(void);
33 extern void misc_suite_setup(void);
34 
35 /* AND ADD YOUR SUITE SETUP FUNCTION HERE (2 of 2) */
37 {
50  NULL
51 };
52 
53 
54 /*
55 ** The main() function for setting up and running the tests.
56 ** Returns a CUE_SUCCESS on successful running, another
57 ** CUnit error code on failure.
58 */
59 int main(int argc, char *argv[])
60 {
61  int index;
62  char *suite_name;
63  CU_pSuite suite_to_run;
64  char *test_name;
65  CU_pTest test_to_run;
66  CU_ErrorCode errCode = 0;
67  CU_pTestRegistry registry;
68  int num_run;
69  int num_failed;
70  PG_SuiteSetup *setupfunc = setupfuncs;
71 
72  /* install the custom error handler */
74 
82  );
83 
84  /* initialize the CUnit test registry */
85  if (CUE_SUCCESS != CU_initialize_registry())
86  {
87  errCode = CU_get_error();
88  printf(" Error attempting to initialize registry: %d. See CUError.h for error code list.\n", errCode);
89  return errCode;
90  }
91 
92  /* Register all the test suites. */
93  while ( *setupfunc )
94  {
95  (*setupfunc)();
96  setupfunc++;
97  }
98 
99  /* Run all tests using the CUnit Basic interface */
100  CU_basic_set_mode(CU_BRM_VERBOSE);
101  if (argc <= 1)
102  {
103  errCode = CU_basic_run_tests();
104  }
105  else
106  {
107  /* NOTE: The cunit functions used here (CU_get_registry, CU_get_suite_by_name, and CU_get_test_by_name) are
108  * listed with the following warning: "Internal CUnit system functions. Should not be routinely called by users."
109  * However, there didn't seem to be any other way to get tests by name, so we're calling them. */
110  registry = CU_get_registry();
111  for (index = 1; index < argc; index++)
112  {
113  suite_name = argv[index];
114  test_name = NULL;
115  suite_to_run = CU_get_suite_by_name(suite_name, registry);
116  if (NULL == suite_to_run)
117  {
118  /* See if it's a test name instead of a suite name. */
119  suite_to_run = registry->pSuite;
120  while (suite_to_run != NULL)
121  {
122  test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
123  if (test_to_run != NULL)
124  {
125  /* It was a test name. */
126  test_name = suite_name;
127  suite_name = suite_to_run->pName;
128  break;
129  }
130  suite_to_run = suite_to_run->pNext;
131  }
132  }
133  if (suite_to_run == NULL)
134  {
135  printf("\n'%s' does not appear to be either a suite name or a test name.\n\n", suite_name);
136  }
137  else
138  {
139  if (test_name != NULL)
140  {
141  /* Run only this test. */
142  printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
143  /* This should be CU_basic_run_test, but that method is broken, see:
144  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
145  * This one doesn't output anything for success, so we have to do it manually. */
146  errCode = CU_run_test(suite_to_run, test_to_run);
147  if (errCode != CUE_SUCCESS)
148  {
149  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
150  }
151  else
152  {
153  num_run = CU_get_number_of_asserts();
154  num_failed = CU_get_number_of_failures();
155  printf("\n %s - asserts - %3d passed, %3d failed, %3d total.\n\n",
156  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
157  }
158  }
159  else
160  {
161  /* Run all the tests in the suite. */
162  printf("\nRunning all tests in suite '%s'.\n", suite_name);
163  /* This should be CU_basic_run_suite, but that method is broken, see:
164  * https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
165  * This one doesn't output anything for success, so we have to do it manually. */
166  errCode = CU_run_suite(suite_to_run);
167  if (errCode != CUE_SUCCESS)
168  {
169  printf(" Error attempting to run tests: %d. See CUError.h for error code list.\n", errCode);
170  }
171  else
172  {
173  num_run = CU_get_number_of_tests_run();
174  num_failed = CU_get_number_of_tests_failed();
175  printf("\n %s - tests - %3d passed, %3d failed, %3d total.\n",
176  (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
177  num_run = CU_get_number_of_asserts();
178  num_failed = CU_get_number_of_failures();
179  printf(" - asserts - %3d passed, %3d failed, %3d total.\n\n",
180  (num_run - num_failed), num_failed, num_run);
181  }
182  }
183  }
184  }
185  /* Presumably if the CU_basic_run_[test|suite] functions worked, we wouldn't have to do this. */
186  CU_basic_show_failures(CU_get_failure_list());
187  printf("\n\n"); /* basic_show_failures leaves off line breaks. */
188  }
189  num_failed = CU_get_number_of_failures();
190  CU_cleanup_registry();
191  return num_failed;
192 }
193 
200 static void cu_error_reporter(const char *fmt, va_list ap) {
201  vsnprintf (cu_error_msg, MAX_CUNIT_MSG_LENGTH, fmt, ap);
203 }
204 
206  memset(cu_error_msg, '\0', MAX_CUNIT_MSG_LENGTH);
207 }
208 
210  uint16_t i;
211  uint16_t nbands = rt_raster_get_num_bands(raster);
212 
213  for (i = 0; i < nbands; ++i) {
214  rt_band band = rt_raster_get_band(raster, i);
215  rt_band_destroy(band);
216  }
217 
218  rt_raster_destroy(raster);
219  raster = NULL;
220 }
221 
222 rt_band cu_add_band(rt_raster raster, rt_pixtype pixtype, int hasnodata, double nodataval) {
223  void* mem = NULL;
224  int32_t bandNum = 0;
225  size_t datasize = 0;
226  rt_band band = NULL;
227  uint16_t width = 0;
228  uint16_t height = 0;
229 
230  width = rt_raster_get_width(raster);
231  height = rt_raster_get_height(raster);
232 
233  datasize = rt_pixtype_size(pixtype) * width * height;
234  mem = rtalloc(datasize);
235  CU_ASSERT(mem != NULL);
236 
237  if (hasnodata)
238  memset(mem, nodataval, datasize);
239  else
240  memset(mem, 0, datasize);
241 
242  band = rt_band_new_inline(width, height, pixtype, hasnodata, nodataval, mem);
243  CU_ASSERT(band != NULL);
244  rt_band_set_ownsdata_flag(band, 1);
245 
246  bandNum = rt_raster_add_band(raster, band, rt_raster_get_num_bands(raster));
247  CU_ASSERT(bandNum >= 0);
248 
249  return band;
250 }
251 
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 pixtype_suite_setup(void)
Definition: cu_pixtype.c:232
void raster_wkb_suite_setup(void)
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
void band_stats_suite_setup(void)
void default_rt_info_handler(const char *fmt, va_list ap)
Definition: rt_context.c:93
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
#define MAX_CUNIT_MSG_LENGTH
void spatial_relationship_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_context.c:151
band
Definition: ovdump.py:57
def fmt
Definition: pixval.py:92
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
void * default_rt_reallocator(void *mem, size_t size)
Definition: rt_context.c:54
void cu_error_msg_reset()
rt_pixtype
Definition: librtcore.h:185
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_band.c:242
PG_SuiteSetup setupfuncs[]
void default_rt_warning_handler(const char *fmt, va_list ap)
Definition: rt_context.c:80
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 rt_band_set_ownsdata_flag(rt_band band, int flag)
Definition: rt_band.c:534
void raster_basics_suite_setup(void)
void raster_geometry_suite_setup(void)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
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_band.c:58
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_pixel.c:39
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
void default_rt_deallocator(void *mem)
Definition: rt_context.c:61
void gdal_suite_setup(void)
Definition: cu_gdal.c:590
void misc_suite_setup(void)
void * default_rt_allocator(size_t size)
The default memory/logging handlers installed by lwgeom_install_default_allocators() ...
Definition: rt_context.c:47
void band_misc_suite_setup(void)
Definition: cu_band_misc.c:583
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...
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
Definition: rt_raster.c:405
void band_basics_suite_setup(void)
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
void mapalgebra_suite_setup(void)
void raster_misc_suite_setup(void)
int main(int argc, char *argv[])