/** @file  tst_psVector.c
 *
 *  @brief Test driver for psVector integer functions
 *
 *  This test driver contains the following tests for psVector test point 1:
 *     A)  Create S32 vector
 *     B)  Add data to S32 vector
 *     C)  Reallocate S32 vector bigger
 *     D)  Reallocate S32 vector smaller
 *     E)  Free S32 vector
 *     F)  Attempt to create a S32 vector with zero size
 *     G)  Attempt to realloc a null S32 vector
 *
 *  @author  Ross Harman, MHPCC
 *
 *  @version $Revision: 1.7 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2005-02-17 19:26:24 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
 *
 */

#include "pslib.h"
#include "psTest.h"

psS32 main(psS32 argc,
           char* argv[])
{


    // Test A - Create S32 vector
    printPositiveTestHeader(stdout,"psVector", "Create S32 vector");
    psVector *psVec = psVectorAlloc(5, PS_TYPE_S32);
    printf("Vector size = %d\n", psVec->nalloc);
    printf("Vector type = %d\n", psVec->type.type);
    printf("Vector dimen = %d\n", psVec->type.dimen);
    printFooter(stdout, "psVector", "Create S32 vector", true);


    // Test B - Add data to integer vector
    printPositiveTestHeader(stdout, "psVector", "Add data to S32 vector");
    for(psS32 i = 0; i < 5; i++) {
        psVec->data.S32[i] = i*10;
        printf("Elem %d = %d\n", i, psVec->data.S32[i]);
    }
    printf("Vector size = %d\n", psVec->nalloc);
    printf("Vector population = %d\n", psVec->n);
    printFooter(stdout, "psVector", "Add data to S32 vector", true);


    // Test C - Reallocate S32 vector bigger
    printPositiveTestHeader(stdout,"psVector", "Reallocate S32 vector bigger");
    psVec = psVectorRealloc(psVec,10);
    printf("Adding more elements to S32 vector...\n");
    for(psS32 i = 5; i < 10; i++) {
        psVec->data.S32[i] = i*10;
        psVec->n++;
        printf("Elem %d = %d\n", i, psVec->data.S32[i]);
    }
    printf("Vector size = %d\n", psVec->nalloc);
    printf("Vector population = %d\n", psVec->n);
    printFooter(stdout, "psVector", "Reallocate S32 vector bigger", true);


    // Test D - Reallocate S32 vector smaller
    printPositiveTestHeader(stdout,"psVector","Reallocate S32 vector smaller");
    psVec = psVectorRealloc(psVec,3);
    printf("Vector size = %d\n", psVec->nalloc);
    for(psS32 i = 0; i < 3; i++) {
        printf("Elem %d = %d\n", i, psVec->data.S32[i]);
    }
    printf("Vector size = %d\n", psVec->nalloc);
    printf("Vector population = %d\n", psVec->n);
    printFooter(stdout, "psVector", "Reallocate integer S32 smaller", true);


    // Test E - Free S32 vector
    printPositiveTestHeader(stdout, "psVector", "Free S32 vector");
    psFree(psVec);
    psS32 leaks = psMemCheckLeaks(0, NULL, stdout, false);
    if(leaks != 0) {
        printf("ERROR: Found %d memory leaks\n",leaks);
    }
    psS32 nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psVector" ,"Free S32 vector", true);


    // Test F - Attempt to create a S32 vector with zero size
    printNegativeTestHeader(stdout,"psVector", "Attempt to create a S32 vector with zero size",
                            "Invalid value for nalloc", 0);
    psLogMsg(__func__,PS_LOG_INFO, "Following should be an error message.");
    psVector *vecBad = psVectorAlloc(0, PS_TYPE_S32);
    if(vecBad != NULL) {
        printf("ERROR: Return is not NULL\n");
    }
    printFooter(stdout, "psVector", "Attempt to create a S32 vector with zero size", true);


    // Test G - Attempt to realloc a null S32 vector
    printNegativeTestHeader(stdout,"psVector", "Attempt to realloc a null S32 vector",
                            "Null input vector", 0);
    psLogMsg(__func__,PS_LOG_INFO, "Following should be an error message.");
    psVectorRealloc(NULL,6);
    printFooter(stdout, "psVector", "Attempt to realloc a null S32 vector", true);

    return 0;
}
