开发者

How do I cover unintuitive code blocks?

开发者 https://www.devze.com 2022-12-26 23:08 出处:网络
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 se

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 == 开发者_JS百科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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号