uves_propertylist-test.c

00001 /* $Id: uves_propertylist-test.c,v 1.3 2009/06/05 05:49:02 amodigli Exp $
00002  *
00003  * This file is part of the ESO Common Pipeline Library
00004  * Copyright (C) 2001-2005 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2009/06/05 05:49:02 $
00024  * $Revision: 1.3 $
00025  * $Name: uves-5_0_0 $
00026  */
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #undef CX_DISABLE_ASSERT
00032 #undef CX_LOG_DOMAIN
00033 
00034 
00035 #include <uves_propertylist.h>
00036 
00037 #include <cpl_test.h>
00038 //#include "cpl_init.h"
00039 //#include "cpl_error.h"
00040 //#include "uves_propertylist_impl.h"
00041 
00042 #include <cpl.h>
00043 #include <cxmemory.h>
00044 #include <cxmessages.h>
00045 #include <qfits.h>
00046 
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <math.h>
00051 
00052 #define FLT_EPS  1.0e-6
00053 #define DBL_EPS  1.0e-14
00054 
00055 
00056 static void
00057 test_property_dump(cpl_property *property)
00058 {
00059 
00060     const cxchar *name = cpl_property_get_name(property);
00061     const cxchar *comment = cpl_property_get_comment(property);
00062 
00063     cxchar c;
00064 
00065     long size = cpl_property_get_size(property);
00066 
00067     cpl_type type = cpl_property_get_type(property);
00068 
00069 
00070     fprintf(stderr, "Property at address %p\n", (void *)property);
00071     fprintf(stderr, "\tname   : %p '%s'\n", (void *)name, name);
00072     fprintf(stderr, "\tcomment: %p '%s'\n", (void *)comment, comment);
00073     fprintf(stderr, "\ttype   : %#09x\n", type);
00074     fprintf(stderr, "\tsize   : %ld\n", size);
00075     fprintf(stderr, "\tvalue  : ");
00076 
00077 
00078     switch (type) {
00079     case CPL_TYPE_CHAR:
00080         c = cpl_property_get_char(property);
00081         if (!c)
00082             fprintf(stderr, "''");
00083         else
00084             fprintf(stderr, "'%c'", c);
00085         break;
00086 
00087     case CPL_TYPE_BOOL:
00088         fprintf(stderr, "%d", cpl_property_get_bool(property));
00089         break;
00090 
00091     case CPL_TYPE_INT:
00092         fprintf(stderr, "%d", cpl_property_get_int(property));
00093         break;
00094 
00095     case CPL_TYPE_LONG:
00096         fprintf(stderr, "%ld", cpl_property_get_long(property));
00097         break;
00098 
00099     case CPL_TYPE_FLOAT:
00100         fprintf(stderr, "%.7g", cpl_property_get_float(property));
00101         break;
00102 
00103     case CPL_TYPE_DOUBLE:
00104         fprintf(stderr, "%.15g", cpl_property_get_double(property));
00105         break;
00106 
00107     case CPL_TYPE_STRING:
00108         fprintf(stderr, "'%s'", cpl_property_get_string(property));
00109         break;
00110 
00111     default:
00112         fprintf(stderr, "unknown.");
00113         break;
00114 
00115     }
00116 
00117     fprintf(stderr, "\n");
00118 
00119     return;
00120 
00121 }
00122 
00123 
00124 static void
00125 test_plist_dump(uves_propertylist *plist)
00126 {
00127 
00128     cxlong i;
00129     cxlong sz = uves_propertylist_get_size(plist);
00130 
00131 
00132     fprintf(stderr, "Property list at address %p:\n", (void *) plist);
00133 
00134     for (i = 0; i < sz; i++) {
00135         cpl_property *p = uves_propertylist_get(plist, i);
00136         test_property_dump(p);
00137     }
00138 
00139     return;
00140 
00141 }
00142 
00143 static int test_main(void)
00144 {
00145 
00146     const cxchar *keys[] = {
00147         "a", "b", "c", "d", "e", "f", "g",
00148         "A", "B", "C", "D", "E", "F", "G"
00149     };
00150 
00151     const cxchar *comments[] = {
00152         "A character value",
00153         "A boolean value",
00154         "A integer value",
00155         "A long integer value",
00156         "A floating point number",
00157         "A double precision number",
00158         "A string value",
00159     };
00160 
00161     cpl_type types[] = {
00162         CPL_TYPE_CHAR,
00163         CPL_TYPE_BOOL,
00164         CPL_TYPE_INT,
00165         CPL_TYPE_LONG,
00166         CPL_TYPE_FLOAT,
00167         CPL_TYPE_DOUBLE,
00168         CPL_TYPE_STRING
00169     };
00170 
00171     cxlong i;
00172 
00173     cxfloat fval[] = {-1.23456789, 0.};
00174     cxdouble dval[] = {-1.23456789, 0.};
00175 
00176     uves_propertylist *plist, *_plist;
00177 
00178     qfits_header *header, *_header;
00179 
00180     struct fcard {
00181         const cxchar *key;
00182         const cxchar *val;
00183         const cxchar *com;
00184         cpl_type type;
00185     };
00186 
00187     struct fcard hdr[] = {
00188         {"SIMPLE", "T",
00189          "Standard FITS format (NOST-100.0)",
00190          CPL_TYPE_BOOL},
00191         {"BITPIX", "16",
00192          "# of bits storing pix values",
00193          CPL_TYPE_INT},
00194         {"NAXIS", "2",
00195          "# of axes in frame",
00196          CPL_TYPE_INT},
00197         {"NAXIS1", "2148",
00198          "# pixels/axis",
00199          CPL_TYPE_INT},
00200         {"NAXIS2", "2340",
00201          "# pixels/axis",
00202          CPL_TYPE_INT},
00203         {"ORIGIN", "ESO",
00204          "European Southern Observatory",
00205          CPL_TYPE_STRING},
00206         {"DATE", "2002-03-08T04:27:21.420",
00207          "Date this file was written (dd/mm/yyyy)",
00208          CPL_TYPE_STRING},
00209         {"MJD-OBS", "52341.17813019",
00210          "Obs start 2002-03-08T04:16:30.448",
00211          CPL_TYPE_DOUBLE},
00212         {"DATE-OBS", "2002-03-08T04:16:30.448",
00213          "Date of observation",
00214          CPL_TYPE_STRING},
00215         {"EXPTIME", "600.000",
00216          "Total integration time. 00:10:00.000",
00217          CPL_TYPE_DOUBLE},
00218         {"TELESCOP", "VLT",
00219          "ESO <TEL>",
00220          CPL_TYPE_STRING},
00221         {"RA", "181.41734",
00222          "12:05:40.1 RA (J2000) pointing",
00223          CPL_TYPE_DOUBLE},
00224         {"DEC", "-7.65555",
00225          "-07:39:19.9  DEC (J2000) pointing",
00226          CPL_TYPE_DOUBLE},
00227         {"EQUINOX", "2000.",
00228          "Standard FK5 (years)",
00229          CPL_TYPE_DOUBLE},
00230         {"RADECSYS", "FK5",
00231          "Coordinate reference frame",
00232          CPL_TYPE_STRING},
00233         {"LST", "38309.370",
00234          "10:38:29.370 LST at start",
00235          CPL_TYPE_DOUBLE},
00236         {"UTC", "15438.000",
00237          "04:17:18.000 UT at start",
00238          CPL_TYPE_DOUBLE},
00239         {"OBSERVER", "UNKNOWN",
00240          "Name of observer",
00241          CPL_TYPE_STRING},
00242         {"INSTRUME", "UNKNOWN",
00243          "Instrument used",
00244          CPL_TYPE_STRING},
00245         {"PI-COI", "'555555555'",
00246          "Name of PI and COI",
00247          CPL_TYPE_STRING},
00248         {"OBJECT", "None",
00249          "Original target",
00250          CPL_TYPE_STRING},
00251         {"PCOUNT", "0",
00252          "Number of parameters per group",
00253          CPL_TYPE_INT},
00254         {"GCOUNT", "1",
00255          "Number of groups",
00256          CPL_TYPE_INT},
00257         {"CRVAL1", "181.41734",
00258          "12:05:40.1, RA at ref pixel",
00259          CPL_TYPE_DOUBLE},
00260         {"CRPIX1", "2341.8585366",
00261          "Reference pixel in X",
00262          CPL_TYPE_DOUBLE},
00263         {"CDELT1", "0.20500000",
00264          "SS arcsec per pixel in RA",
00265          CPL_TYPE_DOUBLE},
00266         {"CTYPE1", "RA---TAN",
00267          "pixel coordinate system",
00268          CPL_TYPE_STRING},
00269         {"CRVAL2", "-7.65555",
00270          "-07:39:19.9, DEC at ref pixel",
00271          CPL_TYPE_DOUBLE},
00272         {"CRPIX2", "2487.8585366",
00273          "Reference pixel in Y",
00274          CPL_TYPE_DOUBLE},
00275         {"CDELT2", "0.20500000",
00276          "SS arcsec per pixel in DEC",
00277          CPL_TYPE_DOUBLE},
00278         {"CTYPE2", "DEC--TAN",
00279          "pixel coordinate system",
00280          CPL_TYPE_STRING},
00281         {"BSCALE", "1.0",
00282          "pixel=FITS*BSCALE+BZERO",
00283          CPL_TYPE_DOUBLE},
00284         {"BZERO", "32768.0",
00285          "pixel=FITS*BSCALE+BZERO",
00286          CPL_TYPE_DOUBLE},
00287         {"CD1_1", "0.000057",
00288          "Translation matrix element",
00289          CPL_TYPE_DOUBLE},
00290         {"CD1_2", "0.000000",
00291          "Translation matrix element",
00292          CPL_TYPE_DOUBLE},
00293         {"CD2_1", "0.000000",
00294          "Translation matrix element",
00295          CPL_TYPE_DOUBLE},
00296         {"CD2_2", "0.000057",
00297          "Translation matrix element",
00298          CPL_TYPE_DOUBLE},
00299         {"HIERARCH ESO OBS DID", "ESO-VLT-DIC.OBS-1.7",
00300          "OBS Dictionary",
00301          CPL_TYPE_STRING},
00302         {"HIERARCH ESO OBS OBSERVER", "UNKNOWN",
00303          "Observer Name",
00304          CPL_TYPE_STRING},
00305         {"HIERARCH ESO OBS PI-COI NAME", "UNKNOWN",
00306          "PI-COI name",
00307          CPL_TYPE_STRING},
00308         {"HIERARCH ESO INS GRAT NAME", "HR",
00309          "Grating name",
00310          CPL_TYPE_STRING},
00311         {"HIERARCH ESO PRO CATG", "X",
00312          "Product category",
00313          CPL_TYPE_STRING},
00314         {"HIERARCH ESO TPL NEXP", "5",
00315          "Number of exposures",
00316          CPL_TYPE_INT},
00317         {"HISTORY", "1st history record", NULL, CPL_TYPE_STRING},
00318         {"COMMENT", "1st comment record", NULL, CPL_TYPE_STRING},
00319         {"HISTORY", "2st history record", NULL, CPL_TYPE_STRING},
00320         {"COMMENT", "2st comment record", NULL, CPL_TYPE_STRING},
00321         {"COMMENT", "3st comment record", NULL, CPL_TYPE_STRING},
00322         {"HISTORY", "3st history record", NULL, CPL_TYPE_STRING},
00323         {"END", NULL, NULL, CPL_TYPE_STRING}
00324     };
00325 
00326     FILE *file;
00327 
00328     const cxchar *longname = "0123456789012345678901234567890123456789"
00329         "0123456789012345678901234567890123456789";
00330 
00331 
00332     /*
00333      * Test 1: Create a property list and check its validity.
00334      */
00335 
00336     plist = uves_propertylist_new();
00337 
00338     cx_assert(plist != NULL);
00339     cx_assert(uves_propertylist_is_empty(plist));
00340     cx_assert(uves_propertylist_get_size(plist) == 0);
00341 
00342 
00343     /*
00344      * Test 2: Append properties to the list created in the previous test
00345      *         and verify the data.
00346      */
00347 
00348     uves_propertylist_append_char(plist, keys[0], 'a');
00349     uves_propertylist_set_comment(plist, keys[0], comments[0]);
00350 
00351     uves_propertylist_append_bool(plist, keys[1], 1);
00352     uves_propertylist_set_comment(plist, keys[1], comments[1]);
00353 
00354     uves_propertylist_append_int(plist, keys[2], -1);
00355     uves_propertylist_set_comment(plist, keys[2], comments[2]);
00356 
00357     uves_propertylist_append_long(plist, keys[3], 32768);
00358     uves_propertylist_set_comment(plist, keys[3], comments[3]);
00359 
00360     uves_propertylist_append_float(plist, keys[4], fval[0]);
00361     uves_propertylist_set_comment(plist, keys[4], comments[4]);
00362 
00363     uves_propertylist_append_double(plist, keys[5], dval[0]);
00364     uves_propertylist_set_comment(plist, keys[5], comments[5]);
00365 
00366     uves_propertylist_append_string(plist, keys[6], comments[6]);
00367     uves_propertylist_set_comment(plist, keys[6], comments[6]);
00368 
00369     cx_assert(!uves_propertylist_is_empty(plist));
00370     cx_assert(uves_propertylist_get_size(plist) == 7);
00371 
00372 
00373     for (i = 0; i < uves_propertylist_get_size(plist); i++) {
00374         cpl_property *p = uves_propertylist_get(plist, i);
00375 
00376         cx_assert(!strcmp(cpl_property_get_name(p), keys[i]));
00377         cx_assert(!strcmp(cpl_property_get_comment(p), comments[i]));
00378         cx_assert(cpl_property_get_type(p) == types[i]);
00379 
00380         cx_assert(uves_propertylist_contains(plist, keys[i]));
00381         cx_assert(!strcmp(uves_propertylist_get_comment(plist, keys[i]),
00382                           comments[i]));
00383         cx_assert(uves_propertylist_get_type(plist, keys[i]) == types[i]);
00384     }
00385 
00386     cx_assert(uves_propertylist_get_char(plist, keys[0]) == 'a');
00387     cx_assert(uves_propertylist_get_bool(plist, keys[1]) == 1);
00388     cx_assert(uves_propertylist_get_int(plist, keys[2]) == -1);
00389     cx_assert(uves_propertylist_get_long(plist, keys[3]) == 32768);
00390 
00391     fval[1] = uves_propertylist_get_float(plist, keys[4]);
00392     cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float)));
00393 
00394     dval[1] = uves_propertylist_get_double(plist, keys[5]);
00395     cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double)));
00396 
00397     cx_assert(!strcmp(uves_propertylist_get_string(plist, keys[6]),
00398                       comments[6]));
00399 
00400 
00401     /*
00402      * Test 3: Modify the values of the property list entries
00403      *         and verify the data.
00404      */
00405 
00406     cx_assert(uves_propertylist_set_char(plist, keys[0], 'b') == 0);
00407     cx_assert(uves_propertylist_get_char(plist, keys[0]) == 'b');
00408 
00409     cx_assert(uves_propertylist_set_bool(plist, keys[1], 0) == 0);
00410     cx_assert(uves_propertylist_get_bool(plist, keys[1]) == 0);
00411 
00412     cx_assert(uves_propertylist_set_int(plist, keys[2], -1) == 0);
00413     cx_assert(uves_propertylist_get_int(plist, keys[2]) == -1);
00414 
00415     cx_assert(uves_propertylist_set_long(plist, keys[3], 1) == 0);
00416     cx_assert(uves_propertylist_get_long(plist, keys[3]) == 1);
00417 
00418     fval[0] = 9.87654321;
00419     cx_assert(uves_propertylist_set_float(plist, keys[4], fval[0]) == 0);
00420     fval[1] = uves_propertylist_get_float(plist, keys[4]);
00421     cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float)));
00422 
00423     dval[0] = -9.87654321;
00424     cx_assert(uves_propertylist_set_double(plist, keys[5], dval[0]) == 0);
00425     dval[1] = uves_propertylist_get_double(plist, keys[5]);
00426     cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double)));
00427 
00428     cx_assert(uves_propertylist_set_string(plist, keys[6], comments[0]) == 0);
00429     cx_assert(!strcmp(uves_propertylist_get_string(plist, keys[6]),
00430                       comments[0]));
00431 
00432     /*
00433      * Test 4: Check that trying to modify an entry with a different
00434      *         type is properly failing.
00435      */
00436 
00437     if (0) test_plist_dump(plist);
00438 
00439     cx_assert(uves_propertylist_set_char(plist, keys[1], 'a') ==
00440               CPL_ERROR_TYPE_MISMATCH);
00441     if (0) test_plist_dump(plist);
00442 
00443 
00444     cx_assert(uves_propertylist_set_bool(plist, keys[2], 1) ==
00445               CPL_ERROR_TYPE_MISMATCH);
00446 
00447     cx_assert(uves_propertylist_set_int(plist, keys[3], 1) ==
00448               CPL_ERROR_TYPE_MISMATCH);
00449     cx_assert(uves_propertylist_set_long(plist, keys[4], 1) ==
00450               CPL_ERROR_TYPE_MISMATCH);
00451     cx_assert(uves_propertylist_set_float(plist, keys[5], 1.) ==
00452               CPL_ERROR_TYPE_MISMATCH);
00453     cx_assert(uves_propertylist_set_double(plist, keys[6], 1.) ==
00454               CPL_ERROR_TYPE_MISMATCH);
00455 
00456     cx_assert(uves_propertylist_set_string(plist, keys[0], comments[0]) ==
00457               CPL_ERROR_TYPE_MISMATCH);
00458 
00459     /*
00460      * Test 5: Verify that values are inserted correctly into the property
00461      *         list.
00462      */
00463 
00464     cx_assert(uves_propertylist_insert_char(plist, keys[0],
00465                                            keys[7], 'a') == 0);
00466     cx_assert(uves_propertylist_insert_after_char(plist, keys[0],
00467                                                  keys[7], 'c') == 0);
00468 
00469     cx_assert(uves_propertylist_insert_bool(plist, keys[1],
00470                                            keys[8], 0) == 0);
00471     cx_assert(uves_propertylist_insert_after_bool(plist, keys[1],
00472                                                  keys[8], 1) == 0);
00473 
00474     cx_assert(uves_propertylist_insert_int(plist, keys[2],
00475                                           keys[9], 0) == 0);
00476     cx_assert(uves_propertylist_insert_after_int(plist, keys[2],
00477                                                 keys[9], 1) == 0);
00478 
00479     cx_assert(uves_propertylist_insert_long(plist, keys[3], keys[10],
00480                                            123456789) == 0);
00481     cx_assert(uves_propertylist_insert_after_long(plist, keys[3], keys[10],
00482                                                  123456789) == 0);
00483 
00484     cx_assert(uves_propertylist_insert_float(plist, keys[4], keys[11],
00485                                             fval[0]) == 0);
00486     cx_assert(uves_propertylist_insert_after_float(plist, keys[4], keys[11],
00487                                                   -fval[0]) == 0);
00488 
00489     cx_assert(uves_propertylist_insert_double(plist, keys[5], keys[12],
00490                                              dval[0]) == 0);
00491     cx_assert(uves_propertylist_insert_after_double(plist, keys[5], keys[12],
00492                                                    -dval[0]) == 0);
00493 
00494     cx_assert(uves_propertylist_insert_string(plist, keys[6],
00495                                              keys[13], "") == 0);
00496     cx_assert(uves_propertylist_insert_after_string(plist, keys[6],
00497                                                    keys[13], "") == 0);
00498     for (i = 0; i < 7; i++) {
00499         cpl_property *p0 = uves_propertylist_get(plist, 3 * i);
00500         cpl_property *p1 = uves_propertylist_get(plist, 3 * i + 1);
00501         cpl_property *p2 = uves_propertylist_get(plist, 3 * i + 2);
00502 
00503         cx_assert(!strcmp(cpl_property_get_name(p0), keys[i + 7]));
00504         cx_assert(!strcmp(cpl_property_get_name(p1), keys[i]));
00505         cx_assert(!strcmp(cpl_property_get_name(p2), keys[i + 7]));
00506 
00507         switch (cpl_property_get_type(p0)) {
00508         case CPL_TYPE_CHAR:
00509             cx_assert(cpl_property_get_char(p0) == 'a');
00510             cx_assert(cpl_property_get_char(p2) == 'c');
00511             break;
00512 
00513         case CPL_TYPE_BOOL:
00514             cx_assert(cpl_property_get_bool(p0) == 0);
00515             cx_assert(cpl_property_get_bool(p2) == 1);
00516             break;
00517 
00518         case CPL_TYPE_INT:
00519             cx_assert(cpl_property_get_int(p0) == 0);
00520             cx_assert(cpl_property_get_int(p2) == 1);
00521             break;
00522 
00523         case CPL_TYPE_LONG:
00524             cx_assert(cpl_property_get_long(p0) == 123456789);
00525             cx_assert(cpl_property_get_long(p2) == 123456789);
00526             break;
00527 
00528         case CPL_TYPE_FLOAT:
00529             fval[1] = cpl_property_get_float(p0);
00530             cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float)));
00531 
00532             fval[1] = -cpl_property_get_float(p2);
00533             cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float)));
00534             break;
00535 
00536         case CPL_TYPE_DOUBLE:
00537             dval[1] = cpl_property_get_double(p0);
00538             cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double)));
00539 
00540             dval[1] = -cpl_property_get_double(p2);
00541             cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double)));
00542             break;
00543 
00544         case CPL_TYPE_STRING:
00545             cx_assert(!strcmp(cpl_property_get_string(p0), ""));
00546             cx_assert(!strcmp(cpl_property_get_string(p2), ""));
00547             break;
00548 
00549         default:
00550             /* This point should never be reached */
00551             cx_log("uves_propertylist-test", CX_LOG_LEVEL_ERROR, "file %s: "
00552                    "line %d: Invalid value type encountered", __FILE__,
00553                    __LINE__);
00554             break;
00555         }
00556     }
00557 
00558 
00559     /*
00560      * Test 6: Verify that modification of or insertion at/after a non
00561      *         existing elements is reported correctly.
00562      */
00563     cx_assert(uves_propertylist_contains(plist, "Non-existing key") == 0);
00564 
00565     cx_assert(uves_propertylist_set_char(plist, "Non-existing key", 'a') ==
00566               CPL_ERROR_DATA_NOT_FOUND);
00567     cx_assert(uves_propertylist_set_bool(plist, "Non-existing key", 1) ==
00568               CPL_ERROR_DATA_NOT_FOUND);
00569     cx_assert(uves_propertylist_set_int(plist, "Non-existing key", 1) ==
00570               CPL_ERROR_DATA_NOT_FOUND);
00571     cx_assert(uves_propertylist_set_long(plist, "Non-existing key", 1) ==
00572               CPL_ERROR_DATA_NOT_FOUND);
00573     cx_assert(uves_propertylist_set_float(plist, "Non-existing key", 1) ==
00574               CPL_ERROR_DATA_NOT_FOUND);
00575     cx_assert(uves_propertylist_set_double(plist, "Non-existing key", 1) ==
00576               CPL_ERROR_DATA_NOT_FOUND);
00577     cx_assert(uves_propertylist_set_string(plist, "Non-existing key", "") ==
00578               CPL_ERROR_DATA_NOT_FOUND);
00579 
00580     cx_assert(uves_propertylist_insert_char(plist, "Non-existing key",
00581                                            "h", 'a') == 1);
00582     cx_assert(uves_propertylist_insert_bool(plist, "Non-existing key",
00583                                            "h", 1) == 1);
00584     cx_assert(uves_propertylist_insert_int(plist, "Non-existing key",
00585                                           "h", 1) == 1);
00586     cx_assert(uves_propertylist_insert_long(plist, "Non-existing key",
00587                                            "h", 1) == 1);
00588     cx_assert(uves_propertylist_insert_float(plist, "Non-existing key",
00589                                             "h", 1) == 1);
00590     cx_assert(uves_propertylist_insert_double(plist, "Non-existing key",
00591                                              "h", 1) == 1);
00592     cx_assert(uves_propertylist_insert_string(plist, "Non-existing key",
00593                                              "h", "") == 1);
00594 
00595     cx_assert(uves_propertylist_insert_after_char(plist, "Non-existing key",
00596                                                  "h", 'a') == 1);
00597     cx_assert(uves_propertylist_insert_after_bool(plist, "Non-existing key",
00598                                                  "h", 1) == 1);
00599     cx_assert(uves_propertylist_insert_after_int(plist, "Non-existing key",
00600                                                 "h", 1) == 1);
00601     cx_assert(uves_propertylist_insert_after_long(plist, "Non-existing key",
00602                                                  "h", 1) == 1);
00603     cx_assert(uves_propertylist_insert_after_float(plist, "Non-existing key",
00604                                                   "h", 1) == 1);
00605     cx_assert(uves_propertylist_insert_after_double(plist, "Non-existing key",
00606                                                    "h", 1) == 1);
00607     cx_assert(uves_propertylist_insert_after_string(plist, "Non-existing key",
00608                                                    "h", "") == 1);
00609 
00610 
00611     /*
00612      * Test 7: Create a copy of the property list and verify that original
00613      *         and copy are identical but do not share any resources.
00614      */
00615 
00616     _plist = uves_propertylist_duplicate(plist);
00617     cx_assert(_plist != NULL);
00618     cx_assert(_plist != plist);
00619 
00620     for (i = 0; i < uves_propertylist_get_size(plist); i++) {
00621         cpl_property *p = uves_propertylist_get(plist, i);
00622         cpl_property *_p = uves_propertylist_get(_plist, i);
00623 
00624         cx_assert(cpl_property_get_name(p) != cpl_property_get_name(_p));
00625         cx_assert(!strcmp(cpl_property_get_name(p),
00626                           cpl_property_get_name(_p)));
00627         cx_assert(cpl_property_get_comment(p) == NULL ||
00628                   (cpl_property_get_comment(p) !=
00629                    cpl_property_get_comment(_p)));
00630         cx_assert(cpl_property_get_comment(p) == NULL ||
00631                   !strcmp(cpl_property_get_comment(p),
00632                           cpl_property_get_comment(_p)));
00633 
00634         switch (cpl_property_get_type(p)) {
00635         case CPL_TYPE_CHAR:
00636             cx_assert(cpl_property_get_char(p) ==
00637                       cpl_property_get_char(_p));
00638             break;
00639 
00640         case CPL_TYPE_BOOL:
00641             cx_assert(cpl_property_get_bool(p) ==
00642                       cpl_property_get_bool(_p));
00643             break;
00644 
00645         case CPL_TYPE_INT:
00646             cx_assert(cpl_property_get_int(p) ==
00647                       cpl_property_get_int(_p));
00648             break;
00649 
00650         case CPL_TYPE_LONG:
00651             cx_assert(cpl_property_get_long(p) ==
00652                       cpl_property_get_long(_p));
00653             break;
00654 
00655         case CPL_TYPE_FLOAT:
00656             fval[0] = cpl_property_get_float(p);
00657             fval[1] = cpl_property_get_float(_p);
00658             cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float)));
00659             break;
00660 
00661         case CPL_TYPE_DOUBLE:
00662             dval[0] = cpl_property_get_double(p);
00663             dval[1] = cpl_property_get_double(_p);
00664             cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double)));
00665             break;
00666 
00667         case CPL_TYPE_STRING:
00668             cx_assert(!strcmp(cpl_property_get_string(p),
00669                               cpl_property_get_string(_p)));
00670             break;
00671 
00672         default:
00673             /* This point should never be reached */
00674             cx_log("uves_propertylist-test", CX_LOG_LEVEL_ERROR, "file %s: "
00675                    "line %d: Invalid value type encountered", __FILE__,
00676                    __LINE__);
00677             break;
00678         }
00679     }
00680 
00681     uves_propertylist_delete(_plist);
00682 
00683     /*
00684      * Test 8: Erase elements from the property list and verify the list
00685      *         structure and the data.
00686      */
00687 
00688     for (i = 0; i < 7; i++) {
00689         uves_propertylist_erase(plist, keys[i + 7]);
00690         cx_assert(uves_propertylist_contains(plist, keys[i + 7]) == 1);
00691 
00692         uves_propertylist_erase(plist, keys[i + 7]);
00693         cx_assert(uves_propertylist_contains(plist, keys[i + 7]) == 0);
00694     }
00695     cx_assert(uves_propertylist_get_size(plist) == 7);
00696 
00697     for (i = 0; i < 7; i++) {
00698         cpl_property *p = uves_propertylist_get(plist, i);
00699         cx_assert(!strcmp(cpl_property_get_name(p), keys[i]));
00700     }
00701 
00702     if (0) test_plist_dump(plist);
00703     
00704     cx_assert(uves_propertylist_get_char(plist, keys[0]) == 'b');
00705     cx_assert(uves_propertylist_get_bool(plist, keys[1]) == 0);
00706     cx_assert(uves_propertylist_get_int(plist, keys[2]) == -1);
00707     cx_assert(uves_propertylist_get_long(plist, keys[3]) == 1);
00708 
00709     fval[0] = 9.87654321;
00710     fval[1] = uves_propertylist_get_float(plist, keys[4]);
00711     cx_assert(!memcmp(&fval[0], &fval[1], sizeof(float)));
00712 
00713     dval[0] = -9.87654321;
00714     dval[1] = uves_propertylist_get_double(plist, keys[5]);
00715     cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double)));
00716 
00717     cx_assert(!strcmp(uves_propertylist_get_string(plist, keys[6]),
00718                       comments[0]));
00719 
00720     /*
00721      * Test 9: Erase all elements from the property list and verify that
00722      *         the list is empty.
00723      */
00724 
00725     uves_propertylist_empty(plist);
00726 
00727     cx_assert(uves_propertylist_is_empty(plist));
00728     cx_assert(uves_propertylist_get_size(plist) == 0);
00729 
00730     uves_propertylist_delete(plist);
00731 
00732 
00733     /*
00734      * Test 10: Create a property list from an input (FITS) file. Verify the
00735      *          loaded data.
00736      */
00737 
00738     /* Create a sample FITS header and save it to a file */
00739 
00740     header = qfits_header_new();
00741 
00742     for (i = 0; (cxsize)i < sizeof hdr / sizeof(struct fcard); i++)
00743         qfits_header_append(header, hdr[i].key, hdr[i].val, hdr[i].com, NULL);
00744 
00745     file = fopen("cpltest1.fits", "w");
00746     qfits_header_dump(header, file);
00747     fclose(file);
00748 
00749     /* Load the 1st FITS header into a property list */
00750 
00751     plist = uves_propertylist_load("cpltest1.fits", 0);
00752     cx_assert(plist != NULL);
00753 
00754     cx_assert(uves_propertylist_contains(plist, "END") == 0);
00755     cx_assert(uves_propertylist_get_size(plist) ==
00756               (sizeof hdr / sizeof(struct fcard) - 1));
00757 
00758     for (i = 0; i < uves_propertylist_get_size(plist); i++) {
00759         const cxchar *s = hdr[i].key;
00760         cpl_property *p = uves_propertylist_get(plist, i);
00761 
00762         /*
00763          * Strip HIERARCH from the keyword if it is present. HIERARCH
00764          * is not carried over to the property name.
00765          */
00766 
00767         if (strstr(hdr[i].key, "HIERARCH"))
00768             s = hdr[i].key + strlen("HIERARCH") + 1;
00769 
00770         cx_assert(!strcmp(cpl_property_get_name(p), s));
00771         cx_assert(hdr[i].com == NULL ||
00772                   !strcmp(cpl_property_get_comment(p), hdr[i].com));
00773         cx_assert(cpl_property_get_type(p) == hdr[i].type);
00774 
00775         switch (hdr[i].type) {
00776         case CPL_TYPE_BOOL:
00777             cx_assert(cpl_property_get_bool(p) ==
00778                       (*hdr[i].val == 'T' ? 1 : 0));
00779             break;
00780 
00781         case CPL_TYPE_INT:
00782             cx_assert(cpl_property_get_int(p) == atoi(hdr[i].val));
00783             break;
00784 
00785         case CPL_TYPE_DOUBLE:
00786             dval[0] = cpl_property_get_double(p);
00787             dval[1] = atof(hdr[i].val);
00788             cx_assert(!memcmp(&dval[0], &dval[1], sizeof(double)));
00789             break;
00790 
00791         case CPL_TYPE_STRING:
00792             cx_assert(!strcmp(cpl_property_get_string(p),
00793                               qfits_pretty_string(hdr[i].val)));
00794             break;
00795 
00796         default:
00797             cx_log("uves_propertylist-test", CX_LOG_LEVEL_ERROR, "file %s: "
00798                    "line %d: Invalid value type encountered", __FILE__,
00799                    __LINE__);
00800             break;
00801         }
00802     }
00803 
00804 
00805     /*
00806      * Test 11: Convert the qfits FITS header back into a property list and
00807      *          verify that the original property list and the one created
00808      *          from the FITS header are identical.
00809      */
00810 
00811     _plist = uves_propertylist_from_fits(header) ;
00812     cx_assert(_plist != NULL);
00813     cx_assert(uves_propertylist_get_size(plist) ==
00814               uves_propertylist_get_size(_plist));
00815 
00816     for (i = 0; i < uves_propertylist_get_size(plist); i++) {
00817         cpl_property *p = uves_propertylist_get(plist, i);
00818         cpl_property *_p = uves_propertylist_get(_plist, i);
00819 
00820         cx_assert(strcmp(cpl_property_get_name(p),
00821                          cpl_property_get_name(_p)) == 0);
00822         cx_assert(strcmp(cpl_property_get_comment(p),
00823                          cpl_property_get_comment(_p)) == 0);
00824         cx_assert(cpl_property_get_type(p) == cpl_property_get_type(_p));
00825 
00826         switch (cpl_property_get_type(p)) {
00827         case CPL_TYPE_BOOL:
00828             cx_assert(cpl_property_get_bool(p) == cpl_property_get_bool(_p));
00829             break;
00830 
00831         case CPL_TYPE_INT:
00832             cx_assert(cpl_property_get_int(p) == cpl_property_get_int(_p));
00833             break;
00834 
00835         case CPL_TYPE_DOUBLE:
00836             cx_assert(cpl_property_get_double(p) ==
00837                       cpl_property_get_double(_p));
00838             break;
00839 
00840         case CPL_TYPE_STRING:
00841             cx_assert(strcmp(cpl_property_get_string(p),
00842                              cpl_property_get_string(_p)) == 0);
00843             break;
00844 
00845         default:
00846             cx_log("uves_propertylist-test", CX_LOG_LEVEL_ERROR,
00847                    "file %s: line %d: Invalid value type encountered",
00848                    __FILE__, __LINE__);
00849             break;
00850         }
00851     }
00852 
00853     uves_propertylist_delete(_plist);
00854 
00855 
00856     /*
00857      * Test 12: Create a qfits header from the property list and verify
00858      *          the header data. Note that uves_propertylist_to_fits establishes
00859      *          the DICB sorting order.
00860      */
00861 
00862     qfits_header_sort(&header);
00863 
00864     file = fopen("cpltest1.fits", "w");
00865     qfits_header_dump(header, file);
00866     fclose(file);
00867 
00868 
00869     _header = uves_propertylist_to_fits(plist);
00870     cx_assert(_header != NULL);
00871 
00872     file = fopen("cpltest2.fits", "w");
00873     qfits_header_dump(_header, file);
00874     fclose(file);
00875 
00876 
00877     for (i = 0; i < header->n; i++) {
00878         cxchar key[FITS_LINESZ + 1];
00879         cxchar val[FITS_LINESZ + 1];
00880         cxchar com[FITS_LINESZ + 1];
00881 
00882         cxchar _key[FITS_LINESZ + 1];
00883         cxchar _val[FITS_LINESZ + 1];
00884         cxchar _com[FITS_LINESZ + 1];
00885 
00886 
00887         qfits_header_getitem(header, i, key, val, com, NULL);
00888         cx_assert(qfits_header_getitem(_header, i, _key, _val,
00889                                        _com, NULL) == 0);
00890 
00891         cx_assert(!strcmp(key, _key));
00892         cx_assert(_com == NULL || !strcmp(com, _com));
00893 
00894         switch (qfits_get_type(val)) {
00895         case QFITS_FLOAT:
00896             fval[0] = atof(val);
00897             fval[1] = atof(_val);
00898 
00899             cx_assert(fabs(fval[0] - fval[1]) < FLT_EPS);
00900             break;
00901 
00902         default:
00903             cx_assert(strlen(val) == 0 ||
00904                       !strcmp(qfits_pretty_string(val),
00905                               qfits_pretty_string(_val)));
00906             break;
00907         }
00908 
00909     }
00910 
00911     qfits_header_destroy(header);
00912     qfits_header_destroy(_header);
00913 
00914 
00915     /*
00916      * Test 13: Copy all propertys matching a given pattern from one
00917      *          property list to another.
00918      */
00919 
00920     _plist = uves_propertylist_new();
00921 
00922     uves_propertylist_copy_property_regexp(_plist, plist, "^ESO .*", 0);
00923     cx_assert(uves_propertylist_get_size(_plist) == 6);
00924     cx_assert(uves_propertylist_contains(_plist, "ESO OBS DID") != 0);
00925     cx_assert(uves_propertylist_contains(_plist, "ESO OBS OBSERVER") != 0);
00926     cx_assert(uves_propertylist_contains(_plist, "ESO OBS PI-COI NAME") != 0);
00927     cx_assert(uves_propertylist_contains(_plist, "ESO INS GRAT NAME") != 0);
00928     cx_assert(uves_propertylist_contains(_plist, "ESO PRO CATG") != 0);
00929     cx_assert(uves_propertylist_contains(_plist, "ESO TPL NEXP") != 0);
00930 
00931     uves_propertylist_empty(_plist);
00932     cx_assert(uves_propertylist_is_empty(_plist) != 0);
00933 
00934     uves_propertylist_copy_property_regexp(_plist, plist, "^ESO .*", 1);
00935     cx_assert(uves_propertylist_get_size(_plist) ==
00936               (uves_propertylist_get_size(plist) - 6));
00937 
00938 
00939     /*
00940      * Test 14: Erase all properties matching the given pattern from the
00941      *          property list.
00942      */
00943 
00944     uves_propertylist_empty(_plist);
00945     cx_assert(uves_propertylist_is_empty(_plist) != 0);
00946 
00947     uves_propertylist_copy_property_regexp(_plist, plist, "^ESO .*", 0);
00948     cx_assert(uves_propertylist_get_size(_plist) == 6);
00949 
00950     uves_propertylist_erase_regexp(_plist, "^ESO OBS .*", 0);
00951     cx_assert(uves_propertylist_get_size(_plist) == 3);
00952 
00953     uves_propertylist_erase_regexp(_plist, "ESO TPL NEXP", 0);
00954     cx_assert(uves_propertylist_get_size(_plist) == 2);
00955 
00956     uves_propertylist_delete(_plist);
00957     uves_propertylist_delete(plist);
00958 
00959 
00960     /*
00961      * Test 15: Create a property list from a file. Only properties matching
00962      *          the given pattern are loaded.
00963      */
00964 
00965     plist = NULL;
00966 
00967     plist = uves_propertylist_load_regexp("cpltest1.fits", 0,
00968                                          "^ESO .*", 0);
00969     cx_assert(plist != NULL);
00970     cx_assert(uves_propertylist_is_empty(plist) == 0);
00971     cx_assert(uves_propertylist_get_size(plist) == 6);
00972     cx_assert(uves_propertylist_contains(plist, "ESO OBS DID") != 0);
00973     cx_assert(uves_propertylist_contains(plist, "ESO OBS OBSERVER") != 0);
00974     cx_assert(uves_propertylist_contains(plist, "ESO OBS PI-COI NAME") != 0);
00975     cx_assert(uves_propertylist_contains(plist, "ESO INS GRAT NAME") != 0);
00976     cx_assert(uves_propertylist_contains(plist, "ESO PRO CATG") != 0);
00977     cx_assert(uves_propertylist_contains(plist, "ESO TPL NEXP") != 0);
00978 
00979     uves_propertylist_delete(plist);
00980 
00981 
00982     /*
00983      * Test 16: Append a property list to another.
00984      */
00985 
00986     plist = uves_propertylist_new();
00987     _plist = uves_propertylist_new();
00988 
00989     uves_propertylist_append_char(plist, keys[0], 'a');
00990     uves_propertylist_set_comment(plist, keys[0], comments[0]);
00991 
00992     uves_propertylist_append_bool(plist, keys[1], 1);
00993     uves_propertylist_set_comment(plist, keys[1], comments[1]);
00994 
00995     uves_propertylist_append_int(plist, keys[2], -1);
00996     uves_propertylist_set_comment(plist, keys[2], comments[2]);
00997 
00998     uves_propertylist_append_long(plist, keys[3], 32768);
00999     uves_propertylist_set_comment(plist, keys[3], comments[3]);
01000 
01001     uves_propertylist_append_float(_plist, keys[4], fval[0]);
01002     uves_propertylist_set_comment(_plist, keys[4], comments[4]);
01003 
01004     uves_propertylist_append_double(_plist, keys[5], dval[0]);
01005     uves_propertylist_set_comment(_plist, keys[5], comments[5]);
01006 
01007     uves_propertylist_append_string(_plist, keys[6], comments[6]);
01008     uves_propertylist_set_comment(_plist, keys[6], comments[6]);
01009 
01010     cx_assert(!uves_propertylist_is_empty(plist));
01011     cx_assert(uves_propertylist_get_size(plist) == 4);
01012 
01013     cx_assert(!uves_propertylist_is_empty(_plist));
01014     cx_assert(uves_propertylist_get_size(_plist) == 3);
01015 
01016     uves_propertylist_append(plist, _plist);
01017 
01018     cx_assert(!uves_propertylist_is_empty(plist));
01019     cx_assert(uves_propertylist_get_size(plist) == 7);
01020 
01021     cx_assert(!uves_propertylist_is_empty(_plist));
01022     cx_assert(uves_propertylist_get_size(_plist) == 3);
01023 
01024     for (i = 0; i < uves_propertylist_get_size(plist); i++) {
01025         cpl_property *p = uves_propertylist_get(plist, i);
01026 
01027         cx_assert(!strcmp(cpl_property_get_name(p), keys[i]));
01028         cx_assert(!strcmp(cpl_property_get_comment(p), comments[i]));
01029         cx_assert(cpl_property_get_type(p) == types[i]);
01030 
01031         cx_assert(uves_propertylist_contains(plist, keys[i]));
01032         cx_assert(!strcmp(uves_propertylist_get_comment(plist, keys[i]),
01033                           comments[i]));
01034         cx_assert(uves_propertylist_get_type(plist, keys[i]) == types[i]);
01035     }
01036 
01037 
01038     /*
01039      * Test 17: Create a FITS header using a list containing a property with
01040      *          a name of length 80 characters (the length of a FITS card)
01041      */
01042 
01043     uves_propertylist_empty(plist);
01044 
01045     uves_propertylist_append_string(plist, longname, comments[6]);
01046 
01047     qfits_header_destroy(uves_propertylist_to_fits(plist));
01048 
01049 
01050     uves_propertylist_delete(_plist);
01051     _plist = NULL;
01052 
01053     uves_propertylist_delete(plist);
01054     plist = NULL;
01055 
01056     cx_assert( cpl_error_get_code() == CPL_ERROR_UNSPECIFIED );
01057     cpl_error_reset();
01058 
01059     /*
01060      * All tests succeeded
01061      */
01062 
01063     return 0;
01064 
01065 }
01066 
01067 int main(void)
01068 {
01069     cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
01070 
01071     //cpl_msg_set_level(CPL_MSG_DEBUG);
01072     test_main();
01073 
01074     return cpl_test_end(0);
01075 }

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1