/** @file  tst_psLookupTable_01.c
*
*  @brief Test driver for psLookupTable functions
*
*  This test driver contains the following tests for psMetadata:
*     Test A - Read table and interpolate value
*     Test B - Read table and interpolate all values
*     Test C - Read beyond lower table bound
*     Test D - Read beyond upper table bound
*     Test E - Free data
*
*  @author  Ross Harman, MHPCC
*
*  @version $Revision: 1.5.2.1 $  $Name: not supported by cvs2svn $
*  @date  $Date: 2005-01-19 02:17:05 $
*
*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
*
*/

#include "pslib.h"
#include "psTest.h"
#include <string.h>



int main(int argc, char* argv[])
{


    // Test A - Read table and interpolate value
    printPositiveTestHeader(stdout, "psMetadata", "Test A - Read table and interpolate value");
    psLookupStatusType status1 = 0;
    psF64 index1 = 2450905.0;
    psF64 out1 = 0.0;
    psLookupTable* table1 = NULL;

    table1 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6);
    table1 = psLookupTableRead(table1);
    out1 = psLookupTableInterpolate(table1, index1, 0, &status1);
    if(fabs(out1-31.5)>FLT_EPSILON) {
        printf("ERROR: Incorrect value, %lf. Expected 31.5\n", out1);
    } else if(status1 != PS_LOOKUP_SUCCESS) {
        printf("ERROR: Bad return status, %d\n", status1);
    }
    printFooter(stdout, "psMetadata", "Test A - Read table and interpolate value", true);


    // Test B - Read table and interpolate all values
    printPositiveTestHeader(stdout, "psMetadata", "Test B - Read table and interpolate all values");
    psU64 i = 0;
    psF64 index2 = 2450905.0;
    psVector *out2 = NULL;
    psLookupTable* table2 = NULL;
    psVector *stats2 = NULL;
    psF64 truthData2[] = {31.5, 41317, 0};

    table2 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6);
    table2 = psLookupTableRead(table2);
    stats2 = psVectorAlloc(table2->numCols, PS_TYPE_U32);
    out2 = psLookupTableInterpolateAll(table2, index2, stats2);

    for(i=0; i<table2->numCols; i++) {
        if(fabs(out2->data.F64[i]-truthData2[i])>FLT_EPSILON) {
            printf("ERROR: Incorrect value, %lf. Expected %lf\n", out2->data.F64[i], truthData2[i]);
        } else if(status1 != PS_LOOKUP_SUCCESS) {
            printf("ERROR: Bad return status, %d for column %lld\n", stats2->data.U32[i], (long long int)i);
        }
    }
    printFooter(stdout, "psMetadata", "Test B - Read table and interpolate all values", true);


    // Test C - Read beyond lower table bound
    printPositiveTestHeader(stdout, "psMetadata", "Test C - Read beyond lower table bound");
    psLookupStatusType status3 = 0;
    psF64 index3 = 2451179.7;
    psF64 out3 = 0.0;
    psLookupTable* table3 = NULL;

    table3 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6);
    table3 = psLookupTableRead(table3);
    out3 = psLookupTableInterpolate(table3, index3, 0, &status3);
    if(status3 != PS_LOOKUP_PAST_BOTTOM) {
        printf("ERROR: Bad return status, %d\n", status3);
    }
    printFooter(stdout, "psMetadata", "Test C - Read beyond lower table bound", true);

    // Test D - Read beyond upper table bound
    printPositiveTestHeader(stdout, "psMetadata", "Test D - Read beyond upper table bound");
    psLookupStatusType status4 = 0;
    psF64 index4 = 0.0;
    psF64 out4 = 0.0;
    psLookupTable* table4 = NULL;

    table4 = psLookupTableAlloc(UTC_DAT_FILE, 0, 2451179.6);
    table4 = psLookupTableRead(table4);
    out4 = psLookupTableInterpolate(table4, index4, 0, &status4);
    if(status4 != PS_LOOKUP_PAST_TOP) {
        printf("ERROR: Bad return status, %d\n", status4);
    }
    printFooter(stdout, "psMetadata", "Test D - Read beyond upper table bound", true);


    // Test E - Free data
    printPositiveTestHeader(stdout, "psMetadata", "Test E - Free data");
    psFree(table1);
    psFree(table2);
    psFree(table3);
    psFree(table4);
    psFree(out2);
    psFree(stats2);
    if (psMemCheckLeaks(0, NULL, stdout, false)) {
        psError(PS_ERR_UNKNOWN,true,"Memory leaks detected");
        return 10;
    }
    psMemCheckCorruption(0);
    int nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psMetadata", "Test E - Free data", true);


    return 0;
}
