
/** @file  tst_psMetadata_04.c
*
*  @brief Test driver for psMetadata functions
*
*  This test driver contains the following tests for psMetadata:
*     Test A - Allocate metadata and items
*     Test B - Lookup metadata item by name
*     Test C - Attempt to use null metadata
*     Test D - Attempt to use null key
*     Test E - Attempt to lookup non-existant metadata item
*     Test F - Lookup metadata item by index
*     Test G - Attempt to use null metadata
*     Test H - Attempt to get non-existant metadata item
*     Test I - Free psMetadata
*     Test J - Attempt to look up with an invalid metadata object
*
*  @author  Ross Harman, MHPCC
*
*  @version $Revision: 1.5 $  $Name: not supported by cvs2svn $
*  @date  $Date: 2004-10-08 20:53:21 $
*
*  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;
    psMetadataItem *item2 = NULL;
    psMetadataItem *item3 = NULL;
    psMetadataItem *item4 = NULL;
    psMetadataItem *item = NULL;
    psMetadata *metadata = NULL;
    psHash* tmpTable = NULL;
    psList* tmpList = NULL;

    // Test A - Allocate metadata and items
    printPositiveTestHeader(stdout, "psMetadata", "Test A - Allocate metadata items");
    metadata = psMetadataAlloc();
    item1 = psMetadataItemAlloc("myItem1", PS_META_BOOL, "I am a boolean", true);
    item2 = psMetadataItemAlloc("myItem2", PS_META_S32, "I am a integer", 55);
    item3 = psMetadataItemAlloc("myItem3", PS_META_BOOL, "I am a boolean", false);
    item4 = psMetadataItemAlloc("myItem4", PS_META_S32, "I am a integer", 66);
    printMetadataItem(item1);
    printMetadataItem(item2);
    printMetadataItem(item3);
    printMetadataItem(item4);
    psMetadataAddItem(metadata, item1, PS_LIST_HEAD);
    psMetadataAddItem(metadata, item2, PS_LIST_HEAD);
    psMetadataAddItem(metadata, item3, PS_LIST_HEAD);
    psMetadataAddItem(metadata, item4, PS_LIST_HEAD);
    printFooter(stdout, "psMetadata", "Test A - Allocate metadata items", true);


    // Test B - Lookup metadata item by name
    printPositiveTestHeader(stdout, "psMetadata", "Test B - Lookup metadata item by name");
    item = psMetadataLookup(metadata, "myItem2");
    printf("Found item named %s\n", item->name);
    if(item == NULL) {
        printf("ERROR: Item should not be null\n");
        return 10;
    }
    printFooter(stdout, "psMetadata", "Test B - Lookup metadata item by name", true);


    // Test C - Attempt to use null metadata
    printNegativeTestHeader(stdout,"psMetadata", "Test C - Attempt to use null metadata",
                            "Null metadata collection not allowed", 0);
    if ( psMetadataLookup(NULL, "myItem2") != NULL ) {
        psError(__func__,"psMetadataLookup did not return NULL with null metadata.");
        return 11;
    }
    printFooter(stdout, "psMetadata", "Test C - Attempt to use null metadata", true);


    // Test D - Attempt to use null key
    printNegativeTestHeader(stdout,"psMetadata", "Test D - Attempt to use null key",
                            "Null key name not allowed", 0);
    if ( psMetadataLookup(metadata, NULL) != NULL ) {
        psError(__func__,"psMetadata did not return NULL with null key.");
        return 12;
    }
    printFooter(stdout, "psMetadata", "Test D - Attempt to use null key", true);


    // Test E - Attempt to lookup non-existant metadata item
    printNegativeTestHeader(stdout,"psMetadata", "Test E - Attempt to lookup non-existant metadata item",
                            "Couldn't find metadata item. Name: AARGH", 0);
    if ( psMetadataLookup(metadata, "AARGH") != NULL ) {
        psError(__func__,"psMetadata did not return NULL with a non-existant item.");
        return 13;
    }
    printFooter(stdout, "psMetadata", "Test E - Attempt to lookup non-existant metadata item", true);


    // Test F - Lookup metadata item by index
    printPositiveTestHeader(stdout, "psMetadata", "Test F - Lookup metadata item by index");
    item = psMetadataGet(metadata, 0);
    printf("Found item named %s\n", item->name);
    if(item == NULL) {
        printf("ERROR: Item should not be null\n");
        return 14;
    }
    printFooter(stdout, "psMetadata", "Test F - Lookup metadata item by index", true);


    // Test G - Attempt to use null metadata
    printNegativeTestHeader(stdout,"psMetadata", "Test G - Attempt to use null metadata",
                            "Null metadata collection not allowed", 0);
    item = psMetadataGet(NULL, 0);
    if ( item != NULL ) {
        psError(__func__,"psMetadataGet did not return NULL with null metadata structure.");
        return 15;
    }
    printFooter(stdout, "psMetadata", "Test G - Attempt to use null metadata", true);


    // Test H - Attempt to get non-existant metadata item
    printNegativeTestHeader(stdout,"psMetadata", "Test H - Attempt to get non-existant metadata item",
                            "Couldn't find metadata item with given index. Index: 22", 0);
    if ( psMetadataGet(metadata, 22) != NULL ) {
        psError(__func__,"psMetadataGet did not return NULL with non-existant metadata item.");
        return 16;
    }
    printFooter(stdout, "psMetadata", "Test H - Attempt to get non-existant metadata item", true);

    // Test J - Attempt to look up with an invalid metadata object
    printNegativeTestHeader(stdout,"psMetadata","Test J - Attemp to look up with an invalid metadata",
                            "Lookup item with invalid metadata object.", 0 );
    tmpTable = metadata->table;
    metadata->table = NULL;
    if ( psMetadataLookup(metadata,"myItem2") != NULL ) {
        psError(__func__,"psMetadataLookup did not return null for invalid metadata object.");
        return 20;
    }
    metadata->table = tmpTable;
    printFooter(stdout, "psMetadata","Test J - Attempt to lookup an invalid metadata object.",true);

    // Test K - Attempt get item  with an invalid metadata object
    printNegativeTestHeader(stdout,"psMetadata","Test K - Attemp to get item with an invalid metadata",
                            "Get item with invalid metadata object.", 0 );
    tmpList = metadata->list;
    metadata->list = NULL;
    if ( psMetadataGet(metadata,0) != NULL ) {
        psError(__func__,"psMetadataLookup did not return null for invalid metadata object.");
        return 20;
    }
    metadata->list = tmpList;
    printFooter(stdout, "psMetadata","Test K - Attempt to get item with  invalid metadata object.",true);

    // Test I - Free psMetadata
    printPositiveTestHeader(stdout, "psMetadata", "Test I - Free psMetadata");
    psFree(metadata);
    psFree(item1);
    psFree(item2);
    psFree(item3);
    psFree(item4);
    if ( psMemCheckLeaks(0, NULL, stdout) != 0 ) {
        psError(__func__,"memory leaks detected.");
        return 20;
    }
    psMemCheckCorruption(0);
    int nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
        return 21;
    }
    printFooter(stdout, "psMetadata", "Test I - Free psMetadata", true);


    return 0;
}
