/*****************************************************************************/
/*
 * Test code
 */
#include <stdlib.h>
//#include <stdio.h>
//#include <assert.h>
//#include <string.h>
#include <math.h>
#include "psLib.h"
#include "psMetadata.h"

int main(void)
{
    psSetTraceLevel("metadata", 1);	// turn on tracing
    psSetLogFormat("NM");		// suppress some of the psLogMsg verbosity

    psMetadata *ms = psMetadataAlloc(); // set of metadata
    psMetadataItem *meta = NULL;	    // used to traverse a set of metadata

    for (int i = 0; i < 10; i += 5) {
	float sqrti = sqrt(i);
	psMetadataAppend(ms, PS_META_INT, &i, NULL, "const.%d", i);
	psMetadataAppend(ms, PS_META_FLOAT, &sqrti, "square root", "const.sqrt%d", i);
    }
    psMetadataAppend(ms, PS_META_STR | PS_META_NON_UNIQUE, "Bonjour", "French", "lang.hello");
    psMetadataAppend(ms, PS_META_STR | PS_META_NON_UNIQUE, "Aloha", "Hawaiian", "lang.hello");
    psMetadataAppend(ms, PS_META_STR | PS_META_NON_UNIQUE, "Good Morning", "English", "lang.hello");
    /*
     * Print all metadata starting "lang"
     */
    psMetadataItemFree(psMetadataRemove(ms, "lang.hello.0"));

    psMetadataSetIterator(ms);
    while ((meta = psMetadataGetNext(ms, "lang")) != NULL) {
	psMetadataItemPrint(stdout, meta, "");
    }
    /*
     * Remove a key
     */
    psMetadataItemFree(psMetadataRemove(ms, "lang.hello"));
    /*
     * Print all metadata
     */
    fprintf(stdout, "------\n");
    psMetadataSetIterator(ms);
    while ((meta = psMetadataGetNext(ms, NULL)) != NULL) {
	psMetadataItemPrint(stdout, meta, "");
    }
    /*
     * Look up a key by name
     */
    fprintf(stdout, "------\n");
    fprintf(stdout, "Looking up by name:\n");
    meta = psMetadataLookup(ms, "const.5");
    if (meta != NULL) {
	psMetadataItemPrint(stdout, meta, "");
    }
    /*
     * Clean up
     */
    psMetadataFree(ms);
    /*
     * Check for memory leaks
     */
    fprintf(stderr,"Checking for memory leaks:\n");
    psTraceReset();
    psMemCheckLeaks(0, NULL, stderr);
    
    return 0;
}
