/** @file  tst_psString.c
 *
 *  @brief Test driver for psStringCopy functions
 *
 *  This test driver contains the following test points for psStringCopy
 *  and psStringNCopy functions.
 *    1) Verify string copy - psStringCopy
 *    2) Verify empty string copy - psStringCopy
 *    3) Verify string copy with length - psStringNCopy
 *    4) Verify empty string copy with length - psStringNCopy
 *    5) Copy string to larger string - psStringNCopy
 *    6) Copy string with negative size - psStringNCopy
 *    7) Verifiy creation of string literal - PS_STRING
 *
 *  Return:   Number of test points which failed
 *
 *  @author  Eric Van Alst, MHPCC
 *
 *  @version $Revision: 1.11 $  $Name: not supported by cvs2svn $
 *  @date  $Date: 2004-11-04 19:30:05 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 *
 */

#include <string.h>
#include "pslib.h"
#include "psTest.h"

static psS32 testStringCopy00(void);
static psS32 testStringCopy01(void);
static psS32 testStringCopy02(void);
static psS32 testStringCopy03(void);
static psS32 testStringCopy04(void);
static psS32 testStringCopy05(void);
static psS32 testStringCopy06(void);

testDescription tests[] = {
                              {testStringCopy00, 0, "Verify string copy", 0, false},
                              {testStringCopy01, 1, "Verify empty string copy", 0, false},
                              {testStringCopy02, 2, "Verify string copy with length", 0, false},
                              {testStringCopy03, 3, "Verify empty string copy with length", 0, false},
                              {testStringCopy04, 4, "Copy string to larger string", 0, false},
                              {testStringCopy05, 5, "Copy string with negative size", 0, false},
                              {testStringCopy06, 6, "Verify creation of string literal", 0, false},
                              {NULL}
                          };

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

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

/*
psS32 main( psS32 argc,
          char * argv[] )
{
    psBool  tpResult = false;
    char  stringval[20] = "E R R O R";
    char  stringval1[20] = "e r r o r";
    char  *stringvalnocopy = "F A I L";
    char  *substringval = "e r r";
    psS32   substringlen = 6;
    char  *emptyval = "";
    psS32   tpFails = 0;
    char  *strResult;
    psS32   increaseSize = 5;
    psS32   negativeSize = -5;
    psS32   result = 0;
    psS32   result1 = 0;
    psS32   memBlockAllocated = 0;
    psMemBlock ***memBlockPtr = NULL;
 
*/

static psS32 testStringCopy00(void)
{
    char  stringval[20] = "E R R O R";
    psS32   result = 0;
    psS32   result1 = 0;
    char  *strResult;

    // Test point #1 Verify string copy - psStringCopy
    strResult = psStringCopy(stringval);
    // Perform string compare
    result = strcmp(strResult, stringval);
    // Modify original string
    stringval[0]='G';
    result1 = strcmp(strResult, stringval);
    stringval[0]='E';
    if ( ( result != 0 ) || ( result1 == 0) ) {
        fprintf(stderr, "Failed test point #1 strcmp result = %d expected 0\n",result);
        fprintf(stderr, "                               src = %s expected %s\n",
                strResult, stringval);
        fprintf(stderr, "             changed strcmp result = %d expected 1\n",result1);
        fprintf(stderr, "             changed           src = %s expected %s\n",strResult,
                stringval);
        return 1;
    }

    // Free memory allocated
    psFree(strResult);

    return 0;
}

static psS32 testStringCopy01(void)
{
    char  *emptyval = "";
    psS32   result = 0;
    char  *strResult;

    // Test point #2 Verify empty string copy - psStringCopy
    strResult = psStringCopy(emptyval);
    // Perform string compare
    result = strcmp(strResult, emptyval);
    if ( result != 0 ) {
        fprintf(stderr,"Failed test point #2 strcmp result = %d expected 0\n",result);
        fprintf(stderr,"                               src = %s expected %s\n",
                strResult, emptyval);
        return 1;
    }

    // Free memory allocated
    psFree(strResult);

    return 0;
}

static psS32 testStringCopy02(void)
{
    psS32   result = 0;
    psS32   result1 = 0;
    char  *strResult;
    char  stringval1[20] = "e r r o r";
    psS32   substringlen = 5;
    char  *substringval = "e r r";

    // Test point #3 Verify string copy with length - psStringNCopy
    strResult = psStringNCopy(stringval1, substringlen);
    // Perform string compare and get string length
    result = strncmp(strResult, substringval, substringlen);
    // Change original string
    stringval1[0] = 'g';
    result1 = strncmp(strResult, substringval, substringlen);
    if ( ( result != 0 ) || ( result1 != 0 ) ) {
        fprintf(stderr,"Failed test point #3 strcmp result = %d expected 0\n",result);
        fprintf(stderr,"                               src = %s expected %s\n",
                strResult, substringval);
        fprintf(stderr,"             changed strcmp result = %d expected 0\n",result1);
        fprintf(stderr,"                               src = %s expected %s\n",
                strResult, substringval);
        return 1;
    }
    // Free memory allocated
    psFree(strResult);

    return 0;
}

static psS32 testStringCopy03(void)
{
    psS32   result = 0;
    psS32   result1 = 0;
    char  *strResult;
    char  *stringvalnocopy = "F A I L";

    // Test point #4 Verify empty string copy with length - psStringNCopy
    strResult = psStringNCopy(stringvalnocopy, 0);
    // Perform string compare and get sting length
    result = strcmp(strResult, stringvalnocopy);
    result1 = strlen(strResult);
    if ( result == 0 ) {
        fprintf(stderr,"Failed test point #4 strcmp result = %d didn't expected %d\n",result,0);
        fprintf(stderr,"                               src = %s didn't expected %s\n",
                strResult, stringvalnocopy);
        return 1;
    }
    // Free memory
    psFree(strResult);

    return 0;
}

static psS32 testStringCopy04(void)
{
    psS32   result = 0;
    psS32   result1 = 0;
    char  *strResult;
    char  stringval[20] = "E R R O R";
    psS32   increaseSize = 5;

    // Test point #5 Copy string to larger string - psStringNCopy
    strResult = psStringNCopy(stringval, (strlen(stringval) + increaseSize));
    // Perform string compare and get string length
    result = strcmp(strResult, stringval);
    result1 = strlen(strResult);
    // The strings should still compare
    if ( ( result != 0 ) ||
            ( result1 != strlen(stringval) ) ) {
        fprintf(stderr,"Failed test point #5 strcmp result = %d expected %d\n",result,0);
        fprintf(stderr,"                               src = %s expected %s\n",
                strResult, stringval);
        fprintf(stderr,"                     strlne result = %d expected %d\n",result1,
                (psS32)strlen(stringval));
        return 1;
    }
    // Free memory
    psFree(strResult);

    return 0;
}

static psS32 testStringCopy05(void)
{
    char  *strResult;
    char  stringval[20] = "E R R O R";
    psS32   negativeSize = -5;

    // Test point #6 Copy string with negative size - psStringNCopy
    strResult = psStringNCopy(stringval, negativeSize);
    if ( strResult != NULL ) {
        fprintf(stderr,"Failed test point #6 return value = %p expected NULL\n",
                strResult);
        return 1;
    }
    // Memory should not have been allocated

    return 0;
}

static psS32 testStringCopy06(void)
{
    char  *strResult;
    char  stringval[20] = "E R R O R";
    psS32   result = 0;

    // Test point #7 Verify creation of string literal - PS_STRING
    strResult = PS_STRING(E R R O R);
    result = strcmp(strResult, stringval);
    if ( result != 0 ) {
        fprintf(stderr,"Failed test point #7 strcmp result = %d expected %d",result,0);
        return 1;
    }
    // Memory should not have been allocated

    return 0;
}
