/* $Id: cpl_frameset_io-test.c,v 1.13 2008/02/07 10:54:01 llundin Exp $ * * This file is part of the ESO Common Pipeline Library * Copyright (C) 2001-2005 European Southern Observatory * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * $Author: llundin $ * $Date: 2008/02/07 10:54:01 $ * $Revision: 1.13 $ * $Name: $ */ #undef CX_DISABLE_ASSERT #undef CX_LOG_DOMAIN #include #include #include #include #include #include #include "cpl_tools.h" #include "cpl_frameset_io.h" #include "cpl_image_io.h" #ifndef IMAGESZ #define IMAGESZ 512 #endif int main(void) { const cxchar *names[] = { "cpl_frameset-io_test1.fits", "cpl_frameset-io_test2.fits", "cpl_frameset-io_test3.fits", "cpl_frameset-io_test4.fits", }; const cxchar *tags[] = { "FLAT", "FLAT", "FLAT", "FLAT", }; cpl_frame_group groups[] = { CPL_FRAME_GROUP_RAW, CPL_FRAME_GROUP_RAW, CPL_FRAME_GROUP_RAW, CPL_FRAME_GROUP_RAW, }; cpl_frame_level levels[] = { CPL_FRAME_LEVEL_NONE, CPL_FRAME_LEVEL_NONE, CPL_FRAME_LEVEL_NONE, CPL_FRAME_LEVEL_NONE, }; cxlong i; cxlong has_primary; cpl_frameset *frameset; cpl_image *img1; cpl_imagelist *imlist; cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING); /* Insert tests below */ /* * Test 1A: Create a frameset and check its validity. */ frameset = cpl_frameset_new(); cpl_test_nonnull(frameset); cpl_test(cpl_frameset_is_empty(frameset)); cpl_test_eq(cpl_frameset_get_size(frameset), 0); /* * Test 1B: Load everything from an empty frameset * - should fail, since the created imagelist may not be empty */ imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_FLOAT, 0, -1); cpl_test_null(imlist); cpl_test_error(CPL_ERROR_ILLEGAL_INPUT); /* * Test 2A: Test error-handling of cpl_imagelist_load_frameset() */ imlist = cpl_imagelist_load_frameset(NULL, CPL_TYPE_FLOAT, 0, -1); cpl_test_null(imlist); cpl_test_error(CPL_ERROR_NULL_INPUT); /* Add frames to the frame set created in the previous test and verify the data.*/ /* The corresponding files do not exist yet */ for (i = 0; (cxsize)i < CX_N_ELEMENTS(names); i++) { cpl_frame *_frame = cpl_frame_new(); remove(names[i]); cpl_test_zero(cpl_frame_set_filename(_frame, names[i])); cpl_test_zero(cpl_frame_set_tag(_frame, tags[i])); cpl_test_zero(cpl_frame_set_type(_frame, CPL_FRAME_TYPE_IMAGE)); cpl_test_zero(cpl_frame_set_group(_frame, groups[i])); cpl_test_zero(cpl_frame_set_level(_frame, levels[i])); cpl_test_zero(cpl_frameset_insert(frameset, _frame)); } cpl_test_zero(cpl_frameset_is_empty(frameset)); cpl_test_eq(cpl_frameset_get_size(frameset), CX_N_ELEMENTS(names)); cpl_test_eq(cpl_frameset_count_tags(frameset, tags[0]), 4); /* * Test 2B: Test error-handling of cpl_imagelist_load_frameset() with missing files. */ imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_FLOAT, 0, -1); cpl_test_null(imlist); cpl_test_error(CPL_ERROR_ILLEGAL_INPUT); img1 = cpl_image_fill_test_create(IMAGESZ, IMAGESZ); cpl_test_nonnull(img1); /* * Test 2BB: Test error-handling of cpl_imagelist_load_frameset() on frame with a single image in primary HDU and more than one image in 1st extension. */ /* Create file, with image */ cpl_test_zero(cpl_image_save(img1, names[0], CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_DEFAULT)); imlist = cpl_imagelist_new(); cpl_test_zero(cpl_imagelist_set(imlist, cpl_image_duplicate(img1), 0)); cpl_test_zero(cpl_imagelist_set(imlist, cpl_image_duplicate(img1), 1)); cpl_test_zero(cpl_imagelist_save(imlist, names[0], CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_EXTEND)); cpl_imagelist_delete(imlist); /* Replicate the file */ for (i = 1; (cxsize)i < CX_N_ELEMENTS(names); i++) { const char * cmd = cpl_sprintf("cp %s %s", names[0], names[i]); cpl_test_zero(system(cmd)); cpl_free((char *)cmd); } imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_FLOAT, 2, -1); cpl_test_null(imlist); cpl_test_error(CPL_ERROR_ILLEGAL_INPUT); /* * Create images to insert into frameset * First case: primary HDU image is NULL */ for (has_primary = 0; has_primary <= 1; has_primary++) { /* The fits file contains three or four images */ const cxlong nimg = 3 + has_primary; cxlong iplane; cpl_msg_info(cpl_func, "Testing with %u frames and %d X %d-images, " "has_primary=%d", CX_N_ELEMENTS(names), IMAGESZ, IMAGESZ, (int)has_primary); /* Create file, either with or without image */ cpl_test_zero(cpl_image_save(has_primary ? img1 : NULL, names[0], CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_DEFAULT)); /* Append some extensions */ for (i = has_primary; i < nimg; i++) { /* Append an extension */ cpl_test_zero(cpl_image_save(img1, names[0], CPL_BPP_IEEE_FLOAT, NULL, CPL_IO_EXTEND)); } /* Replicate the file */ for (i = 1; (cxsize)i < CX_N_ELEMENTS(names); i++) { const char * cmd = cpl_sprintf("cp %s %s", names[0], names[i]); cpl_test(system(cmd) == 0); cpl_free((char *)cmd); } /* * Test 2D: Test error-handling of cpl_imagelist_load_frameset() with invalid type. */ imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_INVALID, 0, -1); cpl_test_null(imlist); cpl_test_error(CPL_ERROR_ILLEGAL_INPUT); /* * Test 2E: Test error-handling of cpl_imagelist_load_frameset() with invalid plane number. */ imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_FLOAT, 2, -1); cpl_test_null(imlist); cpl_test_error(CPL_ERROR_ILLEGAL_INPUT); /* * Test 2F: Test error-handling of cpl_imagelist_load_frameset() with invalid extension number. */ imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_FLOAT, 0, 1+nimg); cpl_test_null(imlist); cpl_test_error(CPL_ERROR_ILLEGAL_INPUT); /* * Test 3: Load frameset into imagelist, asking for all (1) plane(s) in one extension. */ for (iplane = 0; iplane <= 1; iplane++) { for (i = 0; i < nimg; i++) { imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_FLOAT, iplane, i+1-has_primary); cpl_test_nonnull(imlist); cpl_test_error(CPL_ERROR_NONE); cpl_test_eq(cpl_imagelist_get_size(imlist), cpl_frameset_get_size(frameset)); cpl_imagelist_delete(imlist); } } /* * Test 4: Load frameset into imagelist, asking for all (1) plane(s) in all extensions. */ for (iplane = 0; iplane <= 1; iplane++) { imlist = cpl_imagelist_load_frameset(frameset, CPL_TYPE_FLOAT, iplane, -1); cpl_test_nonnull(imlist); cpl_test_error(CPL_ERROR_NONE); cpl_test_eq(cpl_imagelist_get_size(imlist), nimg * cpl_frameset_get_size(frameset)); cpl_imagelist_delete(imlist); } } cpl_image_delete(img1); cpl_frameset_delete(frameset); if (!cpl_error_get_code()) { for (i = 0; (cxsize)i < CX_N_ELEMENTS(names); i++) { cpl_test_zero(remove(names[i])); } } return cpl_test_end(0); }