/*****************************************************************************
    This code will test whether a hash table can be allocated successfully,
    then deallocated successfully.
 *****************************************************************************/
#include <stdio.h>
#include "pslib.h"
#include "psTest.h"
#include "psHash.h"
#define NUM_HASH_TABLE_BUCKETS 10
psS32 main()
{
    psHash *myHashTable = NULL;
    psS32 testStatus      = true;
    psS32 i               = 0;
    psS32 currentId = psMemGetId();
    psS32 memLeaks        = 0;
    printPositiveTestHeader(stdout,
                            "psHash functions",
                            "psHashAlloc()");

    myHashTable = psHashAlloc(NUM_HASH_TABLE_BUCKETS);

    if (myHashTable == NULL) {
        fprintf(stderr, "%s: could not allocate a hash table.", __func__);
        testStatus = false;
    }

    if (myHashTable->nbucket != NUM_HASH_TABLE_BUCKETS) {
        fprintf(stderr, "%s: myHashTable->nbucket not set properly.\n",
                __func__);
        testStatus = false;

    }

    if (myHashTable->buckets == NULL) {
        fprintf(stderr, "%s: myHashTable->buckets is NULL.\n",
                __func__);
        testStatus = false;

    }

    for (i=0;i<NUM_HASH_TABLE_BUCKETS;i++) {
        if (myHashTable->buckets[i] != NULL) {
            fprintf(stderr, "%s: hash table bucket[%d] not equal to NULL.\n",
                    __func__, i);
            testStatus = false;
        }
    }

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

    psFree(myHashTable);

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

    return (!testStatus);
}
