/** @file  tst_psMatrix_05.c
*
*  @brief Test driver for psMatrix multiplication function
*
*  This test driver contains the following tests for psMatrix test point 5:
*     A)  Create input and output images
*     B)  Multiply images
*     C)  Free input and output images
*
*  @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=0; i<IMAGE->numRows; i++) {           \
    for(int j=0; j<IMAGE->numCols; j++) {       \
        printf("%f ", IMAGE->data.F64[i][j]);   \
    }                                          \
    printf("\n");                              \
}


int main( int argc,
          char* argv[] )
{
    psImage * outImage = NULL;
    psImage *inImage1 = NULL;
    psImage *inImage2 = NULL;


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


    // Test B - Multiply images
    printPositiveTestHeader( stdout, "psMatrix", "Multiply images" );
    psMatrixMultiply( outImage, inImage1, inImage2 );
    PRINT_MATRIX( outImage );
    printFooter( stdout, "psMatrix", "Multiply images", true );


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

    return 0;
}
