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