Index: /trunk/psLib/test/image/tst_psImageIO.c
===================================================================
--- /trunk/psLib/test/image/tst_psImageIO.c	(revision 998)
+++ /trunk/psLib/test/image/tst_psImageIO.c	(revision 998)
@@ -0,0 +1,255 @@
+/** @file  tst_psImageIO.c
+ *
+ *  @brief Contains the tests for psImageIO.[ch]
+ *
+ *
+ *  @author Robert DeSonia, MHPCC
+ *
+ *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-06-11 03:46:38 $
+ *
+ *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
+ */
+
+#include <math.h>
+#include <float.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "psTest.h"
+#include "pslib.h"
+
+#define GENIMAGE(img,c,r,TYP) \
+img = psImageAlloc(c,r,PS_TYPE_##TYP); \
+for (unsigned int row=0;row<r;row++) { \
+    ps##TYP* imgRow = img->data.TYP[row]; \
+    for (unsigned int col=0;col<c;col++) { \
+        imgRow[col] = (ps##TYP)(row+2*col); \
+    } \
+}
+
+static int testImageRead(void);
+static int testImageWrite(void);
+
+testDescription tests[] = {
+                              {testImageRead,"567-testImageRead",0},
+                              {testImageWrite,"569-testImageWrite",0},
+                              {NULL}
+                          };
+
+int main()
+{
+    psLogSetLevel(PS_LOG_INFO);
+
+    testImageRead();
+
+    if (! runTestSuite(stderr,"psImage",tests)) {
+        psAbort(__FILE__,"One or more tests failed");
+    }
+    return 0;
+}
+
+int testImageRead(void)
+{
+    int N = 256;
+    int M = 128;
+    psImage* image = NULL;
+
+    /*
+        This function shall open the specified FITS file, read the specified data
+        and place the data into a psImage structure. This function shall generate
+        an error message and return NULL if any of the input parameters are out of
+        range, image file doesn't exist or image is zero or one dimensional.
+     
+        Verify the returned psImage structure contains expected values, if the input
+        parameter filename specifies an available FITS file with known 2dimensional
+        data, input parameters col, row, ncol, nrow, z specify data range with the
+        FITS file. Cases should include 1x1, Nx1, 1xN, NxN and MxN sub images and
+        total FITS file image. (done in macro)
+     
+        Verify the returned psImage structure is equal to the input parameter
+        'output', if specified. (done in macro)
+     
+        */
+
+    /* generate FITS file to read */
+
+    #define testReadTypeSize(m, n, readM0, readN0, readM, readN, TYP, filename) \
+    { \
+        psImage* img = NULL; \
+        psImage* img2 = NULL; \
+        psImage* img3 = NULL; \
+        psImage* img4 = NULL; \
+        psImage* img_ref = NULL; \
+        \
+        GENIMAGE(img,m,n,TYP); \
+        img2 = psImageCopy(img2,img,PS_TYPE_##TYP); \
+        GENIMAGE(img3,m,n,TYP); \
+        psImageClip(img3,100,100,400,400); \
+        img4 = psImageCopy(img4,img3,PS_TYPE_##TYP); \
+        remove(filename); \
+        if (psImageWriteSection(img3,0,0,1,NULL,0,filename) != 0) { \
+            psError(__func__,"Failed to write test image %s",filename); \
+            return 1; \
+        } \
+        if (psImageWriteSection(img,0,0,0,NULL,0,filename) != 0) { \
+            psError(__func__,"Failed to write test image %s",filename); \
+            return 2; \
+        } \
+        if (psImageWriteSection(img,0,0,1,NULL,1,filename) != 0) { \
+            psError(__func__,"Failed to write test image %s",filename); \
+            return 3; \
+        } \
+        if (psImageWriteSection(img3,0,0,0,NULL,1,filename) != 0) { \
+            psError(__func__,"Failed to write test image %s",filename); \
+            return 4; \
+        } \
+        psImageFree(img); \
+        img_ref = img = psImageAlloc(2,2,PS_TYPE_F32); \
+        psImageFree(img3); \
+        img3 = NULL; \
+        img = psImageReadSection(img,readM0,readN0,readM,readN,0,NULL,0,filename); \
+        if (img_ref != img) { \
+            psError(__func__,"psImageReadSection didn't recycle the psImage?"); \
+            return 5; \
+        } \
+        img3 = psImageReadSection(img3,readM0,readN0,readM,readN,1,NULL,0,filename); \
+        if (img == NULL) { \
+            psError(__func__,"Failed to read test image %s",filename); \
+            return 6; \
+        } \
+        for (unsigned int row = readN0; row < readN; row++) { \
+            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
+            ps##TYP* img2Row = img2->data.TYP[row]; \
+            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
+            ps##TYP* img4Row = img4->data.TYP[row]; \
+            for (unsigned int col = readM0; col < readM; col++) { \
+                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
+                    psError(__func__,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
+                    return 7; \
+                } \
+                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
+                    psError(__func__,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
+                    return 8; \
+                } \
+            } \
+        } \
+        psImageFree(img); \
+        img = NULL; \
+        psImageFree(img3); \
+        img3 = NULL; \
+        img3 = psImageReadSection(img3,readM0,readN0,readM,readN,0,NULL,1,filename); \
+        img = psImageReadSection(img,readM0,readN0,readM,readN,1,NULL,1,filename); \
+        if (img == NULL) { \
+            psError(__func__,"Failed to read test image %s",filename); \
+            return 9; \
+        } \
+        for (unsigned int row = readN0; row < readN; row++) { \
+            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
+            ps##TYP* img2Row = img2->data.TYP[row]; \
+            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
+            ps##TYP* img4Row = img4->data.TYP[row]; \
+            for (unsigned int col = readM0; col < readM; col++) { \
+                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
+                    psError(__func__,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
+                    return 10; \
+                } \
+                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
+                    psError(__func__,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
+                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
+                    return 11; \
+                } \
+            } \
+        } \
+        psImageFree(img); \
+        psImageFree(img2); \
+        psImageFree(img3); \
+        psImageFree(img4); \
+    }
+
+    #define testReadType(TYP,filename) \
+    testReadTypeSize(1,1,0,0,1,1,TYP,"1x1_" filename); \
+    testReadTypeSize(M,1,M/4,0,M/2,1,TYP,"Mx1_" filename); \
+    testReadTypeSize(1,N,0,N/4,1,N/2,TYP,"1xN_" filename); \
+    testReadTypeSize(M,N,M/4,N/4,M/2,N/2,TYP,"MxN_" filename);
+
+    testReadType(U8,"U8.fits");
+    testReadType(S8,"S8.fits");
+    testReadType(S16,"S16.fits");
+    testReadType(U16,"U16.fits");
+    testReadType(S32,"S32.fits");
+    testReadType(U32,"U32.fits");
+    testReadType(F32,"F32.fits");
+    testReadType(F64,"F64.fits");
+
+    /*
+    Verify the returned psImage structure pointer is null, error log message is
+    generated and program execution doesn't stop, if input parameter filename
+    specifies a file that doesn't exist.
+    */
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error as file doesn't exist.");
+    image = psImageReadSection(image,0,0,M,N,0,NULL,0,"foobar.fits");
+    if (image != NULL) {
+        psError(__func__,"Image returned though filename did not exist.");
+        return 12;
+    }
+
+    /*
+    Verify the returned psImage structure pointer is null, error log message is
+    generatedand program execution doesn't stop, if input parameter filename
+    is null.Verify the returned psImage structurepointer is null, error log
+    message is generated and program execution doesn't stop, if input
+    parameters col,row, ncol, nrow and/or z specify a data range which is not
+    within the data range of the FITS file.
+    */
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error as filename is NULL.");
+    image = psImageReadSection(image,0,0,M,N,0,NULL,0,NULL);
+    if (image != NULL) {
+        psError(__func__,"Image returned though filename was NULL.");
+        return 13;
+    }
+
+    /*
+    Verify the returned psImage structure pointer is null, error log message is
+    generated and program execution doesn't stop,if input parameters extname
+    and/or extnum specify an extension which is not valid in the FITS file.
+    */
+
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error as extnum is invalid.");
+    image = psImageReadSection(image,0,0,M,N,0,NULL,4,"MxN_F32.fits");
+    if (image != NULL) {
+        psError(__func__,"Image returned though extnum was invalid.");
+        return 14;
+    }
+    psLogMsg(__func__,PS_LOG_INFO,"Following should be an error as extname is invalid.");
+    image = psImageReadSection(image,0,0,M,N,0,"bogus",0,"MxN_F32.fits");
+    if (image != NULL) {
+        psError(__func__,"Image returned though extnum was invalid.");
+        return 15;
+    }
+
+    /*
+    Verify the returned psImage structure pointer is null, error log message is
+    generated and program execution doesn't stop,if input parameter filename
+    specifies a FITS file that is zero or one dimensional.
+
+    XXX
+    N.B: since I can't write anything but 2 or 3 dimensional image, I can't
+    do this programatically at this time.  I verified that this functionality
+    exists in the source code via inspection. (line 101 in psImageIO.c for v1.3)
+    -rdd
+    */
+
+    return 0;
+}
+
+int testImageWrite(void)
+{
+
+    return 0;
+}
