/*****************************************************************************
    This code will test whether a hash table can be de-allocated successfully.
 *****************************************************************************/
#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 currentId = psMemGetId();
    ID* id = NULL;
    int memLeaks        = 0;

    printPositiveTestHeader(stdout,
                            "psHash functions",
                            "psHashFree()");

    myHashTable = psHashAlloc(NUM_HASH_TABLE_BUCKETS);
    id = IdAlloc("IDA");
    psHashAdd(myHashTable, "ENTRY00", id);
    psFree(id);
    id = IdAlloc("IDB");
    psHashAdd(myHashTable, "ENTRY01", id);
    psFree(id);
    id = IdAlloc("IDC");
    psHashAdd(myHashTable, "ENTRY02", id);
    psFree(id);
    id = IdAlloc("IDD");
    psHashAdd(myHashTable, "ENTRY03", id);
    psFree(id);
    psFree(myHashTable);

    if (imGlobal != 4) {
        fprintf(stderr, "%s: only (%d/4) entries were freed",
                __func__, imGlobal);
        testStatus = false;
    }

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

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

    return (!testStatus);
}
