/** @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.1 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2004-07-15 23:52:34 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */

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

int main(int 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(int 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(10, psVec);
    printf("Adding more elements to S32 vector...\n");
    for(int 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(3, psVec);
    printf("Vector size = %d\n", psVec->nalloc);
    for(int 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);
    psMemCheckLeaks(0, NULL, stdout);
    int 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(6, NULL);
    printFooter(stdout, "psVector", "Attempt to realloc a null S32 vector", true);

    return 0;
}
