/*****************************************************************************
    This routine must ensure that PS_STAT_MIN is correctly computed
    by the procedure psVectorStats().
 *****************************************************************************/
#include <stdio.h>
#include "pslib.h"
#include "psTest.h"
#define N 10

psS32 main()
{
    psStats *myStats    = NULL;
    psS32 testStatus      = true;
    psS32 globalTestStatus = true;
    psS32 i               = 0;
    psVector *myVector  = NULL;
    psVector *maskVector= NULL;
    float min           = 1e99;
    float realMinNoMask   = 1e99;
    float realMinWithMask = 1e99;
    psS32 currentId       = psMemGetId();
    psS32 memLeaks        = 0;

    /*************************************************************************/
    /*  Allocate and initialize data structures                      */
    /*************************************************************************/
    myStats = psStatsAlloc(PS_STAT_MIN);
    myVector = psVectorAlloc(N, PS_TYPE_F32);
    myVector->n = N;
    maskVector = psVectorAlloc(N, PS_TYPE_U8);
    maskVector->n = N;

    // Set the appropriate values for the vector data.
    for (i=0;i<N;i++) {
        myVector->data.F32[i] = (float) i;
    }

    // Set the mask vector and calculate the expected minimum.
    for (i=0;i<N;i++) {
        if (myVector->data.F32[i] < realMinNoMask) {
            realMinNoMask = myVector->data.F32[i];
        }

        if (i < (N/2)) {
            maskVector->data.U8[i] = 1;
        } else {
            maskVector->data.U8[i] = 0;
            if (myVector->data.F32[i] < realMinWithMask) {
                realMinWithMask = myVector->data.F32[i];
            }
        }
    }

    /*************************************************************************/
    /*  Call psVectorStats() with no vector mask.                    */
    /*************************************************************************/
    printPositiveTestHeader(stdout,
                            "psStats functions",
                            "PS_STAT_MIN: no vector mask");

    myStats = psVectorStats(myStats, myVector, NULL, NULL, 0);
    min = myStats->min;

    printf("Called psVectorStats() on a vector with no elements masked.\n");
    printf("The expected min was %f; the calculated min was %f\n",
           realMinNoMask, min);
    if (min == realMinNoMask) {
        testStatus = true;
    } else {
        testStatus = false;
        globalTestStatus = false;
    }
    printFooter(stdout,
                "psVector functions",
                "PS_STAT_MIN: no vector mask",
                testStatus);

    /*************************************************************************/
    /*  Call psVectorStats() with vector mask.                       */
    /*************************************************************************/
    printPositiveTestHeader(stdout,
                            "psStats functions",
                            "PS_STAT_MIN: with vector mask");

    myStats = psVectorStats(myStats, myVector, NULL, maskVector, 1);
    min = myStats->min;
    printf("Called psVectorStats() on a vector with last N/2 elements masked.\n");
    printf("The expected min was %f; the calculated min was %f\n",
           realMinWithMask, min);
    if (min == realMinWithMask) {
        testStatus = true;
    } else {
        testStatus = false;
        globalTestStatus = false;
    }

    printFooter(stdout,
                "psVector functions",
                "PS_STAT_MIN: with vector mask",
                testStatus);

    /*************************************************************************/
    /*  Deallocate data structures                                   */
    /*************************************************************************/
    printPositiveTestHeader(stdout,
                            "psStats functions",
                            "psStats(): deallocating memory");

    psFree(myStats);
    psFree(myVector);
    psFree(maskVector);

    psMemCheckCorruption(1);
    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    if (0 != memLeaks) {
        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    }

    printFooter(stdout,
                "psVector functions",
                "psStats(): deallocating memory",
                testStatus);

    return (!globalTestStatus);
}
