
/** @file  tst_psMetadata_06.c
*
*  @brief Test driver for psMetadata functions
*
*  This test driver contains the following tests for psMetadata:
*     Test A - Allocate metadata and items
*     Test B - Add leaf node on top of existing leaf node
*     Test C - Add leaf node to existing folder node
*     Test D - Add folder node on top of existing leaf node
*     Test E - Free psMetadata
*
*  @author  Ross Harman, MHPCC
*
*  @version $Revision: 1.3 $  $Name: not supported by cvs2svn $
*  @date  $Date: 2004-10-12 01:54:00 $
*
*  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 *item1a = NULL;
    psMetadataItem *item1b = NULL;
    psMetadataItem *item1c = NULL;
    psMetadataItem *item2a = NULL;
    psMetadataItem *item2b = NULL;
    psMetadata *metadata = NULL;


    // Test A - Allocate metadata and items
    printPositiveTestHeader(stdout, "psMetadata", "Test A - Allocate metadata and items");
    item1a = psMetadataItemAlloc("myItem1", PS_META_BOOL, "I am a boolean", true);
    item1b = psMetadataItemAlloc("myItem1", PS_META_S32, "I am a signed integer", 111);
    item1c = psMetadataItemAlloc("myItem1", PS_META_S32, "I am a signed integer", 222);
    item2a = psMetadataItemAlloc("myItem2", PS_META_S32, "I am a signed integer", 333);
    item2b = psMetadataItemAlloc("myItem2", PS_META_ITEM_SET, "I am a folder node", NULL);
    metadata = psMetadataAlloc();
    printMetadataItem(item1a);
    printMetadataItem(item1b);
    printMetadataItem(item1c);
    printMetadataItem(item2a);
    printMetadataItem(item2b);
    printFooter(stdout, "psMetadata", "Test A - Allocate metadata and items", true);


    // Test B - Add leaf node on top of existing leaf node
    printPositiveTestHeader(stdout, "psMetadata", "Test B - Add leaf node on top of existing leaf node");
    if( ! psMetadataAddItem(metadata, item1a, PS_LIST_HEAD) ) {
        psError(__func__,"psMetadataAddItem returned false for adding a node top.");
        return 20;
    }
    if ( ! psMetadataAddItem(metadata, item1b, PS_LIST_HEAD) ) {
        psError(__func__,"psMetadataAddItem returned false for adding a node top.");
        return 21;
    }

    printFooter(stdout, "psMetadata", "Test B - Add leaf node on top of existing leaf node", true);


    // Test C - Add leaf node to existing folder node
    printPositiveTestHeader(stdout, "psMetadata", "Test C - Add leaf node to existing folder node");
    if ( ! psMetadataAddItem(metadata, item1c, PS_LIST_HEAD) ) {
        psError(__func__,"psMetadataAddItem return false for adding a not to existing node.");
        return 22;
    }
    printFooter(stdout, "psMetadata", "Test C - Add leaf node to existing folder node", true);


    // Test D - Add folder node on top of existing leaf node
    printPositiveTestHeader(stdout, "psMetadata", "Test D - Add folder node on top of existing leaf node");
    if ( ! psMetadataAddItem(metadata, item2a, PS_LIST_HEAD) ) {
        psError(__func__,"psMetadataAddItem returned false for adding a node to existing leaf node.");
        return 23;
    }
    if ( ! psMetadataAddItem(metadata, item2b, PS_LIST_HEAD) ) {
        psError(__func__,"psMetadataAddItem returned false for adding a nod to existing leaf node.");
        return 24;
    }
    printFooter(stdout, "psMetadata", "Test D - Add folder node on top of existing leaf node", true);


    // Test E - Free psMetadata
    printPositiveTestHeader(stdout, "psMetadata", "Test E - Free psMetadata");
    psFree(item1a);
    psFree(item1b);
    psFree(item1c);
    psFree(item2a);
    psFree(item2b);
    psFree(metadata);
    if ( psMemCheckLeaks(0, NULL, stdout) != 0 ) {
        psError(__func__,"Memory leaks detected.");
        return 25;
    }
    psMemCheckCorruption(0);
    int nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psMetadata", "Test E - Free psMetadata", true);


    return 0;
}
