/** @file  tst_psMatrix_04.c
 *
 *  @brief Test driver for psMatrix invert function
 *
 *  This test driver contains the following tests for psMatrix test point 4:
 *     A)  Create input and output images
 *     B)  Invert matrix and calculate determinant
 *     C)  Calculate determinant only
 *     D)  Free input and output images
 *     E)  Attempt to use null input image argument
 *     F)  Attempt to use null input float argument
 *  
 *  @author  Ross Harman, MHPCC
 *
 *  @version $Revision: 1.4 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2004-08-06 22:34:06 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */

#include "pslib.h"
#include "psTest.h"

#define PRINT_MATRIX(IMAGE)                         \
for(int i=IMAGE->numRows-1; i>-1; i--) {            \
    for(int j=0; j<IMAGE->numCols; j++) {           \
        printf("%f ", IMAGE->data.F64[i][j]);       \
    }                                               \
    printf("\n");                                   \
}


int main(int argc,
         char* argv[])
{
    float det = 0.0f;
    float *det2 = NULL;
    psImage *outImage = NULL;
    psImage *inImage = NULL;
    psImage *tempImage = NULL;


    // Test A - Create input and output images
    printPositiveTestHeader(stdout, "psMatrix", "Create input and output images");
    outImage = (psImage*)psImageAlloc(3, 3, PS_TYPE_F64);
    inImage = (psImage*)psImageAlloc(3, 3, PS_TYPE_F64);
    inImage->data.F64[0][0] =  2;
    inImage->data.F64[0][1] =  4;
    inImage->data.F64[0][2] =  3;
    inImage->data.F64[1][0] =  0;
    inImage->data.F64[1][1] =  1;
    inImage->data.F64[1][2] = -1;
    inImage->data.F64[2][0] =  3;
    inImage->data.F64[2][1] =  5;
    inImage->data.F64[2][2] =  7;
    PRINT_MATRIX(inImage);
    printFooter(stdout, "psMatrix", "Create input and output images", true);


    // Test B - Invert matrix and calculate determinant
    printPositiveTestHeader(stdout, "psMatrix", "Invert matrix and calculate determinant");
    tempImage = outImage;
    outImage = psMatrixInvert(outImage, inImage, &det);
    PRINT_MATRIX(outImage);
    printf("\ndet = %f\n", det);
    if(outImage->type.dimen != PS_DIMEN_IMAGE) {
        printf("Error: Resulting image is not PS_DIMEN_IMAGE\n");
    } else
        if(outImage != tempImage) {
            printf("Error: Return pointer not equal to output argument pointer\n");
        }
    printFooter(stdout, "psMatrix", "Invert matrix and calculate determinant", true);


    // Test C - Calculate determinant only
    printPositiveTestHeader(stdout, "psMatrix", "Calculate determinant only");
    det2 = psMatrixDeterminant(inImage);
    printf("det = %f\n", *det2);
    printFooter(stdout, "psMatrix", "Calculate determinant only", true);


    // Test D - Free input and output images
    printPositiveTestHeader(stdout, "psMatrix", "Free input and output images");
    psFree(outImage);
    psFree(inImage);
    psFree(det2);
    psMemCheckLeaks(0, NULL, stdout);
    int nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psMatrix" ,"Free input and output images", true);


    // Test E - Attempt to use null input image argument
    printNegativeTestHeader(stdout,"psMatrix", "Attempt to use null input image argument",
                            "Invalid operation: inImage or its data is NULL.", 0);
    psImage *badOutImage = (psImage*)psImageAlloc(3, 3, PS_TYPE_F64);
    psMatrixInvert(badOutImage, NULL, &det);
    printFooter(stdout, "psMatrix", "Attempt to use null input image argument", true);


    // Test F - Attempt to use null input float argument
    printNegativeTestHeader(stdout,"psMatrix", "Attempt to use null input float argument",
                            "Invalid operation: determinant argument is NULL.", 0);
    psImage *badInImage = (psImage*)psImageAlloc(3, 3, PS_TYPE_F64);
    psMatrixInvert(badOutImage, badInImage, NULL);
    printFooter(stdout, "psMatrix", "Attempt to use null input float argument", true);

    return 0;
}
