/*****************************************************************************
    This code will test whether the call psHashKeyList() function works.
 *****************************************************************************/
#include <stdio.h>
#include "pslib.h"
#include "psTest.h"
#include "psHash.h"
#define NUM_HASH_TABLE_BUCKETS 100
psS32 imGlobal = 0;

typedef struct
{
    char *name;
}
ID;
static void IdFree(ID *id);

static ID *IdAlloc(const char *name)
{
    ID *id = psAlloc(sizeof(ID));
    p_psMemSetDeallocator(id,(psFreeFcn)IdFree);

    id->name = psStringCopy(name);

    return id;
}

static void IdFree(ID *id)
{
    imGlobal++;
    psFree(id->name);
}

psS32 main()
{
    psHash *myHashTable = NULL;
    psS32 testStatus      = true;
    psS32 i               = 0;
    char *myKeys[] = {"ENTRY00", "ENTRY01", "ENTRY02", "ENTRY03", NULL
                     };
    char *myData[] = {"IDA", "IDB", "IDC", "IDD", NULL
                     };
    psS32 currentId = psMemGetId();
    psS32 memLeaks        = 0;
    psList *myLinkList = NULL;
    psListElem *tmp = NULL;
    ID* id = NULL;

    printPositiveTestHeader(stdout,"psHash functions","psHashKeyList()");
    myHashTable = psHashAlloc(NUM_HASH_TABLE_BUCKETS);
    i = 0;
    while (myKeys[i] != NULL) {
        id = IdAlloc(myData[i]);
        psHashAdd(myHashTable, myKeys[i], id);
        psFree(id);
        i++;
    }
    myLinkList = psHashKeyList(myHashTable);
    tmp = myLinkList->head;
    while (tmp != NULL) {
        printf("Linked List Entries: %s\n", (char *) tmp->data);
        tmp = tmp->next;
    }
    printFooter(stdout,"psHash functions","psHashKeyList()",testStatus);
    psFree(myLinkList);

    printNegativeTestHeader(stdout,"psHashKeyList","NULL table","Can not lookup with NULL table",0);
    myLinkList = psHashKeyList(NULL);
    if(myLinkList != NULL) {
        psError(PS_ERR_UNKNOWN,true,"psHashKeyList retrieved a key list from a NULL table.");
        return 1;
    }
    printFooter(stdout,"psHashKeyList","NULL table",true);

    psFree(myHashTable);

    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    if (memLeaks != 0) {
        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    }
    psMemCheckCorruption(1);

    return(!testStatus);
}
