/** @file  tst_psTime_04.c
 *
 *  @brief Test driver for psTime functions
 *
 *  This test driver contains the following tests for psTime:
 *      Test A - Initialize time
 *      Test B - Attempt to open non-existant time config file
 *      Test C - Attempt to open non-existant time data files
 *      Test D - Attempt to read incorrect number of files
 *      Test E - Attempt to read incorrect number of from values
 *      Test F - Attempt to read data file with typo in number
 *      Test G - Free data
 *      Test H - Attempt to use all timer functions
 *      Test I - Tidal Corrections to UT1-UTC
 *
 *  @author  Ross Harman, MHPCC
 *  @author  Eric Van Alst, MHPCC
 *  @author  David Robbins, MHPCC
 *
 *  @version $Revision: 1.9 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2006-01-31 23:24:21 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 *
 */
#include "config.h"

#include "pslib_strict.h"
#include "psTest.h"

static psS32 testTimeInit1(void);
static psS32 testTimeInit2(void);
static psS32 testTimeInit3(void);
static psS32 testTimeInit4(void);
static psS32 testTimeInit5(void);
static psS32 testTimeInit6(void);
static psS32 testTimer1(void);
static psS32 testTideUT1Corr(void);

testDescription tests[] = {
                              {testTimeInit1,1,"p_psTimeInit",0,false},
                              {testTimeInit2,2,"p_psTimeInit",0,false},
                              {testTimeInit3,3,"p_psTimeInit",0,false},
                              {testTimeInit4,4,"p_psTimeInit",0,false},
                              {testTimeInit5,5,"p_psTimeInit",0,false},
                              {testTimeInit6,6,"p_psTimeInit",0,false},
                              {testTimer1,8,"psTimer",0,false},
                              {testTideUT1Corr,9,"psTime_TideUT1Corr",0,false},
                              {NULL}
                          };

psS32 main(psS32 argc, char* argv[])
{
    if(!runTestSuite(stderr,"psTime",tests,argc,argv)) {
        return 1;
    }

    return 0;
}

psS32 testTimeInit1(void)
{
    // Test A - Initialize time
    psLibInit("pslib.config");
    psLibFinalize();

    return 0;
}

psS32 testTimeInit2(void)
{
    // Test B - Attempt to open non-existant time config file
    psLibInit("zzz");

    return 0;
}

psS32 testTimeInit3(void)
{
    // Test C - Attempt to open non-existant time data files
    psLibInit("test.psTime.config1");
    psLibFinalize();

    return 0;
}

psS32 testTimeInit4(void)
{
    // Test D - Attempt to read incorrect number of files
    psLibInit("test.psTime.config2");
    psLibFinalize();

    return 0;
}

psS32 testTimeInit5(void)
{
    // Test E - Attempt to read incorrect number of from values
    psLibInit("test.psTime.config3");
    psLibFinalize();

    return 0;
}

psS32 testTimeInit6(void)
{
    // Test F - Attempt to read data file with typo in number
    psLibInit("test.psTime.config4");
    psLibFinalize();

    return 0;
}

psS32 testTimer1(void)
{
    psF64 testTime = 0.0;
    psF64 testTime2 = 0.0;
    psF64 testTime3 = 0.0;

    if ( !psTimerStart("newTime") ) {
        printf("TimerStart Failed\n");
        return 1;
    }
    testTime = psTimerMark("newTime");
    if (testTime == 0.0) {
        printf("TimerMark Failed\n");
        return 2;
    }
    testTime2 = psTimerClear("newTime");
    if (testTime2 == 0.0) {
        printf("TimerClear Failed\n");
        return 3;
    }
    psTimerStart("newTime");
    testTime3 = psTimerStop();
    if (testTime3 == 0.0) {
        printf("TimerStop Failed\n");
        return 4;
    }
    return 0;
}

psS32 testTideUT1Corr(void)
{
    psTime *empty = NULL;
    psTime *tide = NULL;
    psTime *noTide = psTimeAlloc(PS_TIME_UTC);
    noTide->sec = 1049160600;
    noTide->nsec = 0;
    noTide->leapsecond = false;

    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
    empty = psTime_TideUT1Corr(tide);
    if (empty != NULL) {
        psError(PS_ERR_BAD_PARAMETER_NULL, false,
                "psTime_TideUT1Corr failed to return NULL for NULL input time.\n");
        return 1;
    }

    empty = psTime_TideUT1Corr(noTide);
    if (empty->sec != 1049160599 || empty->nsec != 656981971) {
        psError(PS_ERR_BAD_PARAMETER_VALUE, false,
                "psTime_TideUT1Corr failed to return correct values.\n");
        printf("\nsec = %ld, nsec = %u\n", (long int)empty->sec, empty->nsec);
        return 2;
    }

    if (!p_psTimeFinalize()) {
        return 3;
    }

    psFree(empty);
    psFree(noTide);

    return 0;
}

