/*****************************************************************************
    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
int 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);
}

int main()
{
    psHash *myHashTable = NULL;
    int testStatus      = true;
    int i               = 0;
    char *myKeys[] = {"ENTRY00", "ENTRY01", "ENTRY02", "ENTRY03", NULL
                     };
    char *myData[] = {"IDA", "IDB", "IDC", "IDD", NULL
                     };
    int currentId = psMemGetId();
    int 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;
    }

    psFree(myLinkList);

    printFooter(stdout,
                "psHash functions",
                "psHashKeyList()",
                testStatus);

    psFree(myHashTable);

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

    return(!testStatus);
}
