/** @file  tst_psImageManip.c
*
*  @brief Contains the tests for psImageManip.[ch]
*
*
*  @author Robert DeSonia, MHPCC
*
*  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
*  @date $Date: 2004-12-27 21:12:47 $
*
*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
*/

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

static psS32 testArrayAdd( void );

testDescription tests[] = {
                              {testArrayAdd, 788, "psArrayAdd", 0, false},
                              {NULL}
                          };

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

    return ( ! runTestSuite( stderr, "psArray", tests, argc, argv ) );
}

psS32 testArrayAdd( void )
{
    int subtest = 0;
    float* data;
    int nalloc = 5;
    int n = 0;
    int delta = 5;

    // allocate the array.
    psArray* arr = psArrayAlloc(nalloc);
    arr->n = n;

    // test arrayAdd until n == nalloc
    while (n < nalloc) {
        data = psAlloc(sizeof(float));
        arr = psArrayAdd(arr, delta*2, data); // make delta unique versus next delta used.

        subtest++;
        if (arr->nalloc != nalloc) {
            psError(PS_ERR_UNKNOWN,true,
                    "psArrayAdd expanded the psArray unnecessarily.  n=%d",
                    n);
            return subtest;
        }

        subtest++;
        if (arr->n != ++n) {
            psError(PS_ERR_UNKNOWN,true,
                    "psArrayAdd did not increment the size of the psArray. n=%d",
                    n);
            return subtest;
        }

        subtest++;
        if (arr->data[n-1] != data) {
            psError(PS_ERR_UNKNOWN,true,
                    "psArrayAdd didn't set the element to data. n=%d",
                    n);
            return subtest;
        }
    }

    // now try to add an element when the array is full.
    data = psAlloc(sizeof(float));
    arr = psArrayAdd(arr, delta, data);

    // make sure the array was expanded
    subtest++;
    if (arr->nalloc != nalloc+delta) {
        psError(PS_ERR_UNKNOWN,true,
                "psArrayAdd did not expand the psArray when it was already full."
                " old nalloc=%d, nalloc=%d, delta=%d",
                nalloc, arr->nalloc, delta);
        return subtest;
    }
    nalloc = arr->nalloc;

    subtest++;
    if (arr->n != ++n) {
        psError(PS_ERR_UNKNOWN,true,
                "psArrayAdd did not increment psArray.n by 1 after expanding it.");
        return subtest;
    }

    subtest++;
    if (arr->data[n-1] != data) {
        psError(PS_ERR_UNKNOWN,true,
                "psArrayAdd didn't set the second element to data.");
        return subtest;
    }

    // make the array full again (operation tested already)
    while (arr->n < arr->nalloc) {
        data = psAlloc(sizeof(float));
        arr = psArrayAdd(arr, 0, data);
    }
    nalloc = arr->nalloc;
    n = arr->n;

    // now add to full array with delta = 0; verify that the array is
    // expanded by 10
    data = psAlloc(sizeof(float));
    arr = psArrayAdd(arr, 0, data);

    subtest++;
    if (arr->nalloc != nalloc+10) {
        psError(PS_ERR_UNKNOWN,true,
                "psArrayAdd did not expand the psArray by 10 when delta < 1."
                " old nalloc=%d, nalloc=%d",
                nalloc, arr->nalloc);
        return subtest;
    }

    psFree(arr);

    return 0;  // the value that indicates success is part of the testDescription
}

