/** @file  tst_pmAstrometry.c
 *
 *  @brief Contains the tests for pmAstrometry.[ch]
 *
 *  @author George Gusciora, MHPCC
 *
 *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2005-09-13 20:54:04 $
 *
 *  XXX: The meory dereferencing is not quite right with these functions.
 *  If I call the alloc functions with non-NULL pointers, there will be
 *  memory leaks.
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 */

#include "config.h"

#include <math.h>
#include <string.h>

#include "psTest.h"
#include "pslib_strict.h"
#include "pmAstrometry.h"

static psS32 testFPAAlloc(void);
static psS32 testChipAlloc(void);
static psS32 testCellAlloc(void);
static psS32 testReadoutAlloc(void);

testDescription tests[] = {
                              {testFPAAlloc,739,"pmFPAAlloc",0,false},
                              {testChipAlloc,740,"pmChipAlloc",0,false},
                              {testCellAlloc,741,"pmCellAlloc",0,false},
                              {testReadoutAlloc,742,"pmReadoutAlloc",0,false},
                              {NULL}
                          };

psS32 main(psS32 argc, char* argv[])
{
    psLogSetLevel(PS_LOG_INFO);
    psLogSetFormat("HLNM");

    return ! runTestSuite(stderr,"pmAstrometry",tests,argc,argv);
}

static psS32 testFPAAlloc(void)
{
    // XXX: Do something more with these arguments.
    const psMetadata *camera = psMetadataAlloc();
    psDB *db = NULL;
    pmFPA* fpa = pmFPAAlloc(camera, db);

    if (fpa == NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.");
        return 1;
    }

    if (fpa->fromTangentPlane != NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->fromTangentPlane to NULL.");
        return 2;
    }

    if (fpa->toTangentPlane != NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->toTangentPlane to NULL.");
        return 3;
    }
    if (fpa->projection != NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->projection to NULL.");
        return 4;
    }

    if (fpa->concepts == NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->concepts.");
        return 5;
    }

    if (fpa->analysis == NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->analysis.");
        return 6;
    }

    if (fpa->camera != camera) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->camera.");
        return 7;
    }

    if (fpa->chips == NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->chips.");
        return 8;
    }

    if (fpa->header != NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->header to NULL.");
        return 9;
    }

    if (fpa->db != db) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc did not set ->db.");
        return 10;
    }

    psFree(camera);
    psFree(fpa);

    return 0;
}

static psS32 testChipAlloc(void)
{
    // XXX: Do something more with these arguments.
    /*
        const psMetadata *camera = psMetadataAlloc();
        psDB *db = NULL;
        pmFPA* fpa = pmFPAAlloc(camera, db);
        if (fpa == NULL) {
            psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.");
            return 1;
        }
    */
    pmFPA* fpa = NULL;
    pmChip *chip = pmChipAlloc(fpa);
    if (chip == NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmChipAlloc returned a NULL.");
        return 1;
    }

    if (chip->col0 != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->col0 set improperly.\n");
        return 5;
    }

    if (chip->row0 != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->row0 set improperly.\n");
        return 6;
    }

    if (chip->toFPA != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->toChip set improperly.\n");
        return 7;
    }

    if (chip->fromFPA != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->toFPA set improperly.\n");
        return 8;
    }

    if (chip->concepts == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->concepts set improperly.\n");
        return 21;
    }

    if (chip->analysis == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->analysis set improperly.\n");
        return 10;
    }

    if (chip->cells == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->cells set improperly.\n");
        return 22;
    }

    if (chip->parent != fpa) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->parent set improperly.\n");
        return 23;
    }

    if (chip->valid != false) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->valid set improperly.\n");
        return 24;
    }

    if (chip->extname != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->extname set improperly.\n");
        return 25;
    }

    if (chip->pixels != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->pixels set improperly.\n");
        return 26;
    }

    if (chip->header != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "chip->header set improperly.\n");
        return 27;
    }

    //    psFree(camera);
    psFree(chip);
    psFree(fpa);

    return 0;
}


static psS32 testCellAlloc(void)
{
    // XXX: Do something more with these arguments.
    /*
        const psMetadata *camera = psMetadataAlloc();
        psDB *db = NULL;
        pmFPA* fpa = pmFPAAlloc(camera, db);
        if (fpa == NULL) {
            psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.n");
            return 1;
        }
     
        pmChip *chip = pmChipAlloc(fpa);
        if (chip == NULL) {
            psLogMsg(__func__,PS_LOG_ERROR, "pmChipAlloc returned a NULL.n");
            return 2;
        }
    */
    pmFPA* fpa = NULL;
    pmChip *chip = NULL;
    pmCell *cell = pmCellAlloc(chip);
    if (cell == NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmCellAlloc returned a NULL.n");
        return 3;
    }

    if (cell->col0 != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->col0 set improperly.\n");
        return 5;
    }

    if (cell->row0 != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->row0 set improperly.\n");
        return 6;
    }

    if (cell->toChip != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->toChip set improperly.\n");
        return 7;
    }

    if (cell->toFPA != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->toFPA set improperly.\n");
        return 8;
    }

    if (cell->toSky != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->toSky set improperly.\n");
        return 9;
    }

    if (cell->analysis == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->analysis set improperly.\n");
        return 10;
    }

    if (cell->concepts == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->concepts set improperly.\n");
        return 21;
    }

    if (cell->readouts == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->readouts set improperly.\n");
        return 22;
    }

    if (cell->parent != chip) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->parent set improperly.\n");
        return 23;
    }

    if (cell->valid != false) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->valid set improperly.\n");
        return 24;
    }

    if (cell->extname != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->extname set improperly.\n");
        return 25;
    }

    if (cell->pixels != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->pixels set improperly.\n");
        return 26;
    }

    if (cell->header != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "cell->header set improperly.\n");
        return 27;
    }

    psFree(fpa);
    psFree(chip);
    psFree(cell);

    return 0;
}

static psS32 testReadoutAlloc(void)
{
    // XXX: Do something more with these arguments.
    /*
        const psMetadata *camera = psMetadataAlloc();
        psDB *db = NULL;
        pmFPA* fpa = pmFPAAlloc(camera, db);
     
        if (fpa == NULL) {
            psLogMsg(__func__,PS_LOG_ERROR, "pmFPAAlloc returned a NULL.\n");
            return 1;
        }
     
        pmChip *chip = pmChipAlloc(fpa);
        if (chip == NULL) {
            psLogMsg(__func__,PS_LOG_ERROR, "pmChipAlloc returned a NULL.\n");
            return 2;
        }
     
        pmCell *cell = pmCellAlloc(chip);
        if (cell == NULL) {
            psLogMsg(__func__,PS_LOG_ERROR, "pmCellAlloc returned a NULL.\n");
            return 3;
        }
    */

    pmFPA* fpa = NULL;
    pmChip *chip = NULL;
    pmCell *cell = NULL;
    pmReadout *readout = pmReadoutAlloc(cell);
    if (readout == NULL) {
        psLogMsg(__func__,PS_LOG_ERROR, "pmReadoutAlloc returned a NULL.\n");
        return 4;
    }

    if (readout->col0 != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->col0 set improperly.\n");
        return 5;
    }

    if (readout->row0 != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->row0 set improperly.\n");
        return 6;
    }

    if (readout->colBins != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->colBins set improperly.\n");
        return 7;
    }

    if (readout->rowBins != -1) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->colBins set improperly.\n");
        return 8;
    }

    if (readout->image != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->image set improperly.\n");
        return 9;
    }

    if (readout->mask != NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->mask set improperly.\n");
        return 10;
    }

    if (readout->analysis == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->analysis set improperly.\n");
        return 11;
    }

    if (readout->concepts == NULL) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->concepts set improperly.\n");
        return 12;
    }

    if (readout->parent != cell) {
        psLogMsg(__func__, PS_LOG_ERROR, "pmReadout->parent set improperly.\n");
        return 15;
    }

    psFree(fpa);
    psFree(chip);
    psFree(cell);
    psFree(readout);

    return 0;
}
