/** @file  tst_psMetadataIO.c
*
*  @brief Test driver for psMetadataIO functions
*
*  This test driver contains the following tests for psMetadata:
*    Test A - Read config file with overwrite set true
*    Test B - Read config file with overwrite set false
*    Test C - Read config file without auto-allocation of metadata
*    Test D - Attempt to use null fileName argument
*    Test E - Attempt to open nonexistant file
*    Test F - Free psMetadata
*
*  @author  Ross Harman, MHPCC
*
*  @version $Revision: 1.2 $  $Name: not supported by cvs2svn $
*  @date  $Date: 2004-10-06 22:51:31 $
*
*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
*
*/

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

static void printMetadataItem(psMetadataItem *metadataItem, char *spaces);

static void printMetadataItemList(psList *metadataItemList)
{
    psMetadataItem *entryChild = NULL;

    psListSetIterator(metadataItemList, PS_LIST_HEAD);
    entryChild = psListGetCurrent(metadataItemList);
    while (entryChild != NULL) {
        printMetadataItem(entryChild, "    ");
        entryChild = psListGetNext(metadataItemList);
    }
}

static void printMetadataList(psList *metadataItemList)
{
    psMetadataItem *entryChild = NULL;

    psListSetIterator(metadataItemList, PS_LIST_HEAD);
    entryChild = psListGetCurrent(metadataItemList);
    while (entryChild != NULL) {
        printMetadataItem(entryChild, " ");
        entryChild = psListGetNext(metadataItemList);
    }
}
static void printMetadataItem(psMetadataItem *metadataItem, char *spaces)
{

    psVector *vec = NULL;


    printf("%sKey Name: %8s  ", spaces, metadataItem->name);
    printf("Key Type: %2d  ", metadataItem->type);

    switch (metadataItem->type ) {
    case PS_META_ITEM_SET:
        printf("Key Value: %17c", ' ');
        break;
    case PS_META_BOOL:
        printf("Key Value: %15d  ", metadataItem->data.B);
        break;
    case PS_META_S32:
        printf("Key Value: %15d  ", metadataItem->data.S32);
        break;
    case PS_META_F32:
        printf("Key Value: %15.3f  ", metadataItem->data.F32);
        break;
    case PS_META_F64:
        printf("Key Value: %15.3f  ", metadataItem->data.F64);
        break;
    case PS_META_STR:
        printf("Key Value: %15s  ", (char*)metadataItem->data.V);
        break;
    case PS_META_VEC:
        vec = (psVector*)metadataItem->data.V;
        printf("Key Value: ");
        for(int i=0; i<vec->nalloc;i++) {
            printf("%d ", vec->data.S32[i]);
        }
        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 ", metadataItem->type);
    }
    printf("Key Comment: %s\n", metadataItem->comment);

    if(metadataItem->items->size) {
        printMetadataItemList(metadataItem->items);
    }
}

static void printMetadataTable(psHash *mdTable)
{
    int i;
    psHashBucket* ptr = NULL;
    for(i=0; i<mdTable->nbucket; i++) {
        ptr = mdTable->buckets[i];
        while (ptr != NULL) {
            printMetadataItem(ptr->data, " ");
            ptr = ptr->next;
        }
    }
}

static void printMetadata(psMetadata *metadata)
{
    printf("\nContents of metadata list:\n");
    printMetadataList(metadata->list);
    printf("\nContents of metadata table:\n");
    printMetadataTable(metadata->table);
}


int main(int argc, char* argv[])
{
    // Test A - Read config file with overwrite set true
    printPositiveTestHeader(stdout, "psMetadata", "Test A - Read config file with overwrite set true");
    psMetadata *metadata1 = NULL;
    int failedLines1 = 0;
    failedLines1 = psMetadataParseConfig(&metadata1, "test.config", true);
    printf("Failed lines: %d Expected: 6\n", failedLines1);
    printMetadata(metadata1);
    printFooter(stdout, "psMetadata", "Test A - Read config file with overwrite set true", true);


    // Test B - Read config file with overwrite set false
    printPositiveTestHeader(stdout, "psMetadata", "Test B - Read config file with overwrite set false");
    psMetadata *metadata2 = NULL;
    int failedLines2 = 0;
    failedLines2 = psMetadataParseConfig(&metadata2, "test.config", false);
    printf("Failed lines: %d Expected: 7", failedLines2);
    printMetadata(metadata2);
    printFooter(stdout, "psMetadata", "Test B - Read config file with overwrite set false", true);


    // Test C - Read config file without auto-allocation of metadata
    printPositiveTestHeader(stdout, "psMetadata", "Test C - Read config file without auto-allocation of metadata");
    psMetadata *metadata3 = psMetadataAlloc();
    int failedLines3 = 0;
    failedLines3 = psMetadataParseConfig(&metadata3, "test.config", false);
    printf("Failed lines: %d Expected: 7", failedLines3);
    printMetadata(metadata3);
    printFooter(stdout, "psMetadata", "Test C - Read config file without auto-allocation of metadata", true);


    // Test D - Attempt to use null fileName argument
    printNegativeTestHeader(stdout,"psMetadataIO", "Test D - Attempt to use null fileName argument",
                            "Null failedLines not allowed", 0);
    psMetadataParseConfig(&metadata1, NULL, true);
    printFooter(stdout, "psMetadata", "Test D - Attempt to use null fileName argument", true);


    // Test E - Attempt to open nonexistant file
    printNegativeTestHeader(stdout,"psMetadataIO", "Test E - Attempt to open nonexistant file",
                            "Error opening file", 0);
    psMetadataParseConfig(&metadata1, "abcedfg", true);
    printFooter(stdout, "psMetadata", "Test E - Attempt to open nonexistant file", true);


    // Test F - Free psMetadata
    printPositiveTestHeader(stdout, "psMetadata", "Test F - Free psMetadata");
    psFree(metadata1);
    psFree(metadata2);
    psFree(metadata3);
    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 F - Free psMetadata", true);


    return 0;
}
