tags:

views:

131

answers:

2

For some reason, I'm having a hard time trying to cover the block of code below. This code is an excerpt from the UNIX uniq command. I'm trying to write test cases to cover all blocks, but can't seem to reach this block:

if (nfiles == 2)
{
    // Generic error routine
}

In context:

int main (int argc, char **argv)
{
    int optc = 0;
    bool posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
    int nfiles = 0;
    char const *file[2];

    file[0] = file[1] = "-";
    program_name = argv[0];

    skip_chars = 0;
    skip_fields = 0;
    check_chars = SIZE_MAX;

    for (;;)
    {
        /* Parse an operand with leading "+" as a file after "--" was
        seen; or if pedantic and a file was seen; or if not
        obsolete.  */

        if (optc == -1 || (posixly_correct && nfiles != 0) || ((optc = getopt_long (argc, argv, "-0123456789Dcdf:is:uw:", longopts, NULL)) == -1))
        {
            if (optind == argc)
                break;
            if (nfiles == 2)
            {
                // Handle errors
            }
            file[nfiles++] = argv[optind++];
        }
        else switch (optc)
        {
            case 1:
            {
                unsigned long int size;
                if (optarg[0] == '+' && posix2_version () < 200112 && xstrtoul (optarg, NULL, 10, &size, "") == LONGINT_OK && size <= SIZE_MAX)
                    skip_chars = size;
                else if (nfiles == 2)
                {
                    // Handle error
                }
                else
                    file[nfiles++] = optarg;
            }
            break;
        }
    }
}

Any help would be greatly appreciated. Thanks.

A: 

It appears this could be reached when more than 2 files are supplied on the command line. In that case, nfiles would reach the value 2 after the name of the second file has been stored in file[1]. When the code checking nfiles == 2 is reached a third time, then the value will already be 2, and the error handling will execute.

There are two if statements in question. That in switch case "1" can only be reached by using the option in longopts with val == 1.

Heath Hunnicutt
I'm capable of generating a test to cover the 2nd if statement, however, it seems that the statement: if (optind == argc) break; is catching everything in the first case.
You have tried simply "uniq a b c" or something similar?
Heath Hunnicutt
I think you might need to do `uniq a b c d` to trigger that case.
caf
A: 

I just thought I would mention in passing that the automatic generation of test cases to satisfy coverage criteria is transitioning from being a research topic to useful applications. One prototype that I know of is PathCrawler.

The difficulties in the source code that may prevent this or a similar tool to work as hoped are the usual suspects: aliasing and dynamic memory allocation.

Pascal Cuoq
Hey, thanks a lot for the source. I'll check it out.
Ha, you are very kind. You make me feel guilty for providing one example only, so here's another one: http://www.cis.upenn.edu/~alur/CIS670/cute.pdf
Pascal Cuoq