/** @file  tst_psMetadata_02.c
 *
 *  @brief Test driver for psMetadata functions
 *
 *  This test driver contains the following tests for psMetadata:
 *     Test A - Allocate metadata items
 *     Test B - Attempt to create metadata item with null name
 *     Test C - Attempt to create metadata item with invalid type
 *     Test D - Allocate metadata
 *     Test E - Attempt to add metadata item to null metadata
 *     Test F - Attempt to add null metadata item to metadata
 *     Test G - Free psMetadata
 *
 *  @author  Ross Harman, MHPCC
 *  @author  Robert DeSonia, MHPCC
 *
 *  @version $Revision: 1.2 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2005-09-26 21:13:33 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 *
 */

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

static void printMetadataItem(psMetadataItem *metadataItem)
{
    printf("Key Name: %8s  ", metadataItem->name);
    printf("Key mdType: 0x%08x  ", metadataItem->type);

    switch (metadataItem->type) {
    case PS_DATA_METADATA_MULTI:
        printf("Key Value: %17c", ' ');
        break;
    case PS_DATA_BOOL:
        printf("Key Value: %15d  ", metadataItem->data.B);
        break;
    case PS_DATA_S32:
        printf("Key Value: %15d  ", metadataItem->data.S32);
        break;
    case PS_DATA_F32:
        printf("Key Value: %15.3f  ", metadataItem->data.F32);
        break;
    case PS_DATA_F64:
        printf("Key Value: %15.3f  ", metadataItem->data.F64);
        break;
    case PS_DATA_STRING:
        printf("Key Value: %15s  ", (char*)metadataItem->data.V);
        break;
    default:
        printf("Bad type: %d ", metadataItem->type);
    }
    printf("Key Comment: %s\n", metadataItem->comment);
}


psS32 main( psS32 argc, char* argv[] )
{
    psMetadataItem *item1 = NULL;
    psMetadata *metadata = NULL;
    psMetadataItem *item2 = NULL;
    psMetadataItem *item3 = NULL;
    psMetadataItem *item4 = NULL;
    psMetadataItem *item5 = NULL;
    psMetadataItem *badItem = NULL;


    // Test A - Allocate metadata items
    printPositiveTestHeader(stdout, "psMetadata", "Test A - Allocate metadata items");
    item1 = psMetadataItemAlloc("myItem1", PS_DATA_BOOL, "I am a boolean", true);
    item2 = psMetadataItemAlloc("myItem2", PS_DATA_S32, "I am a signed integer", 111);
    item3 = psMetadataItemAlloc("myItem3", PS_DATA_F32, "I am a single precision floating point", 222.222);
    item4 = psMetadataItemAlloc("myItem4", PS_DATA_F64, "I am a double precision floating point", 333.333);
    item5 = psMetadataItemAlloc("myItem5", PS_DATA_STRING, "I am a string", "HELLO WORLD");
    printMetadataItem(item1);
    printMetadataItem(item2);
    printMetadataItem(item3);
    printMetadataItem(item4);
    printMetadataItem(item5);
    printFooter(stdout, "psMetadata", "Test A - Allocate metadata items", true);


    // Test B - Attempt to create metadata item with null name
    printNegativeTestHeader(stdout,"psMetadata", "Test B - Attempt to create metadata item with null name",
                            "Null value for name not allowed", 0);
    psLogMsg(__func__,PS_LOG_INFO,"Following should produce error for null name.");
    badItem = psMetadataItemAlloc(NULL, PS_DATA_STRING, "I am a string", "HELLO WORLD");
    if (badItem != NULL) {
        psError(PS_ERR_UNKNOWN, true,"psMetadataItemAlloc did not return null with null name item.");
        return 10;
    }
    printFooter(stdout, "psMetadata", "Test B - Attempt to create metadata item with null name", true);


    // Test C - Attempt to create metadata item with invalid type
    printNegativeTestHeader(stdout,"psMetadata", "Test C - Attempt to create metadata item with invalid type",
                            "Invalid psDataType: 6", 0);
    badItem = psMetadataItemAlloc("badItem", -1, "I am bad", "Bad comment");
    printFooter(stdout, "psMetadata", "Test C - Attempt to create metadata item with invalid type", true);


    // Test D - Allocate metadata
    printPositiveTestHeader(stdout, "psMetadata", "Test D - Allocate metadata");
    metadata = psMetadataAlloc();
    printFooter(stdout, "psMetadata", "Test D - Allocate metadata", true);


    // Test E - Attempt add metadata item to null metadata
    printNegativeTestHeader(stdout,"psMetadata", "Test E - Attempt to add metadata item to null metadata",
                            "Null metadata collection not allowed", 0);
    psMetadataAddItem(NULL, item1, PS_LIST_HEAD, PS_META_DEFAULT);
    printFooter(stdout, "psMetadata", "Test E - Attempt to add metadata item to null metadata", true);


    // Test F - Attempt add null metadata item to metadata
    printNegativeTestHeader(stdout,"psMetadata", "Test F - Attempt to add null metadata item to metadata",
                            "Null metadata item not allowed", 0);
    psMetadataAddItem(metadata, NULL, PS_LIST_HEAD, PS_META_DEFAULT);
    printFooter(stdout, "psMetadata", "Test F - Attempt to add null metadata item to metadata", true);


    // Test G - Free psMetadata
    printPositiveTestHeader(stdout, "psMetadata", "Test G - Free psMetadata");
    psFree(item1);
    psFree(item2);
    psFree(item3);
    psFree(item4);
    psFree(item5);
    psFree(badItem);
    psFree(metadata);
    if(psMemCheckLeaks(0, NULL, stdout,false) ) {
        psError(PS_ERR_UNKNOWN,true,"Memory leaks detected.");
        return 10;
    }
    psMemCheckCorruption(0);
    psS32 nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psMetadata", "Test G - Free psMetadata", true);

    return 0;
}
