/** @file  tst_psArray.c
 *
 *  @brief Test driver for psArray integer functions
 *
 *  This test driver contains the following tests for psArray test point 1:
 *     A)  Create void pointer array
 *     B)  Add data to void pointer array
 *     C)  Reallocate void pointer array bigger
 *     D)  Reallocate void pointer array smaller
 *     E)  Free void pointer array
 *
 *  @author  Ross Harman, MHPCC
 *
 *  @version $Revision: 1.3 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2004-09-28 23:26:49 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */

#include <math.h>

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

typedef struct
{
    int x;
    float y;
}
testStruct;


int main(int argc,
         char* argv[])
{
    // Create array of pointers
    testStruct *mySt[10];


    // Test A - Create void pointer array
    printPositiveTestHeader(stdout,"psArray", "Create void pointer array");
    psArray *psArr = psArrayAlloc(5);
    if (psArr->nalloc != 5) {
        psError(__func__,"psArray didn't have proper number of elements.");
        return 1;
    }
    printFooter(stdout, "psArray", "Create void pointer array", true);


    // Test B - Add data to void pointer array
    printPositiveTestHeader(stdout, "psArray", "Add data to void pointer array");
    for(int i = 0; i < 5; i++) {
        testStruct *ts = psAlloc(sizeof(testStruct));
        ts->x = 10*i;
        ts->y = 10.1*i;
        mySt[i] = ts;
        psArr->data[i] = ts;
        psMemIncrRefCounter(ts);
    }

    for(int i = 0; i < 5; i++) {
        testStruct *ts = (testStruct*)psArr->data[i];
        printf("ts[%d].x = %d ts[%d].y = %.2f\n", i, ts->x, i, ts->y);
        if (fabsf(ts->x - 10*i) > 0.01f || fabsf(ts->y - 10.1*i) > 0.01f) {
            psError(__func__,"Couldn't properly get elements from array.");
            return 2;
        }
    }
    printf("array size = %d\n", psArr->nalloc);
    if (psArr->nalloc != 5) {
        psError(__func__,"Array Size wrong");
        return 3;
    }
    printf("array population = %d\n", psArr->n);
    if (psArr->n != 5) {
        psError(__func__,"Array population wrong");
        return 4;
    }
    printFooter(stdout, "psArray", "Add data to void pointer array", true);


    // Test C - Reallocate void pointer array bigger
    printPositiveTestHeader(stdout,"psArray", "Reallocate void pointer array bigger");
    psArr = psArrayRealloc(psArr,10);
    printf("Adding more elements to void pointer array...\n");
    for(int i = 5; i < 10; i++) {
        testStruct *ts = psAlloc(sizeof(testStruct));
        ts->x = 10*i;
        ts->y = 10.1*i;
        mySt[i] = ts;
        psArr->data[i] = ts;
        psArr->n++;
        psMemIncrRefCounter(ts);
    }
    for(int i = 0; i < 10; i++) {
        testStruct *ts = (testStruct*)psArr->data[i];
        printf("ts[%d].x = %d ts[%d].y = %.2f\n", i, ts->x, i, ts->y);
        if (fabsf(ts->x - 10*i) > 0.01f || fabsf(ts->y - 10.1*i) > 0.01f) {
            psError(__func__,"Couldn't properly get elements from array.");
            return 5;
        }
    }
    printf("array size = %d\n", psArr->nalloc);
    if (psArr->nalloc != 10) {
        psError(__func__,"Array Size wrong");
        return 6;
    }
    printf("array population = %d\n", psArr->n);
    if (psArr->n != 10) {
        psError(__func__,"Array Population wrong");
        return 7;
    }
    printFooter(stdout, "psArray", "Reallocate void pointer array bigger", true);


    // Test D - Reallocate void pointer array smaller
    printPositiveTestHeader(stdout,"psArray","Reallocate void pointer array smaller");
    psArr = psArrayRealloc(psArr,3);
    for(int i = 0; i < 3; i++) {
        testStruct *ts = (testStruct*)psArr->data[i];
        printf("ts[%d].x = %d ts[%d].y = %.2f\n", i, ts->x, i, ts->y);
        if (fabsf(ts->x - 10*i) > 0.01f || fabsf(ts->y - 10.1*i) > 0.01f) {
            psError(__func__,"Couldn't properly get elements from array.");
            return 8;
        }
    }
    printf("array size = %d\n", psArr->nalloc);
    if (psArr->nalloc != 3) {
        psError(__func__,"Array Size wrong");
        return 9;
    }
    printf("array population = %d\n", psArr->n);
    if (psArr->n != 3) {
        psError(__func__,"Array Population wrong");
        return 10;
    }
    printFooter(stdout, "psArray", "Reallocate integer void pointer smaller", true);


    // Test E - Free void pointer array
    printPositiveTestHeader(stdout, "psArray", "Free void pointer array");
    psFree(psArr);
    for(int i = 0; i < 10; i++) {
        psFree(mySt[i]);
    }
    psMemCheckLeaks(0, NULL, stdout);
    int nBad = psMemCheckCorruption(0);
    if(nBad) {
        printf("ERROR: Found %d bad memory blocks\n", nBad);
    }
    printFooter(stdout, "psArray" ,"Free void pointer array", true);

    return 0;
}
