/** @file  tst_psMetadata_07.c
*
*  @brief Test driver for psMetadataIterator functions
*
*  This test driver contains the following tests for psMetadata:
*     Test A - Allocate a psMetadataIterator
*     Test B - Set a psMetadataIterator
*     Test C - Get and Increment a psMetadataIterator
*     Test D - Get and Decrement a psMetadataIterator
*     Test E - psMetadataConfigWrite (also tests psMetadataConfigFormat)
*
*  @author  David Robbins, MHPCC
*
*  @version $Revision: 1.7 $  $Name: not supported by cvs2svn $
*  @date  $Date: 2005-09-12 23:05:00 $
*
*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
*
*/
#include "config.h"
#include "pslib_strict.h"
#include "psTest.h"
//#include "psMetadata.h"

static psS32 testMetaIter(void);
static psS32 testMetaWrite(void);

testDescription tests[] = {
                              {testMetaIter, 0, "Test psMetadataIterator fxns", 0, false},
                              {testMetaWrite, 0, "Test psMetadataConfigWrite", 0, false},
                              {NULL}
                          };

static psMetadata *setupMeta(void);

static psMetadata *setupMeta(void)
{
    psMetadata *md = NULL;
    psVector *vec = NULL;
    psMetadata *newMD = NULL;
    psTime *time;
    int i = 0;
    md = psMetadataAlloc();
    newMD = psMetadataAlloc();
    vec = psVectorAlloc(60, PS_DATA_S32);
    time = psTimeAlloc(PS_TIME_TAI);
    for (i = 0; i < 5; i++) {
        vec->data.S32[i] = i+1;
    }
    time->sec = 1000;
    time->nsec = 25;
    time->leapsecond = true;
    psMetadataAddBool(md, PS_LIST_TAIL, "item1", 0, "I am a boolean", true);
    psMetadataAddS32(md, PS_LIST_TAIL, "item2", 0, "I am a integer", 55);
    psMetadataAddF32(md, PS_LIST_TAIL, "item3", 0, "I am a float", 3.14);
    psMetadataAddF64(md, PS_LIST_TAIL, "item4", 0, "I am a double", 6.28);
    psMetadataAddStr(md, PS_LIST_TAIL, "item5", 0, "I am a string", "GNIRTS");
    psMetadataAddVector(md, PS_LIST_TAIL, "vector6", 0, "I am a vector", vec);
    psMetadataAddTime(md, PS_LIST_TAIL, "time01", 0, "I am time", time);

    psMetadataAddS32(newMD, PS_LIST_TAIL, "ITEM01", 0, "I AM INT", 666);
    psMetadata *newestMD = NULL;
    newestMD = psMetadataAlloc();
    psMetadataAddVector(newestMD, PS_LIST_TAIL, "VECTORNEW", 0, "Newest VECTOR", vec);
    psMetadataAddStr(newestMD, PS_LIST_TAIL, "cell", 0, "I am a p-Star", "pStArRs");
    psMetadataAddMetadata(newMD, PS_LIST_TAIL, "META NEW", 0, "I AM Newest METADATA", newestMD);
    psMetadataAddF32(newMD, PS_LIST_TAIL, "ITEM02", 0, "I AM FLOAT", 666.6);
    psMetadataAddF64(newMD, PS_LIST_TAIL, "ITEM03", 0, "I AM DOUBLE", 666.666);

    psMetadataAddMetadata(md, PS_LIST_TAIL, "metadata7", 0, "I am a metadata", newMD);
    psFree(time);
    psFree(newestMD);
    psFree(vec);
    psFree(newMD);
    return md;
}

int main(int argc, char* argv[])
{
    psLogSetLevel( PS_LOG_INFO );
    if( !runTestSuite(stderr,"psMetadata_07",tests,argc,argv)) {
        return 1;
    }
    return 0;
}

//Set a psMetadataIterator //
static psS32 testMetaIter(void)
{
    psMetadata *metadata = NULL;
    metadata = setupMeta();
    psMetadataIterator *iter = NULL;
    psMetadataItem *item1 = NULL;
    psMetadataItem *item2 = NULL;
    psMetadataItem *item3 = NULL;
    printPositiveTestHeader(stdout, "psMetadata", "Test A - Allocate psMetadataIterator");
    iter = psMetadataIteratorAlloc(metadata, PS_LIST_HEAD, NULL);
    if (iter == NULL) {
        fprintf(stderr, "Failed test point - Allocate a psMetadataIterator.  Iterator NULL. \n");
    }
    printFooter(stdout, "psMetadata", "Test A - Allocate psMetadataIterator", true);
    printPositiveTestHeader(stdout, "psMetadata", "Test B - Set psMetadataIterator");
    psMetadataIteratorSet(iter, 2);
    item1 = psMetadataGetAndIncrement(iter);
    if ( !strncmp(item1->name, "item2", 20) ) {
        fprintf(stderr, "Failed test point - IteratorSet - Item names don't match. \n");
    }
    printFooter(stdout, "psMetadata", "Test B - Set psMetadataIterator", true);
    printPositiveTestHeader(stdout, "psMetadata", "Test C - Get and Increment Iterator");
    item2 = psMetadataGetAndDecrement(iter);
    if ( !strncmp(item2->name, "item3", 20) ) {
        fprintf(stderr, "Failed test point - GetAndIncrement - Item names don't match. \n");
    }
    printFooter(stdout, "psMetadata", "Test C - Get and Increment Iterator", true);
    printPositiveTestHeader(stdout, "psMetadata", "Test D - Get and Decrement Iterator");
    item3 = psMetadataGetAndDecrement(iter);
    if ( !strncmp(item3->name, "item2", 20) ) {
        fprintf(stderr, "Failed test point - GetAndDecrement - Item names don't match. \n");
    }
    printFooter(stdout, "psMetadata", "Test D - Get and Decrement Iterator", true);
    psFree(iter);
    psFree(metadata);
    return 0;
}

static psS32 testMetaWrite(void)
{
    psMetadata *newMD = NULL;
    newMD = setupMeta();
    printPositiveTestHeader(stdout, "psMetadata", "Test E - psMetadataConfigWrite");
    if ( !psMetadataConfigWrite(newMD, "mdcfgwrt.out") ) {
        fprintf(stderr, "Failed test point E - Failed to write metadata to file. \n");
    }

    printFooter(stdout, "psMetadata", "Test E - psMetadataConfigWrite", true);
    psFree(newMD);
    return 0;
}
