Professional Software Consulting

test_getField.c

Suite of tests that attempt to break the functions within getField.c.

Source Code
/**
* Copyright (c) 2008, Corey's Consulting LLC.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
*   this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
*   notice, this list of conditions and the following disclaimer in the
*   documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/***************************************************************************

 This module contains functions which correspond 1 to 1 with the functions
 in getField.c.  These functions are intended to test the ones in the
 "primary" module.

 There are three types of results: "successful" tests, which simulate normal
 operation, "failure" tests, which simulate failures during normal
 operation which must be dealt with gracefully, and "errors" - which are
 problems with the test harness itself.

 Successes and failures generate logs as they are expected to happen.
 Errors will generate core dumps and a statement indicating which
 test had problems.


 5/23/2004

***************************************************************************/
#include "getField.h"

#include <assert.h>


static int TEST_getField(void);
static int TEST_getFieldMB(void);
static int TEST_getLocationMB(void);
static int TEST_countFieldsMB(void);


int
main(void)
{
    printf("==============================================================\n");
    printf("           T  E  S  T  I  N  G      B  E  G  I  N  S\n");
    printf("                        (TEST_LocationMB)\n");
    printf("==============================================================\n");
    TEST_getLocationMB();

    printf("==============================================================\n");
    printf("             C  H  A  N  G  E      O  F      T  E  S  T\n");
    printf("                        (TEST_getFieldMB)\n");
    printf("==============================================================\n");
    TEST_getFieldMB();


    printf("==============================================================\n");
    printf("             C  H  A  N  G  E      O  F      T  E  S  T\n");
    printf("                        (TEST_getField)\n");
    printf("==============================================================\n");
    TEST_getField();

    printf("==============================================================\n");
    printf("             C  H  A  N  G  E      O  F      T  E  S  T\n");
    printf("                        (TEST_countFields)\n");
    printf("==============================================================\n");
    TEST_countFieldsMB();

    printf("==============================================================\n");
    printf("        T  E  S  T  I  N  G      C  O  M  P  L  E  T  E\n");
    printf("                         (getField.c)\n");
    printf("==============================================================\n");

    return(0);
}    // end main()


/***************************************************************************

 All test functions core dump on failure, so there are no return values.
 Also, since they are self-contained to test one and only one function,
 they do not require parameters.

****/
static int
TEST_getField(void)
{
    char    caBuf[255+1];
    int     iRv;
    int     ii;
    struct test_r    {
        char    cDelim;
        int     iFldWanted;
        char    *cpLine;
        char    *cpFldExpected;
    } raTest[] = {
    { ',',1,"f1,f2,f3,f4,f5",      "f1" },
    { ',',2,"f1,f2,f3,f4,f5",      "f2" },
    { ',',3,"f1,f2,f3,f4,f5",      "f3" },
    { ',',4,"f1,f2,f3,f4,f5",      "f4" },
    { ',',5,"f1,f2,f3,f4,f5",      "f5" },
    { ',',1,"f1",                  "f1" },
    { ',',1,"f1,f2",               "f1" },
    { ',',2,"f1,f2",               "f2" },
    { '\n',1,"f1\nf2\nf3\nf4\nf5", "f1" },
    { '\n',2,"f1\nf2\nf3\nf4\nf5", "f2" },
    { '\n',3,"f1\nf2\nf3\nf4\nf5", "f3" },
    { '\n',4,"f1\nf2\nf3\nf4\nf5", "f4" },
    { '\n',5,"f1\nf2\nf3\nf4\nf5", "f5" },
    { '\n',1,"f1",                 "f1" },
    { '\n',1,"f1\nf2",             "f1" },
    { '\n',2,"f1\nf2",             "f2" },
    { '^',1,"f1^f2^f3^f4^f5",      "f1" },
    { '^',2,"f1^f2^f3^f4^f5",      "f2" },
    { '^',3,"f1^f2^f3^f4^f5",      "f3" },
    { '^',4,"f1^f2^f3^f4^f5",      "f4" },
    { '^',5,"f1^f2^f3^f4^f5",      "f5" },
    { '^',1,"f1",                  "f1" },
    { '^',1,"f1^f2",               "f1" },
    { '^',2,"f1^f2",               "f2" },
    { 0, -1,NULL,NULL }           
    };


    printf(" ---> TEST:field num too small, expecting %d\n", GF_FIELD_NOT_FOUND);
    memset(caBuf, (char)NULL, sizeof(caBuf));
    iRv = getField("f1,f2,f3,f4", ',', 0, caBuf, sizeof(caBuf));
    printf("iRv=%d\n", iRv);
    assert (iRv == GF_FIELD_NOT_FOUND);
    printf(" ---> TEST:field num way too large, expecting %d\n", GF_FIELD_NOT_FOUND);
    memset(caBuf, (char)NULL, sizeof(caBuf));
    iRv = getField("f1,f2,f3,f4", ',', 17, caBuf, sizeof(caBuf));
    printf("iRv=%d\n", iRv);
    assert (iRv == GF_FIELD_NOT_FOUND);
    printf(" ---> TEST:field num too large by 1, expecting %d\n", GF_FIELD_NOT_FOUND);
    memset(caBuf, (char)NULL, sizeof(caBuf));
    iRv = getField("f1,f2,f3,f4", ',', 5, caBuf, sizeof(caBuf));
    printf("iRv=%d\n", iRv);
    assert (iRv == GF_FIELD_NOT_FOUND);
    printf(" ---> TEST:null line, expecting %d\n", GF_NULL_PARAM);
    memset(caBuf, (char)NULL, sizeof(caBuf));
    iRv = getField(NULL, ',', 1, caBuf, sizeof(caBuf));
    printf("iRv=%d\n", iRv);
    assert (iRv == GF_NULL_PARAM);
    printf(" ---> TEST:null outbuf, expecting %d\n", GF_NULL_PARAM);
    memset(caBuf, (char)NULL, sizeof(caBuf));
    iRv = getField("f1,f2,f3,f4", ',', 1, NULL, sizeof(caBuf));
    printf("iRv=%d\n", iRv);
    assert (iRv == GF_NULL_PARAM);
    printf(" ---> TEST:negative outbuf size, expecting %d\n", GF_SIZE_TOO_SMALL);
    memset(caBuf, (char)NULL, sizeof(caBuf));
    iRv = getField("f1,f2,f3,f4", ',', 1, caBuf, -1);
    printf("iRv=%d\n", iRv);
    assert (iRv == GF_SIZE_TOO_SMALL);
    printf(" ---> TEST:outbuf size too small, expecting %d\n", GF_SIZE_TOO_SMALL);
    memset(caBuf, (char)NULL, sizeof(caBuf));
    iRv = getField("f1,field2,f3,f4", ',', 2, caBuf, 4);
    printf("iRv=%d\n", iRv);
    assert (iRv == GF_SIZE_TOO_SMALL);

    ii = 0;
    while (raTest[ii].cDelim != 0)
    {
        printf(" ---> TEST %d:line=[%s],fldnum=%d,expected=[%s]\n", ii,
            raTest[ii].cpLine, raTest[ii].iFldWanted, raTest[ii].cpFldExpected);
        memset(caBuf, (char)NULL, sizeof(caBuf));
        iRv = getField( raTest[ii].cpLine, 
                        raTest[ii].cDelim,
                        raTest[ii].iFldWanted,
                        caBuf,
                        sizeof(caBuf));
        printf("        iRv=%d,fld got=[%s]\n", iRv, caBuf);
        assert(iRv == GF_SUCCESS);
        assert(strcmp(raTest[ii].cpFldExpected, caBuf) == 0);
        ii++;
    }


    return(0);
}    // end TEST_getField()



/***************************************************************************

 All test functions core dump on failure, so there are no return values.
 Also, since they are self-contained to test one and only one function,
 they do not require parameters.

****/
static int
TEST_getFieldMB(void)
{
int     iRv;
char    caLine[1023];
int     iLenLine;
char    *cpDelim;
char    caDelim[3];
char    caOutBuf[1023];

    iLenLine = 5;
    cpDelim = "|";

    printf("---- TEST 1 (NULL caLine parameter)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(NULL, iLenLine, cpDelim, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_NULL_PARAM);

    printf("---- TEST 2 (outbuf size too small)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        1, caOutBuf, 0);
    assert(iRv == GF_SIZE_TOO_SMALL);

    printf("---- TEST 3 (NULL cpDelim)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, iLenLine, NULL, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_NULL_PARAM);

    printf("---- TEST 4 (NULL caOutBuf)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        1, NULL, sizeof(caOutBuf)-1);
    assert(iRv == GF_NULL_PARAM);

    printf("---- TEST 5 (NULL cpDelim)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, iLenLine, NULL, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_NULL_PARAM);

    printf("---- TEST 6 (bad param 1)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, iLenLine, cpDelim, 0,
        1, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_INVALID_PARMS);

    printf("---- TEST 7 (bad param 2)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, 0, cpDelim, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_INVALID_PARMS);

    printf("---- TEST 8 (single byte delimiter)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    strcpy(caLine, "11111111|22222222|33333333");
    cpDelim = "|";
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d, outbuf=[%s]\n", iRv, caOutBuf);
    assert(iRv == GF_SUCCESS);
    assert(strcmp(caOutBuf, "11111111") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        2, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_SUCCESS);
    printf("outbuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "22222222") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        3, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_SUCCESS);
    assert(strcmp(caOutBuf, "33333333") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        4, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_FIELD_NOT_FOUND);


    printf("---- TEST 9 (two byte delimiter)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    strcpy(caLine, "11111111|&22222222|&33333333");
    cpDelim = "|&";
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    assert(strcmp(caOutBuf, "11111111") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        2, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_SUCCESS);
    printf("outbuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "22222222") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        3, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_SUCCESS);
    assert(strcmp(caOutBuf, "33333333") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        4, caOutBuf, sizeof(caOutBuf)-1);
    assert(iRv == GF_FIELD_NOT_FOUND);


    printf("---- TEST 10 (end of line on cusp of another delimiter)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    strcpy(caLine, "11111111|&22222222|&33333333|&");
    cpDelim = "|&";
    iRv = getFieldMB(caLine, strlen(caLine)-1, cpDelim, strlen(cpDelim),
        3, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "33333333|") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine)-1, cpDelim, strlen(cpDelim),
        4, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_FIELD_NOT_FOUND);


    printf("---- TEST 11 (close to match delimiters, but no cigar)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    strcpy(caLine, "1&|1&111|&22222222|&33333333|&");
    cpDelim = "|&";
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "1&|1&111") == 0);


    printf("---- TEST 12 (three character delimiters)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    strcpy(caLine, "11111111|||22222222|||&33333333|||");
    cpDelim = "|||";
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        1, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "11111111") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        2, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "22222222") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        3, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "&33333333") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        4, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, strlen(caLine), cpDelim, strlen(cpDelim),
        5, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_FIELD_NOT_FOUND);


    printf("---- TEST 13 (three character delimiters, null as middle char)\n");
    memset(caOutBuf, 0, sizeof(caOutBuf));
    strcpy(caLine, "11111111|||22222222|||33333333|||");
    strcpy(caDelim, "|||");
    caDelim[1] = 0;
    caLine[9] = caLine[20] = caLine[31] = 0;
    iRv = getFieldMB(caLine, 33, caDelim, 3,
        1, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "11111111") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, 33, caDelim, 3,
        2, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "22222222") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, 33, caDelim, 3,
        3, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "33333333") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, 33, caDelim, 3,
        4, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_SUCCESS);
    printf("caOutBuf=[%s]\n", caOutBuf);
    assert(strcmp(caOutBuf, "") == 0);

    memset(caOutBuf, 0, sizeof(caOutBuf));
    iRv = getFieldMB(caLine, 33, caDelim, 3,
        5, caOutBuf, sizeof(caOutBuf)-1);
    printf("rv=%d\n", iRv);
    assert(iRv == GF_FIELD_NOT_FOUND);


    return(0);
}    // end TEST_getFieldMB()



/***************************************************************************

 All test functions core dump on failure, so there are no return values.
 Also, since they are self-contained to test one and only one function,
 they do not require parameters.

****/
static int
TEST_getLocationMB(void)
{
char    caLine[1023];
char    *cpDelim;
char    *cpRv;
char    caDelim[3+1];
int     iLenLine;
int     iLenField;
int     iErrVal;



    iLenLine = 5;
    cpDelim = "|";


    printf("---- TEST 1 (invalid parms)\n");

    cpRv = "start buffer";
    cpRv = getLocationMB(NULL, iLenLine, cpDelim, strlen(cpDelim),
        1, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_NULL_PARAM);

    cpRv = "start buffer";
    cpRv = getLocationMB(caLine, 0, cpDelim, strlen(cpDelim),
        1, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_INVALID_PARMS);

    cpRv = "start buffer";
    cpRv = getLocationMB(caLine, iLenLine, NULL, strlen(cpDelim),
        1, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_NULL_PARAM);

    cpRv = "start buffer";
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, 0,
        1, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_INVALID_PARMS);

    cpRv = "start buffer";
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        0, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_INVALID_PARMS);

    cpRv = "start buffer";
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        1, NULL, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_NULL_PARAM);

    cpRv = "start buffer";
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        1, &iLenField, NULL);
    assert(cpRv == NULL);
    assert(iErrVal == GF_NULL_PARAM);


    printf("---- TEST 2 (one byte delimiter)\n");
    cpRv = "start buffer";
    cpDelim = " ";
    strcpy(caLine, "Now is the time");
    iLenLine = strlen(caLine);
    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        1, &iLenField, &iErrVal);
    assert(cpRv == &caLine[0]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 3);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        2, &iLenField, &iErrVal);
    assert(cpRv == &caLine[4]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 2);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        3, &iLenField, &iErrVal);
    assert(cpRv == &caLine[7]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 3);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        4, &iLenField, &iErrVal);
    assert(cpRv == &caLine[11]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 4);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        5, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_FIELD_NOT_FOUND);


    printf("---- TEST 3 (two byte delimiter)\n");
    cpRv = "start buffer";
    cpDelim = "&&";
    strcpy(caLine, "Now&&is&& the&& time&&");
    iLenLine = strlen(caLine)-1;
    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        1, &iLenField, &iErrVal);
    assert(cpRv == &caLine[0]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 3);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        2, &iLenField, &iErrVal);
    assert(cpRv == &caLine[5]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 2);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        3, &iLenField, &iErrVal);
    assert(cpRv == &caLine[9]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 4);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        4, &iLenField, &iErrVal);
    assert(cpRv == &caLine[15]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 6);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        5, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_FIELD_NOT_FOUND);


    printf("---- TEST 4 (three byte delimiter, with nulls)\n");
    cpRv = "start buffer";
    strcpy(caDelim, "& |");
    caDelim[1] = 0;
    memset(caLine, 0, sizeof(caLine));
    strcpy(caLine, "Now& |is& | the& | time&");
    iLenLine = strlen(caLine)+1;
    caLine[4] = caLine[9] = caLine[16] = 0;
    iLenField = -1;
    iErrVal = 153432;
    printf("Calling...\n");
    cpRv = getLocationMB(caLine, iLenLine, caDelim, 3,
        1, &iLenField, &iErrVal);
    printf("returned\n");
    assert(cpRv == &caLine[0]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 3);

    printf("first test completed\n");

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, caDelim, 3,
        2, &iLenField, &iErrVal);
    printf("caLine=%p, cpRv = %p, diff=%d, errval=%d\n", caLine, cpRv,
            cpRv - caLine, iErrVal);
    assert(cpRv == &caLine[6]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 2);

    printf("second test completed\n");
    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, caDelim, 3,
        3, &iLenField, &iErrVal);
    assert(cpRv == &caLine[11]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 4);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, caDelim, 3,
        4, &iLenField, &iErrVal);
    assert(cpRv == &caLine[18]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 7);

    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, caDelim, 3,
        5, &iLenField, &iErrVal);
    assert(cpRv == NULL);
    assert(iErrVal == GF_FIELD_NOT_FOUND);


    printf("---- TEST 5 (one byte delimiter, no field)\n");
    cpRv = "start buffer";
    cpDelim = " ";
    strcpy(caLine, "Now ");
    iLenLine = strlen(caLine);
    iLenField = -1;
    iErrVal = 153432;
    cpRv = getLocationMB(caLine, iLenLine, cpDelim, strlen(cpDelim),
        2, &iLenField, &iErrVal);
    printf("lenField=%d, err=%d\n", iLenField, iErrVal);
    assert(cpRv == &caLine[4]);
    assert(iErrVal == GF_SUCCESS);
    assert(iLenField == 0);



    return(0);
}    // end TEST_getLocationMB()


/***************************************************************************

 All test functions core dump on failure, so there are no return values.
 Also, since they are self-contained to test one and only one function,
 they do not require parameters.

****/
static int
TEST_countFieldsMB(void)
{
char    caLine[1023];
char    *cpDelim;
char    cRv;
int     iLenLine;



    iLenLine = 5;
    cpDelim = "|";


    printf("---- TEST 1 (null line)\n");
    cRv = countFieldsMB(NULL, iLenLine, cpDelim, strlen(cpDelim), TRUE);
    assert(cRv == GF_NULL_PARAM);

    printf("---- TEST 2 (null delim)\n");
    cpDelim = NULL;
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, 5, TRUE);
    assert(cRv == GF_NULL_PARAM);

    printf("---- TEST 3 (line len is negative)\n");
    cpDelim = "";
    cRv = countFieldsMB(caLine, -1, cpDelim, strlen(cpDelim), TRUE);
    assert(cRv == GF_INVALID_PARMS);

    printf("---- TEST 4 (len Delim is too small)\n");
    cpDelim = "";
    iLenLine = 0;
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, 0, TRUE);
    assert(cRv == GF_INVALID_PARMS);

    printf("---- TEST 5 (success: empty string)\n");
    cpDelim = "|";
    iLenLine = 0;
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), TRUE);
    printf("rv=%d\n", cRv);
    assert(cRv == 0);


    strcpy(caLine, "v|second|third|fourth|fifth||seventh||ninth");
    printf("---- TEST 5a (single byte dlm:line=[%s])\n", caLine);
    cpDelim = "|";
    iLenLine = strlen(caLine);
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), TRUE);
    printf("rv=%d\n", cRv);
    assert(cRv == 9);

    strcpy(caLine, "v|second|third|fourth|fifth||seventh||ninth|");
    printf("---- TEST 5b (single byte dlm:line=[%s])\n", caLine);
    cpDelim = "|";
    iLenLine = strlen(caLine);
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), TRUE);
    printf("rv=%d\n", cRv);
    assert(cRv == 10);

    strcpy(caLine, "v&&&second&&&third&&&fourth&&&fifth&&&&&&seventh&&&&&&ninth");
    printf("---- TEST 6 (multi byte dlm:line=[%s])\n", caLine);
    cpDelim = "&&&";
    iLenLine = strlen(caLine);
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), TRUE);
    printf("rv=%d\n", cRv);
    assert(cRv == 9);


    strcpy(caLine, "v&&&second&&&third&&&fourth&&&fifth&&&&&&seventh&&&&&&"
            "ninthvcu9420cjmia2fon&&&jvcid&&&jfkdlafcj&&&&&&jkflds&&&");
    printf("---- TEST 7 (multi byte dlm, not null terminated\n");
    cpDelim = "&&&";
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), TRUE);
    printf("rv=%d\n", cRv);
    assert(cRv == 9);


    printf("---- TEST 8 (skipEmpty, success: empty string)\n");
    cpDelim = "|";
    iLenLine = 0;
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), FALSE);
    printf("rv=%d\n", cRv);
    assert(cRv == 0);


    strcpy(caLine, "v|second|third|fourth|fifth||seventh||ninth");
    printf("---- TEST 9a (skipEmpty, single byte dlm:line=[%s])\n", caLine);
    cpDelim = "|";
    iLenLine = strlen(caLine);
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), FALSE);
    printf("rv=%d\n", cRv);
    assert(cRv == 7);

    strcpy(caLine, "v|second|third|fourth|fifth||seventh||ninth|");
    printf("---- TEST 9b (skipEmpty, single byte dlm:line=[%s])\n", caLine);
    cpDelim = "|";
    iLenLine = strlen(caLine);
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), FALSE);
    printf("rv=%d\n", cRv);
    assert(cRv == 7);

    strcpy(caLine, "v&&&second&&&third&&&fourth&&&fifth&&&&&&seventh&&&&&&ninth");
    printf("---- TEST 10 (skipEmpty, multi byte dlm:line=[%s])\n", caLine);
    cpDelim = "&&&";
    iLenLine = strlen(caLine);
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), FALSE);
    printf("rv=%d\n", cRv);
    assert(cRv == 7);


    strcpy(caLine, "v&&&second&&&third&&&fourth&&&fifth&&&&&&seventh&&&&&&"
            "ninthvcu9420cjmia2fon&&&jvcid&&&jfkdlafcj&&&&&&jkflds&&&");
    printf("---- TEST 11 (skipEmpty, multi byte dlm, not null terminated\n");
    cpDelim = "&&&";
    cRv = countFieldsMB(caLine, iLenLine, cpDelim, strlen(cpDelim), FALSE);
    printf("rv=%d\n", cRv);
    assert(cRv == 7);

    return(0);
}    // end TEST_countFieldsMB()


     Contact Us     

Something wrong with this page or this site? Let the webmaster know by clicking HERE
This website designed, implemented, and maintained by Corey Dulecki
© 2009-2012, Corey's Consulting LLC