/** @file  tst_psMatrix_03.c
 *
 *  @brief Test driver for psMatrix LU functions
 *
 *  This test driver contains the following tests for psMatrix test point 3:
 *     A)  Create input and output images and vectors
 *     B)  Calculate LU matrix
 *     C)  Determine solution to matrix equation
 *     D)  Free input and output images and vectors
 *     E)  Attempt to use null image input argument
 *     F)  Attempt to use null input vector argument
 *     G)  ttempt to use null LU image argument
 *
 *  @author  Ross Harman, MHPCC
 *
 *  @version $Revision: 1.8 $  $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");                               \
}
\
#define PRINT_VECTOR(VECTOR)                    \
for(int i=0; i<VECTOR->n; i++) {                \
    printf("%f\n", VECTOR->data.F64[i]);        \
}


int main(int argc,
         char* argv[])
{
    psImage *luImage = NULL;
    psImage *inImage = NULL;
    psImage *tempImage = NULL;
    psVector *tempVector = NULL;
    psVector *perm = NULL;
    psVector *outVector = NULL;
    psVector *inVector = NULL;


    // Test A - Create input and output images and vectors
    printPositiveTestHeader(stdout, "psMatrix", "Create input and output images and vectors");
    luImage = (psImage*)psImageAlloc(3, 3, PS_TYPE_F64);
    perm = (psVector*)psVectorAlloc(3, PS_TYPE_F64);
    outVector = (psVector*)psVectorAlloc(3, PS_TYPE_F64);
    inVector = (psVector*)psVectorAlloc(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] =  6;
    inImage->data.F64[1][0] =  4;
    inImage->data.F64[1][1] =  5;
    inImage->data.F64[1][2] =  6;
    inImage->data.F64[2][0] =  3;
    inImage->data.F64[2][1] =  1;
    inImage->data.F64[2][2] = -2;
    inVector->data.F64[0] = 18.0;
    inVector->data.F64[1] = 24.0;
    inVector->data.F64[2] =  4.0;
    inVector->n = 3;
    PRINT_MATRIX(inImage);
    printf("\n");
    PRINT_VECTOR(inVector);
    printFooter(stdout, "psMatrix", "Create input and output images and vectors", true);


    // Test B - Calculate LU matrix
    printPositiveTestHeader(stdout, "psMatrix", "Calculate LU matrix");
    tempImage = luImage;
    luImage = psMatrixLUD(luImage, perm, inImage);
    PRINT_MATRIX(luImage);
    if(luImage->type.dimen != PS_DIMEN_IMAGE) {
        printf("Error: Resulting image is not PS_DIMEN_IMAGE\n");
    } else
        if(luImage != tempImage) {
            printf("Error: Return pointer not equal to output argument pointer\n");
        }
    printFooter(stdout, "psMatrix", "Calculate LU matrix", true);


    // Test C - Determine solution to matrix equation
    printPositiveTestHeader(stdout, "psMatrix", "Determine solution to matrix equation");
    tempVector = outVector;
    outVector = psMatrixLUSolve(outVector, luImage, inVector, perm);
    PRINT_VECTOR(outVector);
    if(outVector->type.dimen != PS_DIMEN_VECTOR) {
        printf("Error: Resulting image is not PS_DIMEN_VECTOR\n");
    } else
        if(outVector != tempVector) {
            printf("Error: Return pointer not equal to output argument pointer\n");
        }
    printFooter(stdout, "psMatrix", "Determine solution to matrix equation", true);


    // Test D - Free input and output images and vectors
    printPositiveTestHeader(stdout, "psMatrix", "Free input and output images and vectors");
    psFree(inImage);
    psFree(luImage);
    psFree(perm);
    psFree(outVector);
    psFree(inVector);
    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 and vectors", true);


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


    // Test F - Attempt to use null input vector argument
    printNegativeTestHeader(stdout,"psMatrix", "Attempt to use null input vector argument",
                            "Invalid operation: inVector or its data is NULL.", 0);
    psVector *vectorBad = NULL;
    psVector *vectorBadOut = (psVector*)psVectorAlloc(3, PS_TYPE_F64);
    psVector *permBad = (psVector*)psVectorAlloc(3, PS_TYPE_F64);
    psMatrixLUSolve(vectorBadOut, imageTest, vectorBad, permBad);
    printFooter(stdout, "psMatrix", "Attempt to use null input vector argument", true);


    // Test G - Attempt to use null LU image argument
    printNegativeTestHeader(stdout,"psMatrix", "Attempt to use null LU image argument",
                            "Invalid operation: inImage or its data is NULL.", 0);
    psMatrixLUSolve(vectorBadOut, NULL, vectorBad, permBad);
    printFooter(stdout, "psMatrix", "Attempt to use null LU image argument", true);

    return 0;
}
