/** 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.
* 
*    @version $Revision: 1.20 $  $Name: not supported by cvs2svn $
*    @date $Date: 2005-03-29 22:50:16 $
*
*  Copyright 2004-2005 Maui High Performance Computing Center, Univ. of Hawaii
*   
*****************************************************************************/
#include <stdio.h>
#include "pslib.h"
#include "psTest.h"

// Defines
#define ORDER    3

static psS32 testPolynomial1DAlloc(void);
static psS32 testPolynomial2DAlloc(void);
static psS32 testPolynomial3DAlloc(void);
static psS32 testPolynomial4DAlloc(void);
static psS32 testDPolynomial1DAlloc(void);
static psS32 testDPolynomial2DAlloc(void);
static psS32 testDPolynomial3DAlloc(void);
static psS32 testDPolynomial4DAlloc(void);

testDescription tests[] = {
                              {testPolynomial1DAlloc,578,"psPolynomial1DAlloc",0,false},
                              {testPolynomial2DAlloc,578,"psPolynomial2DAlloc",0,false},
                              {testPolynomial3DAlloc,578,"psPolynomial3DAlloc",0,false},
                              {testPolynomial4DAlloc,578,"psPolynomial4DAlloc",0,false},
                              {testDPolynomial1DAlloc,579,"psDPolynomial1DAlloc",0,false},
                              {testDPolynomial2DAlloc,579,"psDPolynomial2DAlloc",0,false},
                              {testDPolynomial3DAlloc,579,"psDPolynomial3DAlloc",0,false},
                              {testDPolynomial4DAlloc,579,"psDPolynomial4DAlloc",0,false},
                              {NULL}
                          };


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

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


// This test will allocate a 1D polynomial and verify the structure allocated
psS32 testPolynomial1DAlloc(void)
{
    psPolynomial1D*  my1DPoly  = NULL;

    // Allocate polynomial
    my1DPoly = psPolynomial1DAlloc(ORDER,PS_POLYNOMIAL_ORD);
    // Verify structure allocated
    if(my1DPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my1DPoly->n != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my1DPoly->n, ORDER);
        return 2;
    }
    if(my1DPoly->type != PS_POLYNOMIAL_ORD) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my1DPoly->type, PS_POLYNOMIAL_ORD);
        return 3;
    }
    for(psS32 i = 0; i < ORDER; i++) {
        if(my1DPoly->coeff[i] != 0.0) {
            psError(PS_ERR_UNKNOWN,true,"Coeff[%d] %lg not as expected %lg",
                    i, my1DPoly->coeff[i], 0.0);
            return 4;
        }
        if(my1DPoly->coeffErr[i] != 0.0) {
            psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d] %lg not as expected %lg",
                    i, my1DPoly->coeffErr[i], 0.0);
            return 5;
        }
        if(my1DPoly->mask[i] != 0) {
            psError(PS_ERR_UNKNOWN,true,"Mask[%d] %d not as expected %d",
                    i, my1DPoly->mask[i], 0);
            return 6;
        }
    }
    psFree(my1DPoly);

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

    return 0;
}

// This test will allocate a 1D polynomial and verify the structure allocated
psS32 testDPolynomial1DAlloc(void)
{
    psDPolynomial1D*  my1DDPoly  = NULL;

    // Allocate polynomial
    my1DDPoly = psDPolynomial1DAlloc(ORDER,PS_POLYNOMIAL_CHEB);
    // Verify structure allocated
    if(my1DDPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my1DDPoly->n != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my1DDPoly->n, ORDER);
        return 2;
    }
    if(my1DDPoly->type != PS_POLYNOMIAL_CHEB) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my1DDPoly->type, PS_POLYNOMIAL_CHEB);
        return 3;
    }
    for(psS32 i = 0; i < ORDER; i++) {
        if(my1DDPoly->coeff[i] != 0.0) {
            psError(PS_ERR_UNKNOWN,true,"Coeff[%d] %lg not as expected %lg",
                    i, my1DDPoly->coeff[i], 0.0);
            return 4;
        }
        if(my1DDPoly->coeffErr[i] != 0.0) {
            psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d] %lg not as expected %lg",
                    i, my1DDPoly->coeffErr[i], 0.0);
            return 5;
        }
        if(my1DDPoly->mask[i] != 0) {
            psError(PS_ERR_UNKNOWN,true,"Mask[%d] %d not as expected %d",
                    i, my1DDPoly->mask[i], 0);
            return 6;
        }
    }
    psFree(my1DDPoly);

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

    return 0;
}

// This test will allocate a 2D polynomial and verify the structure allocated
psS32 testPolynomial2DAlloc(void)
{
    psPolynomial2D* my2DPoly = NULL;

    // Allocate polynomial
    my2DPoly = psPolynomial2DAlloc(ORDER,ORDER+1,PS_POLYNOMIAL_ORD);
    // Verify structure allocated
    if(my2DPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my2DPoly->nX != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my2DPoly->nX, ORDER);
        return 2;
    }
    // Verify polynomial structure members set properly
    if(my2DPoly->nY != ORDER+1) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my2DPoly->nY, ORDER+1);
        return 3;
    }
    if(my2DPoly->type != PS_POLYNOMIAL_ORD) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my2DPoly->type, PS_POLYNOMIAL_ORD);
        return 4;
    }
    for(psS32 i = 0; i < ORDER; i++) {
        for(psS32 j = 0; j < ORDER+1; j++) {
            if(my2DPoly->coeff[i][j] != 0.0) {
                psError(PS_ERR_UNKNOWN,true,"Coeff[%d][%d] %lg not as expected %lg",
                        i, j, my2DPoly->coeff[i][j], 0.0);
                return 5;
            }
            if(my2DPoly->coeffErr[i][j] != 0.0) {
                psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d][%d] %lg not as expected %lg",
                        i, j, my2DPoly->coeffErr[i][j], 0.0);
                return 6;
            }
            if(my2DPoly->mask[i][j] != 0) {
                psError(PS_ERR_UNKNOWN,true,"Mask[%d][%d] %d not as expected %d",
                        i, j, my2DPoly->mask[i][j], 0);
                return 7;
            }
        }
    }
    psFree(my2DPoly);

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

    return 0;
}

// This test will allocate a 2D polynomial and verify the structure allocated
psS32 testDPolynomial2DAlloc(void)
{
    psDPolynomial2D* my2DDPoly = NULL;

    // Allocate polynomial
    my2DDPoly = psDPolynomial2DAlloc(ORDER,ORDER+1,PS_POLYNOMIAL_CHEB);
    // Verify structure allocated
    if(my2DDPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my2DDPoly->nX != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my2DDPoly->nX, ORDER);
        return 2;
    }
    // Verify polynomial structure members set properly
    if(my2DDPoly->nY != ORDER+1) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my2DDPoly->nY, ORDER+1);
        return 3;
    }
    if(my2DDPoly->type != PS_POLYNOMIAL_CHEB) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my2DDPoly->type, PS_POLYNOMIAL_ORD);
        return 4;
    }
    for(psS32 i = 0; i < ORDER; i++) {
        for(psS32 j = 0; j < ORDER+1; j++) {
            if(my2DDPoly->coeff[i][j] != 0.0) {
                psError(PS_ERR_UNKNOWN,true,"Coeff[%d][%d] %lg not as expected %lg",
                        i, j, my2DDPoly->coeff[i][j], 0.0);
                return 5;
            }
            if(my2DDPoly->coeffErr[i][j] != 0.0) {
                psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d][%d] %lg not as expected %lg",
                        i, j, my2DDPoly->coeffErr[i][j], 0.0);
                return 6;
            }
            if(my2DDPoly->mask[i][j] != 0) {
                psError(PS_ERR_UNKNOWN,true,"Mask[%d][%d] %d not as expected %d",
                        i, j, my2DDPoly->mask[i][j], 0);
                return 7;
            }
        }
    }
    psFree(my2DDPoly);

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

    return 0;
}

// This test will allocate a 3D polynomial and verify the structure allocated
psS32 testPolynomial3DAlloc(void)
{
    psPolynomial3D* my3DPoly = NULL;

    // Allocate polynomial
    my3DPoly = psPolynomial3DAlloc(ORDER,ORDER+1,ORDER+2,PS_POLYNOMIAL_ORD);
    // Verify structure allocated
    if(my3DPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my3DPoly->nX != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my3DPoly->nX, ORDER);
        return 2;
    }
    // Verify polynomial structure members set properly
    if(my3DPoly->nY != ORDER+1) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my3DPoly->nY, ORDER+1);
        return 3;
    }
    // Verify polynomial structure members set properly
    if(my3DPoly->nZ != ORDER+2) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my3DPoly->nZ, ORDER+2);
        return 4;
    }
    if(my3DPoly->type != PS_POLYNOMIAL_ORD) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my3DPoly->type, PS_POLYNOMIAL_ORD);
        return 5;
    }
    for(psS32 i = 0; i < ORDER; i++) {
        for(psS32 j = 0; j < ORDER+1; j++) {
            for(psS32 k = 0; k < ORDER+2; k++) {
                if(my3DPoly->coeff[i][j][k] != 0.0) {
                    psError(PS_ERR_UNKNOWN,true,"Coeff[%d][%d][%d] %lg not as expected %lg",
                            i, j, k, my3DPoly->coeff[i][j][k], 0.0);
                    return 6;
                }
                if(my3DPoly->coeffErr[i][j][k] != 0.0) {
                    psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d][%d][%d] %lg not as expected %lg",
                            i, j, k, my3DPoly->coeffErr[i][j][k], 0.0);
                    return 7;
                }
                if(my3DPoly->mask[i][j][k] != 0) {
                    psError(PS_ERR_UNKNOWN,true,"Mask[%d][%d] %d not as expected %d",
                            i, j, k, my3DPoly->mask[i][j][k], 0);
                    return 8;
                }
            }
        }
    }
    psFree(my3DPoly);

    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psPolynomial3DAlloc(-1,1,1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 9;
    }
    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psPolynomial3DAlloc(1,-1,1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 10;
    }
    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psPolynomial3DAlloc(1,1,-1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 11;
    }

    return 0;
}

// This test will allocate a 3D polynomial and verify the structure allocated
psS32 testDPolynomial3DAlloc(void)
{
    psDPolynomial3D* my3DDPoly = NULL;

    // Allocate polynomial
    my3DDPoly = psDPolynomial3DAlloc(ORDER,ORDER+1,ORDER+2,PS_POLYNOMIAL_CHEB);
    // Verify structure allocated
    if(my3DDPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my3DDPoly->nX != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my3DDPoly->nX, ORDER);
        return 2;
    }
    // Verify polynomial structure members set properly
    if(my3DDPoly->nY != ORDER+1) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my3DDPoly->nY, ORDER+1);
        return 3;
    }
    // Verify polynomial structure members set properly
    if(my3DDPoly->nZ != ORDER+2) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my3DDPoly->nZ, ORDER+2);
        return 4;
    }
    if(my3DDPoly->type != PS_POLYNOMIAL_CHEB) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my3DDPoly->type, PS_POLYNOMIAL_ORD);
        return 5;
    }
    for(psS32 i = 0; i < ORDER; i++) {
        for(psS32 j = 0; j < ORDER+1; j++) {
            for(psS32 k = 0; k < ORDER+2; k++) {
                if(my3DDPoly->coeff[i][j][k] != 0.0) {
                    psError(PS_ERR_UNKNOWN,true,"Coeff[%d][%d][%d] %lg not as expected %lg",
                            i, j, k, my3DDPoly->coeff[i][j][k], 0.0);
                    return 6;
                }
                if(my3DDPoly->coeffErr[i][j][k] != 0.0) {
                    psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d][%d][%d] %lg not as expected %lg",
                            i, j, k, my3DDPoly->coeffErr[i][j][k], 0.0);
                    return 7;
                }
                if(my3DDPoly->mask[i][j][k] != 0) {
                    psError(PS_ERR_UNKNOWN,true,"Mask[%d][%d] %d not as expected %d",
                            i, j, k, my3DDPoly->mask[i][j][k], 0);
                    return 8;
                }
            }
        }
    }
    psFree(my3DDPoly);

    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psDPolynomial3DAlloc(-1,1,1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 9;
    }
    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psDPolynomial3DAlloc(1,-1,1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 10;
    }
    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psDPolynomial3DAlloc(1,1,-1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 11;
    }

    return 0;
}

// This test will allocate a 4D polynomial and verify the structure allocated
psS32 testPolynomial4DAlloc(void)
{
    psPolynomial4D* my4DPoly = NULL;

    // Allocate polynomial
    my4DPoly = psPolynomial4DAlloc(ORDER+3,ORDER,ORDER+1,ORDER+2,PS_POLYNOMIAL_ORD);
    // Verify structure allocated
    if(my4DPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my4DPoly->nX != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DPoly->nX, ORDER);
        return 2;
    }
    // Verify polynomial structure members set properly
    if(my4DPoly->nY != ORDER+1) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DPoly->nY, ORDER+1);
        return 3;
    }
    // Verify polynomial structure members set properly
    if(my4DPoly->nZ != ORDER+2) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DPoly->nZ, ORDER+2);
        return 4;
    }
    // Verify polynomial structure members set properly
    if(my4DPoly->nW != ORDER+3) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DPoly->nW, ORDER+3);
        return 5;
    }
    if(my4DPoly->type != PS_POLYNOMIAL_ORD) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my4DPoly->type, PS_POLYNOMIAL_ORD);
        return 6;
    }
    for(psS32 i = 0; i < ORDER+3; i++) {
        for(psS32 j = 0; j < ORDER; j++) {
            for(psS32 k = 0; k < ORDER+1; k++) {
                for(psS32 l = 0; l < ORDER+2; l++) {
                    if(my4DPoly->coeff[i][j][k][l] != 0.0) {
                        psError(PS_ERR_UNKNOWN,true,"Coeff[%d][%d][%d][%d] %lg not as expected %lg",
                                i, j, k, l, my4DPoly->coeff[i][j][k][l], 0.0);
                        return 7;
                    }
                    if(my4DPoly->coeffErr[i][j][k][l] != 0.0) {
                        psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d][%d][%d][l] %lg not as expected %lg",
                                i, j, k, l, my4DPoly->coeffErr[i][j][k][l], 0.0);
                        return 8;
                    }
                    if(my4DPoly->mask[i][j][k][l] != 0) {
                        psError(PS_ERR_UNKNOWN,true,"Mask[%d][%d][%d][%d] %d not as expected %d",
                                i, j, k, l, my4DPoly->mask[i][j][k][l], 0);
                        return 9;
                    }
                }
            }
        }
    }
    psFree(my4DPoly);

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

    return 0;
}

// This test will allocate a 4D polynomial and verify the structure allocated
psS32 testDPolynomial4DAlloc(void)
{
    psDPolynomial4D* my4DDPoly = NULL;

    // Allocate polynomial
    my4DDPoly = psDPolynomial4DAlloc(ORDER+3,ORDER,ORDER+1,ORDER+2,PS_POLYNOMIAL_ORD);
    // Verify structure allocated
    if(my4DDPoly == NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned NULL not expected");
        return 1;
    }
    // Verify polynomial structure members set properly
    if(my4DDPoly->nX != ORDER) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DDPoly->nX, ORDER);
        return 2;
    }
    // Verify polynomial structure members set properly
    if(my4DDPoly->nY != ORDER+1) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DDPoly->nY, ORDER+1);
        return 3;
    }
    // Verify polynomial structure members set properly
    if(my4DDPoly->nZ != ORDER+2) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DDPoly->nZ, ORDER+2);
        return 4;
    }
    // Verify polynomial structure members set properly
    if(my4DDPoly->nW != ORDER+3) {
        psError(PS_ERR_UNKNOWN,true,"Number of terms %d not as expected %d",
                my4DDPoly->nW, ORDER+3);
        return 5;
    }
    if(my4DDPoly->type != PS_POLYNOMIAL_ORD) {
        psError(PS_ERR_UNKNOWN,true,"Type %d not as expected %d",
                my4DDPoly->type, PS_POLYNOMIAL_ORD);
        return 6;
    }
    for(psS32 i = 0; i < ORDER+3; i++) {
        for(psS32 j = 0; j < ORDER; j++) {
            for(psS32 k = 0; k < ORDER+1; k++) {
                for(psS32 l = 0; l < ORDER+2; l++) {
                    if(my4DDPoly->coeff[i][j][k][l] != 0.0) {
                        psError(PS_ERR_UNKNOWN,true,"Coeff[%d][%d][%d][%d] %lg not as expected %lg",
                                i, j, k, l, my4DDPoly->coeff[i][j][k][l], 0.0);
                        return 7;
                    }
                    if(my4DDPoly->coeffErr[i][j][k][l] != 0.0) {
                        psError(PS_ERR_UNKNOWN,true,"CoeffErr[%d][%d][%d][l] %lg not as expected %lg",
                                i, j, k, l, my4DDPoly->coeffErr[i][j][k][l], 0.0);
                        return 8;
                    }
                    if(my4DDPoly->mask[i][j][k][l] != 0) {
                        psError(PS_ERR_UNKNOWN,true,"Mask[%d][%d][%d][%d] %d not as expected %d",
                                i, j, k, l, my4DDPoly->mask[i][j][k][l], 0);
                        return 9;
                    }
                }
            }
        }
    }
    psFree(my4DDPoly);

    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psDPolynomial4DAlloc(-1,1,1,1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 10;
    }
    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psDPolynomial4DAlloc(1,-1,1,1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 11;
    }
    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psDPolynomial4DAlloc(1,1,-1,1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 12;
    }
    // Attempt to allocate with negative order
    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error msg for negative terms");
    if(psDPolynomial4DAlloc(1,1,1,-1,PS_POLYNOMIAL_ORD) != NULL) {
        psError(PS_ERR_UNKNOWN,true,"Returned structure but expected NULL");
        return 13;
    }

    return 0;
}

