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 
00032 /*-----------------------------------------------------------------------------
00033                                    Function prototypes
00034  -----------------------------------------------------------------------------*/
00035 
00036 static IRPLIB_UTIL_SET_ROW(my_table_set_row);
00037 static IRPLIB_UTIL_CHECK(my_table_check);
00038 
00039 static void test_irplib_image_split(void);
00040 static void test_irplib_dfs_table_convert(void);
00041 static void test_irplib_isnaninf(void);
00042 static void bench_irplib_image_split(int, int);
00043 static void frameset_sort_test(int sz);
00044 
00045 /*----------------------------------------------------------------------------*/
00049 /*----------------------------------------------------------------------------*/
00050 
00051 
00052 /*----------------------------------------------------------------------------*/
00056 /*----------------------------------------------------------------------------*/
00057 
00058 int main(void)
00059 {
00060     /* Initialize CPL for unit testing */
00061     cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00062 
00063     test_irplib_isnaninf();
00064 
00065     test_irplib_dfs_table_convert();
00066 
00067     test_irplib_image_split();
00068 
00069     frameset_sort_test(122); /* test even */
00070     frameset_sort_test(127); /* test odd  */
00071 
00072     if (cpl_msg_get_level() <= CPL_MSG_INFO) {
00073         bench_irplib_image_split(1024, 100);
00074     } else {
00075         bench_irplib_image_split(64, 1);
00076     }
00077 
00078     return cpl_test_end(0);
00079 }
00080 
00081 
00082 /*----------------------------------------------------------------------------*/
00087 /*----------------------------------------------------------------------------*/
00088 static void test_irplib_isnaninf(void)
00089 {
00090     double infinity = DBL_MAX * DBL_MAX;
00091     double number[] = {17, 0};
00092 
00093     /* The computation  oo/oo  must result in NaN according to
00094        the IEEE 754 standard. However, some GCC 4.x versions erroneously
00095        optimize this to 1.
00096 
00097        Alternatively, a NaN could be produced using a IEEE 754 defined bit
00098        pattern. But that is likely to depend on the machine's word size.
00099        Therefore this test is disabled.
00100 
00101        double not_a_number = infinity / infinity;
00102     */
00103 
00104     cpl_test_zero(irplib_isnan(infinity) );
00105     /* cpl_test(  irplib_isnan(not_a_number) ); */
00106     cpl_test_zero(irplib_isnan(number[0]) );
00107     cpl_test_zero(irplib_isnan(number[1]) );
00108 
00109     cpl_test(  irplib_isinf(infinity) );
00110     /* cpl_test_zero(irplib_isinf(not_a_number) ); */
00111     cpl_test_zero(irplib_isinf(number[0]) );
00112     cpl_test_zero(irplib_isinf(number[1]) );
00113 
00114     return;
00115 }
00116 
00117 
00118 static cpl_boolean my_table_set_row(cpl_table * self,
00119                                     const char * line,
00120                                     int irow,
00121                                     const cpl_frame * rawframe,
00122                                     const cpl_parameterlist * parlist)
00123 {
00124 
00125     cpl_ensure_code(self     != NULL, CPL_ERROR_NULL_INPUT);
00126     cpl_ensure_code(line     != NULL, CPL_ERROR_NULL_INPUT);
00127     cpl_ensure_code(irow     >= 0,    CPL_ERROR_ILLEGAL_INPUT);
00128     cpl_ensure_code(rawframe != NULL, CPL_ERROR_NULL_INPUT);
00129     cpl_ensure_code(parlist  != NULL, CPL_ERROR_NULL_INPUT);
00130 
00131     return CPL_ERROR_NONE;
00132 
00133 }
00134 
00135 static cpl_error_code my_table_check(cpl_table * self,
00136                                      const cpl_frameset * useframes,
00137                                      const cpl_parameterlist * parlist)
00138 {
00139 
00140     cpl_ensure_code(self      != NULL, CPL_ERROR_NULL_INPUT);
00141     cpl_ensure_code(useframes != NULL, CPL_ERROR_NULL_INPUT);
00142     cpl_ensure_code(parlist   != NULL, CPL_ERROR_NULL_INPUT);
00143 
00144     return CPL_ERROR_NONE;
00145 
00146 }
00147 
00148 
00149 /*----------------------------------------------------------------------------*/
00155 /*----------------------------------------------------------------------------*/
00156 static void test_irplib_dfs_table_convert(void)
00157 {
00158 
00159     /* FIXME: Room for improvement... */
00160     cpl_error_code error
00161         = irplib_dfs_table_convert(NULL, NULL, NULL, 1024, '#',
00162                                    NULL, NULL, NULL, NULL, NULL, NULL,
00163                                    NULL, NULL, NULL, my_table_set_row,
00164                                    my_table_check);
00165 
00166     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00167 
00168     error =
00169         irplib_table_read_from_frameset(NULL, NULL, 1024, '#', NULL,
00170                                         my_table_set_row);
00171 
00172     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00173 }
00174 
00175 
00176 /*----------------------------------------------------------------------------*/
00182 /*----------------------------------------------------------------------------*/
00183 static void bench_irplib_image_split(int nxy, int nsplit) {
00184 
00185     const double th_low   =  0.0;
00186     const double th_high  = 50.0;
00187     const double alt_low  = th_low  - 1.0;
00188     const double alt_high = th_high + 1.0;
00189     cpl_image  * test     = cpl_image_new(nxy, nxy, CPL_TYPE_FLOAT);
00190     double       tsum = 0.0;
00191     int          i;
00192 
00193     for (i = 0; i < nsplit; i++) {
00194         double time1;
00195         const double time0 = cpl_test_get_cputime();
00196         const cpl_error_code error =
00197             irplib_image_split(test, NULL, test, NULL,
00198                                th_low,  CPL_TRUE, th_high, CPL_TRUE,
00199                                alt_low, alt_high,
00200                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00201         time1 = cpl_test_get_cputime();
00202 
00203         cpl_test_eq_error(error, CPL_ERROR_NONE);
00204 
00205         if (time1 > time0) tsum += time1 - time0;
00206     }
00207 
00208     cpl_msg_info(cpl_func,"Time to split with image size %d [ms]: %g", nxy,
00209                  1e3*tsum/nsplit);
00210 
00211     cpl_image_delete(test);
00212 
00213 }
00214 
00215 
00216 /*----------------------------------------------------------------------------*/
00222 /*----------------------------------------------------------------------------*/
00223 static void test_irplib_image_split(void) {
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 
00230     cpl_image * test   = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
00231     cpl_image * result = cpl_image_new(100, 100, CPL_TYPE_DOUBLE);
00232 
00233     /* Various error conditions */
00234     cpl_error_code error
00235         = irplib_image_split(NULL, test, result, test,
00236                              0.0, CPL_FALSE, 0.0, CPL_FALSE,
00237                              0.0, 0.0,
00238                              CPL_FALSE, CPL_FALSE, CPL_FALSE);
00239     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00240 
00241 
00242     error = irplib_image_split(test, NULL, NULL, NULL,
00243                                th_low,  CPL_TRUE, th_high, CPL_TRUE,
00244                                alt_low, alt_high,
00245                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00246     cpl_test_eq_error(error, CPL_ERROR_NULL_INPUT);
00247 
00248     error = irplib_image_split(test, NULL, result, NULL,
00249                                th_low,  CPL_TRUE, alt_low, CPL_TRUE,
00250                                alt_low, alt_high,
00251                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00252 
00253     cpl_test_eq_error(error, CPL_ERROR_ILLEGAL_INPUT);
00254 
00255     /* Verify against cpl_image_threshold() */
00256     error = cpl_image_fill_noise_uniform(test, -100.0, 100.0);
00257     cpl_test_eq_error(error, CPL_ERROR_NONE);
00258 
00259     error = irplib_image_split(test, NULL, result, NULL,
00260                                th_low,  CPL_TRUE, th_high, CPL_TRUE,
00261                                alt_low, alt_high,
00262                                CPL_TRUE, CPL_FALSE, CPL_TRUE);
00263     cpl_test_eq_error(error, CPL_ERROR_NONE);
00264 
00265     error = cpl_image_threshold(test, th_low, th_high, alt_low, alt_high);
00266     cpl_test_eq_error(error, CPL_ERROR_NONE);
00267 
00268     error = cpl_image_subtract(result, test);
00269     cpl_test_eq_error(error, CPL_ERROR_NONE);
00270 
00271     cpl_test_leq(cpl_image_get_absflux(result), DBL_EPSILON);
00272 
00273     cpl_image_delete(test);
00274     cpl_image_delete(result);
00275 
00276 }
00277 
00278 static void frameset_sort_test(int sz)
00279 {
00280     /* 1. create a test frameset - each frame should contain EXPTIME property */
00281     cpl_frameset * pframeset = cpl_frameset_new();
00282     int          * idx       = cpl_malloc(sz * sizeof(*idx));
00283     double       * exptime   = cpl_malloc(sz * sizeof(*exptime));
00284     cpl_error_code error;
00285     int            i;
00286 
00287     cpl_test_nonnull(pframeset);
00288 
00289     for (i = 0; i < sz; i++) {
00290         cpl_frame        * pframe   = cpl_frame_new();
00291         cpl_propertylist * plist    = cpl_propertylist_new();
00292         char             * filename = cpl_sprintf("dummyon%d.fits", i);
00293         const double       value    = (i % 2) > 0 ? i : sz - i - 1;
00294 
00295 
00296         cpl_test_nonnull(pframe);
00297         /* assign exptime; */
00298         error = cpl_frame_set_filename(pframe, filename);
00299         cpl_test_eq_error(error, CPL_ERROR_NONE);
00300         error = cpl_frame_set_tag(pframe, "ON");
00301         cpl_test_eq_error(error, CPL_ERROR_NONE);
00302         error = cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE);
00303         cpl_test_eq_error(error, CPL_ERROR_NONE);
00304         error = cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW);
00305         cpl_test_eq_error(error, CPL_ERROR_NONE);
00306 
00307         error = cpl_frameset_insert(pframeset, pframe);
00308         cpl_test_eq_error(error, CPL_ERROR_NONE);
00309         error = cpl_propertylist_append_double(plist, "EXPTIME", value);
00310         cpl_test_eq_error(error, CPL_ERROR_NONE);
00311         error = cpl_propertylist_save(plist, filename, CPL_IO_CREATE);
00312         cpl_test_eq_error(error, CPL_ERROR_NONE);
00313 
00314         cpl_propertylist_delete(plist);
00315         cpl_free(filename);
00316     }
00317 
00318     error = irplib_frameset_sort(pframeset, idx, exptime);
00319     cpl_test_eq_error(error, CPL_ERROR_NONE);
00320 
00321     for (i = 0; i < sz; i++) {
00322         int k = i + 1 - (sz % 2);
00323         int j = sz -i - 1 ;
00324         cpl_test_eq(idx[i], (((i + (sz % 2)) % 2)  == 0 ? k : j));
00325     }
00326 
00327     cpl_free(idx);
00328     cpl_free(exptime);
00329     cpl_frameset_delete(pframeset);
00330     cpl_test_zero(system("rm *.fits"));
00331 }

Generated on Mon Feb 6 15:23:49 2012 for VISIR Pipeline Reference Manual by  doxygen 1.5.8