/** tst_Func00.c
*
*    This routine must ensure that the psPolynomial structures are
*    allocated and deallocated by the psPolynomialXXXlloc() procedures.
*    It also calls the various psPolynomialXXXEval() procedures.
*
*    The F32 and F64 polynomials are tested for all orders (1 - 4) and for
*    both ordinary and chebyshev polynomials.
*
*    NOTE: This test code requries the stdout file to verify that the results
*    are good.
*
*    XXX: Modify these tests so that polynomials with a variety of different
*    orders are created.
* 
*    XXX: Compare to FLT_EPSILON
* 
*    @version $Revision: 1.1 $  $Name: not supported by cvs2svn $
*    @date $Date: 2006-12-21 20:05:12 $
*
*  Copyright 2004-2005 Maui High Performance Computing Center, Univ. of Hawaii
*
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <pslib.h>
#include "tap.h"
#include "pstap.h"

#define ORDER    3
#define VERBOSE  0

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

    plan_tests(26);

    // This test will allocate a 1D polynomial and verify the structure allocated
    {
        psMemId id = psMemGetId();
        psPolynomial1D* my1DPoly  = NULL;

        // Allocate polynomial
        my1DPoly = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, ORDER);
        ok(my1DPoly != NULL, "1D polynomial allocated successfully");
        skip_start(my1DPoly == NULL, 3, "Skipping tests because psPolynomial1DAlloc() failed");

        // Verify polynomial structure members set properly
        ok(my1DPoly->nX == ORDER, "psPolynomial1DAlloc(): Number of terms set correctly");
        ok(my1DPoly->type == PS_POLYNOMIAL_ORD, "psPolynomial1DAlloc():  type set correctly");
        bool errorFlag = false;
        for(psS32 i = 0; i < ORDER+1; i++)
        {
            if (my1DPoly->coeff[i] != 0.0) {
                diag("Coeff[%d] %lg not as expected %lg", i, my1DPoly->coeff[i], 0.0);
                errorFlag = true;
            }
            if (my1DPoly->coeffErr[i] != 0.0) {
                diag("CoeffErr[%d] %lg not as expected %lg", i, my1DPoly->coeffErr[i], 0.0);
                errorFlag = true;
            }
            if (my1DPoly->mask[i] != 0) {
                diag("Mask[%d] %d not as expected %d", i, my1DPoly->mask[i], 0);
                errorFlag = true;
            }
        }
        ok(!errorFlag, "psPolynomial1D coefficients set correctly");

        skip_end();
        psFree(my1DPoly);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }


    if (0) {
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial1DAlloc(PS_POLYNOMIAL_ORD, -1) != NULL) {
            diag("psPolynomial1DAlloc() Returned structure but expected NULL");
        }
    }

    // This test will allocate a 2D polynomial and verify the structure allocated
    {
        psMemId id = psMemGetId();
        psPolynomial2D* my2DPoly = NULL;

        // Allocate polynomial
        my2DPoly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, ORDER,ORDER+1);
        ok(my2DPoly != NULL, "2D polynomial allocated successfully");
        skip_start(my2DPoly == NULL, 4, "Skipping tests because psPolynomial2DAlloc() failed");

        // Verify polynomial structure members set properly
        ok(my2DPoly->nX == ORDER, "psPolynomial2DAlloc(): Number of terms (nX) set correctly");
        ok(my2DPoly->nY == ORDER+1, "psPolynomial2DAlloc(): Number of terms (nY) set correctly");
        ok(my2DPoly->type == PS_POLYNOMIAL_ORD, "psPolynomial2DAlloc(): type set correctly");

        bool errorFlag = false;
        for(psS32 i = 0; i < ORDER+1; i++)
        {
            for(psS32 j = 0; j < ORDER+2; j++) {
                if (fabs(my2DPoly->coeff[i][j]) > FLT_EPSILON) {
                    diag("Coeff[%d][%d] %lg not as expected %lg", i, j, my2DPoly->coeff[i][j], 0.0);
                    errorFlag = true;
                }
                if (my2DPoly->coeffErr[i][j] != 0.0) {
                    diag("CoeffErr[%d][%d] %lg not as expected %lg", i, j, my2DPoly->coeffErr[i][j], 0.0);
                    errorFlag = true;
                }
                if (my2DPoly->mask[i][j] != 0) {
                    diag("Mask[%d][%d] %d not as expected %d", i, j, my2DPoly->mask[i][j], 0);
                    errorFlag = true;
                }
            }
        }
        ok(!errorFlag, "psPolynomial2D coefficients set correctly");
        skip_end();
        psFree(my2DPoly);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }

    if (0) {
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, -1, 1) != NULL) {
            diag("psPolynomial2DAlloc() returned structure but expected NULL");
        }
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, 1, -1) != NULL) {
            diag("psPolynomial2DAlloc() returned structure but expected NULL");
        }
    }

    // This test will allocate a 3D polynomial and verify the structure allocated
    {
        psMemId id = psMemGetId();
        psPolynomial3D* my3DPoly = NULL;

        // Allocate polynomial
        my3DPoly = psPolynomial3DAlloc(PS_POLYNOMIAL_ORD, ORDER, ORDER+1, ORDER+2);
        ok(my3DPoly != NULL, "3D polynomial allocated successfully");
        skip_start(my3DPoly == NULL, 5, "Skipping tests because psPolynomial3DAlloc() failed");

        // Verify polynomial structure members set properly
        ok(my3DPoly->nX == ORDER, "psPolynomial3DAlloc(): Number of terms (nX) set correctly");
        ok(my3DPoly->nY == ORDER+1, "psPolynomial3DAlloc(): Number of terms (nY) set correctly");
        ok(my3DPoly->nZ == ORDER+2, "psPolynomial3DAlloc(): Number of terms (nZ) set correctly");
        ok(my3DPoly->type == PS_POLYNOMIAL_ORD, "psPolynomial3DAlloc(): type set correctly");

        bool errorFlag = false;
        for(psS32 i = 0; i < ORDER+1; i++)
        {
            for(psS32 j = 0; j < ORDER+2; j++) {
                for(psS32 k = 0; k < ORDER+3; k++) {
                    if (my3DPoly->coeff[i][j][k] != 0.0) {
                        diag("Coeff[%d][%d][%d] %lg not as expected %lg",
                             i, j, k, my3DPoly->coeff[i][j][k], 0.0);
                        errorFlag = true;
                    }
                    if (my3DPoly->coeffErr[i][j][k] != 0.0) {
                        diag("CoeffErr[%d][%d][%d] %lg not as expected %lg",
                             i, j, k, my3DPoly->coeffErr[i][j][k], 0.0);
                        errorFlag = true;
                    }
                    if (my3DPoly->mask[i][j][k] != 0) {
                        diag("Mask[%d][%d][%d] %d not as expected %d",
                             i, j, k, my3DPoly->mask[i][j][k], 0);
                        errorFlag = true;
                    }
                }
            }
        }
        ok(!errorFlag, "psPolynomial3D coefficients set correctly");
        skip_end();
        psFree(my3DPoly);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }
    if (0) {
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial3DAlloc(PS_POLYNOMIAL_ORD, -1, 1, 1) != NULL) {
            diag("psPolynomial3DAlloc(): returned structure but expected NULL");
        }
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial3DAlloc(PS_POLYNOMIAL_ORD, 1, -1, 1) != NULL) {
            diag("psPolynomial3DAlloc() returned structure but expected NULL");
        }
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial3DAlloc(PS_POLYNOMIAL_ORD, 1, 1, -1) != NULL) {
            diag("psPolynomial3DAlloc(): returned structure but expected NULL");
        }
    }

    // This test will allocate a 4D polynomial and verify the structure allocated
    {
        psMemId id = psMemGetId();
        psPolynomial4D* my4DPoly = NULL;

        // Allocate polynomial
        my4DPoly = psPolynomial4DAlloc(PS_POLYNOMIAL_ORD, ORDER,ORDER+1,ORDER+2,ORDER+3);
        ok(my4DPoly != NULL, "4D polynomial allocated successfully");
        skip_start(my4DPoly == NULL, 6, "Skipping tests because psPolynomial4DAlloc() failed");

        // Verify polynomial structure members set properly
        ok(my4DPoly->nX == ORDER, "psPolynomial4DAlloc(): Number of terms (nX) set correctly");
        ok(my4DPoly->nY == ORDER+1, "psPolynomial4DAlloc(): Number of terms (nY) set correctly");
        ok(my4DPoly->nZ == ORDER+2, "psPolynomial4DAlloc(): Number of terms (nZ) set correctly");
        ok(my4DPoly->nT == ORDER+3, "psPolynomial4DAlloc(): Number of terms (nT) set correctly");
        ok(my4DPoly->type == PS_POLYNOMIAL_ORD, "psPolynomial4D    Alloc(): type set correctly");

        bool errorFlag = false;
        for(psS32 i = 0; i < ORDER+1; i++)
        {
            for(psS32 j = 0; j < ORDER+2; j++) {
                for(psS32 k = 0; k < ORDER+3; k++) {
                    for(psS32 l = 0; l < ORDER+4; l++) {
                        if (my4DPoly->coeff[i][j][k][l] != 0.0) {
                            diag("Coeff[%d][%d][%d][%d] %lg not as expected %lg",
                                 i, j, k, l, my4DPoly->coeff[i][j][k][l], 0.0);
                            errorFlag = true;
                        }
                        if (my4DPoly->coeffErr[i][j][k][l] != 0.0) {
                            diag("CoeffErr[%d][%d][%d][%d] %lg not as expected %lg",
                                 i, j, k, l, my4DPoly->coeffErr[i][j][k][l], 0.0);
                            errorFlag = true;
                        }
                        if (my4DPoly->mask[i][j][k][l] != 0) {
                            diag("Mask[%d][%d][%d][%d] %d not as expected %d",
                                 i, j, k, l, my4DPoly->mask[i][j][k][l], 0);
                            errorFlag = true;
                        }
                    }
                }
            }
        }
        ok(!errorFlag, "psPolynomial4D coefficients set correctly");
        skip_end();
        psFree(my4DPoly);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }

    if (0) {
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial4DAlloc(PS_POLYNOMIAL_ORD, -1, 1, 1, 1) != NULL) {
            diag("psPolynomial4DAlloc(): returned structure but expected NULL");
        }
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial4DAlloc(PS_POLYNOMIAL_ORD, 1, -1, 1, 1) != NULL) {
            diag("psPolynomial4DAlloc(): returned structure but expected NULL");
            psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        }
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial4DAlloc(PS_POLYNOMIAL_ORD, 1, 1, -1, 1) != NULL) {
            diag("psPolynomial4DAlloc(): returned structure but expected NULL");
            psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        }
        // Attempt to allocate with negative order
        psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
        if (psPolynomial4DAlloc(PS_POLYNOMIAL_ORD, 1, 1, 1, -1) != NULL) {
            diag("psPolynomial4DAlloc(): returned structure but expected NULL");
        }
    }
}

