00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023
00024
00025
00026
00027
00028 #include <irplib_utils.h>
00029 #include <string.h>
00030 #include <float.h>
00031 #include <stdint.h>
00032
00033
00034
00035
00036
00037 static IRPLIB_UTIL_SET_ROW(my_table_set_row);
00038 static IRPLIB_UTIL_CHECK(my_table_check);
00039
00040 static void test_irplib_image_split(void);
00041 static void test_irplib_dfs_table_convert(void);
00042 static void test_irplib_table_read_from_frameset(void);
00043 static void test_irplib_isnaninf(void);
00044 static void bench_irplib_image_split(int, int);
00045 static void frameset_sort_test(int sz);
00046 static void test_irplib_aligned_alloc(void);
00047
00048
00052
00053
00054
00055
00059
00060
00061 int main(void)
00062 {
00063
00064 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00065
00066 test_irplib_isnaninf();
00067
00068 test_irplib_dfs_table_convert();
00069 test_irplib_table_read_from_frameset();
00070
00071 test_irplib_image_split();
00072
00073 frameset_sort_test(122);
00074 frameset_sort_test(127);
00075
00076 test_irplib_aligned_alloc();
00077
00078 if (cpl_msg_get_level() <= CPL_MSG_INFO) {
00079 bench_irplib_image_split(1024, 100);
00080 } else {
00081 bench_irplib_image_split(64, 1);
00082 }
00083
00084 return cpl_test_end(0);
00085 }
00086
00087
00088
00093
00094 static void test_irplib_isnaninf(void)
00095 {
00096 double infinity = DBL_MAX * DBL_MAX;
00097 double number[] = {17, 0};
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 cpl_test_zero(irplib_isnan(infinity) );
00111
00112 cpl_test_zero(irplib_isnan(number[0]) );
00113 cpl_test_zero(irplib_isnan(number[1]) );
00114
00115 cpl_test( irplib_isinf(infinity) );
00116
00117 cpl_test_zero(irplib_isinf(number[0]) );
00118 cpl_test_zero(irplib_isinf(number[1]) );
00119
00120 return;
00121 }
00122
00123
00124 static void test_irplib_aligned_alloc(void)
00125 {
00126 void * ptr = NULL;
00127 size_t alignment[] = {2, 4, 8, 16, 32, 64, 128, 4096};
00128 char zero[100] = {0};
00129 size_t i;
00130
00131 for (i = 0; i < sizeof(alignment)/sizeof(alignment[0]); i++) {
00132 ptr = irplib_aligned_malloc(alignment[i], 100);
00133 cpl_test_nonnull(ptr);
00134 cpl_test_error(CPL_ERROR_NONE);
00135 cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
00136 irplib_aligned_free(ptr);
00137 cpl_test_error(CPL_ERROR_NONE);
00138 }
00139
00140 ptr = irplib_aligned_malloc(5, 100);
00141 cpl_test_null(ptr);
00142 irplib_aligned_free(NULL);
00143
00144 for (i = 0; i < sizeof(alignment)/sizeof(alignment[0]); i++) {
00145 ptr = irplib_aligned_calloc(alignment[i], 100, 1);
00146 cpl_test_nonnull(ptr);
00147 cpl_test_error(CPL_ERROR_NONE);
00148 cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
00149 cpl_test_eq(memcmp(ptr, zero, 100), 0);
00150 irplib_aligned_free(ptr);
00151 cpl_test_error(CPL_ERROR_NONE);
00152 }
00153
00154 ptr = irplib_aligned_calloc(5, 100, 1);
00155 cpl_test_null(ptr);
00156 irplib_aligned_free(NULL);
00157 }
00158
00159
00160 static cpl_boolean my_table_set_row(cpl_table * self,
00161 const char * line,
00162 int irow,
00163 const cpl_frame * rawframe,
00164 const cpl_parameterlist * parlist)
00165 {
00166 char str[32] = "";
00167 double val = 0.0;
00168
00169 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00170 cpl_ensure(line != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00171 cpl_ensure(irow >= 0, CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
00172 cpl_ensure(rawframe != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00173 cpl_ensure(parlist != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00174
00175 cpl_test_assert(sscanf(line, "%31s %16lf", &str[0], &val) != EOF);
00176
00177 cpl_test_assert(cpl_table_set_string(self, "MYLABEL1", (cpl_size)irow, str)
00178 == CPL_ERROR_NONE);
00179 cpl_test_assert(cpl_table_set_double(self, "MYLABEL2", (cpl_size)irow, val)
00180 == CPL_ERROR_NONE);
00181
00182 return CPL_TRUE;
00183
00184 }
00185
00186 static cpl_error_code my_table_check(cpl_table * self,
00187 const cpl_frameset * useframes,
00188 const cpl_parameterlist * parlist)
00189 {
00190
00191 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
00192 cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
00193 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
00194
00195 return CPL_ERROR_NONE;
00196
00197 }
00198
00199
00200
00206
00207 static void test_irplib_dfs_table_convert(void)
00208 {
00209
00210
00211 cpl_error_code error
00212 = irplib_dfs_table_convert(NULL, NULL, NULL, 1024, '#',
00213 NULL, NULL, NULL, NULL, NULL, NULL,
00214 NULL, NULL, NULL, my_table_set_row,
00215 my_table_check);
00216
00217 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00218 }
00219
00220
00221
00227
00228 static void test_irplib_table_read_from_frameset(void)
00229 {
00230 cpl_size initial_failed = cpl_test_get_failed();
00231 cpl_error_code error;
00232 FILE * file;
00233 const char * filename1 = "dummy_input_file_for_irplib_utils_test_1.txt";
00234 const char * filename2 = "dummy_input_file_for_irplib_utils_test_2.txt";
00235 const int expected_rows = 5;
00236 cpl_table * self;
00237 cpl_parameterlist * parlist = cpl_parameterlist_new();
00238 cpl_frameset * useframes = cpl_frameset_new();
00239 cpl_frame * frame;
00240
00241 cpl_test_nonnull(parlist);
00242 cpl_test_nonnull(useframes);
00243
00244 error =
00245 irplib_table_read_from_frameset(NULL, NULL, 1024, '#', NULL,
00246 my_table_set_row);
00247
00248 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00249
00250
00251
00252
00253
00254 file = fopen(filename1, "w");
00255 cpl_test_nonnull(file);
00256 cpl_test(fprintf(file, "abc 1.2\nde 4.3\nfhij 5.6\n") >= 0);
00257 (void) fclose(file);
00258 file = fopen(filename2, "w");
00259 cpl_test_nonnull(file);
00260 cpl_test(fprintf(file, "klm -7.8\nnopq 9\n") >= 0);
00261 (void) fclose(file);
00262
00263 frame = cpl_frame_new();
00264 cpl_test_nonnull(frame);
00265 cpl_test_eq_error(cpl_frame_set_filename(frame, filename1), CPL_ERROR_NONE);
00266 cpl_test_eq_error(cpl_frame_set_tag(frame, "TEXT"), CPL_ERROR_NONE);
00267 cpl_test_eq_error(cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY),
00268 CPL_ERROR_NONE);
00269 cpl_test_eq_error(cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW),
00270 CPL_ERROR_NONE);
00271 cpl_test_eq_error(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_TEMPORARY),
00272 CPL_ERROR_NONE);
00273 cpl_test_eq_error(cpl_frameset_insert(useframes, frame), CPL_ERROR_NONE);
00274 frame = cpl_frame_new();
00275 cpl_test_nonnull(frame);
00276 cpl_test_eq_error(cpl_frame_set_filename(frame, filename2), CPL_ERROR_NONE);
00277 cpl_test_eq_error(cpl_frame_set_tag(frame, "TEXT"), CPL_ERROR_NONE);
00278 cpl_test_eq_error(cpl_frame_set_type(frame, CPL_FRAME_TYPE_ANY),
00279 CPL_ERROR_NONE);
00280 cpl_test_eq_error(cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW),
00281 CPL_ERROR_NONE);
00282 cpl_test_eq_error(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_TEMPORARY),
00283 CPL_ERROR_NONE);
00284 cpl_test_eq_error(cpl_frameset_insert(useframes, frame), CPL_ERROR_NONE);
00285
00286 self = cpl_table_new(expected_rows);
00287 cpl_test_nonnull(self);
00288 cpl_table_new_column(self, "MYLABEL1", CPL_TYPE_STRING);
00289 cpl_table_new_column(self, "MYLABEL2", CPL_TYPE_DOUBLE);
00290 cpl_table_set_column_unit(self, "MYLABEL2", "Some_SI_Unit");
00291
00292 error = irplib_table_read_from_frameset(self, useframes, 1024, '#', parlist,
00293 my_table_set_row);
00294 cpl_test_eq_error(error, CPL_ERROR_NONE);
00295
00296
00297 cpl_test_eq(cpl_table_get_nrow(self), expected_rows);
00298 cpl_test_eq_string(cpl_table_get_string(self, "MYLABEL1", 0), "abc");
00299 cpl_test_eq_string(cpl_table_get_string(self, "MYLABEL1", 1), "de");
00300 cpl_test_eq_string(cpl_table_get_string(self, "MYLABEL1", 2), "fhij");
00301 cpl_test_eq_string(cpl_table_get_string(self, "MYLABEL1", 3), "klm");
00302 cpl_test_eq_string(cpl_table_get_string(self, "MYLABEL1", 4), "nopq");
00303 cpl_test_abs(cpl_table_get_double(self, "MYLABEL2", 0, NULL),
00304 1.2, DBL_EPSILON);
00305 cpl_test_abs(cpl_table_get_double(self, "MYLABEL2", 1, NULL),
00306 4.3, DBL_EPSILON);
00307 cpl_test_abs(cpl_table_get_double(self, "MYLABEL2", 2, NULL),
00308 5.6, DBL_EPSILON);
00309 cpl_test_abs(cpl_table_get_double(self, "MYLABEL2", 3, NULL),
00310 -7.8, DBL_EPSILON);
00311 cpl_test_abs(cpl_table_get_double(self, "MYLABEL2", 4, NULL),
00312 9.0, DBL_EPSILON);
00313
00314 cpl_table_delete(self);
00315 cpl_parameterlist_delete(parlist);
00316 cpl_frameset_delete(useframes);
00317
00318
00319 if (cpl_test_get_failed() == initial_failed) {
00320 (void) remove(filename1);
00321 (void) remove(filename2);
00322 }
00323 }
00324
00325
00326
00332
00333 static void bench_irplib_image_split(int nxy, int nsplit) {
00334
00335 const double th_low = 0.0;
00336 const double th_high = 50.0;
00337 const double alt_low = th_low - 1.0;
00338 const double alt_high = th_high + 1.0;
00339 cpl_image * test = cpl_image_new(nxy, nxy, CPL_TYPE_FLOAT);
00340 double tsum = 0.0;
00341 int i;
00342
00343 for (i = 0; i < nsplit; i++) {
00344 double time1;
00345 const double time0 = cpl_test_get_cputime();
00346 const cpl_error_code error =
00347 irplib_image_split(test, NULL, test, NULL,
00348 th_low, CPL_TRUE, th_high, CPL_TRUE,
00349 alt_low, alt_high,
00350 CPL_TRUE, CPL_FALSE, CPL_TRUE);
00351 time1 = cpl_test_get_cputime();
00352
00353 cpl_test_eq_error(error, CPL_ERROR_NONE);
00354
00355 if (time1 > time0) tsum += time1 - time0;
00356 }
00357
00358 cpl_msg_info(cpl_func,"Time to split with image size %d [ms]: %g", nxy,
00359 1e3*tsum/nsplit);
00360
00361 cpl_image_delete(test);
00362
00363 }
00364
00365
00366
00372
00373 static void test_irplib_image_split(void) {
00374
00375 const double th_low = 0.0;
00376 const double th_high = 50.0;
00377 const double alt_low = th_low - 1.0;
00378 const double alt_high = th_high + 1.0;
00379
00380 cpl_image * test = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
00381 cpl_image * result = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
00382
00383
00384 cpl_error_code error
00385 = irplib_image_split(NULL, test, result, test,
00386 0.0, CPL_FALSE, 0.0, CPL_FALSE,
00387 0.0, 0.0,
00388 CPL_FALSE, CPL_FALSE, CPL_FALSE);
00389 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00390
00391
00392 error = irplib_image_split(test, NULL, NULL, NULL,
00393 th_low, CPL_TRUE, th_high, CPL_TRUE,
00394 alt_low, alt_high,
00395 CPL_TRUE, CPL_FALSE, CPL_TRUE);
00396 cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00397
00398 error = irplib_image_split(test, NULL, result, NULL,
00399 th_low, CPL_TRUE, alt_low, CPL_TRUE,
00400 alt_low, alt_high,
00401 CPL_TRUE, CPL_FALSE, CPL_TRUE);
00402
00403 cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
00404
00405
00406 error = cpl_image_fill_noise_uniform(test, -100.0, 100.0);
00407 cpl_test_eq_error(error, CPL_ERROR_NONE);
00408
00409 error = irplib_image_split(test, NULL, result, NULL,
00410 th_low, CPL_TRUE, th_high, CPL_TRUE,
00411 alt_low, alt_high,
00412 CPL_TRUE, CPL_FALSE, CPL_TRUE);
00413 cpl_test_eq_error(error, CPL_ERROR_NONE);
00414
00415 error = cpl_image_threshold(test, th_low, th_high, alt_low, alt_high);
00416 cpl_test_eq_error(error, CPL_ERROR_NONE);
00417
00418 error = cpl_image_subtract(result, test);
00419 cpl_test_eq_error(error, CPL_ERROR_NONE);
00420
00421 cpl_test_leq(cpl_image_get_absflux(result), DBL_EPSILON);
00422
00423 cpl_image_delete(test);
00424 cpl_image_delete(result);
00425
00426 }
00427
00428 static void frameset_sort_test(int sz)
00429 {
00430
00431 cpl_frameset * pframeset = cpl_frameset_new();
00432 int * idx = cpl_malloc(sz * sizeof(*idx));
00433 double * exptime = cpl_malloc(sz * sizeof(*exptime));
00434 cpl_error_code error;
00435 int i;
00436
00437 cpl_test_nonnull(pframeset);
00438
00439 for (i = 0; i < sz; i++) {
00440 cpl_frame * pframe = cpl_frame_new();
00441 cpl_propertylist * plist = cpl_propertylist_new();
00442 char * filename = cpl_sprintf("dummyon%d.fits", i);
00443 const double value = (i % 2) > 0 ? i : sz - i - 1;
00444
00445
00446 cpl_test_nonnull(pframe);
00447
00448 error = cpl_frame_set_filename(pframe, filename);
00449 cpl_test_eq_error(error, CPL_ERROR_NONE);
00450 error = cpl_frame_set_tag(pframe, "ON");
00451 cpl_test_eq_error(error, CPL_ERROR_NONE);
00452 error = cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE);
00453 cpl_test_eq_error(error, CPL_ERROR_NONE);
00454 error = cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW);
00455 cpl_test_eq_error(error, CPL_ERROR_NONE);
00456
00457 error = cpl_frameset_insert(pframeset, pframe);
00458 cpl_test_eq_error(error, CPL_ERROR_NONE);
00459 error = cpl_propertylist_append_double(plist, "EXPTIME", value);
00460 cpl_test_eq_error(error, CPL_ERROR_NONE);
00461 error = cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
00462 cpl_test_eq_error(error, CPL_ERROR_NONE);
00463
00464 cpl_propertylist_delete(plist);
00465 cpl_free(filename);
00466 }
00467
00468 error = irplib_frameset_sort(pframeset, idx, exptime);
00469 cpl_test_eq_error(error, CPL_ERROR_NONE);
00470
00471 for (i = 0; i < sz; i++) {
00472 int k = i + 1 - (sz % 2);
00473 int j = sz -i - 1 ;
00474 cpl_test_eq(idx[i], (((i + (sz % 2)) % 2) == 0 ? k : j));
00475 }
00476
00477 cpl_free(idx);
00478 cpl_free(exptime);
00479 cpl_frameset_delete(pframeset);
00480 cpl_test_zero(system("rm dummyon*.fits"));
00481 }