28 #include <irplib_utils.h>
37 static IRPLIB_UTIL_SET_ROW(my_table_set_row);
38 static IRPLIB_UTIL_CHECK(my_table_check);
40 static void test_irplib_image_split(
void);
41 static void test_irplib_dfs_table_convert(
void);
42 static void test_irplib_isnaninf(
void);
43 static void bench_irplib_image_split(
int,
int);
44 static void frameset_sort_test(
int sz);
45 static void test_irplib_aligned_alloc(
void);
63 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
65 test_irplib_isnaninf();
67 test_irplib_dfs_table_convert();
69 test_irplib_image_split();
71 frameset_sort_test(122);
72 frameset_sort_test(127);
74 test_irplib_aligned_alloc();
76 if (cpl_msg_get_level() <= CPL_MSG_INFO) {
77 bench_irplib_image_split(1024, 100);
79 bench_irplib_image_split(64, 1);
82 return cpl_test_end(0);
92 static void test_irplib_isnaninf(
void)
94 double infinity = DBL_MAX * DBL_MAX;
95 double number[] = {17, 0};
108 cpl_test_zero(irplib_isnan(infinity) );
110 cpl_test_zero(irplib_isnan(number[0]) );
111 cpl_test_zero(irplib_isnan(number[1]) );
113 cpl_test( irplib_isinf(infinity) );
115 cpl_test_zero(irplib_isinf(number[0]) );
116 cpl_test_zero(irplib_isinf(number[1]) );
122 static void test_irplib_aligned_alloc(
void)
125 size_t alignment[] = {2, 4, 8, 16, 32, 64, 128, 4096};
126 char zero[100] = {0};
129 for (i = 0; i <
sizeof(alignment)/
sizeof(alignment[0]); i++) {
130 ptr = irplib_aligned_malloc(alignment[i], 100);
131 cpl_test_nonnull(ptr);
132 cpl_test_error(CPL_ERROR_NONE);
133 cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
134 irplib_aligned_free(ptr);
135 cpl_test_error(CPL_ERROR_NONE);
138 ptr = irplib_aligned_malloc(5, 100);
140 irplib_aligned_free(NULL);
142 for (i = 0; i <
sizeof(alignment)/
sizeof(alignment[0]); i++) {
143 ptr = irplib_aligned_calloc(alignment[i], 100, 1);
144 cpl_test_nonnull(ptr);
145 cpl_test_error(CPL_ERROR_NONE);
146 cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
147 cpl_test_eq(memcmp(ptr, zero, 100), 0);
148 irplib_aligned_free(ptr);
149 cpl_test_error(CPL_ERROR_NONE);
152 ptr = irplib_aligned_calloc(5, 100, 1);
154 irplib_aligned_free(NULL);
158 static cpl_boolean my_table_set_row(cpl_table *
self,
161 const cpl_frame * rawframe,
162 const cpl_parameterlist * parlist)
165 cpl_ensure(
self != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
166 cpl_ensure(line != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
167 cpl_ensure(irow >= 0, CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
168 cpl_ensure(rawframe != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
169 cpl_ensure(parlist != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
175 static cpl_error_code my_table_check(cpl_table *
self,
176 const cpl_frameset * useframes,
177 const cpl_parameterlist * parlist)
180 cpl_ensure_code(
self != NULL, CPL_ERROR_NULL_INPUT);
181 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
182 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
184 return CPL_ERROR_NONE;
196 static void test_irplib_dfs_table_convert(
void)
202 NULL, NULL, NULL, NULL, NULL, NULL,
203 NULL, NULL, NULL, my_table_set_row,
206 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
212 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
223 static void bench_irplib_image_split(
int nxy,
int nsplit) {
225 const double th_low = 0.0;
226 const double th_high = 50.0;
227 const double alt_low = th_low - 1.0;
228 const double alt_high = th_high + 1.0;
229 cpl_image * test = cpl_image_new(nxy, nxy, CPL_TYPE_FLOAT);
233 for (i = 0; i < nsplit; i++) {
235 const double time0 = cpl_test_get_cputime();
236 const cpl_error_code error =
238 th_low, CPL_TRUE, th_high, CPL_TRUE,
240 CPL_TRUE, CPL_FALSE, CPL_TRUE);
241 time1 = cpl_test_get_cputime();
243 cpl_test_eq_error(error, CPL_ERROR_NONE);
245 if (time1 > time0) tsum += time1 - time0;
248 cpl_msg_info(cpl_func,
"Time to split with image size %d [ms]: %g", nxy,
251 cpl_image_delete(test);
263 static void test_irplib_image_split(
void) {
265 const double th_low = 0.0;
266 const double th_high = 50.0;
267 const double alt_low = th_low - 1.0;
268 const double alt_high = th_high + 1.0;
270 cpl_image * test = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
271 cpl_image * result = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
276 0.0, CPL_FALSE, 0.0, CPL_FALSE,
278 CPL_FALSE, CPL_FALSE, CPL_FALSE);
279 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
283 th_low, CPL_TRUE, th_high, CPL_TRUE,
285 CPL_TRUE, CPL_FALSE, CPL_TRUE);
286 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
289 th_low, CPL_TRUE, alt_low, CPL_TRUE,
291 CPL_TRUE, CPL_FALSE, CPL_TRUE);
293 cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
296 error = cpl_image_fill_noise_uniform(test, -100.0, 100.0);
297 cpl_test_eq_error(error, CPL_ERROR_NONE);
300 th_low, CPL_TRUE, th_high, CPL_TRUE,
302 CPL_TRUE, CPL_FALSE, CPL_TRUE);
303 cpl_test_eq_error(error, CPL_ERROR_NONE);
305 error = cpl_image_threshold(test, th_low, th_high, alt_low, alt_high);
306 cpl_test_eq_error(error, CPL_ERROR_NONE);
308 error = cpl_image_subtract(result, test);
309 cpl_test_eq_error(error, CPL_ERROR_NONE);
311 cpl_test_leq(cpl_image_get_absflux(result), DBL_EPSILON);
313 cpl_image_delete(test);
314 cpl_image_delete(result);
318 static void frameset_sort_test(
int sz)
321 cpl_frameset * pframeset = cpl_frameset_new();
322 int * idx = cpl_malloc(sz *
sizeof(*idx));
323 double * exptime = cpl_malloc(sz *
sizeof(*exptime));
324 cpl_error_code error;
327 cpl_test_nonnull(pframeset);
329 for (i = 0; i < sz; i++) {
330 cpl_frame * pframe = cpl_frame_new();
331 cpl_propertylist * plist = cpl_propertylist_new();
332 char * filename = cpl_sprintf(
"dummyon%d.fits", i);
333 const double value = (i % 2) > 0 ? i : sz - i - 1;
336 cpl_test_nonnull(pframe);
338 error = cpl_frame_set_filename(pframe, filename);
339 cpl_test_eq_error(error, CPL_ERROR_NONE);
340 error = cpl_frame_set_tag(pframe,
"ON");
341 cpl_test_eq_error(error, CPL_ERROR_NONE);
342 error = cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE);
343 cpl_test_eq_error(error, CPL_ERROR_NONE);
344 error = cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW);
345 cpl_test_eq_error(error, CPL_ERROR_NONE);
347 error = cpl_frameset_insert(pframeset, pframe);
348 cpl_test_eq_error(error, CPL_ERROR_NONE);
349 error = cpl_propertylist_append_double(plist,
"EXPTIME", value);
350 cpl_test_eq_error(error, CPL_ERROR_NONE);
351 error = cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
352 cpl_test_eq_error(error, CPL_ERROR_NONE);
354 cpl_propertylist_delete(plist);
358 error = irplib_frameset_sort(pframeset, idx, exptime);
359 cpl_test_eq_error(error, CPL_ERROR_NONE);
361 for (i = 0; i < sz; i++) {
362 int k = i + 1 - (sz % 2);
364 cpl_test_eq(idx[i], (((i + (sz % 2)) % 2) == 0 ? k : j));
369 cpl_frameset_delete(pframeset);
370 cpl_test_zero(system(
"rm *.fits"));