/** @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
*
*  @version $Revision: 1.5 $  $Name: not supported by cvs2svn $
*  @date  $Date: 2004-10-07 22:31:31 $
*
*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
*
*/

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

static void printMetadataItem(psMetadataItem *item)
{
    printf("Key Name: %8s  ", item->name);
    printf("Key Type: %2d  ", item->type);

    switch (item->type ) {
    case PS_META_BOOL:
        printf("Key Value: %15d  ", item->data.B);
        break;
    case PS_META_S32:
        printf("Key Value: %15d  ", item->data.S32);
        break;
    case PS_META_F32:
        printf("Key Value: %15.3f  ", item->data.F32);
        break;
    case PS_META_F64:
        printf("Key Value: %15.3f  ", item->data.F64);
        break;
    case PS_META_STR:
        printf("Key Value: %15s  ", (char*)item->data.V);
        break;
    case PS_META_IMG:
    case PS_META_JPEG:
    case PS_META_PNG:
    case PS_META_ASTROM:
    case PS_META_UNKNOWN:
        printf("Value: unknown ");
        break;
    default:
        printf("Bad type: %d ", item->type);
    }
    printf("Key Comment: %s\n", item->comment);
}


int main( int 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_META_BOOL, "I am a boolean", true);
    item2 = psMetadataItemAlloc("myItem2", PS_META_S32, "I am a signed integer", 111);
    item3 = psMetadataItemAlloc("myItem3", PS_META_F32, "I am a single precision floating point", 222.222);
    item4 = psMetadataItemAlloc("myItem4", PS_META_F64, "I am a double precision floating point", 333.333);
    item5 = psMetadataItemAlloc("myItem5", PS_META_STR, "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_META_STR, "I am a string", "HELLO WORLD");
    if (badItem != NULL) {
        psError(__func__,"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 psMetadataType: 6", 0);
    badItem = psMetadataItemAlloc("badItem", PS_META_IMG, "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);
    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);
    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);
    psMemCheckLeaks(0, NULL, stdout);
    psMemCheckCorruption(0);
    int nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psMetadata", "Test G - Free psMetadata", true);

    return 0;
}
