irplib_utils-test.c

00001 /*                                                                            *
00002  *   This file is part of the ESO IRPLIB package                              *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                    *
00004  *                                                                            *
00005  *   This library is free software; you can redistribute it and/or modify     *
00006  *   it under the terms of the GNU General Public License as published by     *
00007  *   the Free Software Foundation; either version 2 of the License, or        *
00008  *   (at your option) any later version.                                      *
00009  *                                                                            *
00010  *   This program is distributed in the hope that it will be useful,          *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00013  *   GNU General Public License for more details.                             *
00014  *                                                                            *
00015  *   You should have received a copy of the GNU General Public License        *
00016  *   along with this program; if not, write to the Free Software              *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA     *
00018  *                                                                            */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #  include <config.h>
00022 #endif
00023 
00024 /*-----------------------------------------------------------------------------
00025                                 Includes
00026  -----------------------------------------------------------------------------*/
00027 
00028 #include <irplib_utils.h>
00029 #include <string.h>
00030 #include <float.h>
00031 #include <stdint.h>
00032 
00033 /*-----------------------------------------------------------------------------
00034                                    Function prototypes
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_isnaninf(void);
00043 static void bench_irplib_image_split(int, int);
00044 static void frameset_sort_test(int sz);
00045 static void test_irplib_aligned_alloc(void);
00046 
00047 /*----------------------------------------------------------------------------*/
00051 /*----------------------------------------------------------------------------*/
00052 
00053 
00054 /*----------------------------------------------------------------------------*/
00058 /*----------------------------------------------------------------------------*/
00059 
00060 int main(void)
00061 {
00062     /* Initialize CPL for unit testing */
00063     cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00064 
00065     test_irplib_isnaninf();
00066 
00067     test_irplib_dfs_table_convert();
00068 
00069     test_irplib_image_split();
00070 
00071     frameset_sort_test(122); /* test even */
00072     frameset_sort_test(127); /* test odd  */
00073 
00074     test_irplib_aligned_alloc();
00075 
00076     if (cpl_msg_get_level() <= CPL_MSG_INFO) {
00077         bench_irplib_image_split(1024, 100);
00078     } else {
00079         bench_irplib_image_split(64, 1);
00080     }
00081 
00082     return cpl_test_end(0);
00083 }
00084 
00085 
00086 /*----------------------------------------------------------------------------*/
00091 /*----------------------------------------------------------------------------*/
00092 static void test_irplib_isnaninf(void)
00093 {
00094     double infinity = DBL_MAX * DBL_MAX;
00095     double number[] = {17, 0};
00096 
00097     /* The computation  oo/oo  must result in NaN according to
00098        the IEEE 754 standard. However, some GCC 4.x versions erroneously
00099        optimize this to 1.
00100 
00101        Alternatively, a NaN could be produced using a IEEE 754 defined bit
00102        pattern. But that is likely to depend on the machine's word size.
00103        Therefore this test is disabled.
00104 
00105        double not_a_number = infinity / infinity;
00106     */
00107 
00108     cpl_test_zero(irplib_isnan(infinity) );
00109     /* cpl_test(  irplib_isnan(not_a_number) ); */
00110     cpl_test_zero(irplib_isnan(number[0]) );
00111     cpl_test_zero(irplib_isnan(number[1]) );
00112 
00113     cpl_test(  irplib_isinf(infinity) );
00114     /* cpl_test_zero(irplib_isinf(not_a_number) ); */
00115     cpl_test_zero(irplib_isinf(number[0]) );
00116     cpl_test_zero(irplib_isinf(number[1]) );
00117 
00118     return;
00119 }
00120 
00121 
00122 static void test_irplib_aligned_alloc(void)
00123 {
00124     void * ptr = NULL;
00125     size_t alignment[] = {2, 4, 8, 16, 32, 64, 128, 4096};
00126     char zero[100] = {0};
00127     size_t i;
00128 
00129     for (i = 0; i < sizeof(alignment)/sizeof(alignment[0]); i++) {
00130         ptr = irplib_aligned_malloc(alignment[i], 100);
00131         cpl_test_nonnull(ptr);
00132         cpl_test_error(CPL_ERROR_NONE);
00133         cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
00134         irplib_aligned_free(ptr);
00135         cpl_test_error(CPL_ERROR_NONE);
00136     }
00137     /* invalid alignment */
00138     ptr = irplib_aligned_malloc(5, 100);
00139     cpl_test_null(ptr);
00140     irplib_aligned_free(NULL);
00141 
00142     for (i = 0; i < sizeof(alignment)/sizeof(alignment[0]); i++) {
00143         ptr = irplib_aligned_calloc(alignment[i], 100, 1);
00144         cpl_test_nonnull(ptr);
00145         cpl_test_error(CPL_ERROR_NONE);
00146         cpl_test_eq(((intptr_t)ptr % alignment[i]), 0);
00147         cpl_test_eq(memcmp(ptr, zero, 100), 0);
00148         irplib_aligned_free(ptr);
00149         cpl_test_error(CPL_ERROR_NONE);
00150     }
00151     /* invalid alignment */
00152     ptr = irplib_aligned_calloc(5, 100, 1);
00153     cpl_test_null(ptr);
00154     irplib_aligned_free(NULL);
00155 }
00156 
00157 
00158 static cpl_boolean my_table_set_row(cpl_table * self,
00159                                     const char * line,
00160                                     int irow,
00161                                     const cpl_frame * rawframe,
00162                                     const cpl_parameterlist * parlist)
00163 {
00164 
00165     cpl_ensure(self     != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00166     cpl_ensure(line     != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00167     cpl_ensure(irow     >= 0,    CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
00168     cpl_ensure(rawframe != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00169     cpl_ensure(parlist  != NULL, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00170 
00171     return CPL_TRUE;
00172 
00173 }
00174 
00175 static cpl_error_code my_table_check(cpl_table * self,
00176                                      const cpl_frameset * useframes,
00177                                      const cpl_parameterlist * parlist)
00178 {
00179 
00180     cpl_ensure_code(self      != NULL, CPL_ERROR_NULL_INPUT);
00181     cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
00182     cpl_ensure_code(parlist   != NULL, CPL_ERROR_NULL_INPUT);
00183 
00184     return CPL_ERROR_NONE;
00185 
00186 }
00187 
00188 
00189 /*----------------------------------------------------------------------------*/
00195 /*----------------------------------------------------------------------------*/
00196 static void test_irplib_dfs_table_convert(void)
00197 {
00198 
00199     /* FIXME: Room for improvement... */
00200     cpl_error_code error
00201         = irplib_dfs_table_convert(NULL, NULL, NULL, 1024, '#',
00202                                    NULL, NULL, NULL, NULL, NULL, NULL,
00203                                    NULL, NULL, NULL, my_table_set_row,
00204                                    my_table_check);
00205 
00206     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00207 
00208     error =
00209         irplib_table_read_from_frameset(NULL, NULL, 1024, '#', NULL,
00210                                         my_table_set_row);
00211 
00212     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00213 }
00214 
00215 
00216 /*----------------------------------------------------------------------------*/
00222 /*----------------------------------------------------------------------------*/
00223 static void bench_irplib_image_split(int nxy, int nsplit) {
00224 
00225     const double th_low   =  0.0;
00226     const double th_high  = 50.0;
00227     const double alt_low  = th_low  - 1.0;
00228     const double alt_high = th_high + 1.0;
00229     cpl_image  * test     = cpl_image_new(nxy, nxy, CPL_TYPE_FLOAT);
00230     double       tsum = 0.0;
00231     int          i;
00232 
00233     for (i = 0; i < nsplit; i++) {
00234         double time1;
00235         const double time0 = cpl_test_get_cputime();
00236         const cpl_error_code error =
00237             irplib_image_split(test, NULL, test, NULL,
00238                                th_low,  CPL_TRUE, th_high, CPL_TRUE,
00239                                alt_low, alt_high,
00240                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00241         time1 = cpl_test_get_cputime();
00242 
00243         cpl_test_eq_error(error, CPL_ERROR_NONE);
00244 
00245         if (time1 > time0) tsum += time1 - time0;
00246     }
00247 
00248     cpl_msg_info(cpl_func,"Time to split with image size %d [ms]: %g", nxy,
00249                  1e3*tsum/nsplit);
00250 
00251     cpl_image_delete(test);
00252 
00253 }
00254 
00255 
00256 /*----------------------------------------------------------------------------*/
00262 /*----------------------------------------------------------------------------*/
00263 static void test_irplib_image_split(void) {
00264 
00265     const double th_low   =  0.0;
00266     const double th_high  = 50.0;
00267     const double alt_low  = th_low  - 1.0;
00268     const double alt_high = th_high + 1.0;
00269 
00270     cpl_image * test   = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
00271     cpl_image * result = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
00272 
00273     /* Various error conditions */
00274     cpl_error_code error
00275         = irplib_image_split(NULL, test, result, test,
00276                              0.0, CPL_FALSE, 0.0, CPL_FALSE,
00277                              0.0, 0.0,
00278                              CPL_FALSE, CPL_FALSE, CPL_FALSE);
00279     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00280 
00281 
00282     error = irplib_image_split(test, NULL, NULL, NULL,
00283                                th_low,  CPL_TRUE, th_high, CPL_TRUE,
00284                                alt_low, alt_high,
00285                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00286     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00287 
00288     error = irplib_image_split(test, NULL, result, NULL,
00289                                th_low,  CPL_TRUE, alt_low, CPL_TRUE,
00290                                alt_low, alt_high,
00291                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00292 
00293     cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
00294 
00295     /* Verify against cpl_image_threshold() */
00296     error = cpl_image_fill_noise_uniform(test, -100.0, 100.0);
00297     cpl_test_eq_error(error, CPL_ERROR_NONE);
00298 
00299     error = irplib_image_split(test, NULL, result, NULL,
00300                                th_low,  CPL_TRUE, th_high, CPL_TRUE,
00301                                alt_low, alt_high,
00302                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00303     cpl_test_eq_error(error, CPL_ERROR_NONE);
00304 
00305     error = cpl_image_threshold(test, th_low, th_high, alt_low, alt_high);
00306     cpl_test_eq_error(error, CPL_ERROR_NONE);
00307 
00308     error = cpl_image_subtract(result, test);
00309     cpl_test_eq_error(error, CPL_ERROR_NONE);
00310 
00311     cpl_test_leq(cpl_image_get_absflux(result), DBL_EPSILON);
00312 
00313     cpl_image_delete(test);
00314     cpl_image_delete(result);
00315 
00316 }
00317 
00318 static void frameset_sort_test(int sz)
00319 {
00320     /* 1. create a test frameset - each frame should contain EXPTIME property */
00321     cpl_frameset * pframeset = cpl_frameset_new();
00322     int          * idx       = cpl_malloc(sz * sizeof(*idx));
00323     double       * exptime   = cpl_malloc(sz * sizeof(*exptime));
00324     cpl_error_code error;
00325     int            i;
00326 
00327     cpl_test_nonnull(pframeset);
00328 
00329     for (i = 0; i < sz; i++) {
00330         cpl_frame        * pframe   = cpl_frame_new();
00331         cpl_propertylist * plist    = cpl_propertylist_new();
00332         char             * filename = cpl_sprintf("dummyon%d.fits", i);
00333         const double       value    = (i % 2) > 0 ? i : sz - i - 1;
00334 
00335 
00336         cpl_test_nonnull(pframe);
00337         /* assign exptime; */
00338         error = cpl_frame_set_filename(pframe, filename);
00339         cpl_test_eq_error(error, CPL_ERROR_NONE);
00340         error = cpl_frame_set_tag(pframe, "ON");
00341         cpl_test_eq_error(error, CPL_ERROR_NONE);
00342         error = cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE);
00343         cpl_test_eq_error(error, CPL_ERROR_NONE);
00344         error = cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW);
00345         cpl_test_eq_error(error, CPL_ERROR_NONE);
00346 
00347         error = cpl_frameset_insert(pframeset, pframe);
00348         cpl_test_eq_error(error, CPL_ERROR_NONE);
00349         error = cpl_propertylist_append_double(plist, "EXPTIME", value);
00350         cpl_test_eq_error(error, CPL_ERROR_NONE);
00351         error = cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
00352         cpl_test_eq_error(error, CPL_ERROR_NONE);
00353 
00354         cpl_propertylist_delete(plist);
00355         cpl_free(filename);
00356     }
00357 
00358     error = irplib_frameset_sort(pframeset, idx, exptime);
00359     cpl_test_eq_error(error, CPL_ERROR_NONE);
00360 
00361     for (i = 0; i < sz; i++) {
00362         int k = i + 1 - (sz % 2);
00363         int j = sz -i - 1 ;
00364         cpl_test_eq(idx[i], (((i + (sz % 2)) % 2)  == 0 ? k : j));
00365     }
00366 
00367     cpl_free(idx);
00368     cpl_free(exptime);
00369     cpl_frameset_delete(pframeset);
00370     cpl_test_zero(system("rm *.fits"));
00371 }
Generated on Mon Feb 17 15:01:44 2014 for NACO Pipeline Reference Manual by  doxygen 1.6.3